|
| 1 | +package Games.FlappyBird.com.kingyu.flappybird.component; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Graphics; |
| 5 | +import java.awt.Rectangle; |
| 6 | +import java.awt.image.BufferedImage; |
| 7 | + |
| 8 | +import Games.FlappyBird.com.kingyu.flappybird.app.Game; |
| 9 | +import Games.FlappyBird.com.kingyu.flappybird.util.Constant; |
| 10 | +import Games.FlappyBird.com.kingyu.flappybird.util.GameUtil; |
| 11 | +import Games.FlappyBird.com.kingyu.flappybird.util.MusicUtil; |
| 12 | + |
| 13 | +/** |
| 14 | + * �鸟类,实现�鸟的绘制与飞行逻辑 |
| 15 | + * |
| 16 | + * @author Kingyu |
| 17 | + */ |
| 18 | +public class Bird { |
| 19 | + public static final int IMG_COUNT = 8; // 图片数� |
| 20 | + public static final int STATE_COUNT = 4; // 状�数 |
| 21 | + private final BufferedImage[][] birdImages; // �鸟的图片数组对象 |
| 22 | + private final int x; |
| 23 | + private int y; // å°�鸟的å��æ ‡ |
| 24 | + private int wingState; // 翅膀状� |
| 25 | + |
| 26 | + // 图片资� |
| 27 | + private BufferedImage image; // 实时的�鸟图片 |
| 28 | + |
| 29 | + // �鸟的状� |
| 30 | + private int state; |
| 31 | + public static final int BIRD_NORMAL = 0; |
| 32 | + public static final int BIRD_UP = 1; |
| 33 | + public static final int BIRD_FALL = 2; |
| 34 | + public static final int BIRD_DEAD_FALL = 3; |
| 35 | + public static final int BIRD_DEAD = 4; |
| 36 | + |
| 37 | + private final Rectangle birdCollisionRect; // 碰撞矩形 |
| 38 | + public static final int RECT_DESCALE = 2; // 补�碰撞矩形宽高的�数 |
| 39 | + |
| 40 | + private final ScoreCounter counter; // 计分器 |
| 41 | + private final GameOverAnimation gameOverAnimation; |
| 42 | + |
| 43 | + public static int BIRD_WIDTH; |
| 44 | + public static int BIRD_HEIGHT; |
| 45 | + |
| 46 | + // åœ¨æž„é€ å™¨ä¸å¯¹èµ„æº�åˆ�始化 |
| 47 | + public Bird() { |
| 48 | + counter = ScoreCounter.getInstance(); // 计分器 |
| 49 | + gameOverAnimation = new GameOverAnimation(); |
| 50 | + |
| 51 | + // 读��鸟图片资� |
| 52 | + birdImages = new BufferedImage[STATE_COUNT][IMG_COUNT]; |
| 53 | + for (int j = 0; j < STATE_COUNT; j++) { |
| 54 | + for (int i = 0; i < IMG_COUNT; i++) { |
| 55 | + birdImages[j][i] = GameUtil.loadBufferedImage(Constant.BIRDS_IMG_PATH[j][i]); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + assert birdImages[0][0] != null; |
| 60 | + BIRD_WIDTH = birdImages[0][0].getWidth(); |
| 61 | + BIRD_HEIGHT = birdImages[0][0].getHeight(); |
| 62 | + |
| 63 | + // åˆ�始化å°�鸟的å��æ ‡ |
| 64 | + x = Constant.FRAME_WIDTH >> 2; |
| 65 | + y = Constant.FRAME_HEIGHT >> 1; |
| 66 | + |
| 67 | + // �始化碰撞矩形 |
| 68 | + int rectX = x - BIRD_WIDTH / 2; |
| 69 | + int rectY = y - BIRD_HEIGHT / 2; |
| 70 | + birdCollisionRect = new Rectangle(rectX + RECT_DESCALE, rectY + RECT_DESCALE * 2, BIRD_WIDTH - RECT_DESCALE * 3, |
| 71 | + BIRD_WIDTH - RECT_DESCALE * 4); // 碰撞矩形的å��æ ‡ä¸Žå°�鸟相å�Œ |
| 72 | + } |
| 73 | + |
| 74 | + // 绘制方法 |
| 75 | + public void draw(Graphics g) { |
| 76 | + movement(); |
| 77 | + int state_index = Math.min(state, BIRD_DEAD_FALL); // 图片资�索引 |
| 78 | + // å°�鸟ä¸å¿ƒç‚¹è®¡ç®— |
| 79 | + int halfImgWidth = birdImages[state_index][0].getWidth() >> 1; |
| 80 | + int halfImgHeight = birdImages[state_index][0].getHeight() >> 1; |
| 81 | + if (velocity > 0) |
| 82 | + image = birdImages[BIRD_UP][0]; |
| 83 | + g.drawImage(image, x - halfImgWidth, y - halfImgHeight, null); // xå��æ ‡äºŽçª—å�£1/4处,yå��æ ‡ä½�窗å�£ä¸å¿ƒ |
| 84 | + |
| 85 | + if (state == BIRD_DEAD) |
| 86 | + gameOverAnimation.draw(g, this); |
| 87 | + else if (state != BIRD_DEAD_FALL) |
| 88 | + drawScore(g); |
| 89 | + // 绘制碰撞矩形 |
| 90 | +// g.setColor(Color.black); |
| 91 | +// g.drawRect((int) birdRect.getX(), (int) birdRect.getY(), (int) birdRect.getWidth(), (int) birdRect.getHeight()); |
| 92 | + } |
| 93 | + |
| 94 | + public static final int ACC_FLAP = 14; // players speed on flapping |
| 95 | + public static final double ACC_Y = 2; // players downward acceleration |
| 96 | + public static final int MAX_VEL_Y = 15; // max vel along Y, max descend speed |
| 97 | + private int velocity = 0; // bird's velocity along Y, default same as playerFlapped |
| 98 | + private final int BOTTOM_BOUNDARY = Constant.FRAME_HEIGHT - GameBackground.GROUND_HEIGHT - (BIRD_HEIGHT / 2); |
| 99 | + |
| 100 | + // �鸟的飞行逻辑 |
| 101 | + private void movement() { |
| 102 | + // 翅膀状�,实现�鸟振翅飞行 |
| 103 | + wingState++; |
| 104 | + image = birdImages[Math.min(state, BIRD_DEAD_FALL)][wingState / 10 % IMG_COUNT]; |
| 105 | + if (state == BIRD_FALL || state == BIRD_DEAD_FALL) { |
| 106 | + freeFall(); |
| 107 | + if (birdCollisionRect.y > BOTTOM_BOUNDARY) { |
| 108 | + if (state == BIRD_FALL) { |
| 109 | + MusicUtil.playCrash(); |
| 110 | + } |
| 111 | + die(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void freeFall() { |
| 117 | + if (velocity < MAX_VEL_Y) |
| 118 | + velocity -= ACC_Y; |
| 119 | + y = Math.min((y - velocity), BOTTOM_BOUNDARY); |
| 120 | + birdCollisionRect.y = birdCollisionRect.y - velocity; |
| 121 | + } |
| 122 | + |
| 123 | + private void die() { |
| 124 | + counter.saveScore(); |
| 125 | + state = BIRD_DEAD; |
| 126 | + Game.setGameState(Game.STATE_OVER); |
| 127 | + } |
| 128 | + |
| 129 | + // �鸟振翅 |
| 130 | + public void birdFlap() { |
| 131 | + if (keyIsReleased()) { |
| 132 | + if (isDead()) |
| 133 | + return; |
| 134 | + MusicUtil.playFly(); // æ’æ”¾éŸ³æ•ˆ |
| 135 | + state = BIRD_UP; |
| 136 | + if (birdCollisionRect.y > Constant.TOP_BAR_HEIGHT) { |
| 137 | + velocity = ACC_FLAP; // �次振翅将速度改为上�速度 |
| 138 | + wingState = 0; // �置翅膀状� |
| 139 | + } |
| 140 | + keyPressed(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // �鸟下� |
| 145 | + public void birdFall() { |
| 146 | + if (isDead()) |
| 147 | + return; |
| 148 | + state = BIRD_FALL; |
| 149 | + } |
| 150 | + |
| 151 | + // å°�鸟å� è�½ï¼ˆå·²æ»ï¼‰ |
| 152 | + public void deadBirdFall() { |
| 153 | + state = BIRD_DEAD_FALL; |
| 154 | + MusicUtil.playCrash(); // æ’æ”¾éŸ³æ•ˆ |
| 155 | + velocity = 0; // 速度置0,防æ¢å°�鸟继ç»ä¸Šå�‡ä¸Žæ°´ç®¡é‡�å� |
| 156 | + } |
| 157 | + |
| 158 | + // 判æ–å°�鸟是å�¦æ»äº¡ |
| 159 | + public boolean isDead() { |
| 160 | + return state == BIRD_DEAD_FALL || state == BIRD_DEAD; |
| 161 | + } |
| 162 | + |
| 163 | + // 绘制实时分数 |
| 164 | + private void drawScore(Graphics g) { |
| 165 | + g.setColor(Color.white); |
| 166 | + g.setFont(Constant.CURRENT_SCORE_FONT); |
| 167 | + String str = Long.toString(counter.getCurrentScore()); |
| 168 | + int x = Constant.FRAME_WIDTH - GameUtil.getStringWidth(Constant.CURRENT_SCORE_FONT, str) >> 1; |
| 169 | + g.drawString(str, x, Constant.FRAME_HEIGHT / 10); |
| 170 | + } |
| 171 | + |
| 172 | + // �置�鸟 |
| 173 | + public void reset() { |
| 174 | + state = BIRD_NORMAL; // �鸟状� |
| 175 | + y = Constant.FRAME_HEIGHT >> 1; // å°�鸟å��æ ‡ |
| 176 | + velocity = 0; // �鸟速度 |
| 177 | + |
| 178 | + int ImgHeight = birdImages[state][0].getHeight(); |
| 179 | + birdCollisionRect.y = y - ImgHeight / 2 + RECT_DESCALE * 2; // å°�鸟碰撞矩形å��æ ‡ |
| 180 | + |
| 181 | + counter.reset(); // �置计分器 |
| 182 | + } |
| 183 | + |
| 184 | + private boolean keyFlag = true; // 按键状�,true为已释放,使当按�按键时�会��调用方法 |
| 185 | + |
| 186 | + public void keyPressed() { |
| 187 | + keyFlag = false; |
| 188 | + } |
| 189 | + |
| 190 | + public void keyReleased() { |
| 191 | + keyFlag = true; |
| 192 | + } |
| 193 | + |
| 194 | + public boolean keyIsReleased() { |
| 195 | + return keyFlag; |
| 196 | + } |
| 197 | + |
| 198 | + public long getCurrentScore() { |
| 199 | + return counter.getCurrentScore(); |
| 200 | + } |
| 201 | + |
| 202 | + public long getBestScore() { |
| 203 | + return counter.getBestScore(); |
| 204 | + } |
| 205 | + |
| 206 | + public int getBirdX() { |
| 207 | + return x; |
| 208 | + } |
| 209 | + |
| 210 | + // 获��鸟的碰撞矩形 |
| 211 | + public Rectangle getBirdCollisionRect() { |
| 212 | + return birdCollisionRect; |
| 213 | + } |
| 214 | +} |
0 commit comments