1 | public final Bob bob; //主角 |
1 | this .bob = new Bob( 5 , 1 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /**刷新界面**/
public void update(float deltaTime, float accelX) {
updateBob(deltaTime, accelX); //刷新主角
updatePlatforms(deltaTime); //刷新跳板
}
/**刷新Bob**/
private void updateBob(float deltaTime, float accelX) {
//碰撞跳板
if (bob.state != Bob.BOB_STATE_HIT && bob.position.y <= 0 .5f) bob.hitPlatform();
//主角x轴方向移动的速度
if (bob.state != Bob.BOB_STATE_HIT) bob.velocity.x = -accelX / 10 * Bob.BOB_MOVE_VELOCITY;
bob.update(deltaTime);
//竖直最大高度
heightSoFar = Math.max(bob.position.y, heightSoFar);
} |
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 | /**渲染游戏中各种物体(Bob,跳板,松鼠,弹簧,城堡,金币)**/
private void renderObjects() {
batch.enableBlending();
batch.begin();
//绘制跳板
renderPlatforms();
//绘制主角
renderBob();
batch.end();
}
/**渲染主角**/
private void renderBob() {
TextureRegion keyFrame; //对应状态下的图片
switch (world.bob.state) {
case Bob.BOB_STATE_FALL:
keyFrame = Assets.bobFall.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
break ;
case Bob.BOB_STATE_JUMP:
keyFrame = Assets.bobJump.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
break ;
case Bob.BOB_STATE_HIT:
default :
keyFrame = Assets.bobHit;
}
float side = world.bob.velocity.x < 0 ? - 1 : 1 ;
if (side < 0 )
batch.draw(keyFrame, world.bob.position.x + 0 .5f, world.bob.position.y - 0 .5f, side * 1 , 1 );
else
batch.draw(keyFrame, world.bob.position.x - 0 .5f, world.bob.position.y - 0 .5f, side * 1 , 1 );
} |
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 | package com.zhf.mylibgdx; import com.badlogic.gdx.graphics.g2d.TextureRegion; /**
* 动画类
* @author ZHF
*
*/ public class Animation {
//动画的两种状态,循环和不循环(不循环则动画播放完毕后停留在最后一张图片上)
public static final int ANIMATION_LOOPING = 0 ;
public static final int ANIMATION_NONLOOPING = 1 ;
//存储图片的数组
final TextureRegion[] keyFrames;
//动画每帧持续时间
final float frameDuriation;
//在Assets中调用,大家自行参看
public Animation(float frameDuration, TextureRegion... keyFrames) {
this .frameDuriation = frameDuration;
this .keyFrames = keyFrames;
}
/**
* 截取动画图片
* @param stateTime 存在此状态的时间
* @param mode 动画模式
* @return
*/
public TextureRegion getKeyFrame(float stateTime, int mode) {
//需要返回的图片在数组中的位置
int frameNumber = ( int ) (stateTime / frameDuriation);
//不循环,停留在最后一张图片上
if (mode == ANIMATION_NONLOOPING) {
frameNumber = Math.min(keyFrames.length - 1 , frameNumber);
} else {
//循环模式
frameNumber = frameNumber % keyFrames.length;
}
//返回keyFrames数组
return keyFrames[frameNumber];
} } |
1 2 3 4 | //主角
public static Animation bobJump; //跳跃的动画
public static Animation bobFall; //下落的动画
public static TextureRegion bobHit; //碰撞图片 |
1 2 3 4 | //主角
bobJump = new Animation( 0 .2f, new TextureRegion(items, 0 , 128 , 32 , 32 ), new TextureRegion(items, 32 , 128 , 32 , 32 ));
bobFall = new Animation( 0 .2f, new TextureRegion(items, 64 , 128 , 32 , 32 ), new TextureRegion(items, 96 , 128 , 32 , 32 ));
bobHit = new TextureRegion(items, 128 , 128 , 32 , 32 ); |
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 | /**游戏运行状态**/
private void updateRunning ( float deltaTime) {
if (Gdx.input.justTouched()) {
guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0 ));
//点击暂停
if (OverlapTester.pointInRectangle(pauseBounds, touchPoint.x, touchPoint.y)) {
Assets.playSound(Assets.clickSound);
state = GAME_PAUSED;
return ;
}
}
ApplicationType appType = Gdx.app.getType();
// should work also with Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer)
if (appType == ApplicationType.Android || appType == ApplicationType.iOS) {
world.update(deltaTime, Gdx.input.getAccelerometerX());
} else {
float accel = 0 ;
if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) accel = 5f;
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) accel = -5f;
world.update(deltaTime, accel);
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /**生成关卡中除了Bob外所有的物体**/
private void generateLevel() {
float y = Platform.PLATFORM_HEIGHT / 2 ;
float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / ( 2 * -gravity.y);
while (y < WORLD_HEIGHT - WORLD_WIDTH / 2 ) {
int type = rand.nextFloat() > 0 .8f ? Platform.PLATFORM_TYPE_MOVING : Platform.PLATFORM_TYPE_STATIC;
float x = rand.nextFloat() * (WORLD_WIDTH - Platform.PLATFORM_WIDTH) + Platform.PLATFORM_WIDTH / 2 ;
Platform platform = new Platform(type, x, y);
platforms.add(platform);
y += (maxJumpHeight - 0 .5f);
y -= rand.nextFloat() * (maxJumpHeight / 3 );
}
} |
1 | float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / ( 2 * -gravity.y); |
1 | public static final Vector2 gravity = new Vector2( 0 , - 12 ); |
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 | /**刷新界面**/ public void update( float deltaTime, float accelX) {
updateBob(deltaTime, accelX); //刷新主角
updatePlatforms(deltaTime); //刷新跳板
if (bob.state != Bob.BOB_STATE_HIT) checkCollisions(); } /**碰撞检测**/ private void checkCollisions() {
// TODO Auto-generated method stub
checkPlatformCollisions(); //跳板的碰撞 } private void checkPlatformCollisions() {
if (bob.velocity.y > 0 ) return ;
int len = platforms.size();
for ( int i = 0 ; i < len; i++) {
Platform platform = platforms.get(i);
if (bob.position.y > platform.position.y) {
//调用工具类中矩形块碰撞检测
if (OverlapTester.overlapRectangles(bob.bounds, platform.bounds)) {
bob.hitPlatform();
listener.jump();
if (rand.nextFloat() > 0 .5f) {
platform.pulverize();
}
break ;
}
}
} } |
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 | package com.zhf.mylibgdx; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; /**
* 工具类:检测各种碰撞
* @author ZHF
*
*/ public class OverlapTester {
/**检测输入的X,Y是否在输入的矩形框r内**/
public static boolean pointInRectangle(Rectangle r, float x, float y) {
return r.x <= x && r.x + r.width >= x && r.y <= y
&& r.y + r.height >= y;
}
/**两个矩形块的碰撞检测**/
public static boolean overlapRectangles (Rectangle r1, Rectangle r2) {
if (r1.x < r2.x + r2.width && r1.x + r1.width > r2.x && r1.y < r2.y + r2.height && r1.y + r1.height > r2.y)
return true ;
else
return false ;
}
/**点是否在矩形内**/
public static boolean pointInRectangle (Rectangle r, Vector2 p) {
return r.x <= p.x && r.x + r.width >= p.x && r.y <= p.y && r.y + r.height >= p.y;
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /**渲染**/
public void render () {
//重点讲解
if (world.bob.position.y > cam.position.y) cam.position.y = world.bob.position.y;
cam.update();
//它的作用都是通过把映射矩阵绑定给SpritBatch,告诉SpritBatch怎么去绘制图形
batch.setProjectionMatrix(cam.combined);
//渲染背景
renderBackground();
//渲染游戏中各种元素(Bob,跳板,松鼠,弹簧。。)下一讲中会具体讲到
renderObjects();
} |