Snake Game Controls not working

edited January 2018 in Questions about Code

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"; }

Tagged:

Answers

  • These lines are inside the if construct but should be outside of it

    if (frameCount % framespeed == 0){
    ypos += speed; 
    }
    

    To achieve this have speedX and speedY and use it throughout

    Set one to 0 and the other to 1 or -1

    Chrisir

Sign In or Register to comment.