提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
飞翔的小鸟可以用来作为Java学习者基础学习完成后的小项目
需要掌握面向对象的使用并且学会 IO流,异常处理,JPanel类的使用,监听器,等java基础等相关知识。
一、项目分析
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
(1)创建一个窗口和画板,把画板放到窗口上,在画板上绘画图片
(2)让小鸟在画面中动起来,可以上下飞
(3)让地面和管道动起来
(4)碰撞检测
(5)绘出开始的界面和游戏结束的界面
二、游戏展示
开始界面
游玩时页面
结束页面
二、代码展示(展示部分代码)
每行代码基本都有注释
1.游戏启动类
静态代码块,一般用于加载静态资源(视频,音频,图片)
static {
try {
//将本地图片读取到程序中
bg = ImageIO.read(BirdGame.class.getResourceAsStream("bg.png"));
ground = ImageIO.read(BirdGame.class.getResourceAsStream("ground.png"));
bird_img = ImageIO.read(BirdGame.class.getResourceAsStream("0.png"));
col_img = ImageIO.read(BirdGame.class.getResourceAsStream("column.png"));
start_img = ImageIO.read(BirdGame.class.getResourceAsStream("start.png"));
over_img = ImageIO.read(BirdGame.class.getResourceAsStream("gameover.png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
用于在绘画板绘制内容的方法:在这个方法里面绘画
public void paint(Graphics g) {
/* g.setColor(Color.PINK); //画笔颜色设置
g.fillRect(0,0,1080,1080);*/
g.drawImage(bg, 0, 0, null); //画背景
g.drawImage(column_g.co_image, column_g.x, column_g.y, null);
g.drawImage(Bird_g.bird_image, Bird_g.x, Bird_g.y, null);
g.drawImage(column_g2.co_image, column_g2.x, column_g2.y, null);
g.drawImage(ground_g.img_ground, ground_g.x, ground_g.y, null);
//开始状态
if (start == START) {
g.drawImage(start_img, 0, 0, null);
}
//结束状态
if (start == GAME_OVER) {
g.drawImage(over_img, 0, 0, null);
}
//画分数
Font font = new Font("微软雅黑", Font.BOLD, 30); //创建字体
g.setFont(font); //给画笔设置字体
g.setColor(Color.red);
g.drawString("分数:"+score,25,40);
g.setColor(Color.WHITE);
g.drawString("分数:"+score,23,38);
}
给当前对象添加鼠标单击事件
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (start == START) {
//如果是开始状态,单击转换为运行状态
start = RUNNING;
}
if (start == RUNNING) {
//运行状态 ,单击小鸟飞
//小鸟飞
Bird_g.up();
}
if (start == GAME_OVER) {
//结束状态 ,单击变成开始状态
Bird_g = new Bird();
column_g = new Column();
column_g2 = new Column();
column_g2.x = column_g.x + column_g.distance;
score = 0;
start = START;
}
}
});
2.管道类
public class Column {
int x, y;
int width, height;
BufferedImage co_image;
int gap; //上下间隙
int distance; //水平间隙
int min = -(1200 / 2 - 144 / 2);
int max = 644 - 146 - 144 / 2 - 1200 / 2;
public Column() {
co_image = BirdGame.col_img;
width = co_image.getWidth();
// width = (int) (Math.random() * 20 + 150);
height = co_image.getHeight();
x = 432;
y = (int) (Math.random() * (max - min) + min);
gap = 144; //144
distance = 244;
}
public void star() {
x = x - 1;
if (x <= -width) {
//柱子越界
x = 432;
//重新随机
y = (int) (Math.random() * (max - min) + min);
}
}
}
3.地面类
public class Ground {
int x, y;
int width, height;
BufferedImage img_ground; //地面图片
public Ground() {
img_ground = BirdGame.ground;
x = 0;
y = BirdGame.bg.getHeight() - img_ground.getHeight();
width = img_ground.getWidth();
height = img_ground.getHeight();
}
//地面移动
public void step() {
x = x - 1;
if (x <= 432 - width) {
x = 0;
}
}
}
4.小鸟类
小鸟撞柱子的方法
public boolean hitColumn(Column column) {
//撞水平方向的柱子
boolean b1 = this.x + this.width >= column.x;
boolean b2 = this.x <= column.x + column.width;
//撞垂直方向的柱子
boolean b3 = this.y + 25 <= column.y + column.height / 2 - column.gap / 2;
boolean b4 = this.y - 30 + this.height >= column.y + column.height / 2 + column.gap / 2;
//满足b1,b2 证明水平方向与柱子相撞
//b1 b2 b3 同时满足 撞的是上面的柱子
// b1 b2 b4 同时满足 撞的是下面的柱子
return b1 && b2 && (b3 || b4);
}
小鸟撞地面的方法
public boolean hitGround(Ground ground) {
boolean isHit = this.y + this.height >= ground.y +30;
return isHit;
}