Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Turtle Module Tutorial 2
1st part link

Welcome, this is part 2 of the turtle module - 1st part =

What will we be learning?

Since you know how to draw a shape, and make a turtle, we will cover how to control the turtle with W, A and D, and we will also add bots in the game.

What you need to know

You need to know how turtle works a bit, the basics, go to part 1 if you don't know, you need to know how the random module works, define function, but if you don't know, I will try to be clear on what those mean.


Of course we need to import turtle, but not only that, we need to import random, random is for the bots so they go randomly.

Since you know how turtle works, I will skip to what you should know

What you should know

If you don't understand some of this, go back to the previous tutorial.

Making the turtle move

In order to make the turtle move, we need to use functions.
first lets make the functions.

If you don't understand this, it means if the forward function gets called, the turtle/player will move forward 10 pixels, if the left function gets called, they will turn 45 degrees left.

How to add keybinds

Now how do we call the function?
We need to use the window function/turtle.Screen()/wn
also you need to do "turtle.listen()" what that does is that turtle listens for the keybinds, you do
We do

That means by pressing the key W will call the function forward, so the turtle will move forward 10 pixels, now lets finish it with left and right

What your code should look like now

Now run it and press the W, A and D keys

Adding Bots

I think you know how to add more turtles, first let create a turtle that will be the bot

You don't need to put the turtle at the position, also goto means they will literally go to that position of x and y, we put penup because so they don't draw when they move, remove penup and look at what I mean, make sure you put penup before they move.

Making the 2nd bot

What your code should look like

Making the Bots move randomly

We need to make bots move randomly in a loop, which means we will need to use the while loop, but if we do, it won't go back to the keybinds and won't respond, so we need to move the keybinds to while True:
copy this in your programme:

then add a while True:

ok adding on the while True:
we need to use the random function, you should know how it works

What that does is it picks a random number from the first to the last, and we will make the bot go forward that many random number.
Now lets make for the 2nd bot

Make sure you put that in the while loop
Now we need to make them move

Now we do it for the 2nd bot

Make sure it is in the while loop

How it should look like


Thanks for reading, I know the bots can go out of the border, but I will cover that in the next tutorial, please do like if it helped, if you like or comment something nice, it will encourage me to do more.


mine is not moving when i push w, a, d


what is use of penup ?


@pmoulalimoin if you don't use penup(), then if your turtle moves, it will draw lines