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

2016/5/2

Balloon-Popping Game

1
2
3
IronRod IronRod

2016/5/2

#
Scenario: There is a man in the middle. There are different coloured balloons moving towards him from all four directions. He has to pop the balloons before they reach him. He can only pop the balloon if the colour of his head corresponds with the colour of the balloon. Problem: I am having problems with the boolean variables that compare the colour of the man's head and balloon. Here is the code (this is in the code of the bullet that hits the balloon):
public static boolean redBalloon=true;
public static boolean blueBalloon=false;
public static boolean yellowBalloon=false;
public static boolean redHead=true;
public static boolean blueHead=false;
public static boolean yellowHead=false;
public void act()
{
hitAndMiss();
}
public void hitAndMiss()
    {           
        getBalloonColour();
        getHeadColour();
        pop();           
    }
    public void pop()
    {
        Actor balloon = getOneIntersectingObject(Balloon.class);
        if (balloon != null)
        {
            if (redHead==true && redBalloon==true||blueHead==true && blueBalloon==true||yellowHead==true && yellowBalloon==true)
         {
             getWorld().removeObject(balloon);           
         }
         else
         {
             getWorld().removeObject(this);            
         }
        }
    }
    public void getBalloonColour()
    {
        Actor balloon = getOneIntersectingObject(Balloon.class);
        if (balloon != null)
        {
            if (balloon.getImage().equals("balloon1.png"))
        {
            redBalloon=true;
            blueBalloon=false;
            yellowBalloon=false;
        }
        if (balloon.getImage().equals("balloon2.png"))
        {
            redBalloon=false;
            blueBalloon=true;
            yellowBalloon=false;
        }
        if (balloon.getImage().equals("balloon3.png"))
        {
            redBalloon=false;
            blueBalloon=false;
            yellowBalloon=true;
        } 
        }
    }
    public void getHeadColour()
    {
        Head head = (Head) getWorld().getObjects(Head.class).get(0);
        if (head.getImage().equals("button-red.png"))
        {
            redHead=true;
            blueHead=false;
            yellowHead=false;
        }
        if (head.getImage().equals("button-blue.png"))
        {
            redHead=false;
            blueHead=true;
            yellowHead=false;
        }
        if (head.getImage().equals("button-yellow.png"))
        {
            redHead=false;
            blueHead=false;
            yellowHead=true;
        }  
    }
}    
Please help!
danpost danpost

2016/5/2

#
It would be best to remove all the static fields and add an instance field for the color and a method to return its value. That way, the color of any balloon can be ascertained by calling the method instead of trying to keep all the static fields up to date. The addition of the static fields makes your job of programming the game a lot harder than it needs to be because you constantly have to update them and the value of all the fields would need to be checked on. Color is a state that each actor (balloon and man) has. If you ever have more than one balloon in the world, the field would only refer to one of them; the problem is, you do not know which one. A good use of static fields (or class fields) might be to create an array of the different color images for the classes (man and balloon). If the images were arranged in the array in the same order with respect to the color, then and index value can be used between the classes for comparison to see if they match in color. So, instead of an instance color field, you would have an instance int field and a method to return the index for the color of the specific object. For the Balloon class, for example
import greenfoot.*;

public class Balloon extends Actor
{
    private static final GreenfootImage[] images =
    {
        new GreenfootImage("balloon1.png"),
        new GreenfootImage("balloon2.png"),
        new GreenfootImage("balloon3.png")
    };

    private int imageIndex;

    public int getColorInt()
    {
        return imageIndex;
    }

    public void setColor(int index)
    {
        imageIndex = index;
        setimage(index]);
    }

    public Balloon()
    {
        setColor(Greenfoot.getRandomNumber(images.length));
    }

    // etc.
GB309 GB309

2016/5/2

#
have you used 'java.awt.Color' as an import Mr.IronRod
IronRod IronRod

2016/5/2

#
Thank you for your suggestion @danpost, I have implemented it into both the balloon class and man class. Can you please, however, give me advice on how to go about referencing this in the code of the bullet: When the bullet hits the balloon I want the balloon to pop if the colour of the balloon that is being hit and the man are the same. If they are not the same the balloon doesn't pop.
danpost danpost

2016/5/2

#
The bullets are produced by the man; so the man can pass the color index to the bullet when the man creates it. Then when a bullet hits a balloon, you can get its color index as follows:
Balloon balloon = (Balloon)getOneIntersectingObject(Balloon.class);
if (balloon != null)
{
    int balloonColorIndex = balloon.getColorInt();
    // etc.
IronRod IronRod

2016/5/2

#
Line 22 of the code earlier is returning an error message. Why is this?
danpost danpost

2016/5/2

#
IronRod wrote...
Line 22 of the code earlier is returning an error message. Why is this?
I am not sure what happened (must have happened during editing). Line 22 should be:
setImage(images[index]);
IronRod IronRod

2016/5/2

#
Thank you, it works now.
IronRod IronRod

2016/5/2

#
Also, there is an error when greenfoot tries to recognise
balloon.getColorInt(); 
In line 4.
danpost danpost

2016/5/2

#
IronRod wrote...
Also, there is an error when greenfoot tries to recognise
balloon.getColorInt(); 
In line 4.
I had edited the code referring to this above. Check it out.
IronRod IronRod

2016/5/2

#
I don't see the new edition. Can you explain what I need to do?
IronRod IronRod

2016/5/2

#
Actually never mind, it was to do with my spelling of color/colour.
IronRod IronRod

2016/5/2

#
Also can you please help me with this section of code to check if the head colour is the same as the balloon. I get a NullPointerException:
Balloon balloon = (Balloon)getOneIntersectingObject(Balloon.class);
        Head head = (Head)getWorld().getObjects(Head.class).get(0);
        int balloonColour = balloon.getColourInt();
        int headColour = head.getColourInt();
        if (balloon!=null)
        {
            if(balloonColour == headColour)
            {
                getWorld().removeObject(this);
                getWorld().removeObject(balloon);
            }
            else
            {
                getWorld().removeObject(this);
            }
        }
Note that the bullet class, head class and man class are separate.
danpost danpost

2016/5/2

#
I presume the given code in your last post is in your bullet class. Add an instance int field in it and a 'public void setColorInt(int index)' method to set its value. Then in the class of the man, a bullet can be created like this:
Bullet bullet = new Bullet();
bullet.setColorInt(colorIndex);
This way, the bullet will retain the color of the man when a balloon is reached and will not have to find the man to get the value.
IronRod IronRod

2016/5/2

#
By the way, its the colour of the head that's changing, not the man.
There are more replies on the next page.
1
2
3