🎓 A1Learner
🔍
0 of 18 lessons completed

Python Snake Keyboard Controls

Keyboard controls connect the arrow keys to the snake's direction. Each arrow key runs a small function, and that function changes the direction the head steps in.

Turning up

go_up is one of four steering functions, one for each arrow key. It sets head.direction to "up", and move() steps the head that way on the next pass.

def go_up():
    if head.direction != "down":
        head.direction = "up"

Turning left

go_left sets the direction to "left", with one guard in front of the change. The guard refuses the turn while head.direction is "right".

def go_left():
    if head.direction != "right":
        head.direction = "left"

Listening for keys

This code sits below the go functions, before the loop starts. listen() comes first, and the onkey calls come after it.

screen.listen()
screen.onkey(go_up, "Up")

Binding the other three keys

Each onkey call names one steering function and one key. The four calls cover all four arrow keys.

screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
screen.onkey(go_right, "Right")

Example

import turtle
import time

# The game window
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake")
screen.tracer(0)

# The snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "right"


def move():
    if head.direction == "up":
        head.sety(head.ycor() + 20)
    if head.direction == "down":
        head.sety(head.ycor() - 20)
    if head.direction == "left":
        head.setx(head.xcor() - 20)
    if head.direction == "right":
        head.setx(head.xcor() + 20)


def go_up():
    if head.direction != "down":
        head.direction = "up"


def go_down():
    if head.direction != "up":
        head.direction = "down"


def go_left():
    if head.direction != "right":
        head.direction = "left"


def go_right():
    if head.direction != "left":
        head.direction = "right"


# The keyboard steers the snake
screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
screen.onkey(go_right, "Right")

# The game loop: redraw, move, wait, repeat
while True:
    screen.update()
    move()
    time.sleep(0.1)

Key points

  • Use screen.listen() to make the window listen for key presses
  • Use screen.onkey to bind a steering function to an arrow key
  • The key names are "Up", "Down", "Left" and "Right"
  • Each go function sets head.direction to its own direction name
  • A go function refuses only the turn that points straight back
  • Turning straight back would run the head into its own neck

In this example, the program builds the same window and head as before, and the head starts with direction "right". The move function steps the head one square in its current direction on every pass of the loop. The four go functions wait beside the loop, and each one holds one direction change and one refusal. screen.listen() switches key listening on, and the four onkey calls bind the arrow keys to those functions. The loop itself never reads the keyboard. A key press runs its function between loop passes, and the next move steps the head the new way. Pressing Left at the start changes nothing, because the snake already moves right and go_left refuses the turn back.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

The snake is moving down and the player presses the Up arrow key. What happens?