Skip to content
← Back to Community
Python Turtle Module Tutorial 2
Profile icon
CodersXD
1st part link

Welcome, this is part 2 of the turtle module - 1st part =
-https://repl.it/talk/learn/Python-Turtle-Module-Tutorial-1/59446

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.

Starting

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.

#Import import turtle import random

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

What you should know

#Import import turtle import random #Setup width = 800 height = 600# wn = turtle.Screen() wn.setup(width, height) wn.bgcolor("blue") wn.title("Bots") #Player player = turtle.Turtle() player.color("green") player.shape("turtle") player.turtlesize(3, 3, 3) player.penup()

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.

def forward(): player.forward(10) def left(): player.left(45) def right(): player.right(45)

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

turtle.listen() wn.onkeypress(forward, "w")

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

turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d")

What your code should look like now

#Import import turtle import random #Setup width = 800 height = 600 wn = turtle.Screen() wn.setup(width, height) wn.bgcolor("blue") wn.title("Bots") #Player player = turtle.Turtle() player.color("green") player.shape("turtle") player.turtlesize(3, 3, 3) player.penup() #Functions def forward(): player.forward(10) def left(): player.left(45) def right(): player.right(45) #Keybinds turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d")

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

bot1 = turtle.Turtle() bot1.turtlesize(3, 3, 3) bot1.color("red") bot1.shape("turtle") bot1.penup() bot1.goto(36, -164)

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

bot2 = turtle.Turtle() bot2.turtlesize(3, 3, 3) bot2.color("lightblue") bot2.shape("turtle") bot2.penup() bot2.goto(212, 164)

What your code should look like

#Import import turtle import random #Setup width = 800 height = 600 wn = turtle.Screen() wn.setup(width, height) wn.bgcolor("blue") wn.title("Bots") #Player player = turtle.Turtle() player.color("green") player.shape("turtle") player.turtlesize(3, 3, 3) player.penup() #Functions def forward(): player.forward(10) def left(): player.left(45) def right(): player.right(45) #Keybinds turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d") #Bot1 bot1 = turtle.Turtle() bot1.turtlesize(3, 3, 3) bot1.color("red") bot1.shape("turtle") bot1.penup() bot1.goto(36, -164) #Bot2 bot2 = turtle.Turtle() bot2.turtlesize(3, 3, 3) bot2.color("lightblue") bot2.shape("turtle") bot2.penup() bot2.goto(212, 164)

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:

#Keybinds turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d")

then add a while True:

#Making bots move while True: turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d")

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

bot1_forward = random.randint(1, 10) bot1_left = random.randint(1, 20) bot1_right = random.randint(1, 20)

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

bot2_forward = random.randint(1, 10) bot2_left = random.randint(1, 20) bot2_right = random.randint(1, 20)

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

bot1.forward(bot1_forward) bot1.left(bot1_left) bot1.right(bot1_right)

Now we do it for the 2nd bot

bot2.forward(bot2_forward) bot2.left(bot2_left) bot2.right(bot2_right)

Make sure it is in the while loop

How it should look like

#Import import turtle import random #Setup width = 800 height = 600 wn = turtle.Screen() wn.setup(width, height) wn.bgcolor("blue") wn.title("Bots") #Player player = turtle.Turtle() player.color("green") player.shape("turtle") player.turtlesize(3, 3, 3) player.penup() #Functions def forward(): player.forward(10) def left(): player.left(45) def right(): player.right(45) #Keybinds turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d") #Bot1 bot1 = turtle.Turtle() bot1.turtlesize(3, 3, 3) bot1.color("red") bot1.shape("turtle") bot1.penup() bot1.goto(36, -164) #Bot2 bot2 = turtle.Turtle() bot2.turtlesize(3, 3, 3) bot2.color("lightblue") bot2.shape("turtle") bot2.penup() bot2.goto(212, 164) #Making bots move while True: turtle.listen() wn.onkeypress(forward, "w") wn.onkeypress(left, "a") wn.onkeypress(right, "d") bot1_forward = random.randint(1, 10) bot1_left = random.randint(1, 20) bot1_right = random.randint(1, 20) bot2_forward = random.randint(1, 10) bot2_left = random.randint(1, 20) bot2_right = random.randint(1, 20) bot1.forward(bot1_forward) bot1.left(bot1_left) bot1.right(bot1_right) bot2.forward(bot2_forward) bot2.left(bot2_left) bot2.right(bot2_right)

Ending

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.

Voters
Profile icon
DrExperiment
Profile icon
eytand0001
Profile icon
MowatMsFowl
Profile icon
rickyviola
Profile icon
CodersXD
Comments
hotnewtop
Profile icon
ArtyEevee

mine is not moving when i push w, a, d
https://replit.com/@ArtyEevee/etchasketch#main.py

Profile icon
pmoulalimoin

what is use of penup ?

Profile icon
CodersXD

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