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

2012/11/10

Move the snake

1
2
3
4
5
danpost danpost

2012/11/13

#
Yes, moving the snake starting from the rear and working forward is much simpler than moving the snake starting from the front and working back (which is what was done there).
Tezuka Tezuka

2012/11/13

#
I still dont get why it has to be lengh-1 and i-1 in the method
danpost danpost

2012/11/13

#
The 'for' loop will start 'i' out at the last segment in the snake. If there are three segments (length is three), then the array will have them in snake, snake, and snake. To refer to the last segment, we need to subtract one from the number of segments to get the last non-null element in the array (hence 'length - 1'). If you noticed, the 'for' loop stops executing just as it gets to the head of the snake ('i > 0'); that is because the head does not have another segment to follow. Now, the ('i - 1'). If 'i' is equals to two, referring to the third and last segment of the snake, then 'i - 1' refers to the second segment. The third segment is to move into the location that the second segment is moving from. So, we get the location of the second segment, and re-locate the third segment there. Then, we move the second segment to the location of the first. Then the head will need moved (but not by following another segment).
Tezuka Tezuka

2012/11/13

#
thx I think I got it now a bit complicated
Tezuka Tezuka

2012/11/13

#
Hi danpost Do you know how i can draw a square and add it to my snake and food?
danpost danpost

2012/11/13

#
I am not sure how you mean. You need to be more specific; please. Would the square be something special for the images (maybe to specify some unique thing about the food and snake)? Exactly how would you like to place the square?
Tezuka Tezuka

2012/11/13

#
What I meant was I want to draw a rectangle and set my snakes image to the rectangle same with my food class
danpost danpost

2012/11/13

#
Have you looked at the GreenfootImage API for possible methods that could be used?
Tezuka Tezuka

2012/11/13

#
no:D
Tezuka Tezuka

2012/11/13

#
nvm about i will just get some nice snake pictures
Tezuka Tezuka

2012/11/13

#
     public void keyboardControll()
    {
        if(Greenfoot.isKeyDown("left") && direction !="right")
        {
            speedX = -speed;
            speedY = 0;
            direction = "left";
        }

        if(Greenfoot.isKeyDown("right") && direction !="left")
        {
            speedX = speed;
            speedY = 0;
            direction = "right";
        }

        if(Greenfoot.isKeyDown("up") && direction !="down")
        {
            speedX = 0;
            speedY = -speed;
            direction = "up";
        }

        if(Greenfoot.isKeyDown("down") && direction !="up")
        {
            speedX = 0;
            speedY = speed;
            direction = "down";
        }
    }
If my snake goes down and I press up +right key the snakes dies any idea?
danpost danpost

2012/11/13

#
Actually, that is how you have it coded. That is, if direction is down then the second 'if' will execute because the right key is down and the direction is not left; speedX will take on a value, speedY will become 0, and the direction will be set to right. At this point, the third 'if' will execute because the up key is pressed and the direction is no longer down, but right; speedX will become 0, and speedY will become -0 (or 0), and the direction will be set to up. With both speedX and speedY at 0, your snake 'dies'. Instead of using strings to show direction, use an 'int' value. One 'int' value for direction is all that is needed to contol the snake (forget speedX, speedY, and the Strings). Actually, if you wanted, you could keep one speed value called 'speed' (just to cover any chance in the future of having the speed changed). The main direction code is fairly simple. For your method above, use:
private void keyboardControll()
{
    int dx = 0; // to be 0, 1, or -1 for the x direction
    int dy = 0; // to be 0, 1, or -1 for the y direction
    if (Greenfoot.isKeyDown("right")) dx++;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("left")) dx--; // notice how opposite keys cancel each other
    if (Greenfoot.isKeyDown("up")) dy--;
    if (dx * dy != 0 || dx + dy == 0) return; // ensures one is zero and the other is not zero
    direction = (2 - dy) * (dy * dy) + (1 - dx) * (dx * dx); // set direction
}
The last statement sets the directions as follows: 0 = right 1 = down 2 = left 3 = up The second factor on one side of the sum will result in zero and the other will result in one. When 'dx * dx' is one, the first factor on the dx side will result in 0 or 2 (for right or left). When 'dy * dy' is one, the first factor on the dy side will result in 1 or 3 (for down or up). In your 'move' method for the head of the snake, use the following to determine x and y movements:
int x = (1-direction) * ((direction+1)%2);
int y = (2-direction) * (direction%2);
// validate that the parameters in the following statement are within world boundaries
setLocation(getX() + speed * x, getY() + speed * y);
In determining x and y, the same kind of coding is used. The second parts will be 0 or 1, depending on if the direction in along the x-axis or y-axis. The first part will return 1 or -1, depending on the direction along that axis.
Tezuka Tezuka

2012/11/13

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

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Actor
{
    private Counter counter;
    private String segment;
    private int direction;
    private int speed;
    private int speedX;
    private int speedY;
    private int length;
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public Snake(Counter pointsCounter)
    {
        pointsCounter = counter;
    }

    public Snake(String segment)
    {
        this.segment = segment;
        direction = 0;
        speedX = speed = 20;
        speedY = 0;
        length = 3;
    }

    public void act() 
    {
        if( segment == "head")
        {
            Playground playground = (Playground)getWorld();
            for( int  i = length -1; i > 0; i--)
            {
                playground.snake[i].setLocation(playground.snake[i-1].getX(),playground.snake[i-1].getY());
            }
            keyboardControl();
            move();
            eat();
        }
    }    

    public void keyboardControl()
    {
        if(Greenfoot.isKeyDown("d")) // checks if right key is pressed
        {
            if(direction==1 || direction==3)
            {
                direction = 0;
            }
        }
        else if(Greenfoot.isKeyDown("s")) // checks if down key is pressed
        {
            if(direction==0 || direction==2)
            {
                direction = 1;
            }
        }
        else if(Greenfoot.isKeyDown("a")) // checks if left key is pressed
        {
            if(direction==1 || direction==3)
            {
                direction = 2;
            }
        }
        else if(Greenfoot.isKeyDown("w")) // checks if up key is pressed
        {
            if(direction==0 || direction==2)
            {
                direction = 3;
            }
        }
    }

    public void move()
    {
        // check if hit the edge of screen

        Playground playground  = (Playground)getWorld();
        if( getX()+speedX < 0 || getX() + speedX >= 400)
        {
            die();
        }

        if (getY() + speedY  < 0 || getY() + speedY>= 400)
        {
            die();
        }
        setLocation(getX() + speedX, getY() + speedY);

        // check if hit the body
        Actor body = getOneObjectAtOffset(speedX, speedY, Snake.class);
        if(body !=null)
        {
            die();
        }

    }

    public void die()
    {
        Greenfoot.stop();
    }

    public void eat()
    {
        Actor food = getOneObjectAtOffset(0 , 0, Food.class);
        if( food !=null)
        {
            Playground playground = (Playground)getWorld();
            playground.removeObject(food);
            playground.snake[length] = new Snake("body");
            playground.addObject(playground.snake[length], playground.snake[length - 1].getX(), playground.snake[length - 1].getY());

            length++;

        }
    }
}
I tried with this code but it didnt work
Tezuka Tezuka

2012/11/13

#
nvm i got it
danpost danpost

2012/11/14

#
// Line 34 needs to be
length++;
EDIT: Do not worry about any change to line 17.
There are more replies on the next page.
1
2
3
4
5