forked from Arnob-sen/our_java_game_project_flappy_bird
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlappyBird.java
More file actions
124 lines (101 loc) · 3.28 KB
/
FlappyBird.java
File metadata and controls
124 lines (101 loc) · 3.28 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
package FlappyBird;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class FlappyBird implements ActionListener, KeyListener {
public static final int FPS = 60, WIDTH = 640, HEIGHT = 480;
private Bird bird;
private JFrame frame;
private JPanel panel;
private ArrayList<Rectangle> rects;
private int time, scroll;
private Timer t;
private boolean paused;
public void go() {
frame = new JFrame("Flappy Bird");
bird = new Bird();
rects = new ArrayList<Rectangle>();
panel = new GamePanel(this, bird, rects);
frame.add(panel);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.addKeyListener(this);
paused = true;
t = new Timer(1000/FPS, this);
t.start();
}
public static void main(String[] args) {
new FlappyBird().go();
}
@Override
public void actionPerformed(ActionEvent e) {
panel.repaint();
if(!paused) {
bird.physics();
if(scroll % 90 == 0) {
Rectangle r = new Rectangle(WIDTH, 0, GamePanel.PIPE_W, (int) ((Math.random()*HEIGHT)/5f + (0.2f)*HEIGHT));
int h2 = (int) ((Math.random()*HEIGHT)/5f + (0.2f)*HEIGHT);
Rectangle r2 = new Rectangle(WIDTH, HEIGHT - h2, GamePanel.PIPE_W, h2);
rects.add(r);
rects.add(r2);
}
ArrayList<Rectangle> toRemove = new ArrayList<Rectangle>();
boolean game = true;
for(Rectangle r : rects) {
r.x-=3;
if(r.x + r.width <= 0) {
toRemove.add(r);
}
if(r.contains(bird.x, bird.y)) {
JOptionPane.showMessageDialog(frame, "You lose!\n"+"Your score was: "+time+".");
game = false;
}
}
rects.removeAll(toRemove);
time++;
scroll++;
if(bird.y > HEIGHT || bird.y+bird.RAD < 0) {
game = false;
}
if(!game) {
rects.clear();
bird.reset();
time = 0;
scroll = 0;
paused = true;
}
}
else {
}
}
public int getScore() {
return time;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP) {
bird.jump();
}
else if(e.getKeyCode()==KeyEvent.VK_SPACE) {
paused = false;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public boolean paused() {
return paused;
}
}