# libgdx示例-SuperJumper分析 3. 游戏中的物体与主游戏逻辑

shines 分享于 2012-01-28

经过之前两节的分析，现在，游戏的资源加载和初始化已经完成，并且除了主游戏界面，其他界面也已经绘制完成，接下来就游戏的主界面的实现了。先讲一下作者是怎么定义游戏中的物体。

```public class GameObject {
public final Vector2 position;
public final Rectangle bounds;

public GameObject(float x, float y, float width, float height) {
this.position = new Vector2(x,y);
this.bounds = new Rectangle(x-width/2, y-height/2, width, height);
}
}

```

```public class DynamicGameObject extends GameObject {
public final Vector2 velocity;
public final Vector2 accel;

public DynamicGameObject(float x, float y, float width, float height) {
super(x, y, width, height);
velocity = new Vector2();
accel = new Vector2();
}
}

```

Bob ，主角        Castle ，城堡        Coin， 金币        Platform ， 平台        Spring ， 弹簧        Squirrel ，空中飞行的松鼠

```    static final int GAME_READY = 0;
static final int GAME_RUNNING = 1;
static final int GAME_PAUSED = 2;
static final int GAME_LEVEL_END = 3;
static final int GAME_OVER = 4;

int state;

```

```@Override public void present (float deltaTime) {
GLCommon gl = Gdx.gl;
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL10.GL_TEXTURE_2D);

renderer.render();

guiCam.update();
batcher.setProjectionMatrix(guiCam.combined);
batcher.enableBlending();
batcher.begin();
switch (state) {
break;
case GAME_RUNNING:
presentRunning();
break;
case GAME_PAUSED:
presentPaused();
break;
case GAME_LEVEL_END:
presentLevelEnd();
break;
case GAME_OVER:
presentGameOver();
break;
}
batcher.end();
}

```

```@Override public void update (float deltaTime) {
if (deltaTime > 0.1f) deltaTime = 0.1f;

switch (state) {
break;
case GAME_RUNNING:
updateRunning(deltaTime);
break;
case GAME_PAUSED:
updatePaused();
break;
case GAME_LEVEL_END:
updateLevelEnd();
break;
case GAME_OVER:
updateGameOver();
break;
}
}
```

```private void updateReady () {
if (Gdx.input.justTouched()) {
state = GAME_RUNNING;
}
}```

```    OrthographicCamera guiCam;
Vector3 touchPoint;
SpriteBatch batcher;
World world;
WorldListener worldListener;
WorldRenderer renderer;
Rectangle pauseBounds;
Rectangle resumeBounds;
Rectangle quitBounds;
int lastScore;
String scoreString;

public GameScreen (Game game) {
super(game);
guiCam = new OrthographicCamera(320, 480);
guiCam.position.set(320 / 2, 480 / 2, 0);
touchPoint = new Vector3();
batcher = new SpriteBatch();
worldListener = new WorldListener() {
@Override public void jump () {
Assets.playSound(Assets.jumpSound);
}

@Override public void highJump () {
Assets.playSound(Assets.highJumpSound);
}

@Override public void hit () {
Assets.playSound(Assets.hitSound);
}

@Override public void coin () {
Assets.playSound(Assets.coinSound);
}
};

world = new World(worldListener);
renderer = new WorldRenderer(batcher, world);
pauseBounds = new Rectangle(320 - 64, 480 - 64, 64, 64);
resumeBounds = new Rectangle(160 - 96, 240, 192, 36);
quitBounds = new Rectangle(160 - 96, 240 - 36, 192, 36);
lastScore = 0;
scoreString = "SCORE: 0";
}

```
除了常规的guiCam ，touchPoint， batcher 以及一些用于检测触屏区域的Bounds外，还多了三个其他Screen没有的类，分别是：World，WorldRenderer，以及WorldListener。

```    guiCam = new OrthographicCamera(320, 480);
guiCam.position.set(320 / 2, 480 / 2, 0);

```

```    static final float FRUSTUM_WIDTH = 10;
static final float FRUSTUM_HEIGHT = 15;

OrthographicCamera cam;

public WorldRenderer (SpriteBatch batch, World world) {
this.world = world;
this.cam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
this.cam.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);
this.batch = batch;
}
```

```    guiCam.update();
batcher.setProjectionMatrix(guiCam.combined);
batcher.begin();
batcher.draw(...)
batcher.end();
```

```    @Override public void present (float deltaTime) {
GLCommon gl = Gdx.gl;
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL10.GL_TEXTURE_2D);

renderer.render();

guiCam.update();
batcher.setProjectionMatrix(guiCam.combined);
batcher.enableBlending();
batcher.begin();
switch (state) {
break;
case GAME_RUNNING:
presentRunning();
break;
case GAME_PAUSED:
presentPaused();
break;
case GAME_LEVEL_END:
presentLevelEnd();
break;
case GAME_OVER:
presentGameOver();
break;
}
batcher.end();
}

batcher.draw(Assets.ready, 160 - 192 / 2, 240 - 32 / 2, 192, 32);   //在屏幕中间绘制Ready字符
}
```

public static final float WORLD_WIDTH = 10;       public static final float WORLD_HEIGHT = 15 * 20;

经过之前两节的分析，现在，游戏的资源加载和初始化已经完成，并且除了主游戏界面，其他界面也已经绘制完成，接下来就游戏的主界面的实现了。先讲一下作者是怎么定义游戏中的物体。 首

×
• 登录
• 注册

×