diff --git a/resources/img/ShrinkPotion.png b/resources/img/ShrinkPotion.png new file mode 100644 index 0000000..cc482a9 Binary files /dev/null and b/resources/img/ShrinkPotion.png differ diff --git a/src/main/java/com/kingyu/flappybird/component/Bird.java b/src/main/java/com/kingyu/flappybird/component/Bird.java index 2c8ec20..781f69c 100644 --- a/src/main/java/com/kingyu/flappybird/component/Bird.java +++ b/src/main/java/com/kingyu/flappybird/component/Bird.java @@ -33,6 +33,7 @@ public class Bird { public static final int BIRD_FALL = 2; public static final int BIRD_DEAD_FALL = 3; public static final int BIRD_DEAD = 4; + private boolean shrinkEffect; // 缩小药水效果标志 private final Rectangle birdCollisionRect; // 碰撞矩形 public static final int RECT_DESCALE = 2; // 补偿碰撞矩形宽高的参数 @@ -211,4 +212,18 @@ public int getBirdX() { public Rectangle getBirdCollisionRect() { return birdCollisionRect; } + + // 处理碰撞到缩小药水的逻辑 + public void handleShrinkEffect() { + if (shrinkEffect) { + // 处理缩小药水效果,例如减小鸟的面积 + // ... + shrinkEffect = false; // 关闭缩小药水效果 + } + } + + // 设置缩小药水效果 + public void setShrinkEffect(boolean shrinkEffect) { + this.shrinkEffect = shrinkEffect; + } } diff --git a/src/main/java/com/kingyu/flappybird/component/GameElementLayer.java b/src/main/java/com/kingyu/flappybird/component/GameElementLayer.java index ba30d1c..58e1144 100644 --- a/src/main/java/com/kingyu/flappybird/component/GameElementLayer.java +++ b/src/main/java/com/kingyu/flappybird/component/GameElementLayer.java @@ -15,10 +15,12 @@ public class GameElementLayer { private final List pipes; // 水管的容器 - + private final List potions; // 缩小药水的容器 + private int shrinkPotionCounter; // 计数器,用于控制缩小药水的生成 // 构造器 public GameElementLayer() { pipes = new ArrayList<>(); + potions = new ArrayList<>(); } // 绘制方法 @@ -34,6 +36,8 @@ public void draw(Graphics g, Bird bird) { i--; } } + // 生成和绘制缩小药水 + generateAndDrawShrinkPotion(g, bird); // 碰撞检测 isCollideBird(bird); pipeBornLogic(bird); @@ -226,4 +230,38 @@ public void reset() { } pipes.clear(); } + + // 生成和绘制缩小药水 + private void generateAndDrawShrinkPotion(Graphics g, Bird bird) { + shrinkPotionCounter++; + + if (shrinkPotionCounter % 10 == 0) { // 每十轮刷新一个缩小药水 + ShrinkPotion potion = new ShrinkPotion(); + // 设置缩小药水的位置 + int x = Constant.FRAME_WIDTH + Constant.FRAME_WIDTH / 2; // 在屏幕外右侧生成 + int y = GameUtil.getRandomNumber(Constant.FRAME_HEIGHT / 6, Constant.FRAME_HEIGHT * 5 / 6); + potion.setAttribute(x, y); // 假设 POTION_HEIGHT 为缩小药水的高度 + potions.add(potion); + } + + // 遍历并绘制缩小药水 + for (int i = 0; i < potions.size(); i++) { + ShrinkPotion potion = potions.get(i); + if (potion.isInFrame()) { + potion.draw(g); + potion.move(); + } else { + potions.remove(i); + i--; + } + + // 处理碰撞逻辑 + if (potion.getRect().intersects(bird.getBirdCollisionRect())) { + bird.setShrinkEffect(true); // 启用缩小药水效果 + // 可以根据需要添加其他逻辑,例如播放音效等 + potions.remove(i); + i--; + } + } + } } diff --git a/src/main/java/com/kingyu/flappybird/component/ShrinkPotion.java b/src/main/java/com/kingyu/flappybird/component/ShrinkPotion.java new file mode 100644 index 0000000..a6cb0ff --- /dev/null +++ b/src/main/java/com/kingyu/flappybird/component/ShrinkPotion.java @@ -0,0 +1,66 @@ +package com.kingyu.flappybird.component; + +import java.awt.Graphics; +import java.awt.Rectangle; +import java.awt.image.BufferedImage; + +import com.kingyu.flappybird.util.Constant; +import com.kingyu.flappybird.util.GameUtil; + +/** + * Сҩˮ࣬СҩˮͻʹͼС֮һ + */ +public class ShrinkPotion { + int x; // Сҩˮĺ + int y; // Сҩˮ + + int width; + int speed; + private static BufferedImage img; // СҩˮͼƬ + public static final int POTION_WIDTH = img.getWidth(); + public static final int POTION_HEIGHT = img.getHeight(); + private Rectangle potionRect; // ײľ + + // 췽ʼСҩˮλúͼƬ + public ShrinkPotion() { + this.speed = Constant.GAME_SPEED; + this.width = POTION_WIDTH; + potionRect = new Rectangle(); + potionRect.width = POTION_WIDTH; + } + + public void setAttribute(int x, int y) { + this.x = x; + this.y = y; + setRectangle(this.x, this.y, POTION_HEIGHT); + } + public void setRectangle(int x, int y, int height) { + potionRect.x = x; + potionRect.y = y; + potionRect.height = height; + } + + // Сҩˮ + public void draw(Graphics g) { + g.drawImage(img, x, y, null); + } + + // ȡСҩˮľΣײ + public Rectangle getRect() { + return potionRect; + } + + // СҩˮƶԸҪضƶ߼ + public void move() { + // 򵥵ƶԸϷ޸ƶ߼ + int speed = Constant.GAME_SPEED; + x -= speed; + potionRect.setLocation(x, y); + } + + + // жСҩˮǷȫڴ + public boolean isInFrame() { + return x + img.getWidth() > 0; + } +} diff --git a/src/main/java/com/kingyu/flappybird/util/Constant.java b/src/main/java/com/kingyu/flappybird/util/Constant.java index f762bf2..3df4e65 100644 --- a/src/main/java/com/kingyu/flappybird/util/Constant.java +++ b/src/main/java/com/kingyu/flappybird/util/Constant.java @@ -49,6 +49,7 @@ public class Constant { public static final String SCORE_IMG_PATH = "resources/img/score.png"; public static final String OVER_IMG_PATH = "resources/img/over.png"; public static final String AGAIN_IMG_PATH = "resources/img/again.png"; + public static final String SHRINK_POTION_IMG_PATH = "resources/img/ShrinkPotion.png"; public static final String SCORE_FILE_PATH = "resources/score"; // 分数文件路径 @@ -77,4 +78,5 @@ public class Constant { public static final Font CURRENT_SCORE_FONT = new Font("华文琥珀", Font.BOLD, 32);// 字体 public static final Font SCORE_FONT = new Font("华文琥珀", Font.BOLD, 24);// 字体 + }