Как добавить экран меню Screen и счетчик прогресса в игру для андроид с LibGDX — мультиплатформенным фреймворком для создания игр, мы покажем в этом уроке на примере усовершенствования простой игры.
Полный код игры под видео:
Drop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package info.fandroid.drop; import com.badlogic.gdx.Game; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; /** * Created by Vitaly on 13.10.2015. */ public class Drop extends Game { SpriteBatch batch; BitmapFont font; @Override public void create() { batch = new SpriteBatch(); font = new BitmapFont(); this.setScreen(new MainMenuScreen(this)); } @Override public void render() { super.render(); } @Override public void dispose() { super.dispose(); batch.dispose(); font.dispose(); } } |
GameScreen.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
package info.fandroid.drop; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.Screen; import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.GL20; 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; import java.util.Iterator; public class GameScreen implements Screen { final Drop game; OrthographicCamera camera; Texture dropImage; Texture bucketImage; Sound dropSound; Music rainMusic; Rectangle bucket; Vector3 touchPos; Array<Rectangle> raindrops; long lastDropTime; int dropsGatchered; String dropString; public GameScreen (final Drop gam) { this.game = gam; camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); touchPos = new Vector3(); dropImage = new Texture("droplet.png"); bucketImage = new Texture("bucket.png"); dropSound = Gdx.audio.newSound(Gdx.files.internal("waterdrop.wav")); rainMusic = Gdx.audio.newMusic(Gdx.files.internal("undertreeinrain.mp3")); rainMusic.setLooping(true); rainMusic.play(); bucket = new Rectangle(); bucket.x = 800 / 2 - 64 / 2; bucket.y = 20; bucket.width = 64; bucket.height = 64; 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) { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); game.batch.setProjectionMatrix(camera.combined); game.batch.begin(); game.font.draw(game.batch, "Drops Collected: " + dropsGatchered, 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(); if(Gdx.input.isTouched()){ touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPos); bucket.x = (int) (touchPos.x -64 / 2); } if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime(); if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime(); if (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)){ dropsGatchered++; dropSound.play(); iter.remove(); } } } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { dropImage.dispose(); bucketImage.dispose(); dropSound.dispose(); rainMusic.dispose(); } @Override public void show() { rainMusic.play(); } } |
MainMenuScreen.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package info.fandroid.drop; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; /** * Created by Vitaly on 13.10.2015. */ 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); } @Override public void show() { } @Override public void render(float delta) { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.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(); } } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } } |
Больше уроков:
Уроки Android Studio: тут
Инструменты android разработчика: тут
Дизайн android приложений: тут
Уроки создания игр для android: тут
Основы программирования на JAVA: тут
<<LibGDX: Урок 3. Создаем простую игру | Делаем android игры
LibGDX: Урок 5. Пишем игру Flappy Bird для андроид | Делаем android игры>>