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

2012/12/19

Help with program? (Game won't end)

beemo27 beemo27

2012/12/19

#
I'm new to this-- I've been browsing the discussions and I've found a lot of helpful stuff, but I can't figure out the last part of my game (based off the crab scenario). Short story is a pony is collecting items, and I want the game to end if it picks up a certain number of items. What happens instead is that it gets more than the required amount, but then the game just sits there instead of stopping properly.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Pinkie Pie the Pony will move about and collect supplies for her party. 
 * 
 */
public class Pony extends Animal
{
private GreenfootImage pinkie1 = new GreenfootImage("pinkiejump.png");
private GreenfootImage pinkie2 = new GreenfootImage("pinkiewalks.png");
private boolean image1 = true;
private int balloonsPopped;
private int confettiGathered;
private int cupcakesEaten;

    public Pony()
    {
        balloonsPopped = 0;
        confettiGathered = 0;
        cupcakesEaten = 0;
    }
    
     private void walk()
    {
        move();
        if (image1) 
            setImage(pinkie1);
        else 
            setImage(pinkie2);
        image1 = !image1;
    }
    
    /**
     * Act - do whatever the Pony wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //action
        walk();     
        checkKeyPress();
        lookForBalloons();
        lookForConfetti();
        lookForCupcakes();
        win();

    }    
    
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(5);
        }
    }
    
    public void checkKeyPress()
     {
        if (Greenfoot.isKeyDown("left"))
        {
           setRotation(180);
           move();
        }
        if (Greenfoot.isKeyDown("right"))
        {
          setRotation(0);
          move();
        }
        if (Greenfoot.isKeyDown("up"))
        {
           setRotation(270);
           move();
        }
        if (Greenfoot.isKeyDown("down"))
        {
          setRotation(90);
          move();
        }
    }
    
    public void lookForBalloons()
    {
        if (canSee(Balloons.class))
        {
            eat(Balloons.class);
            Greenfoot.playSound("boing.mp3");
            balloonsPopped++;
            if (balloonsPopped == 4)
            {
                Greenfoot.playSound("okee.wav");
            }
        }
    }
            
     public void lookForConfetti()
    {
        if (canSee(Confetti.class))
        {
            eat(Confetti.class);
            Greenfoot.playSound("clang.mp3");
            confettiGathered++;
            if (confettiGathered == 3)
            {
                Greenfoot.playSound("style.wav");
            }
        }
    }    
            
    public void lookForCupcakes()
    {
        if (canSee(Cupcake.class))
        {
            eat(Cupcake.class);
            Greenfoot.playSound("bloop.mp3");
            cupcakesEaten++;
            if (cupcakesEaten == 6)
            {
               Greenfoot.playSound("yay.mp3");
            }
        }
       
       
    }
   
    public void win()
    {
         if (cupcakesEaten == 6 && confettiGathered == 3 && balloonsPopped == 4)
            {
                 Greenfoot.playSound("party.wav");
                 Greenfoot.stop();
                }
        }
}
There are only 4 balloons and 3 confetti being placed on the screen, and a random number of cupcakes (between 6 and 12, I guess). What's currently happening is that the pony will successfully complete the lookForEtcetera methods and even when all have been accomplished... nothing happens. My professor and I both looked it over, but couldn't figure it out. Can anyone tell me what I'm missing? Thank you!!
davemib123 davemib123

2012/12/19

#
it would be helpful if you post your game up. then it can be debugged fairly quickly :)
beemo27 beemo27

2012/12/19

#
.. you know what, a few moments after I posted this, I just figured out that I'd had some broken code in another class that was causing the problem. I'm so sorry to spam! Thank you though!
davemib123 davemib123

2012/12/19

#
not a problem beemo27. Welcome to the greenfoot community.
You need to login to post a reply.