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

2016/5/24

stopGame problem

smcgee smcgee

2016/5/24

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Forest extends World
{
    public int score;
    private int time;

    public Forest() 
    {
        super(1500, 800, 1);
        prepare();
        score = 6;
        showScore();
        time = 3000; 
        showTime(); 
    }

    public void act()
    {
        countTime();

        if (score == 0)
        {
            Greenfoot.stop();
        } 
    }

    public void removeScore(int points)
    {
        score = score--;
        showScore();
    }

    public void showScore()
    {
        showText("Score: " + score, 80, 25);
    }

    private void prepare()
    {
        Apple apple = new Apple(); 
        addObject(apple, 750, 500);
        Banana banana = new Banana();
        addObject(banana, 750, 500);
        Wombat wombat = new Wombat();
        addObject(wombat, 1499, (Greenfoot.getRandomNumber(500)));
        Wombat wombat2 = new Wombat();
        addObject(wombat2, Greenfoot.getRandomNumber(1500), 450);
        Wombat wombat3 = new Wombat();
        addObject(wombat3, Greenfoot.getRandomNumber(1500), 150);
        Wombat wombat4 = new Wombat();
        addObject(wombat4, Greenfoot.getRandomNumber(1500), 300);
        Wombat wombat5 = new Wombat();
        addObject(wombat5, Greenfoot.getRandomNumber(1500), 50);
        Wombat wombat6 = new Wombat();
        addObject(wombat6, Greenfoot.getRandomNumber(1500), 150);
        banana.setLocation(422,403);
    }

    private void showTime()
    {
        showText("Time: " + time, 80, 50);
    }

    private void countTime()
    {
        time--;
        showTime();
        if (time == 3000)
        {
            Greenfoot.stop();
        }
    }
  
    private void showEndMessage()
    {
        showText("You have won!", 750, 400);
    }
}
smcgee wrote...
The problem with this program is two fold. The apple class removes the wombat class. But each time a wombat is removed I'd like the total score to be reduced by 1. Once the total score is less than 1 the game should stop. But since the score remains at 6 the game goes on forever.
danpost danpost

2016/5/24

#
smcgee wrote...
The problem with this program is two fold. The apple class removes the wombat class. But each time a wombat is removed I'd like the total score to be reduced by 1. Once the total score is less than 1 the game should stop. But since the score remains at 6 the game goes on forever.
Actually, both issues are due to only one problem -- that of making the value of 'score' decrease. The code needed for that would be in the Apple class. Also, I do not know if you are wanting to keep or use the 'countTime' method or not; but, if you do decide to use it, it will not work with the current code. The initial value of 'time' is 3000; so, if the method is called and it value is decreased it will no longer, on this call to the method or any other, be 3000 and the game will not stop there either.
smcgee smcgee

2016/5/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
 
public class Apple extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int delta = -2;
    public int score;

    public void act()
    {
        checkKeypress();
        checkEdge();
        removeWombat();

    }

    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX()-5, getY());
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            setLocation(getX()+5, getY());
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
        }
    }

    public void removeWombat()
    {

        if ( isTouching(Wombat.class) ) 
        {
            World myWorld = getWorld();
            removeTouching(Wombat.class); 
            World world;
            world = getWorld();
            score = score--;

        }
    }


    private void checkEdge()
    {
        if (isAtEdge()) 
        {
            delta = -delta;
        }
    }

}
smcgee wrote...
Thank you for your assistance. Here is my apple class. I looked at it again after your comments and cleaned it up a bit but I just can't get it to work.
smcgee smcgee

2016/5/25

#
 public void act()
    {
        countTime();//since we wanted to keep time in the program we to call the countTime method inside
        // an Act method.

       if (score == 0)
            {
                Greenfoot.stop();
            } 
    }
Thank you for the correction on the time code. I was not even paying attention to that issue. It now works perfectly!
danpost danpost

2016/5/25

#
To clean the Apple class up some more, you can remove these lines: 5, 6, 7, 13, 43, 45, 46, and 52 through 60. If you need an explanation as to why on any of these lines, just ask.
smcgee smcgee

2016/5/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Apple extends Actor
{
    public int score;

    public void act()
    {
        checkKeypress();
        removeWombat();
    }

    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX()-5, getY());
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            setLocation(getX()+5, getY());
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
        }
    }

    public void removeWombat()
    {
        if ( isTouching(Wombat.class) ) 
        {

            removeTouching(Wombat.class); 

            score = score--;
        }
    }
   
}
smcgee wrote...
I have deleted the code you suggested! How can I make the score reduce each time a wombat is removed from the world?
smcgee smcgee

2016/5/26

#
As to an explanation, I think I understand why most of the code could be removed - redundancy - references to images we choose not use - but the removal of the delta surprised me until I realized the code was already in another class.
danpost danpost

2016/5/26

#
smcgee wrote...
As to an explanation, I think I understand why most of the code could be removed - redundancy - references to images we choose not use - but the removal of the delta surprised me until I realized the code was already in another class.
The only redundancy was with 'myWorld' and 'world' in the 'removeWombat' method -- but neither of them were being used after being declared. Most of it was because none of that code actually did anything -- the images were not being used, the 'world' and 'myWorld' variables were not being used and the only thing 'delta' was doing was changing from positive to negative and back -- its value, either way, was not being used for anything (being a 'private' field, it could not have been used from outside the class, either).
smcgee wrote...
How can I make the score reduce each time a wombat is removed from the world?
Now, this is a redundancy. You have a 'score' field in the Apple class, which is really not used for anything (it is just being decremented when a wombat is removed) and you have one in the Forest class, whose value is initialized at '6' and has a method in the class to decrement its value (the method does not make use of the parameter value given it -- so either the parameter should be removed or the method should subtract its value instead of decrementing the value of the 'score' field. Since 'score' is something that seems to belong to the game itself (and not the apple -- although the apple is what actually scores the points), I would keep the one in the Forest class and remove the one in the Apple class. This will also prevent any issues if you decide to remove and replace the apple for any reason (where the current score would be lost). In the 'removeWombat' method of the Apple class, you can change line 40 (in your latest Apple class posting) to the following:
((Forest)getWorld()).removeScore(0);
I put the '0' in for the parameter to remind you of the issue with it.
smcgee smcgee

2016/5/27

#
danpost - I followed all of your suggestions but your last comments were confusing and seemed deliberately hurtful. smcgee
danpost danpost

2016/5/27

#
smcgee wrote...
danpost - I followed all of your suggestions but your last comments were confusing and seemed deliberately hurtful. smcgee
Nothing was intended at all to be hurtful -- only helpful. I am sorry if anything came across that way.
smcgee smcgee

2016/5/27

#
I appreciate that - you do great work and so many benefit from your contribution to teaching future programmers.
You need to login to post a reply.