libGDX - 添加分数&显示在屏幕的左上角? [英] libGDX - Add scores & display it at top left corner of the screen?

查看:155
本文介绍了libGDX - 添加分数&显示在屏幕的左上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是libGDX中简单游戏的示例代码。
当雨水落入水桶时,分数应增加1。
总分应显示在左上角。



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

  public class Drop implements ApplicationListener {
Texture dropImage;
纹理bucketImage;
Sound dropSound;
音乐雨音乐;
SpriteBatch批次;
Orthographic相机;
矩形桶;
数组<矩形>雨滴;
long lastDropTime;

@Override
public void create(){
//加载Droplet和bucket的图像,每个64x64像素
dropImage = new Texture(Gdx。 files.internal( droplet.png));
bucketImage = new Texture(Gdx.files.internal(bucket.png));

//加载掉落声音效果和降雨背景music
dropSound = Gdx.audio.newSound(Gdx.files.internal(drop.wav));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal(rain.mp3));

//立即开始播放背景音乐
rainMusic.setLooping(true);
rainMusic.play();

//创建相机和SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false,800,480);
batch = new SpriteBatch();

//创建一个Rectangle来逻辑地表示存储桶
bucket = new Rectangle();
bucket.x = 800/2 - 64/2; //水平居中桶
bucket.y = 20; //桶的左下角是底部屏幕边缘上方20像素
bucket.width = 64;
bucket.height = 64;

//创建raindrops数组并生成第一个雨滴
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(雨滴);
lastDropTime = TimeUtils.nanoTime();
}

@Override
public void render(){
//用深蓝色清除屏幕。 glClearColor的
//参数是用于清除屏幕的颜色[0,1]
//范围内的红色,绿色
//蓝色和alpha分量。
Gdx.gl.glClearColor(0,0,0.2f,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

//告诉相机更新其矩阵。
camera.update();

//告诉SpriteBatch在摄像机指定的
//坐标系中渲染。
batch.setProjectionMatrix(camera.combined);

//开始一个新批次并绘制桶并且
//所有下降
batch.begin();
batch.draw(bucketImage,bucket.x,bucket.y);
for(Rectangle raindrop:raindrops){
batch.draw(dropImage,raindrop.x,raindrop.y);
}
batch.end();

//处理用户输入
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();

//如果(bucket.x< 0)bucket.x = 0,请确保存储桶保持在屏幕界限
内;
if(bucket.x> 800 - 64)bucket.x = 800 - 64;

//检查我们是否需要创建一个新的雨滴
if(TimeUtils.nanoTime() - lastDropTime> 1000000000)spawnRaindrop();

//移动雨滴,移除
//屏幕底部边缘下面的任何内容或者点击桶中的任何内容。在后一种情况下,我们还会播放
//音效。
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(){
//处理所有原生资源
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(){
}
}


解决方案

这里是一个libGDX用户wiki的链接,当我遇到Pong翻拍的相同问题时,我发现它很有用。



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

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

  private int score; 
private String yourScoreName;
BitmapFont yourBitmapFontName;

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

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

第三步:增加得分变量并在碰撞逻辑方法中更改yourScoreName字符串变量(当雨滴重叠桶。)

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

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

  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坐标得分纹理)。



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



同样的逻辑将适用于显示, 游戏结束。出于启发式目的,我将为您创建逻辑。



阅读链接以深入了解魔术。


Here is the sample code of a simple game in libGDX. When a rain drop falls into a bucket,score should be increased by one. And total score should be shown at the top left corner.

If 3 drops are missed than display GAME OVER. I know to increase the score but i don't know to display it. Thank you.

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 is a link to a libGDX users wiki that I found helpful when I had encountered the same question for a Pong remake.

To display the score, the tools that you need are: an int variable to keep score, a String or CharacterSequence variable to help display the score (I'll use a String), and a BitmapFont which is used to display a font type.

First step: declare all of these tools.

private int score;
private String yourScoreName;
BitmapFont yourBitmapFontName;

Second step: initialize them in the create method

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

Tertiary step: increment the score variable and change the yourScoreName String variable in your collision logic method (when the raindrop overlaps the bucket).

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

Fourth step: In the render() method, set the color of the font and call the draw method on your BitmapFont between spriteBatch.begin and spriteBatch.end.

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

Play with: the parameters in the font.setColor() method so that you can see them in contrast to your background color, the number parameters in the font.draw() method to get the score related textures displayed in the top left corner (the last two number parameters in the font.draw() method represent the x and y coordinates of the score texture).

Fifth step: run it, then smile.

The same logic will apply to displaying, "GAME OVER." For heuristic purposes, I'll leave the creation of the logic up to you.

Read the documentation in the link to gain a deeper understanding of the magic.

这篇关于libGDX - 添加分数&amp;显示在屏幕的左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