This page is part of the Snake in MIT Scratch Tutorial.
Using the Arrow Keys to Control the Snake
During the game loop the system checks to see if the user pressed an arrow key and if that input would cause the snake to change direction.
If such an input is detected then the snake will immediately move in that direction and the movement timer will be reset.
The snake can only turn at right angles so when it is traveling up or down it can turn left or right and when it is traveling left or right it can turn up or down.
The snake cannot turn back on itself so pressing an arrow key opposite to the direction the snake is traveling will have no effect.
The snake will also ignore arrow presses in the same direction that is already traveling. If the player wants the snake to move immediately they must change direction.
Handling Input in Scratch
The Scratch function that processes the user’s input can be seen here:
The function contains some nested if statements that are used to ensure the key pressed will cause the snake to change direction.
The first if statement determines if the snake is traveling horizontally or vertically. After this another if statement checks the appropriate arrow keys.
If the if statements evaluate as true then the system uses the following function to make the snake change direction:
The function sets the Snake Direction variable to the new direction. When the snake is moved it will use this value when deciding where to go.
The Direction Changed variable is set to 1. This is done so that the main game loop will know that the snake’s direction has changed and will skip the move wait timer. The Direction Changed variable is set back to zero after the snake is moved.
The Direction values as used to represent the four directions the snake can move. The values are set as part of the Initialize Game Data function:
The four directions are assigned the values 1, 2, 3, and 4. This programming practice is called enumeration. It is a convenient way to name values.
The Handle Input function is called at the beginning of the game loop:
If input caused the snake to change direction then the Direction Changed variable will have a value of 1. This is checked in the Update Snake function:
After calling Handle Input a check to see if the Snake Move Wait time has elapsed or if the Direction Changed variable has a value of 1.
At the very end of the game loop the Direction Changed variable is set back to zero:
The input has been processed and so the value with be zero until another input is detected.
At the start of game loop the Handle Input function is called. The function checks keyboard input and sets the Direction Changed value to 1 if the input should cause the snake to change direction.
The input is checked using if statements so that the snake is allowed to turn at right angles.
If the Direction Changed value is 1 then the next snake move will be performed even if the Snake Move Wait time has not passed.
Next Section: Calculating the Snake’s Next Position