-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappyBird.java
249 lines (205 loc) · 8.27 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
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
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class FlappyBird extends JPanel implements ActionListener, KeyListener, MouseListener {
// Screen dimensions
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int boardWidth = (int) screenSize.getWidth();
int boardHeight = (int) screenSize.getHeight();
// Offset adjustments for elements
int offsetX = 0;
int offsetY = 0;
// Thresholds for transitions
private int randomThreshold;
private int transitionThreshold;
// Game elements
private BufferedImage blendedImage;
private ArrayList<Player> players = new ArrayList<>();
private ArrayList<Pipe> pipes;
private Random random = new Random();
// Game images
Image backgroundImg, birdImg, topPipeImg, bottomPipeImg, gameoverImg, playbuttonImg;
Image nighttimeImg, leaderImg, scoreboardImg, silverImg, bronzeImg, goldImg, uddImg;
// Game objects
Bird bird;
Timer gameLoop;
Timer placePipeTimer;
boolean gameOver = true;
double score = 0;
// Speeds and physics
int velocityX = -4;
int velocityY = 0;
int gravity = 1;
// Medal and button objects
Udd udd;
Gold gold;
Silver silver;
Bronze bronze;
Scoreb scoreb;
Leader leader;
Game game;
Play play;
// Constructor
public FlappyBird() {
// Panel setup
setPreferredSize(new Dimension(boardWidth, boardHeight));
setBackground(Color.blue);
setFocusable(true);
addKeyListener(this);
addMouseListener(this);
// Random threshold for transitions
randomThreshold = (int) (Math.random() * (50 - 100 + 1)) + 100;
transitionThreshold = 100;
// Initialize buffered image
blendedImage = new BufferedImage(boardWidth, boardHeight, BufferedImage.TYPE_INT_ARGB);
// Load images
loadImages();
// Initialize game objects
initializeGameObjects();
// Initialize pipe placement timer
placePipeTimer = new Timer(1500, e -> placePipes());
placePipeTimer.start();
// Initialize game loop timer
gameLoop = new Timer(1000 / 60, this);
gameLoop.start();
}
// Load all necessary images
private void loadImages() {
backgroundImg = new ImageIcon(getClass().getResource("./fullscreenbg.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();
gameoverImg = new ImageIcon(getClass().getResource("./logo.png")).getImage();
playbuttonImg = new ImageIcon(getClass().getResource("./playbutton.png")).getImage();
nighttimeImg = new ImageIcon(getClass().getResource("./fsnighttimebg.png")).getImage();
leaderImg = new ImageIcon(getClass().getResource("./leaderboard.png")).getImage();
scoreboardImg = new ImageIcon(getClass().getResource("./scoreboard.png")).getImage();
bronzeImg = new ImageIcon(getClass().getResource("./bronze.png")).getImage();
silverImg = new ImageIcon(getClass().getResource("./silver.png")).getImage();
goldImg = new ImageIcon(getClass().getResource("./gold.png")).getImage();
uddImg = new ImageIcon(getClass().getResource("./udd.png")).getImage();
}
// Initialize game-related objects
private void initializeGameObjects() {
bird = new Bird(birdImg);
pipes = new ArrayList<>();
udd = new Udd(uddImg);
gold = new Gold(goldImg);
silver = new Silver(silverImg);
bronze = new Bronze(bronzeImg);
scoreb = new Scoreb(scoreboardImg);
leader = new Leader(leaderImg);
game = new Game(gameoverImg);
play = new Play(playbuttonImg);
}
// Place new pipes on the board
private void placePipes() {
int randomPipeY = (int) (random.nextInt(pipeHeight / 2) - pipeHeight / 5);
int openingSpace = boardHeight / 4;
Pipe topPipe = new Pipe(topPipeImg, boardWidth, randomPipeY);
Pipe bottomPipe = new Pipe(bottomPipeImg, boardWidth, randomPipeY + pipeHeight + openingSpace);
pipes.add(topPipe);
pipes.add(bottomPipe);
}
// Main paint method
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
// Drawing routine
private void draw(Graphics g) {
drawBackground(g);
g.drawImage(bird.img, bird.x, bird.y, bird.width, bird.height, null);
for (Pipe pipe : pipes) {
g.drawImage(pipe.img, pipe.x, pipe.y, pipe.width, pipe.height, null);
}
drawScoreboard(g);
}
// Draw the background with blending effects
private void drawBackground(Graphics g) {
if (score >= randomThreshold) {
float transitionFactor = Math.min(1, (float) (score - randomThreshold) / transitionThreshold);
Image blended = blendImages(backgroundImg, nighttimeImg, transitionFactor);
g.drawImage(blended, 0, 0, boardWidth, boardHeight, null);
} else {
g.drawImage(backgroundImg, 0, 0, boardWidth, boardHeight, null);
}
}
// Helper to blend images
private Image blendImages(Image img1, Image img2, float alpha) {
BufferedImage blended = new BufferedImage(boardWidth, boardHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = blended.createGraphics();
g2d.drawImage(img1, 0, 0, boardWidth, boardHeight, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(img2, 0, 0, boardWidth, boardHeight, null);
g2d.dispose();
return blended;
}
// Draw the scoreboard and medals
private void drawScoreboard(Graphics g) {
if (gameOver) {
g.drawImage(scoreboardImg, scoreb.x, scoreb.y, scoreb.width, scoreb.height, null);
g.setColor(new Color(84, 56, 71));
g.setFont(new Font("Dubai Medium", Font.BOLD, 52));
g.drawString(String.valueOf((int) score), scoreb.x + 200, scoreb.y + 100);
g.drawImage(gameoverImg, game.x, game.y, game.width, game.height, null);
g.drawImage(playbuttonImg, play.x, play.y, play.width, play.height, null);
if (score >= 10 && score <= 80) {
g.drawImage(bronzeImg, bronze.x, bronze.y, bronze.width, bronze.height, null);
} else if (score >= 81 && score <= 150) {
g.drawImage(silverImg, silver.x, silver.y, silver.width, silver.height, null);
} else if (score >= 151 && score <= 250) {
g.drawImage(goldImg, gold.x, gold.y, gold.width, gold.height, null);
} else if (score > 250) {
g.drawImage(uddImg, udd.x, udd.y, udd.width, udd.height, null);
}
}
}
// Player leaderboard update and logic omitted for brevity
@Override
public void actionPerformed(ActionEvent e) {
// Game loop updates
if (!gameOver) {
velocityY += gravity;
bird.y += velocityY;
for (Pipe pipe : pipes) {
pipe.x += velocityX;
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
velocityY = -11;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
// Helper classes omitted for brevity
public static void main(String[] args) {
JFrame frame = new JFrame("Flappy Bird");
FlappyBird game = new FlappyBird();
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}