Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demo1——not_finished #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added resources/img/ShrinkPotion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/main/java/com/kingyu/flappybird/component/Bird.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 补偿碰撞矩形宽高的参数
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

public class GameElementLayer {
private final List<Pipe> pipes; // 水管的容器

private final List<ShrinkPotion> potions; // 缩小药水的容器
private int shrinkPotionCounter; // 计数器,用于控制缩小药水的生成
// 构造器
public GameElementLayer() {
pipes = new ArrayList<>();
potions = new ArrayList<>();
}

// 绘制方法
Expand All @@ -34,6 +36,8 @@ public void draw(Graphics g, Bird bird) {
i--;
}
}
// 生成和绘制缩小药水
generateAndDrawShrinkPotion(g, bird);
// 碰撞检测
isCollideBird(bird);
pipeBornLogic(bird);
Expand Down Expand Up @@ -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--;
}
}
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/kingyu/flappybird/component/ShrinkPotion.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/kingyu/flappybird/util/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"; // 分数文件路径

Expand Down Expand Up @@ -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);// 字体


}