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.
The commands games use most
forward(),left(),right(),goto(x, y),setheading()penup()/pendown()to move without drawingxcor(),ycor(),distance()for positions and collisionsshape("turtle"),color(),shapesize()for lookswrite()for scores and messages
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:
- #03โโโ
Snake Game in Python (Turtle)
Learn Turtle graphics, Lists and Functions.
- #06โโโ
Pong-Style Paddle Game in Python
Learn Turtle graphics, Functions and if, elif and else.
- #17โโโ
Turtle Race in Python
Learn Turtle graphics, Lists and for loops.
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.