Can anyone explain why this doesn't work?

Hey! I’m new to java and processing. I decided to follow a snake game tutorial for processing on youtube. After making it, I added some stuff myself and I also want to add a pause screen. So wrote thes to pause the game with tab:

void keyPressed() {
  if (keyCode == TAB) pause=true;
  if (pause == true) {
    if (keyCode == TAB) pause=false; 

The first line works by itself. I added the other two lines to be able to close the pause menu with tab but it doesn’t. They also cancel out the first line and it doesn’t pause when clicked tab.
Can someone explain why this is not working?


void keyPressed() {
  if(key == TAB)
    pause = !pause;

I think it should help.

My approach in these cases would be:

if (keyCode == TAB) pause = !pause;

To toggle the current state.


That worked! Thank you so much.

1 Like

It worked thanks! I also have another quick question:
The snake moves with arrow keys but in the current code, you can press the opposite key of its direction; making it colide with himself which ends the game (so for example you can press down while the snake is going up, making it trying to go down and game is over).
So I typed these lines to prevent that for the down arrow ( I would copy paste and edit this for the other keys if it worked) but it doesn’t work:

void keyPressed() {
  newdir=keyCode == DOWN? 0:(keyCode== UP? 1:(keyCode== RIGHT? 2:(keyCode== LEFT? 3:-1)));
  if (newdir != -1) direction = newdir;
// these lines in the bottom
  if(newdir == 1)
    newdir=keyCode == DOWN? 1:(keyCode== UP? 1:(keyCode== RIGHT? 2:(keyCode== LEFT? 3:-1)));

what can I do to make it work?

Welcome, I’m glad to help)

It is better to keep one question per post so each question gets the proper deserved attention.

The code you provided above is a bit hard to debug. It is better if you provide a minimum example demonstrating how the code works. Also, writing all in the same line makes it hard to read. I suggest you always aim to write easier to read code than compressed code.

This is avoided in the snake game by only implementing the side (left and right ) keys. If you are moving vertically, you can go right or left. If you are moving horizontally, you would go up or down, again, by only pressing the left or right keys. Next I assume you are comfortable with geometry and angles. One suggestion is to use an angle to describe direction. Say you start with an angle of 0 moving from right to left. Pressing the left arrow key will increase the angle by 90 making the direction up. If you press the right arrow key, you will subtract 90 making the direction “negative 90” meaning your snake moves down. Adding 90 or -90 allows you to control the direction of the snake. I find this easier instead of trying to implement all the arrow combinations. The possible angles you would get are all multiples of 90 keeping in mind we are working with degree units. To manage direction, you can use a switch statement: 0 to go left to right, +90 to go from bottom to top, -90 to go opposite and 180 or -180 to go from right to left. Angle larger that 180 should be mapped back to the range of 0 to 180.


oh my bad I forgot to explain what I was trying to do. I’m trying to cancel out the down arrow when the direction is up so it does nothing. I understood what you said tho, I’ll look at it.