Skip to content

Commit

Permalink
Flappy Birds - Tidy Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe O'Regan committed Nov 7, 2019
1 parent 0eb3e8f commit 657a872
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
6 changes: 3 additions & 3 deletions FlappyBird/Bird.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class Bird extends Rectangle{
private static final long serialVersionUID = 1L;
public static final int WIDTH=28;
public static final int HEIGHT=20;
Image pic;
public int yMotion;
private Image pic;
private int yMotion;

Bird(int a, int b){
public Bird(int a, int b){
yMotion=0;
x=a;
y=b;
Expand Down
4 changes: 2 additions & 2 deletions FlappyBird/Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class Cloud extends Rectangle{
Random random=new Random();
URL url;

int speed;
private int speed;

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

x=random.nextInt(12) * 100;
Expand Down
80 changes: 46 additions & 34 deletions FlappyBird/FlappyBird.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,35 @@
import java.util.ArrayList;
import java.util.Random;

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

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

public static FlappyBird flappyBird;

static int highScoreEasy = 0;
static int highScoreMedium = 0;
static int highScoreHard = 0;

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

public Renderer renderer;
static int highScoreEasy = 0, highScoreMedium = 0, highScoreHard = 0;

public Bird bird;
Renderer renderer;

public String gameOverStr, startStr, scoreStr, highScoreStr;
Bird bird;

public int ticks, lastTicks, score, overTextWidth, startTextWidth, scoreTextWidth, highScoreTextWidth, difficulty=1;
String gameOverStr, startStr, scoreStr, highScoreStr;

public ArrayList<Pipe> pipes;
int ticks, lastTicks, score, overTextWidth, startTextWidth, scoreTextWidth, highScoreTextWidth, difficulty=1;

public Random rand;
ArrayList<Pipe> pipes;

public boolean gameOver, started;
Random rand;

boolean playCrash=true;
boolean gameOver, started, playCrash;

public FlappyBird(){
started=false;
JFrame jframe = new JFrame("Flappy Bird");
JFrame jframe = new JFrame("Flappy Bird (Joe O'Regan)");
Timer timer = new Timer(20, this);

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

started = false;

pipes = new ArrayList<Pipe>();

init();
Expand All @@ -77,20 +74,35 @@ public void init() {
}

public void startPipes(){
ticks=0;
lastTicks=0;
score = 0;

pipes.clear();

addPipe(difficulty,true);
addPipe(difficulty,true);
addPipe(difficulty,true);
addPipe(difficulty,true);
addPipe(difficulty, true);
addPipe(difficulty, true);
addPipe(difficulty, true);
addPipe(difficulty, true);
}

@Override
public void actionPerformed(ActionEvent e){
ticks++;

cloud1.move();
cloud2.move();
cloud3.move();

if(!started) {
if((ticks%33==0||ticks<=1)) {
bird.jump();
} else if (ticks % 3 == 0) {
bird.fall();
}
bird.move();
}

if(started) {
for(int i=0;i<pipes.size();i++){
Pipe pipe = pipes.get(i);
Expand All @@ -110,9 +122,6 @@ public void actionPerformed(ActionEvent e){
}
}

cloud1.move();
cloud2.move();
cloud3.move();
bird.move();

for(Pipe pipe:pipes){
Expand Down Expand Up @@ -143,10 +152,10 @@ public void actionPerformed(ActionEvent e){
playCrash=false;
SoundEffect.crashFX.play();
}
}

if(gameOver && lastTicks==0){
lastTicks=ticks;
if(gameOver && lastTicks==0){
lastTicks=ticks;
}
}

renderer.repaint();
Expand Down Expand Up @@ -182,8 +191,6 @@ public void repaint(Graphics g){
cloud2.draw(g,this);
cloud3.draw(g,this);

bird.draw(g, this);

for(Pipe pipe:pipes){
pipe.draw(g,this);
}
Expand All @@ -201,11 +208,17 @@ public void repaint(Graphics g){
highScoreStr = "HighScores: ";

if (!started) {
drawText(g, startStr, 0.5, 0.5, 100,Color.white);
drawText(g, startStr, 0.5, 0.5, 100, Color.white);
}

if (!started || gameOver) {
drawText(g, "Difficulty: 1. Easy 2. Medium 3. Hard", 0.9, 0.933, 50, Color.white);
}

bird.draw(g, this);

if (gameOver) {
drawText(g, gameOverStr, 0.5, 0.25,100, Color.black);
drawText(g, gameOverStr, 0.5, 0.25, 100, Color.black);
drawText(g, scoreStr, 0.5, 0.5, 50, Color.black);
setHighScores();
drawText(g, highScoreStr, 0.5, 0.75, 50, Color.black);
Expand Down Expand Up @@ -291,7 +304,6 @@ public void keyReleased(KeyEvent e){
}

public void actionButton(){
// System.out.println("ticks: "+ticks+" lastTicks: "+lastTicks);
if(gameOver){
if(ticks>(lastTicks+50)){ //Delay before restarting
init(); //Start or Restart Game
Expand Down
4 changes: 2 additions & 2 deletions FlappyBird/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
public class Pipe extends Rectangle{
private static final long serialVersionUID = 1L;
final static int WIDTH=100, HEIGHT=500, SPEED=10;
Image pic;
private Image pic;
private boolean bottomPipe;
URL url;

Pipe(int a, int b, boolean bottomPipe){
public Pipe(int a, int b, boolean bottomPipe){
this.bottomPipe=bottomPipe;
x=a;
y=b;
Expand Down
3 changes: 1 addition & 2 deletions FlappyBird/SoundEffect.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
Expand All @@ -8,7 +7,7 @@
import java.io.InputStream;

public class SoundEffect{
Clip clip;
private Clip clip;

static SoundEffect flapFX = new SoundEffect("/flap.wav");
static SoundEffect crashFX = new SoundEffect("/sadwah.wav");
Expand Down

0 comments on commit 657a872

Please sign in to comment.