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
6
7
danpost danpost

2012/11/16

#
What is the complete error message you are geting? (copy/paste to post)
Tezuka Tezuka

2012/11/16

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Playground here. * * @author (your name) * @version (a version number or a date) */ public class Playground extends World { Snake snakeBody; int direction; boolean eaten; /** * Constructor for objects of class Playground. * */ public Playground() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); direction = 0; eaten = false; snakeBody = new Snake; // initialization of the Snake for(int i=0; i<snakeBody.length; i++) { snakeBody = new Snake(); addObject(snakeBody, 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world } } public void checkKey() { if(Greenfoot.isKeyDown("right")) // checks if right key is pressed { if(direction==1 || direction==3) direction = 0; } else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed { if(direction==0 || direction==2) direction = 1; } else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed { if(direction==1 || direction==3) direction = 2; } else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed { if(direction==0 || direction==2) direction = 3; } } public void moveIt() { int previousLocationX = snakeBody.getX(); int previousLocationY = snakeBody.getY(); snakeBody.move(5); if(snakeBody.getX()!=previousLocationX || snakeBody.getY()!=previousLocationY) { for(int i=1; i<snakeBody.length; i++) { previousLocationX = snakeBody.getX(); previousLocationY = snakeBody.getY(); snakeBody.move(5); } } } /** * This method will update the length of the Snake. */ private void growSnake(int x, int y, int rotation) { Snake s = new Snake(); Snake oldSnake = new Snake; for(int i=0; i<snakeBody.length; i++) { oldSnake = snakeBody; } snakeBody = new Snake; for(int i=0; i<snakeBody.length-1; i++) { snakeBody = oldSnake; } snakeBody = s; addObject(snakeBody, x, y); // adds new snake body actor } public void act() { checkKey(); moveIt(); } } why cant I use the keyboard to move my snake?
danpost danpost

2012/11/17

#
For one thing, your 'moveIt" method is totally messed up (I am not even sure what you were trying to do here); and you are not even using the value of the 'direction' variable to move the head of the snake. Another thing is that I see now you are wanting to move five pixels at a time instead of 20, like in your earlier posts. Did you scale down the images to compensate or are you not wanting each segment to take over the same location that the one in front of it was in? That would certainly 'throw a wrench into the works', if you know what I mean.
actinium actinium

2012/11/17

#
heres a link Why dont you have the snake head as one class. You can then create a trail class that adds onto the snake head class, this will make it easier to remove or add trail bits. Check out this comet like object.
Tezuka Tezuka

2012/11/17

#
Danpost this a new scenarie . I wanted to have all my code in the world class instead of the snake class.
Tezuka Tezuka

2012/11/17

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

/**
 * Write a description of class Playground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Playground extends World
{
    Snake[] snakeBody;
    String segment;
    int direction;
    private int speed;
    private int speedX;
    private int speedY;
    boolean eaten;
    /**
     * Constructor for objects of class Playground.
     * 
     */
    public Playground()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        direction = 0;
        eaten = false;
        speed = 2;
        speedX = speed;
        speedY = 0;
        
        snakeBody = new Snake[3];
         for(int i=0; i<snakeBody.length; i++)
        {
            snakeBody[i] = new Snake();
            addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world
        }
        
       
    }

    public void checkKey()
    {
        if(Greenfoot.isKeyDown("right")) // checks if right key is pressed
        {
            if(direction==1 || direction==3)
            { direction = 0;
                speedX = speed;
                speedY = 0;
            }
        }
        else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed
        {
            if(direction==0 || direction==2)
            {direction = 1;
                speedX = 0;
                speedY = speed;
            }
        }
        else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed
        {
            if(direction==1 || direction==3)
            {direction = 2;
                speedX = -speed;
                speedY = 0;
            }
        }
        else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed
        {
            if(direction==0 || direction==2)
            { direction = 3;
                speedX = 0;
                speedY = -speed;
            }
        }
    }

    public void moveIt()
    {
        int previousLocationX = snakeBody[0].getX();
        int previousLocationY = snakeBody[0].getY();
        snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY);
        if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY)
        {
            for(int i=0; i<snakeBody.length; i++)
            {
                previousLocationX = snakeBody[i].getX();
                previousLocationY = snakeBody[i].getY();
                snakeBody[i].setLocation(snakeBody[i].getX()+speedX, snakeBody[i].getY()+speedY);
            }
        }
    }   

    /**
     * This method will update the length of the Snake.
     */
    private void growSnake(int x, int y, int rotation)
    {
        Snake s = new Snake();
        Snake oldSnake[] = new Snake[snakeBody.length];
        for(int i=0; i<snakeBody.length; i++)
        {
            oldSnake[i] = snakeBody[i];
        }
        snakeBody = new Snake[snakeBody.length+1];
        for(int i=0; i<snakeBody.length-1; i++)
        {
            snakeBody[i] = oldSnake[i];
        }
        snakeBody[snakeBody.length-1] = s;       
        addObject(snakeBody[snakeBody.length-1], x, y); // adds new snake body actor
    }

    public void act()
    {
        checkKey();
        moveIt();
       
    }
}
now i could make it move but the head is moving faster than the body
Tezuka Tezuka

2012/11/17

#
nvm I got it. But the the snake body doesnt follow the head if I pressed down the entire body goes down.
danpost danpost

2012/11/17

#
'speedX' and 'speedY' are to be used for the 'head' of the snake only. The segments must play follow the leader somehow. Maybe use 'turnTowards' followed by 'move' (that is, if the location of one segment does move to the exact location of where the segment in front of it was).
Tezuka Tezuka

2012/11/17

#
can I make 2 new variable for the speed of the segment?
Tezuka Tezuka

2012/11/17

#
 public void moveIt()
    {
        int previousLocationX = snakeBody[0].getX();
        int previousLocationY = snakeBody[0].getY();

        snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY);
        if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY)
        {
            for(int i=1; i<snakeBody.length; i++)
            {
                previousLocationX = snakeBody[i].getX();
                previousLocationY = snakeBody[i].getY();
                snakeBody[i].setLocation(snakeBody[i].getX(), snakeBody[i].getY());
            }
        }
    }   
what can I do so the segment follow the snake?
Tezuka Tezuka

2012/11/17

#
How can I make a statement for this if I only want it to apply to the head? for( int i = length -1; i > 0; i--) { playground.snake.setLocation(playground.snake.getX(),playground.snake.getY()); }
danpost danpost

2012/11/17

#
I think you are not keeping things simple and straight in your mind. The move code for the 'head' will be determined by user keystrokes, which determine the 'direction' value. A segments movement is determined by the segment in front of it. If the code is in the world class (which is only called once every act cycle), then check the 'head' movement; and if the 'head' can move, then move the body (advancing each toward or to the segment in front of it) and then move the 'head' wherever 'direction' takes it. If the code is in the snake class (which is called once for each segment of the snake), then you must put a condition that only if the act is executing for the head do we try to move the snake.
Tezuka Tezuka

2012/11/17

#
When iam suppose to do for the segment to follow my head? What do I have to do in the move methode?
danpost danpost

2012/11/17

#
For your 'for' statement in the 'moveIt' method above (lines 9 through 14) use the following:
for (int i = 1; i < snakeBody.length(); i++)
{
    int holdX = snakeBody[i].getX();
    int holdY = snakeBody[i].getY();
    snakeBody[i].setLocation(previousLocationX, previousLocationY);
    previousLocationX = holdX;
    previousLocationY = holdY;
}
Post back on results of using this.
Tezuka Tezuka

2012/11/17

#
the segment jumps to the heads location
There are more replies on the next page.
1
2
3
4
5
6
7