-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappyBird.java
More file actions
186 lines (149 loc) · 5.3 KB
/
flappyBird.java
File metadata and controls
186 lines (149 loc) · 5.3 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
import java.io.IOException;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.sampled.*;
import javax.swing.Timer;
import javax.swing.*;
public class flappyBird extends JPanel implements ActionListener, KeyListener {
Image bg, bird, top, bottom;
Bird mainBird;
Timer loop;
Timer pipeTimer;
ArrayList<pipe> pipes;
Clip clip;
int velocityX = -4;
int velocityY = 0;
int gravity = 1;
boolean gameOver = false;
int score = 0;
JButton restartButton;
public flappyBird() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
setPreferredSize(new Dimension(360, 640));
setFocusable(true);
addKeyListener(this);
bg = new ImageIcon(Objects.requireNonNull(getClass().getResource("./resources/bg.png"))).getImage();
bird = new ImageIcon(Objects.requireNonNull(getClass().getResource("./resources/bird.png"))).getImage();
top = new ImageIcon(Objects.requireNonNull(getClass().getResource("./resources/top.png"))).getImage();
bottom = new ImageIcon(Objects.requireNonNull(getClass().getResource("./resources/bottom.png"))).getImage();
mainBird = new Bird(bird);
pipes = new ArrayList<>();
pipeTimer = new Timer(1500, _ -> pipeWhere());
pipeTimer.start();
loop = new Timer(1000 / 60, this);
loop.start();
playBackgroundMusic();
restartButton = new JButton("Restart");
restartButton.setFocusable(false);
restartButton.setVisible(false);
restartButton.addActionListener(e -> {
try {
restartGame();
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException ex) {
throw new RuntimeException(ex);
}
});
this.add(restartButton);
}
public void pipeWhere() {
int minY = -300;
int maxY = -100;
int randomYpipe = minY + (int) (Math.random() * (maxY - minY));
int freeSpace = 160;
pipe topPipe = new pipe(top);
topPipe.y = randomYpipe;
pipes.add(topPipe);
pipe bottomPipe = new pipe(bottom);
bottomPipe.y = randomYpipe + topPipe.height + freeSpace;
pipes.add(bottomPipe);
}
public void playBackgroundMusic() throws LineUnavailableException, IOException, UnsupportedAudioFileException {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
Objects.requireNonNull(getClass().getResource("./resources/bg_music.wav"))
);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void stopBackgroundMusic() {
if (clip != null && clip.isRunning()) {
clip.stop();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
g.drawImage(bg, 0, 0, 360, 640, null);
g.drawImage(mainBird.birdImg, mainBird.x, mainBird.y, mainBird.bWidth, mainBird.bHeight, null);
for (pipe pipe : pipes) {
g.drawImage(pipe.img, pipe.x, pipe.y, pipe.width, pipe.height, null);
}
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.drawString("Score: " + score, 20, 40);
if (gameOver) {
g.setFont(new Font("Arial", Font.BOLD, 36));
g.drawString("Game Over", 90, 300);
}
}
public void move() {
velocityY += gravity;
mainBird.y += velocityY;
mainBird.y = Math.max(mainBird.y, 0);
for (pipe pipe : pipes) {
pipe.x += velocityX;
}
pipes.removeIf(pipe -> pipe.x + pipe.width < 0);
Rectangle birdRect = new Rectangle(mainBird.x, mainBird.y, mainBird.bWidth, mainBird.bHeight);
for (pipe pipe : pipes) {
Rectangle pipeRect = new Rectangle(pipe.x, pipe.y, pipe.width, pipe.height);
if (birdRect.intersects(pipeRect)) {
gameOver = true;
}
}
if (mainBird.y > 640 - mainBird.bHeight) {
gameOver = true;
}
for (pipe pipe : pipes) {
if (!pipe.scored && pipe.img == top && pipe.x + pipe.width < mainBird.x) {
score++;
pipe.scored = true;
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (!gameOver) {
move();
} else {
pipeTimer.stop();
loop.stop();
stopBackgroundMusic();
restartButton.setVisible(true);
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE && !gameOver) {
velocityY = -9;
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
public void restartGame() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
mainBird.y = 320;
velocityY = 0;
pipes.clear();
score = 0;
gameOver = false;
playBackgroundMusic();
restartButton.setVisible(false);
pipeTimer.start();
loop.start();
}
}