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

2014/5/22

Ending a programme

1
2
11frendash 11frendash

2014/5/22

#
How can i get my game to stop when the class has been eaten or the player has completed the game??? (This is prbably easy to you guys...)
danpost danpost

2014/5/22

#
The first part is easy .. after all of a class has been eaten. In your world class act method add the single line
if (getObjects(ClassName.class).isEmpty())
{
    Greenfoot.stop();
    return;
}
The second part would use the same block code (lines 2 through 5 above); but the code would probably be in the class of the player and the condition would be different.
11frendash 11frendash

2014/6/2

#
thanks, i will try this method... sorry for the long wait in reply
11frendash 11frendash

2014/6/17

#
I put this in my background part then line 2 to 5 in my wombat, but when i had "eaten" the objects it still caried on... im slightly confused, it also did it when the wombat got "eaten". programming for the wombat...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class snowman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Wombat extends Animal
{
   public void act()
   {
   move();
   checkKeypress();
   
       if (canSee(Leaf.class))
       {
           eat(Leaf.class);
       }
                    
       if (canSee(Ant.class))
       {
           eat(Ant.class);
       }
   }
   
   public void eat()
   {
       Actor Ant;
       Ant = getOneObjectAtOffset(0, 0, Ant.class);
       if (Ant != null)
       {
           World world;
           world = getWorld();
           world.removeObject(Ant);
           Greenfoot.playSound("slurp.wav");
       }
       
       {  
        Greenfoot.stop();  
        return;  
       }
   } 
    
   public void lookForAnt()
   {
       if (canSee(Ant.class))
       {
           eat(Ant.class);
           Greenfoot.playSound("slurp.wav");
       }
            
       if (canSee(Leaf.class))
       {
            eat(Leaf.class);
            Greenfoot.playSound("slurp.wav");
       }
   }
        
   public void checkKeypress()
   {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
            
        if(Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        
    }
}
11frendash 11frendash

2014/6/17

#
Need to know asap!!!!! PLEASE
Mint_Greenie Mint_Greenie

2014/6/17

#
here you go
Mint_Greenie Mint_Greenie

2014/6/17

#
@11frendash: Inside the class or method you want to run when the programme should end. Enter the code described below.
//Do something like this...
public GameOver(int score) {
Greenfoot.stop();
        GreenfootImage text = new GreenfootImage("Game Over\nCoins: " + score, 40, Color.RED, new Color(0, 0, 0, 0));
        GreenfootImage image = new GreenfootImage(text.getWidth() + 20, text.getHeight() + 20);
        image.setColor(new Color(255, 255, 255, 128));
        image.fill();
        image.drawImage(text, 10, 10);
        setImage(image);
        Home home = new Home();
        Greenfoot.setWorld(home);
    }
danpost danpost

2014/6/17

#
Just remove lines 39 through 42 of the Wombat class above and put the code I gave above in your world act method replacing 'ClassName' with 'Ant' or 'Leaf', which ever needs to be entirely eaten to win. If both then replace the first line with
if (getObjects(Ant.class).isEmpty() && getObjects(Leaf.class).isEmpty())
11frendash 11frendash

2014/6/18

#
thanks guys i will try both methods
11frendash 11frendash

2014/6/18

#
@Mint_Greenie: the "Color.RED", it doesnt like the "Color. is it supposed to be Colour???
11frendash 11frendash

2014/6/18

#
//my wombat class

import greenfoot.*;

public class Wombat extends Animal
{
   public void act()
   {
   move();
   checkKeypress();
   
       if (canSee(Leaf.class))
       {
           eat(Leaf.class);
       }
                    
       if (canSee(Ant.class))
       {
           eat(Ant.class);
       }
   }
   
   public void GameOver(int score) 
   {  
       Greenfoot.stop();  
            GreenfootImage text = new GreenfootImage("Game Over\nCoins: " + score, 40, Color.RED, new Color(0, 0, 0, 0));  
            GreenfootImage image = new GreenfootImage(text.getWidth() + 20, text.getHeight() + 20);  
            image.setColor(new Color(255, 255, 255, 128));  
            image.fill();  
            image.drawImage(text, 10, 10);  
            setImage(image);  
            Home home = new Home();  
            Greenfoot.setWorld(home);  
    }  

   
   public void eat()
   {
       Actor Ant;
       Ant = getOneObjectAtOffset(0, 0, Ant.class);
       if (Ant != null)
       {
           World world;
           world = getWorld();
           world.removeObject(Ant);
           Greenfoot.playSound("slurp.wav");
       }
       
       {  
        Greenfoot.stop();  
        return;  
       }
   } 
    
   public void lookForAnt()
   {
       if (canSee(Ant.class))
       {
           eat(Ant.class);
           Greenfoot.playSound("slurp.wav");
       }
            
       if (canSee(Leaf.class))
       {
            eat(Leaf.class);
            Greenfoot.playSound("slurp.wav");
       }
   }
        
   public void checkKeypress()
   {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
            
        if(Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        
    }
}
11frendash 11frendash

2014/6/18

#
Found it was just "Greenfoot.stop();"
danpost danpost

2014/6/18

#
11frendash wrote...
@Mint_Greenie: the "Color.RED", it doesnt like the "Color. is it supposed to be Colour???
The compiler will not be able to find the Color class unless you add the following import line:
import java.awt.Color;
11frendash 11frendash

2014/6/18

#
thank you, so so so much, ive been sat here like a lemon!
11frendash 11frendash

2014/6/18

#
   public void lookForLeaf()
   {
       if (canSee(Leaf.class))
       {
           eat(Leaf.class);
           leavesEaten = leavesEaten + 1;
       }
   }
   
There are more replies on the next page.
1
2