Hi,
So I'm creating flappybird in greenfoot for a class project. If you haven't played it the objective is to get the bird through the pipes without touching them, or you lose. My problem is when the bird goes through a pair of pipes it adds two to the score. I only want it to add one when it goes through a pair of pipes. The reason it adds two is because I made it add a point every time a single pipe touches the side of the world. Is there any way I can get it to add one point for a pair of pipes? Thanks! My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | //Pipes import greenfoot.*; import java.util.List; import java.awt.Color; public class Pipes extends Actor { public Pipes() { this (Math.random() < 0 .5D, ( int )(Math.random() * ( double )diff + 50D)); } public Pipes( boolean up) { this (up, ( int )(Math.random() * ( double )diff + 50D)); } public Pipes( boolean up, int height) { GreenfootImage total = new GreenfootImage( 20 , height); total.setColor(Color.GREEN); total.fillRect( 0 , 0 , 90 , height); setImage(total); } public void act() { setLocation(getX() - 5 , getY()); if (getX() == 0 ) { ((Score)(Score)getWorld().getObjects(Score. class ).get( 0 )).upScore(); getWorld().removeObject( this ); } } public static final int minHeight = 50 ; public static final int maxHeight = 350 ; public static int diff = 300 ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | //Flap import greenfoot.*; import java.util.List; public class Flap extends Actor { public Flap() { jump = new GreenfootImage( "flappybird1.png" ); speed = 0 .0D; set = getImage(); } public void act() { speed = speed - gravity; if (speed > 0 .0D) setImage(set); setLocation(getX(), ( int )(( double )getY() + speed)); if (Greenfoot.isKeyDown( "space" )) { speed = -5D; setImage(jump); } if (getY() == getWorld().getHeight() - 1 ) { getWorld().addObject( new done(), getX(), getY()); Greenfoot.delay( 35 ); ((w)getWorld()).clear(); return ; } if (getIntersectingObjects(Pipes. class ).size() > 0 ) { getWorld().addObject( new done(), getX(), getY()); Greenfoot.delay( 35 ); ((w)getWorld()).clear(); return ; } else { return ; } } public static double gravity = - 0 .5D; GreenfootImage jump; GreenfootImage set; private double speed; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | //Score import greenfoot.Actor; import greenfoot.GreenfootImage; import java.awt.Color; import java.awt.Font; public class Score extends Actor { public Score() { score = 0 ; GreenfootImage i = new GreenfootImage( 200 , 60 ); i.setColor(Color.WHITE); i.setFont( new Font( "Arial" , 0 , 75 )); i.drawString(( new StringBuilder()).append( " " ).append(score).toString(), 2 , 55 ); setImage(i); } public void upScore() { score++; GreenfootImage i = new GreenfootImage( 200 , 60 ); i.setColor(Color.WHITE); i.setFont( new Font( "Arial" , 0 , 75 )); i.drawString(( new StringBuilder()).append( " " ).append(score).toString(), 2 , 55 ); setImage(i); } public int getScore() { return score; } public void act() { } private int score; } |