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

2017/11/8

How do you add a step counter

Pimissuper1 Pimissuper1

2017/11/8

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

/**
 * A man can be moved around with the arrow-keys.
 * A man can push a crate, but only one at a time.
 * Each step is counted.
 */
public class Man extends BlockActor
{
    // Images that show the man walking up, down, left and right
    private GreenfootImage imageLeft;
    private GreenfootImage imageRight;
    private GreenfootImage imageDown;
    private GreenfootImage imageUp;
    
    // Field: the current direction of the man
    // the the static fields in BlockActor for definition
    private int direction;
    
    // Field: the number of steps taken so far.
    protected int nrOfSteps;
    
    /**
     * Constructor. Make sure all images (up, down, left, right) are opened.
     * Initial direction is down
     */
    public Man () {
        imageLeft = new GreenfootImage("me_strip4_4.png");
        imageRight = new GreenfootImage("me_strip4_3.png");
        imageDown = new GreenfootImage("me_strip4_1.png");
        imageUp = new GreenfootImage("me_strip4_2.png");
        setImage(imageDown);
        direction = DOWN;
        nrOfSteps = 1;    
    }
  
               
    
    /**
     * The player wants to move the man into a certain direction
     * Make sure the man can push a crate, but only one at a time.
     */
    private void wantsToMoveTo (int direction) {
        // hier moet de code komen om het mannetje de kratjes te laten verplaatsen.
        moveTo(direction);
        showNumberOfSteps ();
        nrOfSteps++;
    }    
    
    /**
     * Act-method. Check if one of the arrow-keys is pressed
     * Then check if the all crates are in place.
     */
    public void act() 
    {
       checkKeypress();
       checkGewonnen();
       
    } 
    
    
    
    /**
     * Check to see if all crates are in place. 
     */
    private void checkGewonnen(){
        ((SokoBanWorld)getWorld()).endOfLevel();
    }

    /**
     * Show the number of steps
     */
    private void showNumberOfSteps (){
        getWorld().showText ("Aantal stappen = " + nrOfSteps, 3, 0);
    }    

    /**
     * Select the right image of the man based on the direction
     */
    private void setImage (int direction) {
        switch (direction) {
            case UP:
                setImage(imageUp);
                break;
            case DOWN:
                setImage(imageDown);
                break;
            case LEFT:
                setImage(imageLeft);
                break;
            case RIGHT:
                setImage(imageRight);
                break;
            default:
                break;
        }     
    }
    
   /**
    * Check to see if the key is pressed
    */
   private void checkKeypress() {
        String key = Greenfoot.getKey();
        int newDirection = -1;
        if (key == "left") {
           newDirection = LEFT;
        }
        if (key == "right") {
           newDirection = RIGHT;
        }
        if (key == "up") {
           newDirection = UP;
        }
        if (key == "down") {
           newDirection = DOWN;
        }
        // if one of the arrow keys has been pressed
        if (newDirection != -1) {
            // if the man was already in the same direction
            if (direction == newDirection){
                // see if we can move the man
                wantsToMoveTo(direction);
            }
            // the man wants to move in a new direction
            // change the image of the man
            else {
                setImage(newDirection);                
                direction = newDirection;
            }
        }
   }
 }
Pimissuper1 Pimissuper1

2017/11/8

#
The step counter is working but it should not go up when walking against a wall (Brick).
Super_Hippo Super_Hippo

2017/11/8

#
1 - check key press 2 - move in the direction depending on the key press 3 - check if a brick blocks the field - if yes, move back - else increase the counter
Pimissuper1 Pimissuper1

2017/11/9

#
But how do you write that in code because we have tried but we couldn't get our code working
danpost danpost

2017/11/9

#
Pimissuper1 wrote...
But how do you write that in code because we have tried but we couldn't get our code working
What exactly have you tried? What is the code for the 'moveTo' method in the BlockActor class?
Pimissuper1 Pimissuper1

2017/11/9

#
/** * A BlockActor is a specific kind of Actor: * There can be only one BlockActor on a specific spot at the same time. * So, a BlockActor cannot move to a spot where already another BlockActor resides */ public class BlockActor extends Actor { // Fields for determing up, down, left and right static final int UP = 0; static final int DOWN = 1; static final int LEFT = 2; static final int RIGHT = 3; /** * Move the blockactor to the given direction, but only * if there is no other blockactor in that direction. */ public void moveTo (int direction) { switch (direction) { case UP: moveTo (0,-1); break; case DOWN: moveTo (0,1); break; case LEFT: moveTo (-1,0); break; case RIGHT: moveTo (1,0); break; default: break; } } /** * Private method: move the blockactor to a new location, * based on the offset */ private void moveTo (int offsetX, int offsetY) { if (isFree (offsetX, offsetY)) { setLocation (getX() + offsetX, getY() + offsetY); } } /** * Private method: check to see if the new location is free * (there is no other blockactor at that location), * based on the given offset */ private boolean isFree (int offsetX, int offsetY) { if (getX()+offsetX < getWorld().getWidth() && getX()+offsetX >=0 && getY()+offsetY < getWorld().getHeight() && getY()+offsetY >=0) { return (getObjectsAtOffset(offsetX,offsetY,BlockActor.class).isEmpty()); } else { return false; } } /** * Get an objectreference to the crate, relative to the current location * If there is no crate, the return value will be null */ public Crate getLinkToCrate (int direction) { Crate returnValue = null; switch (direction) { case UP: returnValue = (Crate)getOneObjectAtOffset (0,-1, Crate.class); break; case DOWN: returnValue = (Crate)getOneObjectAtOffset (0,1, Crate.class); break; case LEFT: returnValue = (Crate)getOneObjectAtOffset (-1,0, Crate.class); break; case RIGHT: returnValue = (Crate)getOneObjectAtOffset (1,0, Crate.class); break; default: break; } return returnValue; } /** * Private method: see if there is a crate at the given offset */ private boolean canSeeCrate (int offsetX, int offsetY) { return !getObjectsAtOffset(offsetX,offsetY,Crate.class).isEmpty(); } /** * Check to see if there is a crate in the given direction * Return true if there is a crate in the given direction * Return false if there is no crate in the given direction */ public boolean canSeeCrate (int direction) { boolean returnValue = false; switch (direction) { case UP: returnValue = canSeeCrate (0,-1); break; case DOWN: returnValue = canSeeCrate (0,1); break; case LEFT: returnValue = canSeeCrate (-1,0); break; case RIGHT: returnValue = canSeeCrate (1,0); break; default: break; } return returnValue; } /** * Check to see if the location in the given direction is free * (there is no blockactor) * Return true if there is no blockactor in the given direction * Return false if the location is already taken */ public boolean isFree (int direction) { boolean returnValue = false; switch (direction) { case UP: returnValue = isFree (0,-1); break; case DOWN: returnValue = isFree (0,1); break; case LEFT: returnValue = isFree (-1,0); break; case RIGHT: returnValue = isFree (1,0); break; default: break; } return returnValue; } }
Pimissuper1 Pimissuper1

2017/11/9

#
I really hope you could help because we have no idea
danpost danpost

2017/11/9

#
In your case, about the only way to see if a move was made on any specific act step is by saving the location coordinates at the beginning of the act step and comparing them to the location at the end of the act step. If different, increment step counter.
Pimissuper1 Pimissuper1

2017/11/9

#
And how do you do that? we have not learned how to compare different locations
Super_Hippo Super_Hippo

2017/11/9

#
//begin of act method
int x=getX(), y=getY();

//...

//end of act method
if (x!=getX() || y!=getY())
{
    //step was made
}
You need to login to post a reply.