Commit 4cc7c4e2 authored by kingyuluk's avatar kingyuluk
Browse files

fix: 修复了某种情况下小鸟死后会继续上升的bug

parent aa7a828c
No preview for this file type
...@@ -9,8 +9,6 @@ import com.bird.util.Constant; ...@@ -9,8 +9,6 @@ import com.bird.util.Constant;
import com.bird.util.GameUtil; import com.bird.util.GameUtil;
import com.bird.util.MusicUtil; import com.bird.util.MusicUtil;
import static com.bird.util.GameUtil.drawTitle;
/** /**
* 小鸟类,小鸟的绘制与飞行逻辑都在此类 * 小鸟类,小鸟的绘制与飞行逻辑都在此类
* *
...@@ -81,11 +79,10 @@ public class Bird { ...@@ -81,11 +79,10 @@ public class Bird {
image = birdImages[STATE_UP][0]; image = birdImages[STATE_UP][0];
g.drawImage(image, x - halfImgWidth, y - halfImgHeight, null); // x坐标于窗口1/4处,y坐标位窗口中心 g.drawImage(image, x - halfImgWidth, y - halfImgHeight, null); // x坐标于窗口1/4处,y坐标位窗口中心
if (state == STATE_DEAD) { if (state == STATE_DEAD)
drawGameOver(g); drawGameOver(g);
} else if (state != STATE_FALL) { else if (state != STATE_FALL)
drawScore(g); drawScore(g);
}
// 绘制矩形 // 绘制矩形
// g.setColor(Color.black); // g.setColor(Color.black);
// g.drawRect((int) birdRect.getX(), (int) birdRect.getY(), (int) birdRect.getWidth(), (int) birdRect.getHeight()); // g.drawRect((int) birdRect.getX(), (int) birdRect.getY(), (int) birdRect.getWidth(), (int) birdRect.getHeight());
...@@ -117,57 +114,48 @@ public class Bird { ...@@ -117,57 +114,48 @@ public class Bird {
wingState++; wingState++;
image = birdImages[Math.min(state, STATE_FALL)][wingState / 10 % IMG_COUNT]; image = birdImages[Math.min(state, STATE_FALL)][wingState / 10 % IMG_COUNT];
// 下方边界: 窗口的高度 - 地面的高度 - 小鸟图片的高度
final int bottomBoundary = Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[0][0].getHeight() >> 1);
final int topBoundary = -50;
switch (state) { switch (state) {
case STATE_DOWN: case STATE_DOWN:
// 物理公式 // 自由落体
speed -= g * T; speed -= g * T;
// 小鸟y轴的位移量
double h = speed * T - g * T * T / 2; double h = speed * T - g * T * T / 2;
y = (int) (y - h); y = Math.min((int) (y - h), bottomBoundary);
birdRect.y = (int) (birdRect.y - h); birdRect.y = Math.min((int) (birdRect.y - h), bottomBoundary);
// 控制坠落的边界,若y坐标 > 窗口的高度 - 地面的高度 - 小鸟图片的高度则死亡 if (birdRect.y == bottomBoundary) {
if (birdRect.y >= Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[state][0].getHeight() >> 1)) { MusicUtil.playCrash();
y = Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[state][0].getHeight() >> 1); birdDead();
birdRect.y = Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[state][0].getHeight() >> 1);
birdFall();
} }
break; break;
case STATE_FALL: case STATE_FALL:
// 鸟死亡,自由落体 // 自由落体
speed -= g * T; speed -= g * T;
h = speed * T - g * T * T / 2; h = speed * T - g * T * T / 2;
y = (int) (y - h); y = Math.min((int) (y - h), bottomBoundary);
birdRect.y = (int) (birdRect.y - h); birdRect.y = Math.min((int) (birdRect.y - h), bottomBoundary);
if (birdRect.y == bottomBoundary)
// 控制坠落的边界,若y坐标 > 窗口的高度 - 地面的高度 - 小鸟图片的高度则死亡
if (birdRect.y >= Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[state][0].getHeight() >> 1)) {
y = Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[state][0].getHeight() >> 1);
birdRect.y = Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (birdImages[state][0].getHeight() >> 1);
GameFrame.setGameState(GameFrame.STATE_OVER); // 改变游戏状态
birdDead(); birdDead();
} break;
case STATE_DEAD:
GameFrame.setGameState(GameFrame.STATE_OVER);
break; break;
case STATE_NORMAL: case STATE_NORMAL:
case STATE_UP: case STATE_UP:
case STATE_DEAD:
break; break;
} }
// 控制上方边界 // 控制上方边界
if (birdRect.y < -1 * Constant.TOP_PIPE_LENGTHENING / 2) { if (birdRect.y < topBoundary) {
birdRect.y = -1 * Constant.TOP_PIPE_LENGTHENING / 2; birdRect.y = topBoundary;
y = -1 * Constant.TOP_PIPE_LENGTHENING / 2; y = topBoundary;
} }
// 控制下方边界
if (birdRect.y > Constant.FRAME_HEIGHT - Constant.GROUND_HEIGHT - (image.getHeight() >> 1)) {
birdFall();
}
} }
// 小鸟振翅 // 小鸟振翅
...@@ -194,6 +182,13 @@ public class Bird { ...@@ -194,6 +182,13 @@ public class Bird {
public void birdFall() { public void birdFall() {
state = STATE_FALL; state = STATE_FALL;
MusicUtil.playCrash(); // 播放音效 MusicUtil.playCrash(); // 播放音效
speed = 0; // 速度置0,防止小鸟继续上升与水管重叠
// 死后画面静止片刻
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
// 小鸟死亡 // 小鸟死亡
...@@ -208,6 +203,7 @@ public class Bird { ...@@ -208,6 +203,7 @@ public class Bird {
countScore.isSaveScore(); // 判断是否保存纪录 countScore.isSaveScore(); // 判断是否保存纪录
} }
// 判断小鸟是否死亡
public boolean isDead() { public boolean isDead() {
return state == STATE_FALL || state == STATE_DEAD; return state == STATE_FALL || state == STATE_DEAD;
} }
...@@ -258,7 +254,7 @@ public class Bird { ...@@ -258,7 +254,7 @@ public class Bird {
// 绘制继续游戏,使图像闪烁 // 绘制继续游戏,使图像闪烁
final int COUNT = 30; // 闪烁周期 final int COUNT = 30; // 闪烁周期
if (flash++ > COUNT) if (flash++ > COUNT)
drawTitle(againImg, g); GameUtil.drawImage(againImg,Constant.FRAME_WIDTH - againImg.getWidth() >> 1, Constant.FRAME_HEIGHT / 5 * 3, g);
if (flash == COUNT * 2) // 重置闪烁参数 if (flash == COUNT * 2) // 重置闪烁参数
flash = 0; flash = 0;
} }
......
...@@ -4,7 +4,6 @@ import java.awt.Graphics; ...@@ -4,7 +4,6 @@ import java.awt.Graphics;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import com.bird.util.Constant; import com.bird.util.Constant;
import com.bird.util.GameUtil; import com.bird.util.GameUtil;
import static com.bird.util.GameUtil.drawTitle;
/** /**
* 游戏启动界面类 * 游戏启动界面类
...@@ -34,7 +33,7 @@ public class GameReady { ...@@ -34,7 +33,7 @@ public class GameReady {
// 使notice的图像闪烁 // 使notice的图像闪烁
final int COUNT = 30; // 闪烁周期 final int COUNT = 30; // 闪烁周期
if (flash++ > COUNT) if (flash++ > COUNT)
drawTitle(noticeImg, g); GameUtil.drawImage(noticeImg, Constant.FRAME_WIDTH - noticeImg.getWidth() >> 1, Constant.FRAME_HEIGHT / 5 * 3, g);
if (flash == COUNT * 2) // 重置闪烁参数 if (flash == COUNT * 2) // 重置闪烁参数
flash = 0; flash = 0;
} }
......
...@@ -81,10 +81,14 @@ public class GameUtil { ...@@ -81,10 +81,14 @@ public class GameUtil {
} }
// 于屏幕x轴中央、y轴3/5处绘制图像 /**
public static void drawTitle(BufferedImage image, Graphics g) { *
int x = Constant.FRAME_WIDTH - image.getWidth() >> 1; * @param image:图片资源
int y = Constant.FRAME_HEIGHT / 5 * 3; * @param x:x坐标
* @param y:y坐标
* @param g:画笔
*/
public static void drawImage(BufferedImage image, int x, int y, Graphics g) {
g.drawImage(image, x, y, null); g.drawImage(image, x, y, null);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment