-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappyBird.java
215 lines (177 loc) · 5.9 KB
/
FlappyBird.java
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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class FlappyBird extends JPanel implements ActionListener, KeyListener {
int boardWidth = 360;
int boardHeight = 640;
//images
Image backgroundImg;
Image birdImg;
Image topPipeImg;
Image bottomPipeImg;
//bird class
int birdX = boardWidth/8;
int birdY = boardWidth/2;
int birdWidth = 34;
int birdHeight = 24;
class Bird {
int x = birdX;
int y = birdY;
int width = birdWidth;
int height = birdHeight;
Image img;
Bird(Image img) {
this.img = img;
}
}
//pipe class
int pipeX = boardWidth;
int pipeY = 0;
int pipeWidth = 64; //scaled by 1/6
int pipeHeight = 512;
class Pipe {
int x = pipeX;
int y = pipeY;
int width = pipeWidth;
int height = pipeHeight;
Image img;
boolean passed = false;
Pipe(Image img) {
this.img = img;
}
}
//game logic
Bird bird;
int velocityX = -4; //move pipes to the left speed (simulates bird moving right)
int velocityY = 0; //move bird up/down speed.
int gravity = 1;
ArrayList<Pipe> pipes;
Random random = new Random();
Timer gameLoop;
Timer placePipeTimer;
boolean gameOver = false;
double score = 0;
FlappyBird() {
setPreferredSize(new Dimension(boardWidth, boardHeight));
// setBackground(Color.blue);
setFocusable(true);
addKeyListener(this);
//load images
backgroundImg = new ImageIcon(getClass().getResource("./flappybirdbg.png")).getImage();
birdImg = new ImageIcon(getClass().getResource("./flappybird.png")).getImage();
topPipeImg = new ImageIcon(getClass().getResource("./toppipe.png")).getImage();
bottomPipeImg = new ImageIcon(getClass().getResource("./bottompipe.png")).getImage();
//bird
bird = new Bird(birdImg);
pipes = new ArrayList<Pipe>();
//place pipes timer
placePipeTimer = new Timer(1500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Code to be executed
placePipes();
}
});
placePipeTimer.start();
//game timer
gameLoop = new Timer(1000/60, this); //how long it takes to start timer, milliseconds gone between frames
gameLoop.start();
}
void placePipes() {
//(0-1) * pipeHeight/2.
// 0 -> -128 (pipeHeight/4)
// 1 -> -128 - 256 (pipeHeight/4 - pipeHeight/2) = -3/4 pipeHeight
int randomPipeY = (int) (pipeY - pipeHeight/4 - Math.random()*(pipeHeight/2));
int openingSpace = boardHeight/4;
Pipe topPipe = new Pipe(topPipeImg);
topPipe.y = randomPipeY;
pipes.add(topPipe);
Pipe bottomPipe = new Pipe(bottomPipeImg);
bottomPipe.y = topPipe.y + pipeHeight + openingSpace;
pipes.add(bottomPipe);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
//background
g.drawImage(backgroundImg, 0, 0, this.boardWidth, this.boardHeight, null);
//bird
g.drawImage(birdImg, bird.x, bird.y, bird.width, bird.height, null);
//pipes
for (int i = 0; i < pipes.size(); i++) {
Pipe pipe = pipes.get(i);
g.drawImage(pipe.img, pipe.x, pipe.y, pipe.width, pipe.height, null);
}
//score
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.PLAIN, 32));
if (gameOver) {
g.drawString("Game Over: " + String.valueOf((int) score), 10, 35);
}
else {
g.drawString(String.valueOf((int) score), 10, 35);
}
}
public void move() {
//bird
velocityY += gravity;
bird.y += velocityY;
bird.y = Math.max(bird.y, 0); //apply gravity to current bird.y, limit the bird.y to top of the canvas
//pipes
for (int i = 0; i < pipes.size(); i++) {
Pipe pipe = pipes.get(i);
pipe.x += velocityX;
if (!pipe.passed && bird.x > pipe.x + pipe.width) {
score += 0.5; //0.5 because there are 2 pipes! so 0.5*2 = 1, 1 for each set of pipes
pipe.passed = true;
}
if (collision(bird, pipe)) {
gameOver = true;
}
}
if (bird.y > boardHeight) {
gameOver = true;
}
}
boolean collision(Bird a, Pipe b) {
return a.x < b.x + b.width && //a's top left corner doesn't reach b's top right corner
a.x + a.width > b.x && //a's top right corner passes b's top left corner
a.y < b.y + b.height && //a's top left corner doesn't reach b's bottom left corner
a.y + a.height > b.y; //a's bottom left corner passes b's top left corner
}
@Override
public void actionPerformed(ActionEvent e) { //called every x milliseconds by gameLoop timer
move();
repaint();
if (gameOver) {
placePipeTimer.stop();
gameLoop.stop();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
//System.out.println("JUMP!");
velocityY = -9;
if (gameOver) {
//restart game by resetting conditions
bird.y = birdY;
velocityY = 0;
pipes.clear();
gameOver = false;
score = 0;
gameLoop.start();
placePipeTimer.start();
}
}
}
// Not needed
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}