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

2012/11/10

Move the snake

3
4
5
6
7
danpost danpost

2012/11/17

#
I am working on a new smoother 'follow-the-leader' movement code for a snake. Hope to have it up and running soon.
Tezuka Tezuka

2012/11/18

#
what should i do to increase the pixar, is there anyway so the snake eats the food?
danpost danpost

2012/11/18

#
In your 'updateFoodLocation' method, make sure that the food is placed at locations that the snake will end up at (multiples of 20). Or add a method in your Snake class returning a boolean indicating if Food intersects the 'head' called with snakeBody.eats():
public boolean eats()
{
    Food food = (Food) getOneIntersectingObject(Food.class);
    if (food != null)
    {
        getWorld().removeObject(food);
        return true;
    }
    return false;
}
That way, all the food need do is intersect the head; instead of having to be placed exactly where the head is.
Tezuka Tezuka

2012/11/18

#
I want to call this method from snake class how should i do private void growSnake(int x, int y)?
Tezuka Tezuka

2012/11/18

#
I Want everything are done in the world how could I intesecting the food?
danpost danpost

2012/11/18

#
Since all the 'intersect' or 'inRange' methods are protected in the Actor class API, your only option would be to write your own 'intersect' method(s). That would mean "for each object (of whatever class), if snakeBody.getX() is between Actor object.getX() - Actor object.getImage().getWidth()/2 and Actor object.getX() + Actor object.getImage().getWidth()/2 AND snakeBody.getY() is between ...==== return true; else, return false.
danpost danpost

2012/11/18

#
One thing I have noticed is that when you initially place your snake body segments into the world, you are placing them 15 pixels apart from each other. With that, your segments need to move at distances of 15; and your food needs to placed at multiples of 15 (between 15 and the last multiple of 15 before the length of width or height of the world). Having done that, you can just see if they are at the same location with something like the following
int newX = snakeBody[0].getX();
int newY = snakeBody[0].getY();
if (!getObjectsAt(newX, newY, Food.class).isEmpty())
{
    removeObjects(getObjectsAt(newX, newY, Food.class));
    growSnake();
}
Tezuka Tezuka

2012/11/18

#
the method growSnake have 2 parameters I cant add it, dont know how to do it
danpost danpost

2012/11/18

#
I said 'something like'. Instead of 'growSnake();' set the boolean 'eaten' to 'true' and refer to it after you move all the segments of the snake, except the last one. Before moving the last one, check 'eaten' and add a segment at that location, if neccessary. The only time you will grow the snake is during that time, so there is no need to have a seperate method for it. Include it right in the 'for' loop
if (i == snakeBody.length - 1)
{
    if (eaten)
    {
        eaten = false;
        // add the new segment at coordinates (snakeBody[i].getX(), snakeBody[i].getY())
        // add new segment to the array, etc.
    }
}
Tezuka Tezuka

2012/11/18

#
import greenfoot.*;

/**
 * This is MyWorld class in which game runs.
 * 
 * @author Chandrasekhar Thotakura
 * @version 1.0.0
 */
public class MyWorld  extends World
{
    /**
     * Defines the variables required in game.
     */
    private Snake snakeBody[];
    private Food food;
    private Counter score;
    private int direction;
    private boolean eaten;
    private int gameState;
    private int speed;
    private int speedX;
    private int speedY;
    int previousLocationX;
    int previousLocationY;

    /**
     * Constructor for objects of class MyWorld.
     */
    public MyWorld()
    {    
        super(300, 450, 1); // constructor of the super class
        direction = 0; // initialization
        eaten = true;; // initialization
        speed = 15;
        speedX = speed;
        speedY = 0;

        setBackground("bluerock.jpg");

        for (int i = 0; i < 1; i++)
        {

            int x = (Greenfoot.getRandomNumber(18) + 1) * 15 + 10;
            int y = (Greenfoot.getRandomNumber(18) + 1) * 15 + 10;
            addObject(new Food(), x, y);
        }

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

        snakeBody = new Snake[3]; // initialization of the Snake
        for(int i=0; i<snakeBody.length; i++)
        {
            snakeBody[i] = new Snake(i==0);
            addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world
        }
    }

    /**
     * Game starts from here.
     */
    public void act() 
    {

        move();
        checkKey();
    }

    /**
     * This method will update the length of the Snake.
     */
    private void growSnake(int x, int y)
    {
        Snake s = new Snake(false);
        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
    }

    /**
     * This method will changes the place where the food is displayed.
     */
    private void updateFoodLocation()
    {
        int x=0, 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 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;
            }
        }
    }

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

        // move body
        if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) // Checks if the snake hits walls
        {
            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( i == snakeBody.length-1)
                {
                    if(eaten)
                    {
                        updateFoodLocation();// updates the location of the food
                        growSnake(previousLocationX, previousLocationY);
                        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())
                {
                    die();
                }
            }
        }
        else
        {
            die();   
        }
    }

    public void die()
    {
        setBackground("GameOver.jpg");
        addObject(score, 19*15, 8);
    }
}
I tried this but get a null exeption
danpost danpost

2012/11/18

#
You need to post (copy/paste) the error trace for assistance.
Tezuka Tezuka

2012/11/18

#
sorry here it is java.lang.NullPointerException at MyWorld.updateFoodLocation(MyWorld.java:114) at MyWorld.move(MyWorld.java:179) at MyWorld.act(MyWorld.java:65) at greenfoot.core.Simulation.actWorld(Simulation.java:571) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
Tezuka Tezuka

2012/11/18

#
nvm I got it, now I just need a method to add the segment. I erased the growmethode()
Tezuka Tezuka

2012/11/18

#
Ok I GOT it now thx for all help u were greate
Tezuka Tezuka

2012/11/18

#
can u explain this for me iam not getting it 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, previousLocationX, previousLocationY); // adds new snake body actor
There are more replies on the next page.
3
4
5
6
7