1

これは、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() {
}
}
4

2 に答える 2

15

これは、Pong のリメイクについて同じ質問に遭遇したときに役立つ libGDX ユーザー wiki へのリンクです

スコアを表示するために必要なツールは、スコアを保持するための int 変数、スコアを表示するための String または CharacterSequence 変数 (ここでは String を使用します)、およびフォント タイプを表示するために使用される BitmapFont です。

最初のステップ: これらのツールをすべて宣言します。

private int score;
private String yourScoreName;
BitmapFont yourBitmapFontName;

2 番目のステップ: create メソッドでそれらを初期化する

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

3 番目のステップ: スコア変数をインクリメントし、衝突ロジック メソッドで yourScoreName 文字列変数を変更します (雨滴がバケツに重なった場合)。

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

ステップ 4: 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() メソッドの number パラメーターを使用して、左上隅に表示されるスコア関連のテクスチャを取得します ( font.draw() メソッドの最後の 2 つの数値パラメーターは、スコア テクスチャの x 座標と y 座標を表します)。

5 番目のステップ: 実行してから、笑ってください。

「GAME OVER」の表示も同様です。ヒューリスティックな目的のために、ロジックの作成はあなたに任せます。

リンク先のドキュメントを読んで、魔法をより深く理解してください。

于 2013-06-15T21:45:16.170 に答える
10

スコアを表示するには、ステージを作成できます。

Stage stage = new Stage();
stage = new Stage();
stage.setCamera(cam);
stage.setViewport(WIDTH, HEIGHT, false);

次に、ラベルとテキスト フォントのようなものを作成する必要があります。そのため、詳細については 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);
于 2013-06-15T21:16:10.463 に答える