gpt4 book ai didi

java - libGDX - 添加分数并将其显示在屏幕的左上角?

转载 作者:行者123 更新时间:2023-11-29 09:33:28 28 4
gpt4 key购买 nike

这是 libGDX 中一个简单游戏的示例代码。当雨滴落入桶中时,分数应增加一分。总分应显示在左上角。

如果错过 3 滴,则显示 GAME OVER。我知道要增加分数,但我不知道要显示它。谢谢。

public class Drop implements ApplicationListener {
Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;
SpriteBatch batch;
OrthographicCamera camera;
Rectangle bucket;
Array<Rectangle> raindrops;
long lastDropTime;

@Override
public void create() {
// load the images for the droplet and the bucket, 64x64 pixels each
dropImage = new Texture(Gdx.files.internal("droplet.png"));
bucketImage = new Texture(Gdx.files.internal("bucket.png"));

// load the drop sound effect and the rain background "music"
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));

// start the playback of the background music immediately
rainMusic.setLooping(true);
rainMusic.play();

// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();

// create a Rectangle to logically represent the bucket
bucket = new Rectangle();
bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
bucket.width = 64;
bucket.height = 64;

// create the raindrops array and spawn the first raindrop
raindrops = new Array<Rectangle>();
spawnRaindrop();
}

private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(0, 800-64);
raindrop.y = 480;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}

@Override
public void render() {
// clear the screen with a dark blue color. The
// arguments to glClearColor are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

// tell the camera to update its matrices.
camera.update();

// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
batch.setProjectionMatrix(camera.combined);

// begin a new batch and draw the bucket and
// all drops
batch.begin();
batch.draw(bucketImage, bucket.x, bucket.y);
for(Rectangle raindrop: raindrops) {
batch.draw(dropImage, raindrop.x, raindrop.y);
}
batch.end();

// process user input
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
bucket.x = touchPos.x - 64 / 2;
}
if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();

// make sure the bucket stays within the screen bounds
if(bucket.x < 0) bucket.x = 0;
if(bucket.x > 800 - 64) bucket.x = 800 - 64;

// check if we need to create a new raindrop
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();

// move the raindrops, remove any that are beneath the bottom edge of
// the screen or that hit the bucket. In the later case we play back
// a sound effect as well.
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y + 64 < 0) iter.remove();
if(raindrop.overlaps(bucket)) {
dropSound.play();
iter.remove();
}
}
}

@Override
public void dispose() {
// dispose of all the native resources
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
batch.dispose();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
}

最佳答案

Here是指向 libGDX 用户 wiki 的链接,当我遇到相同的 Pong 重制问题时,我发现它很有帮助。

要显示分数,您需要的工具是:一个用于保存分数的 int 变量,一个用于帮助显示分数的 String 或 CharacterSequence 变量(我将使用 String),以及一个用于显示字体类型。

第一步:声明所有这些工具。

private int score;
private String yourScoreName;
BitmapFont yourBitmapFontName;

第二步:在create方法中初始化它们

public void create()     
score = 0;
yourScoreName = "score: 0";
yourBitmapFontName = new BitmapFont();

第三步:递增 score 变量并更改碰撞逻辑方法中的 yourScoreName String 变量(当雨滴与水桶重叠时)。

if(raindrop.overlaps(bucket)) {
score++;
yourScoreName = "score: " + score;
dropSound.play();
iter.remove();

第四步:在 render() 方法中,设置字体的颜色并在 spriteBatch.begin 和 spriteBatch.end 之间调用 BitmapFont 上的 draw 方法。

batch.begin(); 
yourBitmapFontName.setColor(1.0f, 1.0f, 1.0f, 1.0f);
yourBitmapFontName.draw(batch, yourScoreName, 25, 100);
batch.end();

使用:font.setColor() 方法中的参数,以便您可以看到它们与背景颜色的对比,font.draw() 方法中的数字参数,以获取顶部显示的分数相关纹理左上角(font.draw()方法中最后两个数字参数代表得分纹理的x和y坐标)。

第五步:运行它,然后微笑。

相同的逻辑将适用于显示“游戏结束”。出于启发式目的,我会将逻辑的创建留给您。

阅读 link 中的文档以更深入地了解魔法。

关于java - libGDX - 添加分数并将其显示在屏幕的左上角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17127201/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com