java flappy bird_Java实例---flappy-bird实例解析
1 packagecom.ftl.flappybird.day6;2
3 import java.awt.Color;//颜色 Color.class
4 importjava.awt.Font;5 importjava.awt.Graphics;6 importjava.awt.Graphics2D;7 importjava.awt.event.MouseAdapter;8 importjava.awt.event.MouseEvent;9 importjava.awt.event.MouseListener;10 import java.awt.image.BufferedImage;//图片类型
11 importjava.util.Random;12
13 import javax.imageio.ImageIO;//读取图片的工具
14 import javax.swing.JFrame;//窗口框
15 import javax.swing.JPanel;//面板 底板
16
17 /**
18 * flappyBird19 *20 *@authorAdministrator21 *22 */
23 public class Demo6 extendsJPanel {24 //声明了背景(background)图变量,没有图片对象
25 BufferedImage background;26 BufferedImage gameStartImg; //游戏开始
27 BufferedImage gameoverImg; //游戏结束
28 Bird bird; //鸟
29 Ground ground; //MyPanel中包含地面
30 Column column1; //为MyPanel添加柱子
31 Column column2; //为MyPanel添加柱子
32 int score; //游戏分数
33 boolean gameOver; //游戏结束
34 boolean started; //游戏开始35
36 //游戏状态
37 intstate;38 public static final int START = 0;39 public static final int RUNNING = 1;40 public static final int GAME_OVER = 2;41
42
43 public Demo6() throwsException {44 state = START; //游戏一开始进入开始状态
45 gameStartImg = ImageIO.read(getClass().getResource("start.png"));46 background = ImageIO.read(getClass().getResource("bg.png"));47 gameoverImg=ImageIO.read(48 getClass().getResource("gameover.png"));49 ground = newGround();50 //利用类来创造对象
51 column1 = new Column(1);52 column2 = new Column(2);53 bird = newBird();54 this.score = 0;55 //this.gameOver = false;
56 }57
58 /**
59 * 重新复写paint,增加旋转,增加分数60 */
61 public voidpaint(Graphics g) {62 g.drawImage(background, 0, 0, null);63 g.drawImage(column1.image, column1.x - column1.width / 2, column1.y64 - column1.height / 2, null);65 g.drawImage(column2.image, column2.x - column2.width / 2, column2.y66 - column2.height / 2, null);67 g.drawImage(bird.image, bird.x - bird.width / 2, bird.y -bird.height68 / 2, null);69 g.drawImage(ground.image, ground.x, ground.y, null);70
71 //增加分数算法
72 Font font = new Font(Font.SANS_SERIF,Font.BOLD,40);73 g.setFont(font);74 g.drawString("" + score, 40 , 60);75 g.setColor(Color.WHITE);76 g.drawString(""+score, 40-3, 60-3);77
78 g.drawImage(ground.image, ground.x,ground.y,null);79 //if(gameOver){80 //g.drawImage(gameoverImg, 0, 0, null);81 //return;82 //}
83 switch(state) {84 caseGAME_OVER:85 g.drawImage(gameoverImg, 0, 0, null);86 break;87 caseSTART:88 g.drawImage(gameStartImg, 0, 0, null);89 break;90 }91 //旋转绘图坐标系
92 Graphics2D g2 = (Graphics2D) g;//向下转型
93 g2.rotate(-this.bird.alpha, this.bird.x, this.bird.y);//设置旋转角度和坐标
94 g.drawImage(bird.image, bird.x - bird.width / 2,95 bird.y - bird.height / 2, null);96 g2.rotate(this.bird.alpha, this.bird.x, this.bird.y);//设置旋转角度和坐标
97
98
99 }100
101 //增加鼠标控制
102 public void action() throwsException {103 MouseListener l = newMouseAdapter() {104 public voidmousePressed(MouseEvent e){105
106 try{107 switch(state) {108 caseGAME_OVER:109 column1 = new Column(1);110 column2 = new Column(2);111 bird = newBird();112 score = 0;113 state =START;114 break;115 caseSTART:116 state =RUNNING;117 caseRUNNING:118 bird.flappy();119 }120 }catch(Exception e1) {121 e1.printStackTrace();122 }123 }124 };125 this.addMouseListener(l);126
127 while (true) {128 // //增加积分逻辑129 //if(!gameOver || started){130 //
131 //this.ground.step();132 //this.column1.step();133 //this.column2.step();134 //this.bird.step();135 //}136 //this.bird.fly();137 //this.ground.step();138 //
139 // //判断是否撞了140 //if(this.bird.hit(ground) || this.bird.hit(column1) || this.bird.hit(column2)){141 //this.gameOver = true;//游戏结束142 //}143 //this.bird.fly();144 // //增加计分操作,柱子的x坐标和鸟的x坐标重合145 //if(this.bird.x == this.column1.x ||146 //this.bird.x == this.column2.x)147 //{148 //this.score++;149 //}
150 switch(state) {151 caseSTART:152 bird.fly();153 ground.step();154 break;155 caseRUNNING:156 column1.step();157 column2.step();158 bird.step(); //上下移动
159 bird.fly(); //动图
160 ground.step();161 //增加计分操作,柱子的x坐标和鸟的x坐标重合
162 if(this.bird.x == this.column1.x ||
163 this.bird.x == this.column2.x)164 {165 this.score++;166 }167 //判断是否撞了
168 if(this.bird.hit(ground) || this.bird.hit(column1) || this.bird.hit(column2)){169 state = GAME_OVER;//游戏结束
170 }171 break;172 }173
174 repaint();175 Thread.sleep(1000 / 60);176 }177 }178
179 public static void main(String[] args) throwsException {180 //创建一个窗口框,被frame变量引用
181 JFrame frame = newJFrame();182 //创建面板,被panel变量引用183 //new MyPanel()执行了构造器,装载照片
184 Demo6 panel = newDemo6();185 //Background 背景,设置背景色=蓝色
186 panel.setBackground(Color.BLUE);187 //在frame引用的框中添加panel引用的面板188 //框添加面板
189 frame.add(panel);190 frame.setSize(432, 644 + 30);191 //居中 Location位置 Relative相对 To于 空
192 frame.setLocationRelativeTo(null);193 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);194 frame.setTitle("FlappyBird2017");195 frame.setIconImage(ImageIO.read(196 Demo6.class.getResource("0.png")));197 //setVisible执行的时候,尽快的执行了198 //paint 方法
199 frame.setVisible(true);200 panel.action();201
202 }203 }204
205 classColumn {206 public int x, y; //x,y是柱子的缝隙中心点
207 public int width, height; //柱子的宽, 高
208 public BufferedImage image; //柱子的图片
209 public int gap; //缝隙
210 public int distance; //2个柱子之间的距离
211 Random random = newRandom();212 //n 代表柱子的编号,如:1,2
213 public Column(int n) throwsException {214 image = ImageIO.read(Demo6.class.getResource("column.png"));215 this.distance = 245;216 //这个不是一开始就直接小鸟进入图片,有118的距离 550 = 118 + 432
217 this.x = 550 + (n - 1) *distance;218 this.y = random.nextInt(128) + 132;219 this.gap = 144;220 this.width =image.getWidth();221 this.height =image.getHeight();222 }223
224 public voidstep() {225 this.x--;226 if (this.x == -this.width / 2) {227 this.x = this.distance * 2 - this.width / 2;228 this.y = random.nextInt(128) + 132;229 }230 }231
232 }233
234 classGround {235 publicBufferedImage image;236 public intx, y;237 public intwidth,heigth;238 public Ground() throwsException {239 image = ImageIO.read(getClass().getResource("ground.png"));240 this.x = 0;241 this.y = 500;242 this.width =image.getWidth();243 this.heigth =image.getHeight();244 }245
246 public voidstep() {247 this.x--; //x减1
248 if (this.x == -109) {249 this.x = 0;250 }251
252 }253 }254
255 classBird {256 publicBufferedImage image;257 public int x, y; //鸟的位置,按照鸟的中心点
258 public intwidth, height;259 public int size; //鸟的大小,是鸟的碰撞检测范围
260 public double g; //重力加速度,是浮点类型(小数类型)
261 public double t; //间隔时间,两次移动的间隔时间
262 public double s; //两次间隔时间,移动的距离:位移
263 public double v0; //初始速度
264 public double speed; //经过时间t以后,的运动速度
265 public double alpha; //鸟的倾角, 以弧度制为单位
266
267 public int index; //动画帧数组元素的小标位置
268 public BufferedImage images[]; //一组图片作为鸟的动画帧
269
270 public Bird() throwsException {271 this.image = ImageIO.read(getClass().getResource("0.png"));272 this.images = new BufferedImage[8];273 this.x = 132;274 this.y = 280;275 this.size = 10;276 this.g = 1;277 this.t = 0.25;278 this.v0 = 10;279 this.s = 0;280 this.speed = this.v0;281 this.alpha = 0;282 this.width = this.image.getWidth();283 this.height = this.image.getHeight();284 for(int i = 0 ; i < 8; i++){285 images[i] = ImageIO.read(getClass().getResource(i + ".png"));286 }287 this.index = 0;288 }289
290 //实现鼠标控制
291 public voidflappy() {292 //重新设置初始速度,重新开始飞
293 this.speed =v0;294 }295
296 //鸟的撞地检查
297 public booleanhit(Ground ground){298 boolean hit = false;299 hit = this.y + this.size / 2 >ground.y;300 if(hit){301 //表示撞地
302 this.y = ground.y - this.size / 2; //鸟儿放在地上
303 this.alpha = -Math.PI / 2; //鸟儿旋转效果
304 }305 returnhit;306 }307
308 //鸟儿撞在柱子上
309 public booleanhit(Column col){310 //判断鸟儿在柱子的范围内(this.x 表示主子内的中心位置)
311 if(this.x > col.x - col.width / 2 - this.size / 2
312 && this.x < col.x + col.width / 2 + this.size / 2){313 //检查是否在间隙之间
314 if(this.y > col.y - col.gap / 2 + this.size / 2
315 && this.y < col.y + col.gap / 2 - this.size / 2){316 return false;317 }318 return true;319 }320 return false;321 }322
323 //实现鸟的动图
324 public voidfly(){325 this.index++;326 image = images[(index / 12) % 8];327 }328
329 //实现鸟儿的移动
330 public voidstep() {331 //当前的上抛初始速度
332 double v0 =speed;333 //计算位移
334 this.s = v0 * t + g * t * t / 2;335 this.y = this.y - (int) s;336 //计算经过时间t以后的速度,是下次计算337 //的初始速度
338 double v = v0 - g *t;339 //将经过时间t以后的速度作为下次的初始速度
340 this.speed =v;341 //System.out.println("位移:" + this.s + "y:" + this.y + "\tt:" + this.t +342 //"\tspeed:" + this.speed);343 //if (this.y >= 400) {344 //this.y = 280;345 //this.speed = 35;346 //}347 //调用Java API提供的反正切函数,计算倾角
348 this.alpha = Math.atan(s / 8);349 }350
351 }
java flappy bird_Java实例---flappy-bird实例解析相关推荐
- java类初始化_Java的类/实例初始化过程
昨天看到群里面有人分享了一道题目,我答错了,于是趁机了解了下Java的类/对象初始化过程: 程序的输出见文章最后 程序A主要考察的是 类实例初始化 .简单验证了下,类实例初始化过程如下:父类实例初始化 ...
- java写exe程序实例,java实现可安装的exe程序实例详解
java实现可安装的exe程序实例详解 通过编写java代码,实现可安装的exe文件的一般思路: 1.在eclipse中创建java项目,然后编写java代码,将编写好的java项目导出一个.jar格 ...
- java字典写实例,基于JAVA的新华字典接口调用代码实例
基于JAVA的新华字典接口调用代码实例 接口描述:基于JA V A的新华字典接口调用代码实例 接口平台:聚合数据 import java.io.BufferedReader; import java. ...
- java拉姆达表达式事例,Java Lambda表达式详解和实例
简介 Lambda表达式是Java SE 8中一个重要的新特性.lambda表达式允许你通过表达式来代替功能接口. lambda表达式就和方法一样,它提供了一个正常的参数列表和一个使用这些参数的主体( ...
- java str2date,java date类与string类实例代码分享
Date类用来指定日期和时间,其构造函数及常用方法如下: publicDate() 从当前时间构造日期时间对象. publicStringtoString() 转换成字符串. publiclongge ...
- Java 线程池详解及实例代码
转载自 Java 线程池详解及实例代码 这篇文章主要介绍了Java 线程池的相关资料,并符实例代码,帮助大家学习参考,需要的朋友可以参考下 线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时 ...
- java 实例对象拷贝,实例详解java对象拷贝
这篇文章主要介绍了java对象拷贝详解及实例的相关资料,需要的朋友可以参考下 java对象拷贝详解及实例 Java赋值是复制对象引用,如果我们想要得到一个对象的副本,使用赋值操作是无法达到目的的:@T ...
- java订单类_基于Java创建一个订单类代码实例
这篇文章主要介绍了基于Java创建一个订单类代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 需求描述 定义一个类,描述订单信息 订单id 订 ...
- java订单类_使用Java创建一个订单类代码实例
这篇文章主要简介了使用Java创建一个订单类代码实例,文中通过示例代码简介的非常具体,对大家的学习或者工作具有一定的参考学习网上卖,需要的朋友可以学习下 需求描述 定义一个类,描述订单信息 订单id ...
- java线程和内核线程的,Java中内核线程理论及实例详解
1.概念 内核线程是直接由操作系统内核控制的,内核通过调度器来完成内核线程的调度并负责将其映射到处理器上执行.内核态下的线程执行速度理论上是最高的,但是用户不会直接操作内核线程,而是通过内核线程的接口 ...
最新文章
- 连接MYSQL的时候报错(找不到请求的.net framework data provider。可能没有安装
- 项目启动QRTZ_LOCKS‘ doesn‘t exist
- html box 竖线,CSS3 小竖条脉冲型LOADING动效
- XnView v1.93.6 Final 注册机
- 使用JDBC来连接数据库
- canvas设置字体粗细用数字没效果_干货 | 用uni-app制作迷你PS小程序
- cpu,cache,Ram,harddisk存储速度
- 01_多操作系统课题研究[2011-01-21]
- ClinkHouse基本使用说明
- 如何在微信中做好微信h5棋牌下载类推广防封防屏蔽?
- 简约大方的HTML表格样式
- CPU个数、CPU物理核数、CPU逻辑核数、vCPU数之间的区别
- 你对前端开发岗的看法
- 苹果cms V10模板主题|白色简约风格自适应模板下载
- Kudu+Impala介绍
- 计算机安装错误及分析,安装Win7/Win8/Win10系统提示无法安装所需的文件及错误代码0X80070002的解决方法...
- linux之“cd /“ 、“cd ~“、“cd /home“
- 通过layui组件的滑动块实现控制图片放大缩小功能!
- android端字体颜色,Android状态字体颜色
- 山东电子职业技术学院html,山东电子职业技术学院全景-360度,720度,高清全景地图-expoon网展...
热门文章
- Oracle RMAN 的 show,list,crosscheck,delete命令整理
- Sinon 入门,看这篇文章就够了
- 全自动苹果CMS火车头采集器,苹果CMS火车头发布插件
- 【转】目前最常见的”无线通信(数据)传输技术“有哪些?
- 计算机的随想作文600字,随想作文600字
- 王慧文:当下社会最稀缺的是“π型人才”
- XSS Challenges xss-quiz.int21h.jp
- oppo小布机器人_腾讯宠粉狂欢季丨OPPO手机、腾讯听听音箱、小布AI机器人……100+份豪礼免费送!...
- 自动化测试 - 12306火车票网站自动登录工具
- 已知销售额怎么计算成本_知道销售总额怎么计算成本价?