Skip to content

Commit 657a872

Browse files
author
Joe O'Regan
committed
Flappy Birds - Tidy Code
1 parent 0eb3e8f commit 657a872

File tree

5 files changed

+54
-43
lines changed

5 files changed

+54
-43
lines changed

FlappyBird/Bird.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class Bird extends Rectangle{
99
private static final long serialVersionUID = 1L;
1010
public static final int WIDTH=28;
1111
public static final int HEIGHT=20;
12-
Image pic;
13-
public int yMotion;
12+
private Image pic;
13+
private int yMotion;
1414

15-
Bird(int a, int b){
15+
public Bird(int a, int b){
1616
yMotion=0;
1717
x=a;
1818
y=b;

FlappyBird/Cloud.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class Cloud extends Rectangle{
1414
Random random=new Random();
1515
URL url;
1616

17-
int speed;
17+
private int speed;
1818

19-
Cloud(){
19+
public Cloud(){
2020
speed=(int) (Math.random()*5)+1;
2121

2222
x=random.nextInt(12) * 100;

FlappyBird/FlappyBird.java

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,35 @@
1313
import java.util.ArrayList;
1414
import java.util.Random;
1515

16-
public class FlappyBird extends JPanel implements ActionListener,MouseListener,KeyListener{
16+
public class FlappyBird extends JPanel implements ActionListener, MouseListener, KeyListener {
1717
private static final long serialVersionUID = 1L;
1818

19-
Cloud cloud1 = new Cloud(); // Same 3 clouds resized and repositioned after moving off screen
20-
Cloud cloud2 = new Cloud();
21-
Cloud cloud3 = new Cloud();
19+
public static final int WIDTH=1200,HEIGHT=800,GROUND_HEIGHT=120;
2220

2321
public static FlappyBird flappyBird;
2422

25-
static int highScoreEasy = 0;
26-
static int highScoreMedium = 0;
27-
static int highScoreHard = 0;
28-
29-
public static final int WIDTH=1200,HEIGHT=800,GROUND_HEIGHT=120;
23+
Cloud cloud1 = new Cloud(); // Same 3 clouds resized and repositioned after moving off screen
24+
Cloud cloud2 = new Cloud();
25+
Cloud cloud3 = new Cloud();
3026

31-
public Renderer renderer;
27+
static int highScoreEasy = 0, highScoreMedium = 0, highScoreHard = 0;
3228

33-
public Bird bird;
29+
Renderer renderer;
3430

35-
public String gameOverStr, startStr, scoreStr, highScoreStr;
31+
Bird bird;
3632

37-
public int ticks, lastTicks, score, overTextWidth, startTextWidth, scoreTextWidth, highScoreTextWidth, difficulty=1;
33+
String gameOverStr, startStr, scoreStr, highScoreStr;
3834

39-
public ArrayList<Pipe> pipes;
35+
int ticks, lastTicks, score, overTextWidth, startTextWidth, scoreTextWidth, highScoreTextWidth, difficulty=1;
4036

41-
public Random rand;
37+
ArrayList<Pipe> pipes;
4238

43-
public boolean gameOver, started;
39+
Random rand;
4440

45-
boolean playCrash=true;
41+
boolean gameOver, started, playCrash;
4642

4743
public FlappyBird(){
48-
started=false;
49-
JFrame jframe = new JFrame("Flappy Bird");
44+
JFrame jframe = new JFrame("Flappy Bird (Joe O'Regan)");
5045
Timer timer = new Timer(20, this);
5146

5247
renderer=new Renderer();
@@ -60,6 +55,8 @@ public FlappyBird(){
6055
jframe.setResizable(false);
6156
jframe.setVisible(true);
6257

58+
started = false;
59+
6360
pipes = new ArrayList<Pipe>();
6461

6562
init();
@@ -77,20 +74,35 @@ public void init() {
7774
}
7875

7976
public void startPipes(){
77+
ticks=0;
78+
lastTicks=0;
8079
score = 0;
8180

8281
pipes.clear();
8382

84-
addPipe(difficulty,true);
85-
addPipe(difficulty,true);
86-
addPipe(difficulty,true);
87-
addPipe(difficulty,true);
83+
addPipe(difficulty, true);
84+
addPipe(difficulty, true);
85+
addPipe(difficulty, true);
86+
addPipe(difficulty, true);
8887
}
8988

9089
@Override
9190
public void actionPerformed(ActionEvent e){
9291
ticks++;
9392

93+
cloud1.move();
94+
cloud2.move();
95+
cloud3.move();
96+
97+
if(!started) {
98+
if((ticks%33==0||ticks<=1)) {
99+
bird.jump();
100+
} else if (ticks % 3 == 0) {
101+
bird.fall();
102+
}
103+
bird.move();
104+
}
105+
94106
if(started) {
95107
for(int i=0;i<pipes.size();i++){
96108
Pipe pipe = pipes.get(i);
@@ -110,9 +122,6 @@ public void actionPerformed(ActionEvent e){
110122
}
111123
}
112124

113-
cloud1.move();
114-
cloud2.move();
115-
cloud3.move();
116125
bird.move();
117126

118127
for(Pipe pipe:pipes){
@@ -143,10 +152,10 @@ public void actionPerformed(ActionEvent e){
143152
playCrash=false;
144153
SoundEffect.crashFX.play();
145154
}
146-
}
147155

148-
if(gameOver && lastTicks==0){
149-
lastTicks=ticks;
156+
if(gameOver && lastTicks==0){
157+
lastTicks=ticks;
158+
}
150159
}
151160

152161
renderer.repaint();
@@ -182,8 +191,6 @@ public void repaint(Graphics g){
182191
cloud2.draw(g,this);
183192
cloud3.draw(g,this);
184193

185-
bird.draw(g, this);
186-
187194
for(Pipe pipe:pipes){
188195
pipe.draw(g,this);
189196
}
@@ -201,11 +208,17 @@ public void repaint(Graphics g){
201208
highScoreStr = "HighScores: ";
202209

203210
if (!started) {
204-
drawText(g, startStr, 0.5, 0.5, 100,Color.white);
211+
drawText(g, startStr, 0.5, 0.5, 100, Color.white);
212+
}
213+
214+
if (!started || gameOver) {
215+
drawText(g, "Difficulty: 1. Easy 2. Medium 3. Hard", 0.9, 0.933, 50, Color.white);
205216
}
206217

218+
bird.draw(g, this);
219+
207220
if (gameOver) {
208-
drawText(g, gameOverStr, 0.5, 0.25,100, Color.black);
221+
drawText(g, gameOverStr, 0.5, 0.25, 100, Color.black);
209222
drawText(g, scoreStr, 0.5, 0.5, 50, Color.black);
210223
setHighScores();
211224
drawText(g, highScoreStr, 0.5, 0.75, 50, Color.black);
@@ -291,7 +304,6 @@ public void keyReleased(KeyEvent e){
291304
}
292305

293306
public void actionButton(){
294-
// System.out.println("ticks: "+ticks+" lastTicks: "+lastTicks);
295307
if(gameOver){
296308
if(ticks>(lastTicks+50)){ //Delay before restarting
297309
init(); //Start or Restart Game

FlappyBird/Pipe.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
public class Pipe extends Rectangle{
99
private static final long serialVersionUID = 1L;
1010
final static int WIDTH=100, HEIGHT=500, SPEED=10;
11-
Image pic;
11+
private Image pic;
1212
private boolean bottomPipe;
1313
URL url;
1414

15-
Pipe(int a, int b, boolean bottomPipe){
15+
public Pipe(int a, int b, boolean bottomPipe){
1616
this.bottomPipe=bottomPipe;
1717
x=a;
1818
y=b;

FlappyBird/SoundEffect.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import javax.sound.sampled.Clip;
32
import javax.sound.sampled.AudioSystem;
43
import javax.sound.sampled.AudioInputStream;
@@ -8,7 +7,7 @@
87
import java.io.InputStream;
98

109
public class SoundEffect{
11-
Clip clip;
10+
private Clip clip;
1211

1312
static SoundEffect flapFX = new SoundEffect("/flap.wav");
1413
static SoundEffect crashFX = new SoundEffect("/sadwah.wav");

0 commit comments

Comments
 (0)