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

2014/5/1

Variables keeping track of values

1
2
gamer121 gamer121

2014/5/1

#
This is my updated code. Unfortunately, I have the same problem. When the two objects intersect only once, the game will stop, as opposed to five times.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends MazeCreature
{
    private int foodEaten = 0;
    private int lives = 5;
    public Player(int size)

    {
        getImage().scale(size,size);
        int angle = Greenfoot.getRandomNumber(4)*90;
        setRotation(angle);
    }

    public Player()

    {
        this(40);
    }

    public void act() 
    {
        checkKeys();
        if(canMove()) {
            moveForward();
            Actor lizard = getOneIntersectingObject(Lizard.class);  
            if (lizard!=null) {  
                lives=lives-1; 
                if (lives==0) {                 
                    Greenfoot.stop();
                }
            }
        } 
        
        Actor food = getOneIntersectingObject(Food.class);
        if(food!=null) {
            getWorld().removeObject(food);
            foodEaten++;
        }
    }

    public void checkKeys()

    {
        if(Greenfoot.isKeyDown("right")) {
            setRotation(EAST);
        }
        if(Greenfoot.isKeyDown("down")){ 
            setRotation(SOUTH);
        }
        if(Greenfoot.isKeyDown("left")) {
            setRotation(WEST);
        }
        if(Greenfoot.isKeyDown("up")) {
            setRotation(NORTH);
        }
    }
}
gamer121 gamer121

2014/5/1

#
This is my updated code. Unfortunately, I have the same problem. When the two objects intersect only once, the game will stop, as opposed to five times. Is this because the 'k=k-1' is executing too many times before the two objects are no longer intersecting?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends MazeCreature
{
    private int foodEaten = 0;
    private int lives = 5;
    public Player(int size)

    {
        getImage().scale(size,size);
        int angle = Greenfoot.getRandomNumber(4)*90;
        setRotation(angle);
    }

    public Player()

    {
        this(40);
    }

    public void act() 
    {
        checkKeys();
        if(canMove()) {
            moveForward();
            Actor lizard = getOneIntersectingObject(Lizard.class);  
            if (lizard!=null) {  
                lives=lives-1; 
                if (lives==0) {                 
                    Greenfoot.stop();
                }
            }
        } 
        
        Actor food = getOneIntersectingObject(Food.class);
        if(food!=null) {
            getWorld().removeObject(food);
            foodEaten++;
        }
    }

    public void checkKeys()

    {
        if(Greenfoot.isKeyDown("right")) {
            setRotation(EAST);
        }
        if(Greenfoot.isKeyDown("down")){ 
            setRotation(SOUTH);
        }
        if(Greenfoot.isKeyDown("left")) {
            setRotation(WEST);
        }
        if(Greenfoot.isKeyDown("up")) {
            setRotation(NORTH);
        }
    }
}
danpost danpost

2014/5/1

#
I think it may be a combination of things. I think the fact that the counters for your MazeCreature object get out of synchronization is one issue. I think that the movement within the running of the counter is another. It might be best to place a single counter in the world class and run it from the world class act method. When the counter reaches 20, call a 'pseudo-act' method for each MazeCreature (call it 'perform' and put an empty one in the MazeCreature class). With the empty one in that class you can run the 'perform' method of the individual classes on the objects cast as MazeCreature objects. From the world class act method:
counter++;
if (counter == 20)
{
    for (Object obj : getObjects(MazeCreature.class)) ((MazeCreature)obj).perform();
    counter = 0;
}
Line 4 above will execute the 'perform' method on all actors that are of a class that extends MazeCreature every twenty act cycles.
danpost danpost

2014/5/1

#
Do you want your player to lose one of its 'lives' when it and a lizard swap squares? The could cross paths but not ever actually occupy the same location.
gamer121 gamer121

2014/5/1

#
Ok, so I added the code into the world class act method (shown below). What do I need to change in my other classes to get it so that I can find the 'symbol - method perform()'?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Maze here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Maze extends World
{
    private int counter;
    /**
     * Constructor for objects of class Maze.
     * 
     */
    public Maze()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 40); 
        addFood(20);
        addObject(new Home(),getWidth()-1,getHeight()-1);
        addObject(new Player(),0,0);
        addObject(new Lizard(),getWidth()-1,getHeight()-1);
    }

    public void act()

    {
        counter++;  
        if (counter == 20)  
        {  
            for (Object obj : getObjects(MazeCreature.class))((MazeCreature)obj).perform();  
            counter = 0;  
        } 
    }

    public void addFood(int n)
    {
        for(int f=0; f<n; f++) {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Food(),x,y);
        }
    }
danpost danpost

2014/5/1

