400-700-1307

java实现Flappy Bird游戏源代码

2019-01-26 14:46

本文实例为大家分享了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();
 }
 }
 
}

开班信息

当前校区

北京校区
  • 北京校区
  • 上海校区
  • 广州校区
  • 沈阳校区
  • 杭州校区
  • 南京校区
  • 郑州校区
  • 成都校区
  • 深圳校区
  • 西安校区
  • 苏州校区
  • 深圳沙井
  • 深圳观澜
  • 深圳龙岗
  • 武汉校区
  • 西宁校区
  • 石家庄校区
  • 湖北咸宁
  • 广东惠州
  • 广东佛山

                    Copyright © 2007-2019 IT兄弟连教育 版权所有 京ICP备11018177号  京公网安备 11010802026987号