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

2012/11/10

Move the snake

2
3
4
5
6
7
danpost danpost

2012/11/17

#
Did you move the 'head' of the snake before the 'for' loop?
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]; // initialization of the Snake
        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=1; i<snakeBody.length; i++)
            {
               int holdX = snakeBody[i].getX();
               int holdY = snakeBody[i].getY();
               
               snakeBody[i].setLocation(previousLocationX, previousLocationY);
               previousLocationX = holdX; 
               previousLocationY = holdY; 
            }
        }
    }   

    /**
     * This method will update the length of the Snake.
     */
    private void growSnake(int x, int y)
    {
        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();
    }
}
the head is moving and the segment is moving but when i pressed act the segment jumps straigth to the heads position
danpost danpost

2012/11/17

#
Lets us simplify the code (make it a bit easier to understand) for the 'moveIt' method (as far as moving the 'head' segment:
private void moveIt()
{
    // save the location of the 'head' segment (segment in front)
    // these are the 'goto' values for the next segment
    int prevX = snakeBody[0].getX();
    int prevY = snakeBody[0].getY();
    // get the direction of movement for the 'head' along horizontal and vertical (0, 1, or -1)
    int dx = (1 - direction) * ((direction + 1)%2);
    int dy = (2 - direction) * (direction%2);
    // get tentative new coordinates for the 'head'
    int newX = prevX + speed * dx;
    int newY = prevY + speed * dy;
    // if  'head' cannot move, exit method
    if (newX < 0 || newX > getWidth() - 1) return;
    if (newY < 0 || newY > getHeight() - 1) return;
    // move head
    snakeBody[0].setLocation(newX, newY);
    // move body segments of snake
    for (int i = 1; i < snakeBody.length(); i++)
    {
        // hold segments location
        int holdX = snakeBody[i].getX();
        int holdY = snakeBody[i].getY();
        // move segment
        snakeBody[i].setLocation(prevX, prevY);
        // set 'goto' values for next segment
        prevX = holdX;
        prevY = holdY;
    }
}
Tezuka Tezuka

2012/11/17

#
I copied ur code still the same
danpost danpost

2012/11/17

#
Change your value for 'speed' to something like '20' and see what happens.
Tezuka Tezuka

2012/11/17

#
It worked with the last method if I only change it to 20 , dont know why though
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;
    Food food;
    Counter score;
    private int direction;
    private int speed;
    private int speedX;
    private int speedY;
    private 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 = 20;
        speedX = speed;
        speedY = 0;

        snakeBody = new Snake[3]; // initialization of the Snake
        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
        }

        score = new Counter("Score : "); // initialization of the score
        addObject(score, 19*15, 8); // adding score 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()
    {
    // save the location of the 'head' segment (segment in front)
    // these are the 'goto' values for the next segment 
    int prevX = snakeBody[0].getX();  
    int prevY = snakeBody[0].getY();  
    // get the direction of movement for the 'head' along horizontal and vertical (0, 1, or -1) 
    int dx = (1 - direction) * ((direction + 1)%2); 
    int dy = (2 - direction) * (direction%2);  
    // get tentative new coordinates for the 'head'    
    int newX = prevX + speed * dx;
    int newY = prevY + speed * dy;
    // if  'head' cannot move, exit method  
    if (newX < 0 || newX > getWidth() - 1) return;
    if (newY < 0 || newY > getHeight() - 1) return;
    // move head  
    snakeBody[0].setLocation(newX, newY);  
    // move body segments of snake 
    for (int i = 1; i < snakeBody.length; i++) 
    { 
    int holdX = snakeBody[i].getX(); 
    int holdY = snakeBody[i].getY();  
    // move segment
    snakeBody[i].setLocation(prevX, prevY);
    // set 'goto' values for next segment  
    prevX = holdX;  
    prevY = holdY;  
    }
    }  
     */

    public void moveIt()
    { 
        // save the location of the 'head' segment (segment in front)
        // these are the 'goto' values for the next segment
        int previousLocationX = snakeBody[0].getX();
        int previousLocationY = snakeBody[0].getY();
        snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY);

        // move body segments of snake 
        if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY)
        { for(int i=1; i<snakeBody.length; i++)
            {   int holdX = snakeBody[i].getX();
                int holdY = snakeBody[i].getY();
                snakeBody[i].setLocation(previousLocationX, previousLocationY); 
                // set 'goto' values for next segment
                previousLocationX = holdX;
                previousLocationY = holdY; 
            }  

          if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food
            {
                growSnake(previousLocationX, previousLocationY);
                updateFoodLocation();
                score.add(5);
            }
        } 
        else
        {
            Greenfoot.stop();
        }
    }   

    /**
     * This method will update the length of the Snake.
     */
    private void growSnake(int x, int y)
    {
        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 updateFoodLocation()
    {
        int x = 0;
        int y = 0;
        boolean overlap = true;
        eaten = false;

        while(overlap)
        {
            x = Greenfoot.getRandomNumber(getWidth());
            y = Greenfoot.getRandomNumber(getHeight());

            for( int i = 0; i < snakeBody.length; i++)
            {
                // Condition to check food will not touch the snake
                if(x!=snakeBody[i].getX() || y!= snakeBody[i].getY())
                {
                    overlap = false;
                    break;
                }
            }
        }
    }

    public void act()
    {
        checkKey();
        moveIt();
    }

}
I tried to add food but get a null exeption on moveIt method in act
Tezuka Tezuka

2012/11/17

#
nvm i figured it out thx for help anyway
Tezuka Tezuka

2012/11/17

#
Why doesnt my snake it the food? help pls
danpost danpost

2012/11/17

