Python Turtle Games

Make games with Python's built-in turtle module: draw shapes, move turtles, handle keys with onkey, run a game loop with ontimer, and speed it up with tracer.

turtle comes with Python, so you can make graphical games without installing anything. A turtle is a little pen you move around the screen; games use several turtles as the player, the ball, the score writer and so on.

example 1 ยท press Run, then edit and run again

The commands games use most

Keyboard and timers (on your computer)

Interactive turtle games use screen.onkey(function, "Up") to react to keys and screen.ontimer(function, 50) to run the next frame. Add screen.tracer(0) and screen.update() to stop flicker. See Snake and Paddle Ball for complete examples.

screen = turtle.Screen()
screen.tracer(0)
screen.listen()
screen.onkey(lambda: player.left(90), "Left")

def frame():
    player.forward(5)
    screen.update()
    screen.ontimer(frame, 30)

frame()
screen.mainloop()

๐Ÿ’ก On this site, turtle drawings appear below the code after it runs. Keyboard-controlled turtle games need a real window, so run those on your computer.

Games that use Turtle graphics

Practise this with 3 of the 20 games:

Frequently asked questions

Is turtle good for making games?

Yes, for small 2D games like Snake, Pong-style games and races. It ships with Python and is easy to read. For bigger games, pygame is faster.

Why does my turtle window close immediately?

Add turtle.done() or screen.mainloop() at the end of the program to keep the window open.

More Python concepts