diff --git a/FlappyBird/Bird.java b/FlappyBird/Bird.java index 6bb3128..f488c12 100644 --- a/FlappyBird/Bird.java +++ b/FlappyBird/Bird.java @@ -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; @@ -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; } diff --git a/FlappyBird/FlappyBird.java b/FlappyBird/FlappyBird.java index e57d779..a78eee9 100644 --- a/FlappyBird/FlappyBird.java +++ b/FlappyBird/FlappyBird.java @@ -15,25 +15,22 @@ 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 columns; + public ArrayList pipes; public Random rand; @@ -41,6 +38,12 @@ public class FlappyBird extends JPanel implements ActionListener,MouseListener,K 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; @@ -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(); + bird=new Bird(WIDTH/2-Bird.WIDTH/2,HEIGHT/2-Bird.HEIGHT/2,Bird.WIDTH,Bird.HEIGHT,"flappy.png"); + pipes=new ArrayList(); - addColumn(true); - addColumn(true); - addColumn(true); - addColumn(true); + addPipe(true); + addPipe(true); + addPipe(true); + addPipe(true); timer.start(); } @@ -74,44 +77,40 @@ public void actionPerformed(ActionEvent e){ ticks++; if(started) { - for(int i=0;icolumn.x+column.width/2-10 && bird.x+bird.width/2column.x+column.width/2-10 && bird.x+bird.width/2pipe.x+pipe.width/2-10 && bird.x+bird.width/2=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; } @@ -182,52 +162,49 @@ public void jump(){ 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); } diff --git a/FlappyBird/Pipe.java b/FlappyBird/Pipe.java index 8f04dea..0d3a65c 100644 --- a/FlappyBird/Pipe.java +++ b/FlappyBird/Pipe.java @@ -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); } } \ No newline at end of file