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

2013/10/31

to end a game

sophiefordream sophiefordream

2013/10/31

#
hello actually i have something to ask ....how can i end a game with multiple of object for example : in MyWorld i have butterfly,elephant etc and after all the actor clicked and removed another actor appear represent that game already over...
sophiefordream sophiefordream

2013/10/31

#
the coding look like this for banana class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Banana here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Banana extends Actor
{
    private static final int EAST = 0;
    private static final int WEST = 1;
    
    private int direction;
 
    /**
     * Act - do whatever the Banana wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
 

        
        
        if ( canMove() )
        {
            move();
        }
        else   
        {
          turnRandom();  
    
        }
              if(Greenfoot.mouseClicked(this))    
{    
       
      getWorld().removeObject(this);    
      System.out.println("bagus! dah habis");  
      Greenfoot.playSound ( "15 FUNKY.mp3");
      
      
      
} 
       
    }
    
    public void move()
    {
        if (!canMove()) {
            return;
        }
        switch(direction) {
            
            case EAST :
                setLocation(getX() + 1, getY());
                break;
            
            case WEST :
                setLocation(getX() - 1, getY());
                break;
        }
    }
    
    public boolean canMove()
    {
       
        int x = getX();
        int y = getY();
        switch(direction) {
            
            case EAST :
                x++;
                break;
            
            case WEST :
                x--;
                break;
        }
         if ( x == 599 || x == 0 )
         {
             return false;
         }
        
        else
        {
            return true;
    
    
        }
    
     }
     
     public void turnRandom()
    {
        // get a random number between 0 and 3...
        int turns = Greenfoot.getRandomNumber(10);
        
        // ...an turn left that many times.
        for(int i=0; i<turns; i++) {
            turnLeft();
        }
    }
    
    public void turnLeft()
    {
        switch(direction) {
            
            case EAST :
                setDirection(WEST);
                break;
            
            case WEST :
                setDirection(EAST);
                break;
        }
    }
    
    public void setDirection(int direction)
    {
        this.direction = direction;
        switch(direction) {
           
            case EAST :
               
                setRotation(0);
                break;
            
            case WEST :
                
                setRotation(0);
                break;
            default :
                break;
        }
    }
     
 
{    
}
}
other subclass has the same coding and just a little bit changes on class name
danpost danpost

2013/10/31

#
In the world class, you can use 'getObjects(null).isEmpty()' to see if no actors are in the world. For a specific set of actors, you will need to combine them like this 'getObjects(Butterfly.class).isEmpty && getObjects(Elephant.class).isEmpty() && /* etc */'.
You need to login to post a reply.