Python's turtle module is a great tool to learn coding by writing animations and games. If your aim is to learn to code, using this module is preferable to using more complex modules. This way, you can focus on the aspects of coding that really matter.
In this tutorial, you'll learn how to make your Turtle move when you press a key on the keyboard. This allows you to make your program interactive. Whether you want to write an animation that needs some input from the user, or you're writing a game, you'll need to be able to link certain actions to certain keypresses.
I will assume you know the basics of how to use the turtle module. You should be familiar with creating a Turtle and moving it around using forward, left and right, for example.
You can use your preferred Python setup and editor, or if you wish, you can use this web-based Python editor.
Note: if using the web-based editor, you will need to replace the function onkeypress in the code below with onkey
Setting the scene
You can start by creating a Turtle and customising the way it looks and its colour. You can even change the background colour:
import turtle window = turtle.Screen() window.bgcolor('dark salmon') player = turtle.Turtle() player.shape('turtle') player.color('turquoise') player.penup() turtle.done()
You've created a Screen and a Turtle, named them and customised them.
Defining a Function to Turn the Turtle
You now need to create a new function that turns your player left by a certain number of degrees. You can also make the turtle change colour whenever it turns left. You can modify the code above to add this function. Remember that any code you run needs to come before the turtle.done() line.
import turtle window = turtle.Screen() window.bgcolor('dark salmon') player = turtle.Turtle() player.shape('turtle') player.color('turquoise') player.penup() def turn_left(): player.color('light green') player.left(10) turtle.done()
The function definition you have just added is just that, a definition. The code within this function will not run unless the function is called.
You could call the function within the code by writing turn_left(). This will make the player change colour and turn left by 10 degrees, but it will happen once when the program runs the line that has turn_left() on it.
Creating a Key Binding
You want to be able to call the function turn_left() whenever a certain key on the keyboard is pressed, let's say the left arrow key.
To do this, you'll need to bind the function turn_left() to the left arrow key on your keyboard. You can create this key binding using the onkeypress() function in the turtle module. This is a function, or more accurately a method, that belongs to the Screen() object.
Note: if using a web-based platform, you may need to replace onkeypress with onkey.
import turtle window = turtle.Screen() window.bgcolor('dark salmon') player = turtle.Turtle() player.shape('turtle') player.color('turquoise') player.penup() def turn_left(): player.color('light green') player.left(10) window.onkeypress(turn_left, "Left") window.listen() turtle.done()
The call to window.onkeypress() needs two arguments which we put inside the brackets. The first is the name of the function we want to call. You can see that since you only need the name of the function here, you put in turn_left without the () that we often put after a function when we want it to run. In this case, you're simply telling your program which function you want it to call when a key is pressed, but you don't want to function to run just yet.
The second argument is a string that represents the key on the keyboard that you wish to use. Each key has a label that is unique to it. The left arrow key on your keyboard is referred to by the string "Left", with an uppercase L. If you wanted to use the key for the letter a, for example, then you would use the string "a".
You also need to tell your program to pay attention for any key presses. This is achieved using the window.listen() command.
When you run programs that have a key binding such as this one, on some computers, you will need to first click on the window where the animation or game is running to make that window active before you can use the keys on your keyboard to control the Turtle.
You can now repeat the process to make the player turn right when the right arrow key is pressed. Have a go at writing the code yourself before you read on.
Have you managed to make the Turtle turn left too?
The final step to complete this animation is to make the turtle move forward continuously. You can do this using a while loop:
while True: player.forward(0.5)
You may notice that the animation is a bit sluggish when you try to turn left and right repeatedly and quickly. You can fix this by taking control of when the drawing updates itself. To do this, you have to use a pair of commands. You first set the tracer() to a value of 0. This tells the turtle module not to display any of the drawings on screen, rather than showing every step the turtle moves.
We then need to update() the display every time the Turtle moves. This happens in the while loop. Here's the finished code:
import turtle # Create and customise the screen window = turtle.Screen() window.bgcolor('dark salmon') window.tracer(0) # Create and customise the turtle player = turtle.Turtle() player.shape('turtle') player.color('turquoise') player.penup() # Define the functions to make the turtle turn by the required angle def turn_left(): player.color('light green') player.left(10) def turn_right(): player.color('light blue') player.right(10) # Create the key bindings window.onkeypress(turn_left, "Left") window.onkeypress(turn_right, "Right") window.listen() # Main loop which makes the turtle move forward contiunously, forever while True: player.forward(0.5) window.update() # turtle.done() This line is no longer needed now there's a while True loop
You have now learned how to control the Turtle you create using the turtle module with the keyboard.
Have fun creating great animations and games using turtle in Python.