1313import java .util .ArrayList ;
1414import 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
0 commit comments