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的Screen对象设为一个MainMenuScreen对象,并把一个Drop实例作为它唯一的参数。

Game实现中一个很常见的错误就是忘记调用super.render()。不调用它的话,create()方法中设置的Screen就不会被渲染。

最后,记得销毁这些对象。 Further reading.

主菜单

现在来看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抽象类的内容,以及创建多界面多状态的游戏。

如需完整代码,请访问: this Github gist

未来

现在,你掌握了多画面的知识,是时候好好利用它。当然为了更真实一些,学习 Scene2d Scene2D.ui Skins 让主菜单更漂亮一些,或者为drop游戏添加一些爆炸。

如果你已经读过Drop手册的后文,你应该已经准备好创建自己的游戏了。最好的办法就是走出去干,所以干吧!

猜你喜欢

转载自blog.csdn.net/bob007abc/article/details/9370087