本文实例为大家分享了java实现flappy bird游戏的具体代码,供大家参考,具体内容如下
1.首先在mainactivity.xml中放置一个view,id为viewdraw
2.开始编程,程序中自定义一个view类的子类,与viewdraw关联,程序除了放置如一个view控件,没有其他控件,程序上面的所有图片都是通过控制canvas画图实现
3.游戏是依据flappybird的游戏过程重新写的算法,功能与原版游戏相似,可能有些地方不足,完全是学习练习编程而已,做的不好见谅
4.原图片大小为384*512,在展示图片时将图片进行了放大,尽可能满足800*1280的全屏展示,如果你使用三星note10,这个程序可以直接复制粘贴运行,否则,可能会遇到图片画错位置的问题,请适当调整
5.程序游戏中使用到的图片最后,只需要按照图片上的名字命名该图片,并导入到程序的图片资源中,就能顺利运行本程序
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
public class mainactivity extends activity { static final int img_width= 384 ; //图片的长宽 static final int img_height= 512 ; private view viewdraw; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewdraw = (view) findviewbyid(r.id.viewdraw); myview v = new myview(viewdraw.getcontext(), null ); setcontentview(v); } @override public boolean onkeydown( int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { finish(); return true ; } return super .onkeydown(keycode, event); } //定义水管障碍物类 class obstacle { int x; int h; public obstacle() { x= 0 ; h= 0 ; } } //自定义view子类,完成动画的刷新 class myview extends view implements runnable { static final int speed= 2 ; // 图形当前坐标 private int x = 20 , y = 20 ,a,b,c,birdheight,time,point,v,record; obstacle pillar[]= new obstacle[ 3 ]; boolean binit,bcourse,bgame,bgameover; refreshhandler mredrawhandler; bitmap birdupbitmap,birddownbitmap,birdmiddlebitmap,groundbitmap,otherstartbitmap; bitmap courseupbitmap,coursedownbitmap,coursemiddlebitmap; bitmap pillarupbitmap,pillardownbitmap,gameoverbitmap,recordbitmap,playagainbitmap,listbitmap,goldbitmap,silverbitmap,bronzebitmap,whitegoldbitmap,gamebackgroundbitmap; // 构造方法 public myview(context context, attributeset attrs) { super (context, attrs); // todo auto-generated constructor stub // 获得焦点 setfocusable( true ); binit= false ; bcourse= false ; bgame= false ; bgameover= false ; //障碍物初始化 pillar[ 0 ]= new obstacle(); pillar[ 1 ]= new obstacle(); pillar[ 2 ]= new obstacle(); //载入图片 otherstartbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_start); birdupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.bird_up); birddownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.bird_down); birdmiddlebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.bird_middle); groundbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.background_ground); pillardownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.pillar_downside); pillarupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.pillar_upside); gameoverbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_gameover); recordbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_record); playagainbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_restart); listbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.other_list); bronzebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_bronze); silverbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_silver); goldbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_gold); whitegoldbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.medals_whitegold); //游戏界面刷新handler mredrawhandler = new refreshhandler(); // 启动线程 new thread( this ).start(); } @override public void run() { // todo auto-generated method stub while ( true ) { // 通过发送消息更新界面 message m = new message(); m.what = 0x101 ; mredrawhandler.sendmessage(m); try { thread.sleep( 20 ); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } //游戏画面更新 void gamedraw(canvas canvas) { if (!binit) { bitmap bm; bm = bitmapfactory.decoderesource(getresources(), r.drawable.other_load); canvas.drawbitmap(bm, 0 , 0 , null ); paint p1 = new paint(); p1.setantialias( true ); p1.setcolor(color.white); p1.settextsize( 20 ); //设置字体大小 } else if (binit) { if (!bcourse) { a-=speed; if (a<= 0 ) a= 384 ; canvas.drawbitmap(otherstartbitmap, 0 , 0 , null ); canvas.drawbitmap(groundbitmap, a, 448 , null ); canvas.drawbitmap(groundbitmap, a- 384 , 448 , null ); b=a% 128 ; if (b>= 0 &&b< 32 ) { canvas.drawbitmap(birdmiddlebitmap, 175 , 220 , null ); } if (b>= 32 &&b< 64 ) { canvas.drawbitmap(birdupbitmap, 175 , 216 , null ); } if (b>= 64 &&b< 96 ) { canvas.drawbitmap(birdmiddlebitmap, 175 , 220 , null ); } if (b>= 96 &&b<= 128 ) { canvas.drawbitmap(birddownbitmap, 175 , 224 , null ); } canvas.drawbitmap(groundbitmap, a, 448 , null ); } else if (bcourse) { if (!bgame) { time += 1 ; int temp = time % 64 ; if (temp >= 0 && temp < 16 ) canvas.drawbitmap(courseupbitmap, 0 , 0 , null ); if (temp >= 16 && temp < 32 ) canvas.drawbitmap(coursemiddlebitmap, 0 , 0 , null ); if (temp >= 32 && temp < 48 ) canvas.drawbitmap(coursedownbitmap, 0 , 0 , null ); if (temp >= 48 && temp < 64 ) canvas.drawbitmap(coursemiddlebitmap, 0 , 0 , null ); a-=speed; if (a<= 0 ) a= 384 ; canvas.drawbitmap(groundbitmap, a, 448 , null ); canvas.drawbitmap(groundbitmap, a- 384 , 448 , null ); } else if (bgame) { if (!bgameover) { time+= 1 ; /***************计算高度************************/ v+= 9.8 ; if (v> 120 ) v= 120 ; else if (v<- 150 ) v=- 150 ; if (v>= 0 ) birdheight+=((v* 5.0 )/ 77 ); else if (v< 0 ) birdheight+=((v* 4.5 )/ 77 ); if (birdheight< 0 ) birdheight= 0 ; else if (birdheight> 415 ) birdheight= 415 ; /***************柱子的移动**********************/ for (c= 0 ;c< 3 ;c++) { pillar[c].x-=speed; if (pillar[c].x<=- 70 ) pillar[c].x= 650 ; if (pillar[c].x== 512 ) { pillar[c].h=( new random()).nextint( 200 )+ 200 ; } } /***************计算分数************************/ for (c= 0 ;c< 3 ;c++) { if (pillar[c].x== 100 ) point++; } /****************地面移动***********************/ a-=speed; /****************判断碰撞,+32判断下侧柱子,-170判断上侧柱子***********************/ if (pillar[ 0 ].x>= 30 &&pillar[ 0 ].x<= 145 &&(pillar[ 0 ].h<(birdheight+ 32 )||(pillar[ 0 ].h- 170 )>birdheight)) bgameover= true ; if (pillar[ 1 ].x>= 30 &&pillar[ 1 ].x<= 145 &&(pillar[ 1 ].h<(birdheight+ 32 )||(pillar[ 1 ].h- 170 )>birdheight)) bgameover= true ; if (pillar[ 2 ].x>= 30 &&pillar[ 2 ].x<= 145 &&(pillar[ 2 ].h<(birdheight+ 32 )||(pillar[ 2 ].h- 170 )>birdheight)) bgameover= true ; //是否碰到地面 if (birdheight> 414 ) bgameover= true ; } /***************显示图像************************/ //显示背景 canvas.drawbitmap(gamebackgroundbitmap, 0 , 0 , null ); //显示柱子 for (c= 0 ;c< 3 ;c++) { canvas.drawbitmap(pillardownbitmap, pillar[c].x, pillar[c].h, null ); canvas.drawbitmap(pillarupbitmap, pillar[c].x, pillar[c].h- 420 , null ); } if (a<= 0 ) a= 384 ; canvas.drawbitmap(groundbitmap, a, 448 , null ); canvas.drawbitmap(groundbitmap, a- 384 , 448 , null ); int temp=time% 16 ; if (temp>= 0 &&temp< 4 ) canvas.drawbitmap(birdmiddlebitmap, 100 , birdheight, null ); if (temp>= 4 &&temp< 8 ) canvas.drawbitmap(birdupbitmap, 100 , birdheight, null ); if (temp>= 8 &&temp< 12 ) canvas.drawbitmap(birdmiddlebitmap, 100 , birdheight, null ); if (temp>= 12 &&temp< 16 ) canvas.drawbitmap(birddownbitmap, 100 , birdheight, null ); if (!bgameover) { /*******************显示分数**********************/ paint p1 = new paint(); p1.setantialias( true ); p1.setcolor(color.white); p1.settextsize( 20 ); //设置字体大小 canvas.drawtext( "score:" +point, 171 , 50 , p1); canvas.drawtext( "acc:" +v, 171 , 80 , p1); canvas.drawtext( "h:" +birdheight, 171 , 110 , p1); } else if (bgameover) { canvas.drawbitmap(gameoverbitmap, 55 , 60 , null ); canvas.drawbitmap(recordbitmap, 35 , 150 , null ); canvas.drawbitmap(playagainbitmap, 35 , 350 , null ); canvas.drawbitmap(listbitmap, 200 , 350 , null ); if (point>= 10 &&point< 20 ) canvas.drawbitmap(bronzebitmap, 67 , 200 , null ); if (point>= 20 &&point< 30 ) canvas.drawbitmap(silverbitmap, 67 , 200 , null ); if (point>= 30 &&point< 40 ) canvas.drawbitmap(goldbitmap, 67 , 200 , null ); if (point>= 40 ) canvas.drawbitmap(whitegoldbitmap, 67 , 200 , null ); paint p1 = new paint(); p1.setantialias( true ); p1.setcolor(color.white); p1.settextsize( 20 ); //设置字体大小 canvas.drawtext( "" +point, 260 , 210 , p1); //最高纪录 canvas.drawtext( "1000" , 260 , 268 , p1); } } } } // 实例化画笔 paint p = new paint(); p.setcolor(color.black); p.setcolor(color.white); canvas.drawtext( "init: " +binit+ " course: " +bcourse+ " game: " +bgame+ " over: " +bgameover, 0 , 10 , p); } @override protected void ondraw(canvas canvas) { // todo auto-generated method stub super .ondraw(canvas); canvas.drawcolor(color.black); //这里的操作是为了在我的平板电脑竖屏尽量全屏显示,原图片很小,我的屏幕分辨率为1280*800,如果不需要对图片进行缩放,可以在只而立只保留gamedraw()部分 canvas.save(); canvas.translate( 16 , 80 ); float scale=( float ) 2.0 ; canvas.scale(scale, scale); canvas.cliprect( 0 , 0 , img_width, img_height); gamedraw(canvas); canvas.restore(); } // 更新界面处理器 class refreshhandler extends handler { @override public void handlemessage(message msg) { // todo auto-generated method stub if (msg.what == 0x101 ) { myview. this .update(); myview. this .invalidate(); } super .handlemessage(msg); } } // 更新坐标 private void update() { } //触屏事件 @override public boolean ontouchevent(motionevent event) { switch (event.getpointercount()) { case 1 : return onsingletouchevent(event); case 2 : return ondoubletouchevent(event); default : return false ; } } //单手指触屏处理 private boolean onsingletouchevent(motionevent event) { int x = ( int ) event.getx(); int y = ( int ) event.gety(); switch (event.getaction()) { case motionevent.action_up: if (!binit) { a=- 2 ; binit= true ; } else if (binit) { if (!bcourse) { if (( new random()).nextint( 3 )== 0 ) { gamebackgroundbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.background_night); coursedownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_night_down); courseupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_night_up); coursemiddlebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_night_middle); } else { gamebackgroundbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.background_day); coursedownbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_day_down); courseupbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_day_up); coursemiddlebitmap = bitmapfactory.decoderesource(getresources(), r.drawable.intro_day_middle); } time = 0 ; a= 0 ; bcourse= true ; } else if (bcourse) { if (!bgame) { time= 0 ; birdheight= 150 ; point= 0 ; pillar[ 0 ].x= 700 ; pillar[ 1 ].x= 940 ; pillar[ 2 ].x= 1180 ; a= 0 ; b= 0 ; c= 0 ; bgame= true ; } else if (bgame) { if (!bgameover) v-= 250 ; else if (bgameover) { bgameover= false ; bcourse= false ; bgame= false ; } } } } break ; case motionevent.action_down: break ; case motionevent.action_move: break ; } return true ; } //双指触屏处理 private boolean ondoubletouchevent(motionevent event) { switch (event.getaction() & motionevent.action_mask) { case motionevent.action_pointer_up: break ; case motionevent.action_pointer_down: { break ; } case motionevent.action_move: postinvalidate(); break ; } return true ; } } } |
图片:
background_day.png
background_ground.png
background_night.png
bird_down.png
bird_middle.png
bird_up.png
intro_day_down.png
intro_day_middle.png
intro_day_up.png
intro_night_down.png
intro_night_middle.png
intro_night_up.png
medals_bronze.png
medals_gold.png
medals_silver.png
medals_whitegold.png
other_gameover.png
other_list.png
other_load.png
other_record.png
other_restart.png
other_start.png
pillar_downside.png
pillar_upside.png
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/sqh4587/article/details/23458877