Libgdx Developer's Guide(Libgdx開發者手冊)-9(一個簡單的遊戲2--擴充套件示例遊戲)
這篇文章的目的是擴充套件我們上次建立的遊戲"Drop"。我們要新增一個選單頁面和一對功能來讓遊戲更有趣一些。
讓我們從向遊戲中引入幾個高階類開始。
Screens 介面
Screens 對於多元件的遊戲非常重要。Screens包含了許多在ApplicationListener中所用的方法,幷包含一對新方法show 和 hide,它們分別在Screen獲取和失去焦點時呼叫。
Game 類
Game抽象類提供了一個ApplicationListener的實現,還有一些幫助方法來設定和處理Screen渲染。
總之,Screen 和 Game物件被用來建立簡單而強大的遊戲結構。
我們從建立一個Game物件開始,它將作為遊戲的入口。
讓我們看些程式碼:
package com.badlogic.drop; import com.badlogic.gdx.Game; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class Drop extends Game { SpriteBatch batch; BitmapFont font; public void create() { batch = new SpriteBatch(); //Use LibGDX's default Arial font. font = new BitmapFont(); this.setScreen(new MainMenuScreen(this)); } public void render() { super.render(); } public void dispose() { batch.dispose(); font.dispose(); } }
一開始我們例項化一個SpriteBatch和一個BitmapFont。建立多個事實上可共享的物件是很差的一種實踐(see DRY)。SpriteBatch物件用來在螢幕上渲染物件,如紋理; BitmapFont物件以及SpriteBatch被用來在螢幕渲染文字。關於這方面內容我們會在Screen類中詳述。
Game實現中一個很常見的錯誤就是忘記呼叫super.render()。不呼叫它的話,create()方法中設定的Screen就不會被渲染。
最後,記得銷燬這些物件。
主選單
現在來看MainMenuScreen類。
package com.badlogic.drop;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class MainMenuScreen implements Screen {
final Drop game;
OrthographicCamera camera;
public MainMenuScreen(final Drop gam) {
game = gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
//...Rest of class omitted for succinctness.
}
在這個程式碼段裡,我們建立了MainMenuScreen的構造器,MainMenuScreen同時實現了Screen介面。Screen介面不提供任何形式的create()方法,因此我們轉而用了構造器。這個構造器唯一需要的引數就是一個Drop例項,以便我們可以在需要時呼叫它的方法的欄位。
接下來,最後MainMenuScreen類裡很“肉”的一個方法是:render(float)
public class MainMenuScreen implements Screen {
//public MainMenuScreen(final Drop gam)....
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch, "Welcome to Drop!!! ", 100, 150);
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
game.batch.end();
if (Gdx.input.isTouched()) {
game.setScreen(new GameScreen(game));
dispose();
}
}
//Rest of class still omitted...
}
這塊程式碼相當簡單,除了我們需要呼叫遊戲的SpriteBatch 和 BitmapFont例項而非建立自己的。game.font.draw(SpriteBatch, String, float,float)實現把文字渲染到螢幕。 LibGDX 自帶了預置的字型,Arial , 因此你可以用預設構造器而仍然獲得一個字型。
然後我們檢視螢幕是否並觸控,如果是,我們裝置遊戲的螢幕為一個GameScreen例項,然後銷燬當前MainMenuScreen例項。MainMenuScreen中需要實現的其他方法都留空,因此繼續忽略它們(該類沒什麼要銷燬的)。
遊戲畫面
現在完成了主選單,是時候建立我們的遊戲了。我們將略去上一章中該遊戲的大部分程式碼以避免冗餘,以及避免不得不思考一個不同的Drop遊戲實現。
package com.badlogic.drop;
import java.util.Iterator;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
public class GameScreen implements Screen {
final Drop game;
Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;
OrthographicCamera camera;
Rectangle bucket;
Array<Rectangle> raindrops;
long lastDropTime;
int dropsGathered;
public GameScreen(final Drop gam) {
this.game = gam;
// 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"));
rainMusic.setLooping(true);
// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
// 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(float delta) {
// 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.
game.batch.setProjectionMatrix(camera.combined);
// begin a new batch and draw the bucket and
// all drops
game.batch.begin();
game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
game.batch.draw(bucketImage, bucket.x, bucket.y);
for (Rectangle raindrop : raindrops) {
game.batch.draw(dropImage, raindrop.x, raindrop.y);
}
game.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)) {
dropsGathered++;
dropSound.play();
iter.remove();
}
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
// start the playback of the background music
// when the screen is shown
rainMusic.play();
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
}
}
這個程式碼和以前的實現有 95% 是一樣的,除了現在我們用構造器代替了ApplicatonListener的create()方法,並傳入一個Drop物件,像在MainMenuScreen類中一樣。當Screen被設定為GameScreen時我們將同時播放音樂。
我們也在遊戲左上角添加了一個字串,來跟蹤收集到的雨滴數。
到這裡你就完成了整個遊戲。這就是所有需要理解的關於Screen介面和 Game抽象類的內容,以及建立多介面多狀態的遊戲。
未來
現在,你掌握了多畫面的知識,是時候好好利用它。當然為了更真實一些,學習 Scene2d Scene2D.ui 和 Skins 讓主選單更漂亮一些,或者為drop遊戲新增一些爆炸。
如果你已經讀過Drop手冊的後文,你應該已經準備好建立自己的遊戲了。最好的辦法就是走出去幹,所以幹吧!