From 657a87215fdb8ca3294d7844a945b540bc3faf89 Mon Sep 17 00:00:00 2001 From: Joe O'Regan Date: Thu, 7 Nov 2019 02:53:56 +0000 Subject: [PATCH] Flappy Birds - Tidy Code --- FlappyBird/Bird.java | 6 +-- FlappyBird/Cloud.java | 4 +- FlappyBird/FlappyBird.java | 80 +++++++++++++++++++++---------------- FlappyBird/Pipe.java | 4 +- FlappyBird/SoundEffect.java | 3 +- 5 files changed, 54 insertions(+), 43 deletions(-) diff --git a/FlappyBird/Bird.java b/FlappyBird/Bird.java index 1ab433f..4443249 100644 --- a/FlappyBird/Bird.java +++ b/FlappyBird/Bird.java @@ -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; diff --git a/FlappyBird/Cloud.java b/FlappyBird/Cloud.java index 351bc1e..909dc2d 100644 --- a/FlappyBird/Cloud.java +++ b/FlappyBird/Cloud.java @@ -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; diff --git a/FlappyBird/FlappyBird.java b/FlappyBird/FlappyBird.java index 59164cd..9fa8f96 100644 --- a/FlappyBird/FlappyBird.java +++ b/FlappyBird/FlappyBird.java @@ -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 pipes; + int ticks, lastTicks, score, overTextWidth, startTextWidth, scoreTextWidth, highScoreTextWidth, difficulty=1; - public Random rand; + ArrayList 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(); @@ -60,6 +55,8 @@ public FlappyBird(){ jframe.setResizable(false); jframe.setVisible(true); + started = false; + pipes = new ArrayList(); init(); @@ -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(lastTicks+50)){ //Delay before restarting init(); //Start or Restart Game diff --git a/FlappyBird/Pipe.java b/FlappyBird/Pipe.java index aa5f8f6..9f72c62 100644 --- a/FlappyBird/Pipe.java +++ b/FlappyBird/Pipe.java @@ -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; diff --git a/FlappyBird/SoundEffect.java b/FlappyBird/SoundEffect.java index 467ad58..82cb993 100644 --- a/FlappyBird/SoundEffect.java +++ b/FlappyBird/SoundEffect.java @@ -1,4 +1,3 @@ - import javax.sound.sampled.Clip; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioInputStream; @@ -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");