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