Skip to content

Commit

Permalink
Flappy Bird - Tidy Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe O'Regan committed Nov 5, 2019
1 parent 86cc17e commit fb0a5d1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 139 deletions.
12 changes: 12 additions & 0 deletions FlappyBird/Bird.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import java.util.Random;

public class Bird extends Rectangle{
public static final int WIDTH=28;
public static final int HEIGHT=20;
Image pic;
private boolean alive;
public int yMotion;

Random random=new Random();

Bird(int a, int b, int w, int h, String s){
yMotion=0;
alive=true;
x=a;
y=b;
Expand All @@ -20,6 +24,14 @@ public class Bird extends Rectangle{
pic=Toolkit.getDefaultToolkit().getImage(s);
}

public void move(){
y+=yMotion;

if(y+yMotion>=FlappyBird.HEIGHT-FlappyBird.GROUND_HEIGHT){
y=FlappyBird.HEIGHT-FlappyBird.GROUND_HEIGHT-height;
}
}

public boolean getAlive(){
return alive;
}
Expand Down
153 changes: 65 additions & 88 deletions FlappyBird/FlappyBird.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@
import java.util.Random;

public class FlappyBird extends JPanel implements ActionListener,MouseListener,KeyListener{
final int COLUMN_SPEED=10;
final int GROUND_HEIGHT=120;
final int BIRD_WIDTH=28;
final int BIRD_HEIGHT=20;

SoundEffect flapFX = new SoundEffect("flap.wav");
SoundEffect crashFX = new SoundEffect("sadwah.wav");

public static FlappyBird flappyBird;

public final int WIDTH=1200,HEIGHT=800;
public static final int WIDTH=1200,HEIGHT=800;
public static final int GROUND_HEIGHT=120;
public final int PIPE_SPEED=10;

public Renderer renderer;

public Bird bird;

public int ticks, yMotion, score;
public int ticks, score;

public ArrayList<Pipe> columns;
public ArrayList<Pipe> pipes;

public Random rand;

public boolean gameOver, started;

boolean playCrash=true;

final String gameOverStr="Game Over!";
final String startStr="Click To Start!";
String scoreStr;

int overTextWidth, startTextWidth, scoreTextWidth;

public FlappyBird(){
started=false;
score=0;
Expand All @@ -58,13 +61,13 @@ public FlappyBird(){
jframe.setResizable(false);
jframe.setVisible(true);

bird=new Bird(WIDTH/2-BIRD_WIDTH/2,HEIGHT/2-BIRD_HEIGHT/2,BIRD_WIDTH,BIRD_HEIGHT,"flappy.png");
columns=new ArrayList<Pipe>();
bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT,"flappy.png");
pipes=new ArrayList<Pipe>();

addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
addPipe(true);
addPipe(true);
addPipe(true);
addPipe(true);

timer.start();
}
Expand All @@ -74,44 +77,40 @@ public void actionPerformed(ActionEvent e){
ticks++;

if(started) {
for(int i=0;i<columns.size();i++){
Pipe column = columns.get(i);
column.x-=COLUMN_SPEED;
for(int i=0;i<pipes.size();i++){
Pipe pipe = pipes.get(i);
pipe.move();
}

if(ticks%2==0 && yMotion<15){
yMotion+=2;
if(ticks%2==0 && bird.yMotion<15){
bird.yMotion+=2;
}

for(int i=0;i<columns.size();i++){
Pipe column = columns.get(i);

if(column.x + column.width < 0){
columns.remove(column);
for(int i=0;i<pipes.size();i++){
Pipe pipe = pipes.get(i);

//if(column.y == 0){
addColumn(false);
//}
if(pipe.x + pipe.width < 0){
pipes.remove(pipe);
addPipe(false);
}
}

bird.y+=yMotion;
bird.move();

for(Pipe column:columns){
//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){
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){
for(Pipe pipe:pipes){
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){
score++;
}
if(column.intersects(bird)){
if(pipe.intersects(bird)){
gameOver=true;

if(bird.x<=column.x){
bird.x=column.x-bird.width;
if(bird.x<=pipe.x){
bird.x=pipe.x-bird.width;
}else{
if(column.y!=0){
bird.y=column.y-bird.height;
}else if(bird.y<column.height){
bird.y=column.height;
if(pipe.y!=0){
bird.y=pipe.y-bird.height;
}else if(bird.y<pipe.height){
bird.y=pipe.height;
}
}
}
Expand All @@ -125,109 +124,87 @@ public void actionPerformed(ActionEvent e){
playCrash=false;
crashFX.play();
}

//if(gameOver){
if(bird.y+yMotion>=HEIGHT-GROUND_HEIGHT){
bird.y=HEIGHT-GROUND_HEIGHT-bird.height;
}
}

renderer.repaint();
}

public void addColumn(boolean start){
int space=300;
int width=100;
//int height=50+rand.nextInt(300);
int startY=100+rand.nextInt(300);

int distance=(rand.nextInt(7)*25)+150;


public void addPipe(boolean start){
int startY=100+rand.nextInt(300); //Y position for pipes
int distance=(rand.nextInt(7)*25)+150; //Distance between pipes (after first 2)

if(start){
//columns.add(new Pipe(WIDTH+width+columns.size() * 300, HEIGHT-height-GROUND_HEIGHT, height));
//columns.add(new Pipe(WIDTH+width+(columns.size()-1)*300, 0, HEIGHT-height-space,false));
columns.add(new Pipe(WIDTH+width+columns.size() * 300,-startY+HEIGHT-GROUND_HEIGHT));
columns.add(new Pipe(WIDTH+width+(columns.size()-1)*300,-startY,false));
pipes.add(new Pipe(WIDTH+Pipe.WIDTH+pipes.size() * 300,HEIGHT-startY-GROUND_HEIGHT,true)); //bottom
pipes.add(new Pipe(WIDTH+Pipe.WIDTH+(pipes.size()-1)*300,-startY,false)); //top
} else{
//columns.add(new Pipe(columns.get(columns.size()-1).x+600, HEIGHT-height-GROUND_HEIGHT,height));
//columns.add(new Pipe(columns.get(columns.size()-1).x,0,HEIGHT-height-space,false));
columns.add(new Pipe(columns.get(columns.size()-1).x+300+distance, -startY+HEIGHT-GROUND_HEIGHT));
columns.add(new Pipe(columns.get(columns.size()-1).x, -startY,false)); // top
pipes.add(new Pipe(pipes.get(pipes.size()-1).x+300+distance,HEIGHT-startY-GROUND_HEIGHT,true)); //bottom
pipes.add(new Pipe(pipes.get(pipes.size()-1).x,-startY,false)); // top
}
}

public void paintColumn(Graphics g, Rectangle column){
g.setColor(Color.green.darker());
g.fillRect(column.x,column.y,column.width,column.height);
}

public void jump(){
//Restart Game
if(gameOver){
bird=new Bird(WIDTH/2-BIRD_WIDTH/2,HEIGHT/2-BIRD_HEIGHT/2,BIRD_WIDTH,BIRD_HEIGHT,"flappy.png");
bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT,"flappy.png");
playCrash=true;
columns.clear();
yMotion=0;
pipes.clear();
bird.yMotion=0;
score=0;

addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
addPipe(true);
addPipe(true);
addPipe(true);
addPipe(true);

gameOver=false;
}

if(!started){
started=true;
} else if(!gameOver){
if(yMotion>0){
yMotion=0;
if(bird.yMotion>0){
bird.yMotion=0;
}

yMotion-=10;
bird.yMotion-=10;
flapFX.play();
}
}

public void repaint(Graphics g){
//Sky
g.setColor(Color.cyan);
g.fillRect(0,0,WIDTH,HEIGHT);

//g.setColor(Color.red);
//g.fillRect(bird.x,bird.y,bird.width,bird.height);
bird.draw(g, this);

for(Pipe column:columns){
//paintColumn(g,column);
column.draw(g,this);
for(Pipe pipe:pipes){
pipe.draw(g,this);
}

//Ground
g.setColor(Color.orange);
g.fillRect(0,HEIGHT-GROUND_HEIGHT,WIDTH,GROUND_HEIGHT);

g.setColor(Color.green);
g.fillRect(0,HEIGHT-GROUND_HEIGHT,WIDTH,20);

//Text
g.setColor(Color.white);
g.setFont(new Font("Arial", 1, 100));
String gameOverStr="Game Over!";
String startStr="Click To Start!";
int overTextWidth=g.getFontMetrics().stringWidth(gameOverStr);
int startTextWidth=g.getFontMetrics().stringWidth(startStr);

overTextWidth=g.getFontMetrics().stringWidth(gameOverStr);
startTextWidth=g.getFontMetrics().stringWidth(startStr);

if(!started){
g.drawString(startStr,WIDTH/2-startTextWidth/2,HEIGHT/2-50);
}

if(gameOver){
g.drawString(gameOverStr,WIDTH/2-overTextWidth/2,HEIGHT/2-50);
}

g.setFont(new Font(g.getFont().getFontName(), 1, 50));
String scoreStr="Score: "+score;
int scoreTextWidth=g.getFontMetrics().stringWidth(scoreStr);
scoreStr="Score: "+score;
scoreTextWidth=g.getFontMetrics().stringWidth(scoreStr);
if(!gameOver&&started){
g.drawString(scoreStr,WIDTH/2-scoreTextWidth/2,100);
}
Expand Down
70 changes: 19 additions & 51 deletions FlappyBird/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,39 @@
import java.util.Random;

public class Pipe extends Rectangle{
final int PIPE_WIDTH=100, PIPE_HEIGHT=500;
final static int WIDTH=100, HEIGHT=500, SPEED=10;
Image pic;
private boolean alive;
private boolean bottomPipe;

Random random=new Random();
/*
Pipe(int a, int b, int h){
alive=true;
x=a;
y=b;
width=PIPE_WIDTH;
height=PIPE_HEIGHT;
pic=Toolkit.getDefaultToolkit().getImage("pipe_bottom.png");
//pic2=Toolkit.getDefaultToolkit().getImage("pipe2.png");
}

Pipe(int a, int b, int h, boolean alive){
this.alive=alive;
x=a;
y=b;
width=PIPE_WIDTH;
height=h;
pic2=Toolkit.getDefaultToolkit().getImage("pipe_top.png");
}
*/
Pipe(int a, int b){
alive=true;
Pipe(int a, int b, boolean bottomPipe){
this.bottomPipe=bottomPipe;
x=a;
y=b;
width=PIPE_WIDTH;
height=PIPE_HEIGHT;
pic=Toolkit.getDefaultToolkit().getImage("pipe_bottom.png");
//pic2=Toolkit.getDefaultToolkit().getImage("pipe2.png");
width=WIDTH;
height=HEIGHT;

if(bottomPipe){
pic=Toolkit.getDefaultToolkit().getImage("pipe_bottom.png");
}else{
pic=Toolkit.getDefaultToolkit().getImage("pipe_top.png");
}
}

Pipe(int a, int b, boolean alive){
this.alive=alive;
x=a;
y=b;
width=PIPE_WIDTH;
height=PIPE_HEIGHT;
if(alive)
pic=Toolkit.getDefaultToolkit().getImage("pipe_bottom.png");
else
pic=Toolkit.getDefaultToolkit().getImage("pipe_top.png");
public void move(){
x-=SPEED;
}

public boolean getAlive(){
return alive;
public boolean getBottomPipe(){
return bottomPipe;
}

public void setAlive(boolean alive){
this.alive=alive;
public void setBottomPipe(boolean bottomPipe){
this.bottomPipe=bottomPipe;
}

public void draw(Graphics g, Component c){
//if(alive){
g.drawImage(pic,x,y,width,height,c);
//}
/*
else{
//g.drawImage(pic,x, height, width, y, x, y, width, height, c);
g.drawImage(pic2,x,y,width,height,c);
}
*/

g.drawImage(pic,x,y,width,height,c);
}
}

0 comments on commit fb0a5d1

Please sign in to comment.