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

2021/6/17

I need help with a snake game

Pimkimi Pimkimi

2021/6/17

#
Hi, I have a problem. I've been trying do make a snake but I don't know how to make all the tails follow the previous ones and the head. Can someone help me please ? Thx
danpost danpost

2021/6/18

#
Pimkimi wrote...
Hi, I have a problem. I've been trying do make a snake but I don't know how to make all the tails follow the previous ones and the head. Can someone help me please ? Thx
What have you tried?
Pimkimi Pimkimi

2021/6/18

#
I wanted the location of the head to be remembered before it moved and then the tail to move to those coordinates. Then I wanted to make the rest of the tail follow the first one, being of the same class. Sorry about my English, it's not my native language. This is the SnakeHead code :
public SnakeHead()
    {
       
        memorize = true;
    }

public void act() 
    {
        setDirection();
        setRotation(MyWorld.direction);
        move(MyWorld.speed);
        eatApple();
        wichCoordinate();
}

public void wichCoordinate()
    {
        if (memorize)
        {
            x1 = getX();
            y1 = getY();
            memorize = false;
        }
        else
        {
            x2 = getX();
            y2 = getY();
            memorize = true;
        }
    }
And this is the Snake (tail) code :
public Snake()
    {
        setRotation(MyWorld.direction);
    }
    
    
    public void act() 
    {
        settingLocation();
        checkSnake();
        setRotation(MyWorld.direction);
        move(MyWorld.speed);
    }
    
    /**
     * Set the Location depending on the value memorized previously
     */
    public void settingLocation()
    {
        if(SnakeHead.memorize)
        {
            setLocation(SnakeHead.x2, SnakeHead.y2);
        }
        else
        {
            setLocation(SnakeHead.x1, SnakeHead.y1);
        }
    }
    
    /**
     * If the tail is touching itself or the head, game over
     */
    public void checkSnake()
    {
        if (isTouching(Snake.class))
        {
            MyWorld world = (MyWorld)getWorld();
            world.gameOver();
        }
        else if (isTouching(SnakeHead.class))
        {
            MyWorld world = (MyWorld)getWorld();
            world.gameOver();
        }
    }
I'm currently making the first tail follow the head so there are probably still errors in the code. However, I don't know how to make the other tails follow the one in front of it.
Pimkimi Pimkimi

2021/6/18

#
if you need the entire code, just ask, i will give it with pleasure
danpost danpost

2021/6/18

#
Pimkimi wrote...
if you need the entire code, just ask, i will give it with pleasure
To have entire snake move as a unit, best would be to control all parts from the head. I have several snake demos in my Demos by danpost collection. You may want to check them out.
Pimkimi Pimkimi

2021/6/18

#
danpost wrote...
Pimkimi wrote...
if you need the entire code, just ask, i will give it with pleasure
To have entire snake move as a unit, best would be to control all parts from the head. I have several snake demos in my Demos by danpost collection. You may want to check them out.
Being a beginner, I don't understand all your code. I don't know how to apply it in my code with the values and classes I have. But thanks anyway! And thank you for your previous quick answers, I appreciate it
You need to login to post a reply.