#
Where is the code that detect the 'head' of the snake intersecting the food object?
Tezuka Tezuka

2012/11/17

#
    public void moveIt()
    { 
        // save the location of the 'head' segment (segment in front)
        // these are the 'goto' values for the next segment
        int previousLocationX = snakeBody[0].getX();
        int previousLocationY = snakeBody[0].getY();
        snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY);

        // move body segments of snake 
        if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY)
        {
            for(int i=1; i<snakeBody.length; i++)
            {   int holdX = snakeBody[i].getX();
                int holdY = snakeBody[i].getY();
                snakeBody[i].setLocation(previousLocationX, previousLocationY); 
                // set 'goto' values for next segment
                previousLocationX = holdX;
                previousLocationY = holdY; 
            }  

            if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food
            {
                growSnake(previousLocationX, previousLocationY);
                updateFoodLocation();
                score.add(5);
            }

            for(int i=1; i<snakeBody.length; i++) // Checks if the snake eats itself
            {
                if(snakeBody[0].getX()== snakeBody[i].getX() && snakeBody[0].getY()== snakeBody[i].getY())
                {
                    Greenfoot.stop();
                }
            }
        } 
        else
        {
            Greenfoot.stop();
        }
    }  
Soryy here it is
danpost danpost

2012/11/17

#
Where is your 'updateFoodLocation' method? Also, lines 28 through 34 above (in the 'moveIt' method) show you checking the 'head' location with each 'body' location. Easier would be (since any food would already be gone):
// replace all code starting at line 28 with
        if (getObjectsAt(snakeBody[0].getX(), snakeBody[0].getY(), null).size() == 1) return;
    }
    Greenfoot.stop();
}
The 'if' statement must count the 'head' as one object found there.
danpost danpost

2012/11/17

#
NVM, I see it above.
Tezuka Tezuka

2012/11/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * 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;
    Food food;
    Counter score;
    private int direction;
    private int speed;
    private int speedX;
    private int speedY;
    private 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 = 20;
        speedX = speed;
        speedY = 0;

        snakeBody = new Snake[3]; // initialization of the Snake
        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
       }

        score = new Counter("Score : "); // initialization of the score
        addObject(score, 19*15, 8); // adding score actor to the world

        food = new Food(); // initialization of the food
        addObject(food, 100, 100); // adding food 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()
    {
    // save the location of the 'head' segment (segment in front)
    // these are the 'goto' values for the next segment 
    int prevX = snakeBody[0].getX();  
    int prevY = snakeBody[0].getY();  
    // get the direction of movement for the 'head' along horizontal and vertical (0, 1, or -1) 
    int dx = (1 - direction) * ((direction + 1)%2); 
    int dy = (2 - direction) * (direction%2);  
    // get tentative new coordinates for the 'head'    
    int newX = prevX + speed * dx;
    int newY = prevY + speed * dy;
    // if  'head' cannot move, exit method  
    if (newX < 0 || newX > getWidth() - 1) return;
    if (newY < 0 || newY > getHeight() - 1) return;
    // move head  
    snakeBody[0].setLocation(newX, newY);  
    // move body segments of snake 
    for (int i = 1; i < snakeBody.length; i++) 
    { 
    int holdX = snakeBody[i].getX(); 
    int holdY = snakeBody[i].getY();  
    // move segment
    snakeBody[i].setLocation(prevX, prevY);
    // set 'goto' values for next segment  
    prevX = holdX;  
    prevY = holdY;  
    }
    }  
     */

    public void moveIt()
    { 
        // save the location of the 'head' segment (segment in front)
        // these are the 'goto' values for the next segment
        int previousLocationX = snakeBody[0].getX();
        int previousLocationY = snakeBody[0].getY();
        snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY);

        // move body segments of snake 
        if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY)
        {
            for(int i=1; i<snakeBody.length; i++)
            {   int holdX = snakeBody[i].getX();
                int holdY = snakeBody[i].getY();
                snakeBody[i].setLocation(previousLocationX, previousLocationY); 
                // set 'goto' values for next segment
                previousLocationX = holdX;
                previousLocationY = holdY; 
            }  

            if(snakeBody[0].getX()==food.getX() && snakeBody[0].getY()==food.getY()) // Checks if the snake eats food
            {
                growSnake(previousLocationX, previousLocationY);
                updateFoodLocation();
                score.add(5);
            }
            
            if(eaten)
            {
                updateFoodLocation();// updates the location of the food
            }

            if( getObjectsAt(snakeBody[0].getX(),snakeBody[0].getY(), null).size() == 1) return;
            {
                Greenfoot.stop();
            }
        } 
        else
        {
            Greenfoot.stop();
        }
    }   

    /**
     * This method will update the length of the Snake.
     */
    private void growSnake(int x, int y)
    {
        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 updateFoodLocation()
    {
        int x = 0;
        int y = 0;
        boolean overlap = true;
        eaten = false;

        while(overlap)
        {
            x = Greenfoot.getRandomNumber(getWidth()/15);
            y = Greenfoot.getRandomNumber(getHeight()/15);

            for( int i = 0; i < snakeBody.length; i++)
            {
                // Condition to check food will not touch the snake
                if(x!=snakeBody[i].getX() || y!= snakeBody[i].getY())
                {
                    overlap = false;
                    break;
                }
            }
        }
        food.setLocation(x*15, y*15);
    }

    public void act()
    {
        checkKey();
        moveIt();
    }
}
It doesnt eats the food
danpost danpost

2012/11/17

#
Your snake is moving more than one pixel per frame and for the intersection of 'head' and 'food', the code is only checking the one pixel that is at the coordinates of where the 'head' is.
There are more replies on the next page.
2
3
4
5
6
7