This site requires JavaScript, please enable it in your browser!
Greenfoot back
jacobgalvan23
jacobgalvan23 wrote ...

2014/5/8

Flappy Bird Help!

jacobgalvan23 jacobgalvan23

2014/5/8

#
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;
     
   
}
Unicus Unicus

2014/5/8

#
You could make two actors for each pipe, but only one of them adds +1, and the other one doesn't. Pipe1 actor will contain this
1
2
3
4
5
if(getX() == 0
        
            ((Score)(Score)getWorld().getObjects(Score.class).get(0)).upScore(); 
            getWorld().removeObject(this); 
        }
and Pipe2 will not. It's not the best way to do it, but with the code you have here, it will work this way. Another way would be to transform the score number from int to double, and increment it by 0.5 The way I would do it, however, would be to add an invisible actor between the pipes, with the right width and height, and remove it as soon as your bird touches it. At the same time, increment the score by 1. This way, even if your bird dies by the time the pipes touches the edge, you will still get your point.
danpost danpost

2014/5/8

#
Or, just change 'score' in lines 27 and 33 in the Score class to 'score/2' (and line 16, if you want).
jacobgalvan23 jacobgalvan23

2014/5/8

#
I just ended up dividing it by two like danpost said, it worked perfect. Thanks a lot for both of your guys help!
You need to login to post a reply.