1515import java .util .Random ;
1616
1717public class FlappyBird extends JPanel implements ActionListener ,MouseListener ,KeyListener {
18- final int COLUMN_SPEED =10 ;
19- final int GROUND_HEIGHT =120 ;
20- final int BIRD_WIDTH =28 ;
21- final int BIRD_HEIGHT =20 ;
22-
2318 SoundEffect flapFX = new SoundEffect ("flap.wav" );
2419 SoundEffect crashFX = new SoundEffect ("sadwah.wav" );
2520
2621 public static FlappyBird flappyBird ;
2722
28- public final int WIDTH =1200 ,HEIGHT =800 ;
23+ public static final int WIDTH =1200 ,HEIGHT =800 ;
24+ public static final int GROUND_HEIGHT =120 ;
25+ public final int PIPE_SPEED =10 ;
2926
3027 public Renderer renderer ;
3128
3229 public Bird bird ;
3330
34- public int ticks , yMotion , score ;
31+ public int ticks , score ;
3532
36- public ArrayList <Pipe > columns ;
33+ public ArrayList <Pipe > pipes ;
3734
3835 public Random rand ;
3936
4037 public boolean gameOver , started ;
4138
4239 boolean playCrash =true ;
4340
41+ final String gameOverStr ="Game Over!" ;
42+ final String startStr ="Click To Start!" ;
43+ String scoreStr ;
44+
45+ int overTextWidth , startTextWidth , scoreTextWidth ;
46+
4447 public FlappyBird (){
4548 started =false ;
4649 score =0 ;
@@ -58,13 +61,13 @@ public FlappyBird(){
5861 jframe .setResizable (false );
5962 jframe .setVisible (true );
6063
61- bird =new Bird (WIDTH /2 -BIRD_WIDTH /2 ,HEIGHT /2 -BIRD_HEIGHT /2 ,BIRD_WIDTH , BIRD_HEIGHT ,"flappy.png" );
62- columns =new ArrayList <Pipe >();
64+ bird =new Bird (WIDTH /2 -Bird . WIDTH /2 ,HEIGHT /2 -Bird . HEIGHT /2 ,Bird . WIDTH , Bird . HEIGHT ,"flappy.png" );
65+ pipes =new ArrayList <Pipe >();
6366
64- addColumn (true );
65- addColumn (true );
66- addColumn (true );
67- addColumn (true );
67+ addPipe (true );
68+ addPipe (true );
69+ addPipe (true );
70+ addPipe (true );
6871
6972 timer .start ();
7073 }
@@ -74,44 +77,40 @@ public void actionPerformed(ActionEvent e){
7477 ticks ++;
7578
7679 if (started ) {
77- for (int i =0 ;i <columns .size ();i ++){
78- Pipe column = columns .get (i );
79- column . x -= COLUMN_SPEED ;
80+ for (int i =0 ;i <pipes .size ();i ++){
81+ Pipe pipe = pipes .get (i );
82+ pipe . move () ;
8083 }
8184
82- if (ticks %2 ==0 && yMotion <15 ){
83- yMotion +=2 ;
85+ if (ticks %2 ==0 && bird . yMotion <15 ){
86+ bird . yMotion +=2 ;
8487 }
8588
86- for (int i =0 ;i <columns .size ();i ++){
87- Pipe column = columns .get (i );
88-
89- if (column .x + column .width < 0 ){
90- columns .remove (column );
89+ for (int i =0 ;i <pipes .size ();i ++){
90+ Pipe pipe = pipes .get (i );
9191
92- // if(column.y == 0){
93- addColumn ( false );
94- //}
92+ if (pipe . x + pipe . width < 0 ){
93+ pipes . remove ( pipe );
94+ addPipe ( false );
9595 }
9696 }
9797
98- bird .y += yMotion ;
98+ bird .move () ;
9999
100- for (Pipe column :columns ){
101- //if(column.y==0 && bird.x+bird.width/2>column.x+column.width/2-10 && bird.x+bird.width/2<column.x+column.width/2+10){
102- if (column .getAlive () && bird .x +bird .width /2 >column .x +column .width /2 -10 && bird .x +bird .width /2 <column .x +column .width /2 +10 ){
100+ for (Pipe pipe :pipes ){
101+ if (pipe .getBottomPipe () && bird .x +bird .width /2 >pipe .x +pipe .width /2 -10 && bird .x +bird .width /2 <pipe .x +pipe .width /2 +10 ){
103102 score ++;
104103 }
105- if (column .intersects (bird )){
104+ if (pipe .intersects (bird )){
106105 gameOver =true ;
107106
108- if (bird .x <=column .x ){
109- bird .x =column .x -bird .width ;
107+ if (bird .x <=pipe .x ){
108+ bird .x =pipe .x -bird .width ;
110109 }else {
111- if (column .y !=0 ){
112- bird .y =column .y -bird .height ;
113- }else if (bird .y <column .height ){
114- bird .y =column .height ;
110+ if (pipe .y !=0 ){
111+ bird .y =pipe .y -bird .height ;
112+ }else if (bird .y <pipe .height ){
113+ bird .y =pipe .height ;
115114 }
116115 }
117116 }
@@ -125,109 +124,87 @@ public void actionPerformed(ActionEvent e){
125124 playCrash =false ;
126125 crashFX .play ();
127126 }
128-
129- //if(gameOver){
130- if (bird .y +yMotion >=HEIGHT -GROUND_HEIGHT ){
131- bird .y =HEIGHT -GROUND_HEIGHT -bird .height ;
132- }
133127 }
134128
135129 renderer .repaint ();
136130 }
137131
138- public void addColumn (boolean start ){
139- int space =300 ;
140- int width =100 ;
141- //int height=50+rand.nextInt(300);
142- int startY =100 +rand .nextInt (300 );
143-
144- int distance =(rand .nextInt (7 )*25 )+150 ;
145-
146-
132+ public void addPipe (boolean start ){
133+ int startY =100 +rand .nextInt (300 ); //Y position for pipes
134+ int distance =(rand .nextInt (7 )*25 )+150 ; //Distance between pipes (after first 2)
147135
148136 if (start ){
149- //columns.add(new Pipe(WIDTH+width+columns.size() * 300, HEIGHT-height-GROUND_HEIGHT, height));
150- //columns.add(new Pipe(WIDTH+width+(columns.size()-1)*300, 0, HEIGHT-height-space,false));
151- columns .add (new Pipe (WIDTH +width +columns .size () * 300 ,-startY +HEIGHT -GROUND_HEIGHT ));
152- columns .add (new Pipe (WIDTH +width +(columns .size ()-1 )*300 ,-startY ,false ));
137+ pipes .add (new Pipe (WIDTH +Pipe .WIDTH +pipes .size () * 300 ,HEIGHT -startY -GROUND_HEIGHT ,true )); //bottom
138+ pipes .add (new Pipe (WIDTH +Pipe .WIDTH +(pipes .size ()-1 )*300 ,-startY ,false )); //top
153139 } else {
154- //columns.add(new Pipe(columns.get(columns.size()-1).x+600, HEIGHT-height-GROUND_HEIGHT,height));
155- //columns.add(new Pipe(columns.get(columns.size()-1).x,0,HEIGHT-height-space,false));
156- columns .add (new Pipe (columns .get (columns .size ()-1 ).x +300 +distance , -startY +HEIGHT -GROUND_HEIGHT ));
157- columns .add (new Pipe (columns .get (columns .size ()-1 ).x , -startY ,false )); // top
140+ pipes .add (new Pipe (pipes .get (pipes .size ()-1 ).x +300 +distance ,HEIGHT -startY -GROUND_HEIGHT ,true )); //bottom
141+ pipes .add (new Pipe (pipes .get (pipes .size ()-1 ).x ,-startY ,false )); // top
158142 }
159143 }
160144
161- public void paintColumn (Graphics g , Rectangle column ){
162- g .setColor (Color .green .darker ());
163- g .fillRect (column .x ,column .y ,column .width ,column .height );
164- }
165-
166145 public void jump (){
146+ //Restart Game
167147 if (gameOver ){
168- bird =new Bird (WIDTH /2 -BIRD_WIDTH /2 ,HEIGHT /2 -BIRD_HEIGHT /2 ,BIRD_WIDTH , BIRD_HEIGHT ,"flappy.png" );
148+ bird =new Bird (WIDTH /2 -Bird . WIDTH /2 ,HEIGHT /2 -Bird . HEIGHT /2 ,Bird . WIDTH , Bird . HEIGHT ,"flappy.png" );
169149 playCrash =true ;
170- columns .clear ();
171- yMotion =0 ;
150+ pipes .clear ();
151+ bird . yMotion =0 ;
172152 score =0 ;
173153
174- addColumn (true );
175- addColumn (true );
176- addColumn (true );
177- addColumn (true );
154+ addPipe (true );
155+ addPipe (true );
156+ addPipe (true );
157+ addPipe (true );
178158
179159 gameOver =false ;
180160 }
181161
182162 if (!started ){
183163 started =true ;
184164 } else if (!gameOver ){
185- if (yMotion >0 ){
186- yMotion =0 ;
165+ if (bird . yMotion >0 ){
166+ bird . yMotion =0 ;
187167 }
188168
189- yMotion -=10 ;
169+ bird . yMotion -=10 ;
190170 flapFX .play ();
191171 }
192172 }
193173
194174 public void repaint (Graphics g ){
175+ //Sky
195176 g .setColor (Color .cyan );
196177 g .fillRect (0 ,0 ,WIDTH ,HEIGHT );
197178
198- //g.setColor(Color.red);
199- //g.fillRect(bird.x,bird.y,bird.width,bird.height);
200179 bird .draw (g , this );
201180
202- for (Pipe column :columns ){
203- //paintColumn(g,column);
204- column .draw (g ,this );
181+ for (Pipe pipe :pipes ){
182+ pipe .draw (g ,this );
205183 }
206184
185+ //Ground
207186 g .setColor (Color .orange );
208187 g .fillRect (0 ,HEIGHT -GROUND_HEIGHT ,WIDTH ,GROUND_HEIGHT );
209-
210188 g .setColor (Color .green );
211189 g .fillRect (0 ,HEIGHT -GROUND_HEIGHT ,WIDTH ,20 );
212190
191+ //Text
213192 g .setColor (Color .white );
214193 g .setFont (new Font ("Arial" , 1 , 100 ));
215- String gameOverStr ="Game Over!" ;
216- String startStr ="Click To Start!" ;
217- int overTextWidth =g .getFontMetrics ().stringWidth (gameOverStr );
218- int startTextWidth =g .getFontMetrics ().stringWidth (startStr );
194+
195+ overTextWidth =g .getFontMetrics ().stringWidth (gameOverStr );
196+ startTextWidth =g .getFontMetrics ().stringWidth (startStr );
219197
220198 if (!started ){
221199 g .drawString (startStr ,WIDTH /2 -startTextWidth /2 ,HEIGHT /2 -50 );
222200 }
223-
224201 if (gameOver ){
225202 g .drawString (gameOverStr ,WIDTH /2 -overTextWidth /2 ,HEIGHT /2 -50 );
226203 }
227204
228205 g .setFont (new Font (g .getFont ().getFontName (), 1 , 50 ));
229- String scoreStr ="Score: " +score ;
230- int scoreTextWidth =g .getFontMetrics ().stringWidth (scoreStr );
206+ scoreStr ="Score: " +score ;
207+ scoreTextWidth =g .getFontMetrics ().stringWidth (scoreStr );
231208 if (!gameOver &&started ){
232209 g .drawString (scoreStr ,WIDTH /2 -scoreTextWidth /2 ,100 );
233210 }
0 commit comments