Skip to content
← Back to Community
Python Turtle Module Tutorial 1
Profile icon
CodersXD

Python Turtle

Welcome to my first python module simple tutorial, this lesson I will teach you the basics of turtle visuals, like if you want to see part 2, it would tell me if someone reads this tutorial.

Introduction

So in turtle you can create games, simple games but it is still amazing, first of all, import turtle.

Setting up

In turtle the command "turtle.Screen()" can change the title, backround color and more, so first shorten it by doing:
wn = turtle.Screen()
You do this so you don't have to write it a lot, you just write 2 letters, your code should look like this:

#Import import turtle #Setup wn = turtle.Screen()

so now we don't have to write turtle.Screen()
just wn to say it

Screen backround color

in order to change the backround color, you need to do:

wn.bgcolor("green")

bgcolor means backround color, and it doesn't need to be green, it can be red, yellow and more.

Now that you run it, you will see a green backround, that is it, but it is really small, how do we make it bigger?

Screen size

The highest height of a screen can be 800, and width 600, I will explain why later on.
write:

"height = 800" and "width = 600"

we write this so we don't have to write 800 and we can use the height or width later on.
next write:
wn.setup(height, width)
the height is 800 and width is 600, you can write 800 there but it will be easier for you later on, setup basically means the screen size, the max height is 800 and width is 600 is because if you put it higher, part of the screen will be blocked, you can test it out.

Title

The title isn't really that important but you can use it, if you did the setup on repl.it, you won't see it, but you will if you use python IDE.
you do "wn.title("Put a title here")
your code should look like this:

#Import import turtle #Setup height = 800 width = 600 wn = turtle.Screen() wn.bgcolor("green") wn.setup(height, width) wn.title("Put a title here")

Draw a shape

We finished the turtle screen, Yay!
now how about we draw a square?
we can't use the turtle.Screen() for the command to draw or place objects and shapes, so we need to use "turtle.Turtle()" it is a command where it is literally a pen, if you make it go forward, it draws where it goes, or you can make it not draw, or with it you can create a character than can move, so first lets do:

pen = turtle.Turtle()

When you run the code, you should see an arrow on your screen, that is a good sign, that is the pen, you can see where it moves, you can also hide it so you can't see it, I will tell you how later on.
to make it draw you need to make it move, how to do that? It is pretty simple, you do:
pen.forward(100) forward is it going forward where it is facing and the 100 is how much pixels it will move, your code should look like this:

#Import import turtle #Setup height = 800 width = 600 wn = turtle.Screen() wn.bgcolor("green") wn.setup(height, width) wn.title("Put a title here") #Pen pen = turtle.Turtle() pen.forward(100)

Now run it, you should see a line, it was facing right of the screen so it moved forward to the right, but how to turn it left so we can make a square? Simple.
You do:
pen.left(90)
it will turn left but it won't go to the left, then you can make it go forward again, try to make a square now, if you don't know how, I will show you:

Scroll down for the answer

The answer is:

pen.forward(100) pen.left(90) pen.forward(100) pen.left(90) pen.forward(100) pen.left(90) pen.forward(100) pen.left(90)

the advanced way is:

for i in range(4): pen.forward(100) pen.left(90)

for i in range(4) means it will do that command 4 times

Want to know how to fill the square?
before the code where you do "pen.forward(100)"
write "pen.begin_fill"
and after the last code of it drawing a square write
"pen.end_fill"
Also if you don't want your pen to draw while it is going somewhere, write "pen.penup()" and when you want it to draw, write "pen.pendown()"
and if you want the triangle to dissappear write "pen.hideturtle()"

Want to know how to change the color?
To do that you do "pen.color("blue")
Doesn't need to be blue
#Creating Shapes
In order to create a shape, it is simple, you do:
pen.shape("square")
Do you want your shape to be bigger
write:

pen.turtlesize(3, 3, 3)

the first 2 are the height and width, and the 3rd is outline size.
I think that is it, if there is an error, then repl.it glitches turtle sometimes, don't add new files to your code, it does work on python IDE perfectly though, if you got an error, comment the link of your repl and I will reply within 24 hours, also like if you want part 2, it tells me if people are actually reading this, I will show you what turtle can do.

-https://repl.it/@CodersXD/Python-Turtle-Bots#main.py
That is only the scratch of the surface though.

Voters
Profile icon
artemstepanenk1
Profile icon
chiccken
Profile icon
02-CSE-CS-Abhin
Profile icon
fletcheroy20
Profile icon
nishpalani
Profile icon
rickyviola
Profile icon
MajdZaher
Profile icon
DanielArghavani
Profile icon
TeddyBraun
Profile icon
JacobSedgebeer
Comments
hotnewtop
Profile icon
CodeLongAndPros

Perhaps use code blocks?
```
This is a code block
```

This is a code block

```python
This is a code block with syntax
```

This is a code block with syntax
Profile icon
ANDREWVOSS

@CodeLongAndPros Yeah, more people need to use markdown.

Profile icon
CodersXD

@CodeLongAndPros Ok, thanks.

Profile icon
CodersXD

@CodeLongAndPros I am new to making tutorials, but I got it now thanks to you.

Profile icon
lyrical

cool

Profile icon
CodersXD

@lyrical Thanks, also I checked your python turtle repl, you saw that when you drew a square, the first line is black, the reason is that you put:

#Import import turtle #Setup height = 800 width = 600 wn = turtle.Screen() wn.bgcolor("Green") wn.setup(height, width) wn.title("Put a title here") #Pen pen = turtle.Turtle() for i in range(4): pen.begin_fill pen.forward(100) pen.left(90) pen.end_fill pen.color("blue")

but you need to do:

#Import import turtle #Setup height = 800 width = 600 wn = turtle.Screen() wn.bgcolor("Green") wn.setup(height, width) wn.title("Put a title here") #Pen pen = turtle.Turtle() pen.begin_fill() pen.color("blue") for i in range(4): pen.begin_fill pen.forward(100) pen.left(90) pen.end_fill()

also you placed this code in the wrong position

pen.begin_fill()

and on pen.end_fill()
you need brackets at the end.
Also the pen.end_fill needs to be out of the for loop
so after it completed the foor loop, that when you want it to finish.
Your code should look like this:

#Import import turtle #Setup height = 800 width = 600 wn = turtle.Screen() wn.bgcolor("Green") wn.setup(height, width) wn.title("Put a title here") #Pen pen = turtle.Turtle() pen.begin_fill() pen.color("blue") for i in range(4): pen.begin_fill pen.forward(100) pen.left(90) pen.end_fill()

If you got any more questions, ask here, I will answer within 24 hours, if you are intrested in turtle and get the first tutorial, here is the 2nd tutorial.
-https://repl.it/talk/learn/Python-Turtle-Module-Tutorial-2/60312

Profile icon
PsyPy

Turtleee. Thanks for this.

Profile icon
CodersXD

@PsyPy, No problem, hope it helped :)

Profile icon
CodersXD

@PsyPy I will do a part 2

Profile icon
CodersXD
Profile icon
pmoulalimoin

that advanced way is not working ..... remain everything working

Profile icon
adrianmccall10

Screen size did not work in repl.it but did work in python IDE.

Profile icon
CodersXD

@adrianmccall10 make sure you don't add any files other than sprites, or it will glitch

Profile icon
CodersXD

@adrianmccall10, and I don't use python turtle repl, I just use python then I import turtle

Profile icon
CodersXD

@adrianmccall10 to fix the problem, you make a new repl

Profile icon
EpicCoder3

good good thanks sans