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

2018/1/19

Help with ending my game

rory rory

2018/1/19

#
Ok so, I'm needing help with figuring out what code I use so that when the time runs out and the game ends, if the sheep has more flowers then the snake the words "you win" are displayed and if the snake has more flowers when the time runs out the words "you lose" are displayed.
CxVercility CxVercility

2018/1/20

#
That's a simple if/else + displaying 2 different Strings, what exactly is your problem?
If(Timeout) {
If(SnakeFlowers>SheepFlowers){
//Snake String display
}
else{
//Sheep String
}
Greenfoot.stop
} 
rory rory

2018/1/20

#
CxVercility, can I show you my code and you help?

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class grassworld extends World
{
    /**
     * Create My world (grass). Our world has a size 
     * of 600x600 cells, where every cell is just 1 pixel.
     */
    public grassworld()
   {    
        super(900, 700, 1); 
   }
   private int timer = 3600;
   {
   if (timer>0);
   {
       timer--;
       if (timer==0) Greenfoot.stop();
   }
   if (Timeout); 
    {
    if 
    (SnakeFlowers>SheepFlowers);
    {
    //Snake String display
    }
    else
    {
     //Sheep String
     }
    Greenfoot.stop;
   }
   }
 
   
Super_Hippo Super_Hippo

2018/1/20

#
private Sheep sheep = new Sheep();
private Snake snake = new Snake();
private int timer = 3600;

public Grassworld()
{
    super(900, 700, 1);
    addObject(sheep, 100, 100);
    addObject(snake, 800, 600);
}

public void act()
{
    if (--timer == 0)
    {
        if (sheep.getFlowers() > snake.getFlowers())
        {
            //display that you won
        }
        else
        {
            //display that you lost
        }
        Greenfoot.stop();
    }
}
//in Sheep and Snake
private int flowersCollected = 0;

//whenever eating one
flowersCollected++;

//getter method so the world can access it
public int getFlowers()
{
    return flowersCollected;
}
You need to login to post a reply.