【问题标题】:libGDX - Add scores & display it at top left corner of the screen?libGDX - 添加分数并将其显示在屏幕的左上角?
【发布时间】:2013-06-15 19:36:01
【问题描述】:

这是 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() {
}
}

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    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();
    

    第三步:增加 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坐标)。

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

    同样的逻辑也适用于显示“GAME OVER”。出于启发式目的,我将由您来创建逻辑。

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

    【讨论】:

    • 我已经尝试过您所说的相同逻辑。但是,font.draw(batch, yourScoreName, 25, 100);给出一个错误。
    • @Shruti 我对代码做了一些小的修改,然后自己制作了游戏。它现在将运行。当代码也为你运行时选择我的答案。
    • 与其维护分数字符串,不如使用 BitmapFontCache 更有效。因此,只需保留一个整数分数,并在它更改时更新 BitmapFontCache 对象。在您的 render() 方法中,只需 draw() BitmapFontCache 对象。
    【解决方案2】:

    要显示分数,您可以创建一个舞台。

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 2012-03-09
      • 2015-09-28
      • 1970-01-01
      • 2011-07-29
      相关资源
      最近更新 更多