-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlappyBirdGame.java
More file actions
317 lines (263 loc) · 10.6 KB
/
FlappyBirdGame.java
File metadata and controls
317 lines (263 loc) · 10.6 KB
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JOptionPane;
/**
* FlappyBirdGame - A simple Flappy Bird clone implemented using Java Swing.
* This class handles the main window and contains the game logic panel.
*/
public class FlappyBirdGame {
// --- MAIN METHOD ---
public static void main(String[] args) {
// Create the main window frame
JFrame frame = new JFrame("Flappy Bird in Java");
// Create and add the game panel
GamePanel gamePanel = new GamePanel();
frame.add(gamePanel);
// Setup frame properties
frame.pack(); // Size the frame to fit the panel
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null); // Center the window
frame.setVisible(true);
// Start the game loop managed by the Timer in the GamePanel
gamePanel.startGame();
}
// --- BIRD CLASS ---
static class Bird {
public int x, y; // Position
public final int SIZE = 40; // Diameter of the bird
public double velocity;
public final double GRAVITY = 0.5;
public final double JUMP_STRENGTH = -9.0;
public Bird(int x, int y) {
this.x = x;
this.y = y;
this.velocity = 0;
}
public void update() {
// Apply gravity to velocity
velocity += GRAVITY;
// Update vertical position
y += velocity;
// Simple velocity damping (optional, makes it smoother)
velocity *= 0.98;
}
public void jump() {
velocity = JUMP_STRENGTH;
}
public void draw(Graphics g) {
// Draw Bird Body (Yellow Circle)
g.setColor(new Color(252, 211, 77)); // Tailwind Yellow-400
g.fillOval(x, y, SIZE, SIZE);
// Draw Outline
g.setColor(new Color(146, 64, 14)); // Dark Brown
g.drawOval(x, y, SIZE, SIZE);
// Draw Eye (Small black circle)
g.setColor(Color.BLACK);
g.fillOval(x + (int)(SIZE * 0.7), y + (int)(SIZE * 0.2), SIZE / 6, SIZE / 6);
}
}
// --- PIPE CLASS ---
static class Pipe {
public int x; // Horizontal position
public final int WIDTH = 60;
public int gapY; // Center Y position of the gap
public final int GAP_HEIGHT = 160;
public final int SPEED = 3;
public boolean scored = false;
public Pipe(int x, int gapY) {
this.x = x;
this.gapY = gapY;
}
public void update() {
x -= SPEED; // Move pipes to the left
}
public void draw(Graphics g, int height) {
g.setColor(new Color(16, 185, 129)); // Tailwind Emerald-500 (Pipe Color)
// Draw Top Pipe
int topPipeHeight = gapY - GAP_HEIGHT / 2;
g.fillRect(x, 0, WIDTH, topPipeHeight);
// Draw Bottom Pipe
int bottomPipeY = gapY + GAP_HEIGHT / 2;
int bottomPipeHeight = height - bottomPipeY;
g.fillRect(x, bottomPipeY, WIDTH, bottomPipeHeight);
// Add Darker borders for detail
g.setColor(new Color(4, 120, 87)); // Darker Emerald
g.drawRect(x, 0, WIDTH, topPipeHeight);
g.drawRect(x, bottomPipeY, WIDTH, bottomPipeHeight);
}
}
// --- GAME PANEL (Handles drawing and logic) ---
static class GamePanel extends JPanel implements ActionListener, KeyListener {
// Game Dimensions
private final int GAME_WIDTH = 480;
private final int GAME_HEIGHT = 720;
// Game Objects
private Bird bird;
private ArrayList<Pipe> pipes;
private Timer timer;
private Random random;
// Game State
private boolean isRunning = false;
private int score;
private long lastPipeTime;
private final long PIPE_INTERVAL = 1500; // milliseconds
// UI elements
private final int GROUND_HEIGHT = 30;
private Font scoreFont = new Font("Arial", Font.BOLD, 48);
public GamePanel() {
setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
setBackground(new Color(125, 211, 252)); // Tailwind Sky-300 (Sky background)
setFocusable(true); // Allows key events to be captured
addKeyListener(this);
random = new Random();
resetGame();
// Setup the game loop timer (e.g., 60 frames per second)
timer = new Timer(1000 / 60, this);
}
// Initialization/Reset function
private void resetGame() {
bird = new Bird(GAME_WIDTH / 4, GAME_HEIGHT / 2, 0);
pipes = new ArrayList<>();
score = 0;
isRunning = false;
}
public void startGame() {
isRunning = true;
lastPipeTime = System.currentTimeMillis();
timer.start();
}
// Timer event handling (the core game loop)
@Override
public void actionPerformed(ActionEvent e) {
if (isRunning) {
updateGame();
repaint(); // Calls paintComponent
}
}
// Game logic update
private void updateGame() {
bird.update();
// Check for pipe spawning
long currentTime = System.currentTimeMillis();
if (currentTime - lastPipeTime > PIPE_INTERVAL) {
spawnPipe();
lastPipeTime = currentTime;
}
// Update and check pipes
for (int i = 0; i < pipes.size(); i++) {
Pipe pipe = pipes.get(i);
pipe.update();
// Check for scoring
// If the pipe passed the bird's center (x + half size) and hasn't been scored
if (!pipe.scored && pipe.x + pipe.WIDTH < bird.x + bird.SIZE / 2) {
score++;
pipe.scored = true;
}
// Check collision
if (checkCollision(pipe)) {
gameOver();
return; // Stop processing after game over
}
}
// Remove off-screen pipes
pipes.removeIf(pipe -> pipe.x + pipe.WIDTH < 0);
// Check ground/ceiling collision
if (bird.y + bird.SIZE > GAME_HEIGHT - GROUND_HEIGHT || bird.y < 0) {
gameOver();
}
}
private void spawnPipe() {
// Random Y for gap center, between 25% and 75% of game height (excluding ground)
int minGapY = (int) (GAME_HEIGHT * 0.25);
int maxGapY = (int) (GAME_HEIGHT * 0.75) - GROUND_HEIGHT;
int gapY = random.nextInt(maxGapY - minGapY) + minGapY;
pipes.add(new Pipe(GAME_WIDTH, gapY));
}
private boolean checkCollision(Pipe pipe) {
// Check if bird's bounding box overlaps pipe's horizontal position
if (bird.x + bird.SIZE > pipe.x && bird.x < pipe.x + pipe.WIDTH) {
// Top pipe collision
int topPipeBottom = pipe.gapY - pipe.GAP_HEIGHT / 2;
if (bird.y < topPipeBottom) {
return true;
}
// Bottom pipe collision
int bottomPipeTop = pipe.gapY + pipe.GAP_HEIGHT / 2;
if (bird.y + bird.SIZE > bottomPipeTop) {
return true;
}
}
return false;
}
private void gameOver() {
isRunning = false;
timer.stop();
repaint(); // Draw the final state
// Use a standard Swing dialog for Game Over message
JOptionPane.showMessageDialog(this,
"Game Over! Your Score: " + score + "\nPress ENTER or click 'OK' to restart.",
"Game Over",
JOptionPane.INFORMATION_MESSAGE);
resetGame(); // Prepare for the next game
startGame(); // Automatically restart
}
// --- RENDERING ---
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // Draws the background
// Draw Pipes
for (Pipe pipe : pipes) {
pipe.draw(g, GAME_HEIGHT);
}
// Draw Bird
bird.draw(g);
// Draw Ground
g.setColor(new Color(76, 175, 80)); // Tailwind Green-600
g.fillRect(0, GAME_HEIGHT - GROUND_HEIGHT, GAME_WIDTH, GROUND_HEIGHT);
g.setColor(new Color(27, 94, 32)); // Darker Green border
g.drawLine(0, GAME_HEIGHT - GROUND_HEIGHT, GAME_WIDTH, GAME_HEIGHT - GROUND_HEIGHT);
// Draw Score
g.setColor(Color.WHITE);
g.setFont(scoreFont);
String scoreStr = String.valueOf(score);
int strWidth = g.getFontMetrics().stringWidth(scoreStr);
g.drawString(scoreStr, (GAME_WIDTH - strWidth) / 2, 60);
// Draw Start/Pause message if not running
if (!isRunning) {
g.setColor(new Color(255, 255, 255, 180)); // Semi-transparent white
g.setFont(new Font("Arial", Font.BOLD, 30));
String startMsg = "Press SPACE to jump!";
int msgWidth = g.getFontMetrics().stringWidth(startMsg);
g.drawString(startMsg, (GAME_WIDTH - msgWidth) / 2, GAME_HEIGHT / 3);
}
}
// --- KEY LISTENER INTERFACE METHODS ---
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_UP) {
if (isRunning) {
bird.jump();
} else {
startGame(); // Start game on first spacebar press
}
}
}
// Unused methods required by KeyListener interface
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}
}