服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现Flappy Bird游戏源代码

java实现Flappy Bird游戏源代码

2021-06-24 10:11Koma_Wong Java教程

这篇文章主要为大家详细介绍了java实现Flappy Bird游戏源代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现flappy bird游戏的具体代码,供大家参考,具体内容如下

?
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
/*
2017/7/23
*/
 
import java.awt.graphics;
//import java.util.timer;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.mouselistener;
import java.awt.event.mouseevent;
import java.awt.event.keylistener;
import java.awt.event.keyevent;
import java.awt.rectangle;
import java.awt.*;
 
import java.util.*;
 
import javax.swing.jframe;
import javax.swing.timer;
import javax.swing.*;
 
 
import javax.swing.jpanel;
 
class renderer extends jpanel
{
 
 private static final long serialversionuid = 1l;
 
 protected void paintcomponent(graphics g)
 {
 super.paintcomponent(g);
 
 flappybird.flappybird.repaint(g);
 }
 
}
public class flappybird implements actionlistener, mouselistener, keylistener
{
 public static flappybird flappybird;
 
 public final int width = 900, height = 800;
 
 public renderer renderer;
 
 public rectangle bird;
 
 public arraylist<rectangle> columns;
 
 public int ticks, ymotion, score;
 
 public boolean gameover, started;
 
 public random rand;
 
 public flappybird()
 {
 jframe jframe = new jframe();
 timer timer = new timer(20,this);
 
 renderer = new renderer();
 rand = new random();
 
 jframe.add(renderer);
 jframe.settitle("flappy bird");
 jframe.setdefaultcloseoperation(jframe.exit_on_close);
 jframe.setsize(width,height);
 jframe.addmouselistener(this);
 jframe.addkeylistener(this);
 jframe.setresizable(false);
 jframe.setvisible(true);
 
 bird = new rectangle(width / 2 - 10, height / 2 - 10, 20, 20);
 
 columns = new arraylist<rectangle>();
 
 addcolumn(true);
 addcolumn(true);
 addcolumn(true);
 addcolumn(true);
 
 timer.start();
 }
 
 public void addcolumn(boolean start)
 {
 int space = 300;
 int width = 100;
 int height = 50 + rand.nextint(300);
 
 if(start)
 {
 columns.add(new rectangle(width + width + columns.size() * 300, height - height - 120, width, height));
 columns.add(new rectangle(width + width + (columns.size()-1)*300, 0, width, height - height - space));
 }
 else
 {
 columns.add(new rectangle(columns.get(columns.size() - 1).x + 600, height - height - 120, width, height));
 columns.add(new rectangle(columns.get(columns.size() - 1).x , 0, width, height - height - space));
 }
 
 }
 
 public void paintcolumn(graphics g, rectangle column)
 {
 g.setcolor(color.green.darker());
 g.fillrect(column.x, column.y, column.width, column.height);
 }
 
 public void jump()
 {
 if (gameover)
 {
 bird = new rectangle(width / 2 - 10, height / 2 - 10, 20, 20);
 
 columns.clear();
 
 ymotion = 0;
 score = 0;
 
 addcolumn(true);
 addcolumn(true);
 addcolumn(true);
 addcolumn(true);
 
 gameover = false;
 }
 
 if(!started)
 {
 started = true;
 }
 else if(!gameover)
 {
 if(ymotion > 0)
 {
 ymotion = 0;
 }
 
 ymotion -= 10;
 }
 }
 
 public void actionperformed(actionevent e)
 {
 
 int speed = 10;
 
 ticks++;
 
 if(started )
 {
 for( int i = 0; i < columns.size(); i++)
 {
 rectangle column = columns.get(i);
 
 column.x -= speed;
 }
 
 if(ticks % 2 ==0 && ymotion < 15)
 {
 ymotion += 2;
 }
 
 for (int i = 0; i < columns.size(); i++)
 {
 rectangle column = columns.get(i);
 
 if (column.x + column.width < 0)
 {
  columns.remove(column);
  if(column.y ==0)
  {
  addcolumn(false);
  }
 }
 }
 
 bird.y += ymotion;
 
 for(rectangle column : columns)
 {
 if(bird.x + bird.width / 2 > column.x + column.width / 2 - 5
 && bird.x + bird.width / 2 < column.x + column.width / 2 + 5
 && column.y == 0)
 {
  score++;
 }
 
 if(column.intersects(bird))
 {
  gameover = true;
 
  if(bird.x <= column.x)
  {
  bird.x = column.x - bird.width;
  }
  else
  {
  if(column.y != 0)
  {
  bird.y = column.y - bird.height;
  }
  else if(bird.y < column.height)
  {
  bird.y = column.height;
  }
  }
 }
 }
 
 if(bird.y > height - 120 || bird.y < 0 )
 {
 gameover = true;
 }
 
 if(bird.y + ymotion >= height -120)//(gameover)
 {
 bird.y = height -120 - bird.height;
 }
 }
 renderer.repaint();
 }
 
 public void repaint(graphics g)
 {
 //system.out.println("hello");
 g.setcolor(color.cyan);
 g.fillrect(0,0,width,height);
 
 g.setcolor(color.orange);
 g.fillrect(0, height - 120, width, 150);
 
 g.setcolor(color.green);
 g.fillrect(0, height - 120, width, 20);
 
 g.setcolor(color.red);
 g.fillrect(bird.x, bird.y, bird.width, bird.height);
 
 for ( rectangle column : columns )
 {
 paintcolumn(g,column);
 }
 
 g.setcolor(color.white);
 g.setfont(new font("arial",1,70));
 
 if(!started)
 {
 g.drawstring("click to start!",90,height / 2-50);
 }
 
 if(gameover)
 {
 g.drawstring("game over! you suck!",40,height / 2-50);
 }
 
 if(!gameover && started)
 {
 g.drawstring(string.valueof(score), width / 2, 100);
 }
 }
 
 public static void main(string[]args)
 {
 flappybird = new flappybird();
 }
 
 public void mouseclicked(mouseevent e)
 {
 jump();
 }
 public void mousepressed(mouseevent e){}
 public void mousereleased(mouseevent e){}
 public void mouseentered(mouseevent e){}
 public void mouseexited(mouseevent e){}
 
 public void keypressed(keyevent e){}
 public void keytyped(keyevent e){}
 public void keyreleased(keyevent e)
 {
 if(e.getkeycode() == keyevent.vk_space)
 {
 jump();
 }
 }
 
}

效果图:

java实现Flappy Bird游戏源代码

java实现Flappy Bird游戏源代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Rong_Toa/article/details/78118064

延伸 · 阅读

精彩推荐