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

2021/11/30

game won

Tlax Tlax

2021/11/30

#
Hello, i have a game that moves 15x'balk' in vertical way. I have all the code for the game and it all works. So if you press mouse on a balk, the balk stops moving, and if you pres it near the middle of the y-axis, it automaticly sets location to exact the middle of the y-axis. My goal is to have all the 'balk' in the middle of the y-axis, so if you have accomplished this the game should show a message like: You won. this is the code I have in the Balk (actor class): if (Greenfoot. mouseClicked(this)) { if (getY()== getWorld().getHeight()/2) { } }
Spock47 Spock47

2021/11/30

#
If I understand correctly, you have won if ALL balks are in the middle of the y-axis. 1. As preparation, you can add the check for ONE balk into the Balk class:
public class Balk extends Actor
{
    ...

    boolean isInMiddle() 
    {
        return getY() == getWorld().getHeight() / 2;
    }
     
}
2. Now you want to check that this is true for every balk. That can be checked best in a position where all balks are known. Your current world is the object that manages all visible balks, so it makes sense to add the check there:
public class MyWorld extends World
{
    ....
    
    private boolean hasWon()
    {
        for (final Balk balk : getObjects(Balk.class)) 
        {
            if (!balk.isInMiddle()) 
            {
                return false;
            }
        }
        return  true;
    }
    
}
3. Finally, you can call this method from wherever you want, e.g. in the act method of the world:
public class MyWorld extends World
{
    ....
    public void act() 
    {
        if (hasWon())
        {
            // celebrate!
            showText("You won!", 100, 100);
        }
        ...
    }

}
Live long and prosper, Spock47
Tlax Tlax

2021/12/1

#
hello, first of all thx for the help. The problem I have now is that the message: 'you won' is shown from the beginning.
Spock47 Spock47

2021/12/1

#
The hasWon method as given, will be returning true especially in the following cases: 1. If no balk exists at all. So, if there is no balk in the world, and the hasWon method is called, it will return true. So, depending on when you add the balks to the world, this can be your problem. You could either make sure that the method is only called after the balks has been added to the world (e.g. it should be ok, if the balks are added in the prepare method called from the constructor and the hasWon method is called from the act.) or add a check to the hasWon method that there have to exist balks prior to a win:
    
    private boolean hasWon()
    {
        final List<Balk> balks = getObjects(Balk.class);
        if (balks.isEmpty()) 
        {   // no balk, no win!
            return false;
        }
        for (final Mouse balk : balks) 
        {
            if (!balk.isInMiddle()) 
            {
                return false;
            }
        }
        return  true;
    }
2. Other than that, it could be possible that the balks are already in the middle position when added to the world. Then the won-message would also show immediately at the start. Here, you just have to make sure that the starting positions of the balks are not in the middle. Live long and prosper, Spock47
You need to login to post a reply.