#
In all subclasses of the MazeCreature class, change 'public void act()' to 'public void perform()'.
gamer121 gamer121

2014/5/1

#
After I do that, I still can't compile the code in my world class because it cannot find 'symbol - method perform()'?
danpost danpost

2014/5/1

#
Did you add the following to your MazeCreature method:
public void perform(){}
gamer121 gamer121

2014/5/1

#
I have done what you said but now no matter how many times the Player intersects with the Lizard, the game won't stop. I will post my code below.
gamer121 gamer121

2014/5/1

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

/**
 * Write a description of class Maze here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Maze extends World
{
    private int counter;
    /**
     * Constructor for objects of class Maze.
     * 
     */
    public Maze()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 40); 
        addFood(20);
        addObject(new Home(),getWidth()-1,getHeight()-1);
        addObject(new Player(),0,0);
        addObject(new Lizard(),getWidth()-1,getHeight()-1);
        prepare();
    }

    public void act()

    {
        counter++;  
        if (counter == 20)  
        {  
            for (Object obj : getObjects(MazeCreature.class))((MazeCreature)obj).perform();  
            counter = 0;
        } 
    }

    public void addFood(int n)
    {
        for(int f=0; f<n; f++) {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Food(),x,y);
        }
    }
gamer121 gamer121

2014/5/1

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MazeCreature extends Actor
{   
    protected static final int EAST = 0;
    protected static final int SOUTH = 90;
    protected static final int WEST = 180;
    protected static final int NORTH = 270;
    private int counter = 0;
    public void moveForward()

    {
        counter++;
        if(counter == 20) {
            move(1);
            counter = 0;
        }
    }
    
    public boolean canMove()
    
    {
       return !facingWall() && !facingEdge();
    }
    
    public boolean facingEdge()
    
    {
        switch(getRotation()) {
            case EAST: return getX()==getWorld().getWidth()-1;
            case WEST: return getX()==0;
            case NORTH: return getY()==0;
            case SOUTH: return getY()==getWorld().getHeight()-1;
        }
        return false;
    }

    public boolean facingWall()

    {
        int xOff = 0, yOff = 0;
        switch(getRotation()){
            case EAST: xOff = 1; break;
            case SOUTH: yOff = 1; break; 
            case WEST: xOff = -1; break; 
            case NORTH: yOff = -1; break; 
        }
        return getOneObjectAtOffset(xOff,yOff,Wall.class)!=null;
    }
    
    public void perform() {}
}
gamer121 gamer121

2014/5/1

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends MazeCreature
{
    private int foodEaten = 0;
    private int lives = 5;
    public Player(int size)

    {
        getImage().scale(size,size);
        int angle = Greenfoot.getRandomNumber(4)*90;
        setRotation(angle);
    }

    public Player()

    {
        this(40);
    }

    public void act() 
    {
        checkKeys();
        if(canMove()) {
            moveForward();
        } 
    }
    
    public void perform()
    
    {
        Actor food = getOneIntersectingObject(Food.class);
        if(food!=null) {
            getWorld().removeObject(food);
            foodEaten++;
        }
    }

    public void checkKeys()

    {
        if(Greenfoot.isKeyDown("right")) {
            setRotation(EAST);
        }
        if(Greenfoot.isKeyDown("down")){ 
            setRotation(SOUTH);
        }
        if(Greenfoot.isKeyDown("left")) {
            setRotation(WEST);
        }
        if(Greenfoot.isKeyDown("up")) {
            setRotation(NORTH);
        }
    }
}
gamer121 gamer121

2014/5/1

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Lizard extends MazeCreature
{
    public Lizard(int size)

    {
        getImage().scale(size,size);
        int angle = Greenfoot.getRandomNumber(4)*90;
        setRotation(angle);
    }

    public Lizard()

    {
        this(40);
    }

    public void act() 
    {
        if(canMove() && !facingLizard() ) {
            moveForward();
        }  else{
            changeDirection();
        }
    }

    public void changeDirection()

    {
        int angle = Greenfoot.getRandomNumber(4)*90;
        setRotation(angle); 
    }
    
    public boolean facingLizard()
    
    {
       int xOff = 0, yOff = 0;
       switch(getRotation()){
            case EAST: xOff = 1; break;
            case SOUTH: yOff = 1; break; 
            case WEST: xOff = -1; break; 
            case NORTH: yOff = -1; break; 
        }
        return getOneObjectAtOffset(xOff,yOff,Lizard.class)!=null; 
    }
}
gamer121 gamer121

2014/5/1

#
Nevermind, I finally got it to work! Thanks for all of your time and help. I really appreciate it!
You need to login to post a reply.
1
2