Продолжаем серию уроков о том, как сделать игру 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 {
private static final int MOVEMENT = 100;
private 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(MOVEMENT * dt, 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 com.badlogic.gdx.utils.Array;
import info.fandroid.game.FlappyDemo;
import info.fandroid.game.sprites.Bird;
import info.fandroid.game.sprites.Tube;
/**
* Created by Vitaly on 06.11.2015.
*/
public class PlayState extends State {
private static final int TUBE_SPACING = 125;
private static final int TUBE_COUNT = 4;
private Bird bird;
private Texture bg;
private Array<Tube> tubes;
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");
tubes = new Array<Tube>();
for (int i = 0; i < TUBE_COUNT; i++){
tubes.add(new Tube(i * (TUBE_SPACING + Tube.TUBE_WIDTH)));
}
}
@Override
protected void handleInput() {
if (Gdx.input.justTouched())
bird.jump();
}
@Override
public void update(float dt) {
handleInput();
bird.update(dt);
camera.position.x = bird.getPosition().x + 80;
for (Tube tube : tubes){
if (camera.position.x - (camera.viewportWidth / 2) > tube.getPosTopTube().x + tube.getTopTube().getWidth()){
tube.reposition(tube.getPosTopTube().x + ((Tube.TUBE_WIDTH + TUBE_SPACING) * TUBE_COUNT));
}
}
camera.update();
}
@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);
for (Tube tube : tubes) {
sb.draw(tube.getTopTube(), tube.getPosBotTube().x, tube.getPosTopTube().y);
sb.draw(tube.getBottomTube(), tube.getPosBotTube().x, tube.getPosBotTube().y);
}
sb.end();
}
@Override
public void dispose() {
}
}
Tube.java
package info.fandroid.game.sprites;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import java.util.Random;
/**
* Created by Vitaly on 13.11.2015.
*/
public class Tube {
public static final int TUBE_WIDTH = 52;
private static final int FLUCTUATION = 130;
private static final int TUBE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture topTube, bottomTube;
private Vector2 posTopTube, posBotTube;
private Random rand;
public Texture getTopTube() {
return topTube;
}
public Texture getBottomTube() {
return bottomTube;
}
public Vector2 getPosTopTube() {
return posTopTube;
}
public Vector2 getPosBotTube() {
return posBotTube;
}
public Tube(float x){
topTube = new Texture("toptube.png");
bottomTube = new Texture("bottomtube.png");
rand = new Random();
posTopTube = new Vector2(x, rand.nextInt(FLUCTUATION) + TUBE_GAP + LOWEST_OPENING);
posBotTube = new Vector2(x, posTopTube.y - TUBE_GAP - bottomTube.getHeight());
}
public void reposition(float x){
posTopTube.set(x, rand.nextInt(FLUCTUATION) + TUBE_GAP + LOWEST_OPENING);
posBotTube.set(x, posTopTube.y - TUBE_GAP - bottomTube.getHeight());
}
}
Больше уроков:
Уроки по android разработке: тут
Дизайн android приложений: тут
Основы программирования на JAVA: тут
Инструменты android разработчика: тут
<<Урок 7. Flappy Bird: научим птичку летать | Делаем android игры на LibGDX
Урок 9. Flappy Bird: реализуем обнаружение столкновений птицы с трубами>>
Почему FLUCTUATION 130 ? как это понять)
Попробуйте изменить значение и поймете