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

#
Hi, I am trying to use the variable 'lives' to keep track of how many times my 'Player' has touched lizard. With the code shown below however, I am unsuccessful in trying to detect the fifth time it touches the lizard so that the 'player' will be removed from the world. Is there a way to fix this problem? Thanks for the help. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Player extends MazeCreature { 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() { if(canMove()) { moveForward(); } Actor lizard = getOneIntersectingObject(Lizard.class); if(lizard!=null) { lives=lives-1; } if(lives==0) { Greenfoot.stop(); } }
danpost danpost

2014/5/1

#
Working on a hunch, could you please post your Lizard class code; and please use the 'code' tag below the 'Post a reply' box to insert your code into your post.
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; 
    }
}
danpost danpost

2014/5/1

#
Sorry, it is not what I thought it might be. Let us try your world class.
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
{

    /**
     * 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 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

#
Do you have any other actors that may interact with the player? .... No. Wait. Let us try your MazeCreature class. That is probably where it is at.
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;
    }
}
danpost danpost

2014/5/1

#
I just ran the project with no Home class and no Wall objects (I created an empty Wall class) and it shows the player's lives going to zero when contacting the lizard. What it does not do, is decrease the value once per contact. Please, confirm that your attempts are detecting 4 hits and not registering the fifth.
gamer121 gamer121

2014/5/1

#
Basically what I am trying to do is when the player comes in contact with the lizard for the fifth time, the game stops. However, with the code I have, whenever the player comes in contact with the lizard just once, the game stops.
danpost danpost

2014/5/1

#
gamer121 wrote...
Basically what I am trying to do is when the player comes in contact with the lizard for the fifth time, the game stops. However, with the code I have, whenever the player comes in contact with the lizard just once, the game stops.
This is not what your leader post suggested. You could add a boolean field to the Player class to track the state of lizard contact. It can be used to regulate the lose of lives. * when the boolean field is false (no contact is currently known to be occurring) and contact is detected, set the value of the field to true and subtract one from the lives counter; * when the boolean field is true (contact has occurred recently) and no contact is detected, set the value of the field back to false.
danpost danpost

2014/5/1

#
Hold on. Your world is gridded and your player is either absolutely on the lizard or absolutely off it (it is not like the images will take some time to move to a point where they no longer overlap). You do not need the boolean field at all. Just move the lizard detection code inside the 'if (canMove()) block, after the 'moveForward' call. You can also move the zero lives check inside the previous 'if' block immediately after 'lives=lives-1'.
gamer121 gamer121

2014/5/1

#
I have added a Boolean field to the Player class but I have no idea where to take it from there. I have assigned an int variable called lives to 5.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends MazeCreature
{
    private int foodEaten = 0;
    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;
        }
        
        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);
        }
    }
    
   public boolean lives()
    
    {
       int lives = 5;
    }
}
danpost danpost

2014/5/1

#
The 'act' method of the Player class should be like this:
public void act() 
{
    if (canMove()) {
        moveForward();
        Actor lizard = getOneIntersectingObject(Lizard.class);
        if (lizard!=null) {
            lives=lives-1;
            if (lives==0) {
                Greenfoot.stop();
            }
        }   
    }
}
gamer121 gamer121

2014/5/1

#
Yes, that is what I had before but it did not work. I have added a Boolean field to the Player class as you told me but I have no idea where to take it from there.
danpost danpost

2014/5/1

#
As far as my last post : that is the code you would have needed from what I had seen up to that point.
There are more replies on the next page.
1
2