1 ./ 初始化分辨率和方法的主类 /
public class Flappy2 extends ApplicationAdapter {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "Flappy 2";
private GameStateManager gsm;
private SpriteBatch batch;
@Override
public void create () {
batch = new SpriteBatch();
gsm = new GameStateManager();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime()); //update method
gsm.render(batch);
}
@Override
public void dispose () {
batch.dispose();
}
}
// Bird.class
public class Bird {
private static final int GRAVITY = -15;
private Vector3 position;
private Vector3 velocity;
private Texture bird;
public Bird(int x, int y){
position = new Vector3(x,y,0);
velocity = new Vector3(0,0,0);
bird = new Texture("bird.png");
}
public void update(float dt){
velocity.add(0, GRAVITY, 0);
velocity.scl(dt);
velocity.add(0, velocity.y, 0);
velocity.scl(1/dt);
}
public Texture getTexture() {
return bird;
}
public Vector3 getPosition() {
return position;
}
public void jump(){
velocity.y = 250; //the on-click jump
}
}
// GameStateManager.class
public class GameStateManager {
private Stack<State> states;
public GameStateManager(){
states = new Stack<State>();
}
public void push(State state){
states.push(state);
}
public void pop(){
states.pop();
}
public void set(State state){
states.pop();
states.push(state);
}
public void update(float dt){
states.peek().update(dt);
}
public void render(SpriteBatch sb){
states.peek().render(sb);
}
}
2 ./*菜单状态正常* /
public class MenuState extends State {
private Texture background;
private Texture playBtn;
public MenuState(GameStateManager gsm) {
super(gsm);
background = new Texture("bg.jpg");
playBtn = new Texture("playbtn.png"); /*menu state button*/
}
@Override
public void handleInput() {
if (Gdx.input.justTouched()) { /*input from user*/
gsm.set(new PlayState(gsm));
dispose();
}
}
@Override
public void update(float dt) {
handleInput();
}
@Override
public void dispose() {
background.dispose();
playBtn.dispose();
}
@Override
public void render(SpriteBatch sb) {
sb.begin();
sb.draw(background,0,0, Flappy2.WIDTH, Flappy2.HEIGHT);
sb.draw(playBtn, (Flappy2.WIDTH/2) - (playBtn.getWidth()/2), Flappy2.HEIGHT/2);
sb.end();
}
}
3 ./ 播放状态类(在菜单状态之后) /
public class PlayState extends State {
private Bird bird;
public PlayState(GameStateManager gsm) {
super(gsm);
bird = new Bird(50,300);
cam.setToOrtho(false, Flappy2.WIDTH/2, Flappy2.HEIGHT/2);
}
@Override
protected void handleInput() {
if(Gdx.input.justTouched()){ /*taking input from user*/
bird.jump();
}
}
@Override
public void update(float dt) {
handleInput();
bird.update(dt);
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bird.getTexture(), bird.getPosition().x, bird.getPosition().y);
sb.end();
}
@Override
public void dispose() {
}
}
/ 4.抽象状态类 /
public abstract class State {
protected OrthographicCamera cam;
protected Vector3 mouse;
protected GameStateManager gsm;
protected State(GameStateManager gsm){
this.gsm = gsm;
cam = new OrthographicCamera(); /*Camera init*/
mouse = new Vector3();/*vector3 object initialised*/
}
protected abstract void handleInput();/*method handles i/p*/
public abstract void update(float dt);/*takes a deltatime argument*/
public abstract void render(SpriteBatch sb);
public abstract void dispose();
}
/* Build.Gradle */ /*build gradle file */
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = "Flappy2"
gdxVersion = '1.9.4'
roboVMVersion = '2.2.0'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
}
}
tasks.eclipse.doLast {
delete ".project"
}
答案 0 :(得分:0)
从我所看到的,速度并没有应用于任何地方的鸟的位置。你是在bird.getPosition()画鸟,但位置总是(50,300)。
将计算出的速度添加到Bird的更新方法中的位置。
position.add(velocity);