We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am currently coding a basic snake game in processing and I want the snake not to move in the opposite direction it is currently moving.
The snake just stops whenever I press the opposite direction but i want the game to ignore the keypress.
if (keyCode == DOWN && !direction.equals("up")){
if (frameCount % framespeed == 0){
ypos += speed;
}
direction = "down";
}
if (keyCode == UP && !direction.equals("down")){
if (frameCount % framespeed == 0){
ypos -= speed;
}
direction = "up";
}
if (keyCode == RIGHT && !direction.equals("left")){
if (frameCount % framespeed == 0){
xpos += speed;
}
direction = "right";
}
if (keyCode == LEFT && !direction.equals("right")){
if (frameCount % framespeed == 0){
xpos -= speed;
}
direction = "left";
}
Answers
These lines are inside the if construct but should be outside of it
To achieve this have speedX and speedY and use it throughout
Set one to 0 and the other to 1 or -1
Chrisir