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

2017/4/6

Wont show "You Win!" but shows "Game Over"

Childiish Childiish

2017/4/6

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

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Seal extends Actor{
private int health = 3;
 
public boolean loseLife(int amount)
{
    health -= amount;
    if (health < 1)
    {
        getWorld().removeObject(this);
        return true;
    }
    return false;
}

   public Seal()
    {
        GreenfootImage image = getImage();  
        image.scale(300, 300);
        setImage(image);
        }
        
    
    /**
     * Act - do whatever the Seal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    
    {
        move(4);
        if (Greenfoot.getRandomNumber(50) < 10)
        {
            turn(Greenfoot.getRandomNumber(3) - 3);
        }
        if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        } 
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
       
        Actor rocket;
        rocket = getOneIntersectingObject(Rocket.class); 
        if (rocket != null)
         {
            World myWorld = getWorld();
            
            Greenfoot.playSound("not_really_fine.wav");
            //Display game over text 
            GameOver gameover = new GameOver(false);
            myWorld.addObject (gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(rocket);
       }  
       Actor seal;
       seal = getOneIntersectingObject(Seal.class);
       if (seal != null)
         {
            World myWorld = getWorld();
            
            
            //Display game over text 
            GameOver gameover = new GameOver(true);
            myWorld.addObject (gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(seal);
       }  

       
    }
}
Thats my seal class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends Actor
{
    /**
     * Act - do whatever the GameOver wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public GameOver(boolean youWin)
    {
        if (youWin)
        {
            setImage(new GreenfootImage("YOU WIN!!!", 110, Color.GREEN, Color.WHITE));
        }
        else
        {
            setImage(new GreenfootImage("GAME OVER", 110, Color.RED, Color.BLACK));
        }
    }
}
and thats my GameOver class. It wont show "You Win!" but shows the Game Over part
Nosson1459 Nosson1459

2017/4/6

#
What do you mean "It wont show "You Win!" but shows the Game Over part", does that mean that it says "GAME OVER" when it's not supposed to, or it means that the actor is there but doesn't say what it's supposed to at all? According to your Seal code if it's intersecting with a Rocket then add the "GAME OVER" text, if it's intersecting with another Seal then display the "YOU WIN!!!" text, after whatever text is added then the game will continue. If the problem is that the wrong text is displaying (which I wouldn't see why it should) then try right-clicking on the class in the sidebar of Greenfoot and adding one in manually to see what happens.
Childiish Childiish

2017/4/7

#
NVM i fixed it haha Thanks anyways though
You need to login to post a reply.