# Snake game steering with Java FX

I am currently trying to make a snake game with Java FX, I read several posts about it and I understand on theory how to move the snake body (for each part of the snake reference the previous one and move it to its former location) but I am facing some issues, my snake is not able to turn around properly:

I would just like it to react as a normal snake, when the head steers into a direction then the other body parts follow one by one.

In my case I created a vector class to store positions and velocity (I use velocity to be able to move all the part at the same speed and therefore be able to keep the 3 pixel gap between them).

I usually trying my best to resolve issues but this time I've spent almost 3 hours knowing the theory and groping for a proper way to do it but without any improvements.

I tested different approaches but this one seems to be the clearest and the nearest to the solution to me:

I got 2 simple classes that are Vector (only holding reference to X and Y) and Block that holds references such as Position, Velocity for the current motion, nextVelocity to anticipate steering, Previous for the previous block in the snake body.

I initialize my snake like that :

    public Snake(){
for(int i = 0; i < 5; i++){
if(i == 0){
}else{
body.add(i, new Block(new Vector(body.get(i-1).getPosition().getX() - 18, body.get(i-1).getPosition().getY())));
body.get(i).setPrevious(body.get(i-1));
}
}
}


Then there are my steer() method for the head as well as my follow() method for the other body parts.

I used the variable nextVelocity that stores the velocity of the previous part if this one is different from the current one, in this case nextVelocity is passed to velocity at the next frame to avoid that the whole body moves at the same time in the direction taken by the head (which apparently doesn't work):

public void steer(Vector velocity){
this.velocity = velocity;
this.shape.relocate(this.getPosition().getX(), this.getPosition().getY());
}

public void follow(Block previous){
if(this.velocity == null) {
this.velocity = new Vector(previous.velocity);
}else if(this.nextVelocity != null) {
this.velocity = new Vector(this.nextVelocity);
this.nextVelocity = null;
}else if(this.previous.velocity != this.velocity){
this.nextVelocity = new Vector(this.previous.velocity);
}
this.shape.relocate(this.getPosition().getX(), this.getPosition().getY());
}


And finally my move() method handling the whole snake moves:

public void move(Vector direction){
for(int i = 0; i < body.size(); i++){
if(i == 0){
}else{
body.get(i).follow(body.get(i-1));
}
}
}


If anyone could point me into the right direction or just let me know where I am mistaking it would be amazing.

• We need to know what this code actually results in, and what you want to happen that is different from the results, to help solve the problem. You can edit your question to include these details. Jun 5 '19 at 0:11
• Sorry I am not used to post on forums. Post edited, I hope it makes sense Jun 5 '19 at 13:54
• Try looping from the tail forward, making each block move to the previous blocks location, before moving it on to the next. When you get to the head you then apply the new direction or the next move. If you're implementing speeding up, you'll need to handle that, but this would ensure that the snakes body always follows the path laid out by the head. Jun 10 '19 at 18:16
• Welcome to the site... this is not a forum. Jun 13 '19 at 6:00