Продолжаем серию уроков о том, как сделать игру Flappy Bird для android. На этом уроке напишем экран игры PlayState и добавим в игру птицу — создадим класс Bird, который опишет нашу птичку и ее поведение.
Исходный код под видео
MenuState.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 |
package info.fandroid.game.states; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import info.fandroid.game.FlappyDemo; /** * Created by Vitaly on 28.10.2015. */ public class MenuState extends State { private Texture background; private Texture playBtn; public MenuState(GameStateManager gsm) { super(gsm); background = new Texture("bg.png"); playBtn = new Texture("playbtn.png"); } @Override public void handleInput() { if(Gdx.input.justTouched()){ gsm.set(new PlayState(gsm)); } } @Override public void update(float dt) { handleInput(); } @Override public void render(SpriteBatch sb) { sb.begin(); sb.draw(background, 0, 0, FlappyDemo.WIDTH, FlappyDemo.HEIGHT); sb.draw(playBtn, (FlappyDemo.WIDTH / 2) - (playBtn.getWidth() / 2), FlappyDemo.HEIGHT / 2); sb.end(); } @Override public void dispose() { background.dispose(); playBtn.dispose(); } } |
PlayState.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 |
package info.fandroid.game.states; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import info.fandroid.game.FlappyDemo; import info.fandroid.game.sprites.Bird; /** * Created by Vitaly on 06.11.2015. */ public class PlayState extends State { private Bird bird; public PlayState(GameStateManager gsm) { super(gsm); bird = new Bird(50, 300); camera.setToOrtho(false, FlappyDemo.WIDTH / 2, FlappyDemo.HEIGHT / 2); } @Override protected void handleInput() { } @Override public void update(float dt) { bird.update(dt); } @Override public void render(SpriteBatch sb) { sb.setProjectionMatrix(camera.combined); sb.begin(); sb.draw(bird.getBird(), bird.getPosition().x, bird.getPosition().y); sb.end(); } @Override public void dispose() { } } |
Bird.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 |
package info.fandroid.game.sprites; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.Vector3; /** * Created by Vitaly on 06.11.2015. */ public class Bird { public static final int GRAVITY = -15; private Vector3 position; private Vector3 velosity; private Texture bird; public Bird(int x, int y){ position = new Vector3(x, y, 0); velosity = new Vector3(0, 0, 0); bird = new Texture("bird.png"); } public Vector3 getPosition() { return position; } public Texture getBird() { return bird; } public void update(float dt){ velosity.add(0, GRAVITY, 0); velosity.scl(dt); position.add(0, velosity.y, 0); velosity.scl(1 / dt); } } |
Больше уроков:
Уроки по android разработке: тут
Дизайн android приложений: тут
Основы программирования на JAVA: тут
Инструменты android разработчика: тут
<< LibGDX: Урок 5. Пишем игру Flappy Bird для андроид | Делаем android игры
Урок 7. Flappy Bird: научим птичку летать| Делаем android игры на LibGDX >>
ссылка на ресурсы игры не открывается
можете дать рабочую ссылку