以下是libGDX中简单游戏的示例代码。 当雨滴落入水桶时,分数应增加1。 总分应显示在左上角。
如果错过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() {
}
}
答案 0 :(得分:14)
Here是libGDX用户wiki的链接,当我遇到Pong翻拍的相同问题时,我发现它很有帮助。
要显示分数,您需要的工具是:保持分数的int变量,帮助显示分数的String或CharacterSequence变量(我将使用String),以及用于显示分数的BitmapFont字体类型。
第一步:声明所有这些工具。
private int score;
private String yourScoreName;
BitmapFont yourBitmapFontName;
第二步:在create方法
中初始化它们public void create()
score = 0;
yourScoreName = "score: 0";
yourBitmapFontName = new BitmapFont();
第三步:增加得分变量并在碰撞逻辑方法中更改yourScoreName字符串变量(当雨滴与存储桶重叠时)。
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坐标。)
第五步:运行它,然后微笑。
同样的逻辑将适用于显示“GAME OVER”。出于启发式的目的,我将把逻辑的创建留给你。
阅读link中的文档,以深入了解魔法。
答案 1 :(得分:9)
要显示分数,您可以创建一个舞台。
Stage stage = new Stage();
stage = new Stage();
stage.setCamera(cam);
stage.setViewport(WIDTH, HEIGHT, false);
然后你必须创建一个标签和一个文本字体。因此,您可以在the libgdx wiki上查看详细信息。
Label text;
LabelStyle textStyle;
BitmapFont font = new BitmapFont();
//font.setUseIntegerPositions(false);(Optional)
textStyle = new LabelStyle();
textStyle.font = font;
text = new Label("Gamever",textStyle);
text.setBounds(0,.2f,Room.WIDTH,2);
text.setFontScale(1f,1f);
然后在游戏丢失时将其添加到舞台上:
stage.addActor(text);