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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值