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

2014/11/12

Does any one know how this works?

chamuzi3 chamuzi3

2014/11/12

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

/**
 * Write a description of class Rat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rat extends Actor
{
     private int speed = 1;
    /**
     * Act - do whatever the Rat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        for (int i = 0; i < speed; i++) move();
    }
    private void move()
    {
       int dx = 0, dy = 0;
       if (Greenfoot.isKeyDown("up")) dy--;
       if (Greenfoot.isKeyDown("down")) dy++;
       if (Greenfoot.isKeyDown("left")) dx--;
       if (Greenfoot.isKeyDown("right")) dx++;
       setLocation(getX() + dx, getY());
       if (getOneIntersectingObject(Wall.class) != null) 
       {
           if (dy == 0)
           {
               setLocation(getX(), getY() + 1);
               if (getOneIntersectingObject(Wall.class) == null) return;
               setLocation(getX(), getY() - 2);
               if (getOneIntersectingObject(Wall.class) == null) return;
               setLocation(getX() - dx, getY() + 1);
               return;
            }
           setLocation(getX() - dx, getY());
        }
        setLocation(getX(), getY() + dy);
        if (getOneIntersectingObject(Wall.class) != null)
        {
            if (dx == 0)
            {
                setLocation(getX() + 1, getY());
                if (getOneIntersectingObject(Wall.class) == null) return;
                setLocation(getX() - 2, getY());
                if (getOneIntersectingObject(Wall.class) == null) return;
                setLocation(getX() + 1, getY() - dy);
                return;
            }
            setLocation(getX(), getY() - dy);
        }
    }
}
danpost danpost

2014/11/12

#
Here is what I see: we have an Actor class that creates Rat objects (line 9) that initially moves 1 pixel per act (line 11). The fact that the 'speed' field has 'private' access and that the value of the field is never changed indicates that this initial speed is also its permanent speed when moving. This makes the 'for' loop in the 'act' method (line 18) unnecessary. The following should be sufficient:
public void act()
{
    move();
}
Now, for the 'move' method: lines 22 through 26 gets user keyboard input for movement of the actor by way of the arrow keys -- no problem there. Lines 27 through 40 deals basically with horizontal movement and lines 41 through 54 deals with vertical movement. It appears that if a wall is encountered while the actor is moving along the top or left edge of the world, an attempt is made to be nudged away from that edge. The nudging tendency is down when moving horizontally and right when moving vertically (because their nudging codes come first). A possible major inconsistency is that if the Rat object was in a bounded world (that is, the world it was in was created by using the World(int, int, int) constructor or by using the World(int, int, int, boolean) constructor where the boolean was set to 'true', then nudging up or left would be impossible being lines 30 and 44 put the actor at the edge of the world to begin with. There is something else that is somewhat inconsistent. Once the actor gets to the top edge of the world, it will be 'stuck' there until a wall nudges it off the edge. This does not happen with the left wall because the horizontal movement is coded first. Hope I made some sense of it. (btw, all in all, I would answer your title with this: 'Not too well.')
You need to login to post a reply.