Skip to content
Sign upLog in
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
33

A Crash Course In Python Turtle!

JWZ6
JWZ6

Hi Everyone!

Me and

@SamiOsman
will be teaching you Python Turtle today! I've seen many Python Turtle tutorials and they aren't exactly the best. Anyways, here it is! A good Python Turtle tutorial is here.

This is definitely not boring (100% true).

Table Of Contents

  • Getting Started
  • Turtle Movement
    • Drawing
      • Introduction
      • Circles And Dots
      • Stamp
      • Making Polygons
    • Positioning
      • Goto
      • Setposition
    • Writing
  • Drawing
    • Color
    • Filling
      • Begin Filling
      • End Filling
    • Pen Visibility
      • Show Turtle
      • Hide Turtle
    • Other Drawing Methods
      • Turtle Speed
      • Turtle Shape
      • Turtle Size
      • Turtle Pen Color
  • Events
    • Onkey
    • Ontimer
    • Onclick
      • Python
      • Python(with Turtle)
  • Window
    • Background
      • Bgcolor
      • Bgpic
      • Reset
    • Input
      • Text Input
      • Number Input
    • Exiting
      • Exit
      • Exit Onclick
    • More Window Methods
      • Setup
      • Title
    • Ending

Getting Started

First, open up a new project. You can do it in either Python or Python(with Turtle). Next, do:

# Importing The Turtle Module import turtle # Giving The Turtle Project A Title turtle.title("Turtle Project")

The turtle.title() won't work if your using Python(with Turtle). You can rename the turtle title. Just be careful and don't get any errors.

Turtle Movement

Drawing (Introduction)

Lets start off by doing turtle.forward(30). This will make the turtle move forward. The number in the () is the amount you want it to go forward. You also have turtle.backward(), turtle.left(), turtle.right(), turtle.up(), and turtle.down. No need for an explanation on how they work. For example, you could have:

# Importing The Turtle Module import turtle # Moving Forward turtle.forward(60) # Turning Left 40 Degrees turtle.right(-40) # Moving Backwards turtle.backward(20) # Turning Right 80 Degrees turtle.left(-80) # Turnin

Negatives also work. The numbers inside the () have to be integers or floats. No strings

Drawing (Circles And Dots)

If you want a circle, do turtle.circle(radius). The radius of the circle goes inside the (). If you use turtle.dot(radius, color), you would get a dot. The differences between the circle and the dot are that the dot would be filled by the color, but the circle wouldn't be. The middle of the dot is the turtle cursor, but the edge of the dot is the cursor. For example, you might have:

# Importing The Turtle Module import turtle # Making a circle with a radius of 20 turtle.circle(20) # Making a dot with the color of blue and the radius of 30 turtle.dot(30,"blue")

Drawing (Stamp)

By using turtle.stamp(), you imprint the turtle shape onto the turtle canvas, wether it's triangle or classic (I'll get to that later). If you don't want the stamp, just don't use it. It looks like this:

# Importing The Turtle Module import turtle # Making A stamp turtle.stamp()

Making Polygons

To begin a polygon, use turtle.begin_poly(). To end one, use turtle.end_poly(). I'm not exactly sure why there's a begin/end polygon function. If you use it, do:

# Importing The Turtle Module import turtle # Using turtle.begin_poly() turtle.begin_poly() # Moving forward turtle.forward(90) # Ending The Polygons turtle.end_poly()

Position

You can position the turtle pen by using turtle.goto(). or you can use setposition(). They're the same thing. By using turtle.goto() , the turtle pen will teleport the turtle to the exact coordinates that you give inside the (). If you put two coordinates then do x, y. Remember, x comes first, not y. For example, you could have:

# Importing The Turtle Module import turtle # This will go to the position of 45(x), 4(y) coordinates turtle.goto(45, 4)

Writing

You can write text by using using turtle.write("text"). For example, you could have:

# Importing Turtle Again import turtle # Using turtle.write() turtle.write("yay")

Drawing

Color

You can change the color by using turtle.color(color). That way you can change the turtle color while you are drawing. Look to Turtle Movement for how to do that. For example you might have:

# Importing The Turtle Module import turtle # Changing The Color turtle.color("green") # Using The For Loop for i in range(4): # Moving Forward turtle.forward(50) # Turning Left turtle.left(90)

Filling

(You have to make a shape before the shape can be filled).

You can start filling by using turtle.begin_fill(). You end the filling by doing turtle.end_fill(). The last color you use before the turtle.end_fill() will be the fill color. For example:

# Importing The Turtle Module import turtle # Specifying the color turtle.color("cyan") # Starting the fill turtle.begin_fill() # Making the circle turtle.circle(20) # Stopping the fill turtle.end_fill()

Pen Visibility

You can either hide the pen or show the pen in Python Turtle. You can hide the turtle pen by using turtle.hideturtle(). Use turtle.showturtle() to show the pen. For example, you might use:

# Importing The Turtle Module import turtle # Hiding The Pen turtle.hideturtle() # Drawing A Circle turtle.circle(20) # Showing The Pen turtle.showturtle()

Other Drawing Methods

Turtle Speed

You can change the speed of how quick the turtle pen is moving by using turtle.speed(). The number range inside the () has to be 0-10. 0.5-0 shows no movement unless you change the turtle speed later on. 1 means very slow while 10 means fast. For example, you could have:

# Importing The Turtle Module like I always do import turtle # Changing The Speed turtle.speed(10) # Fast # Changing the color turtle.color("yellow") # Moving Forward turtle.forward(90)

Turtle Shape

There are 6 different types of turtle shapes: classic, arrow, square, circle, triangle, and turtle. You can use those shapes in turtle.shape(shape). For example, you could have:

# Importing The Turtle Module Like I Always Do import turtle # Changing The Shape turtle.shape("turtle")

Turtle Drawing Size

You can change the turtle drawing size by using turtle.pensize(). The number inside the () must be in between 1-10. 1 is very thin but 10 is thick. For example, you might have:

# Importing The Turtle Module Again import turtle # Changing The Drawing Size turtle.pensize(8) # Drawing A Thick LLine turtle.forward(50)

Turtle Pen Color

You can change the pen color by using turtle.pencolor(""). In the quotation marks put the color that you want the turtle to be. This will not change the drawing color, only the pen color. For example you could have:

# I Always Do This import turtle # Changing the PEN color turtle.pencolor("red") # Changing the DRAWING color turtle.color("blue") # Casually Drawing a Straight Line turtle.forward(50)

Events

Onkey

This will not work for mobile or Python(with Turtle)

Using turtle.onkey(function, "key") will detect if a number, arrow key, or letter is pressed. For arrow keys, use "Up", "Down", "Left", and "Right". This does not work for other keys (like space). The same works for numbers. You could have:

# Importing Turtle import turtle # methods with different work # at different keys def one(): # Writing One20 turtle.write("1") # Using Onkey turtle.onkey(one, "1") # Using Onkey Again turtle.onkey(one, "o") # Using Onkey Once Again turtle.onkey(one, "Up") # Waiting For The Key 1 Or O To Be Pressed turtle.listen()

You must end with a turtle.listen() to make onkey work.

Ontimer

This only works in Python

turtle.ontimer(function, t=0) means that it executes the function after the t = (t in milliseconds). You don't need to use turtle.listen(). For example you could have this:

# Importing Turtle Again import turtle # Creating A function def func(): # Using turtle.write() turtle.right(180) # Using Ontimer turtle.ontimer(func, t = 500)

Onclick

Python

By using turtle.onclick(function (), btn=1, add=None). It will execute the function(). btn=1 means the number of times the user has to click in order to execute the function. add=None means that if it is True, a new binding will be added, and if it is False it will replace a former binding. For example you could have:

# Importing Turtle Again import turtle # Creating A function def func(): # Using turtle.write() turtle.right(180) # Using Onclick turtle.onclick(func(), btn = 1, add = None)

Python (with Turtle)

Using onclick in Python(with Turtle) is simpler than using onclick in Python. The syntax looks like turtle.Screen.onclick(function). For example, you might have

# Importing Turtle Again import turtle # Creating A function def func(): # Using turtle.write() turtle.forward(180) # Using Onclick turtle.Screen.onclick(func)

Window

Background

This does not work for Python(with Turtle)

By using turtle.bgcolor("color"), you create a background of the color. You can also create an image background by using turtle.bgpic("img.png"). The img.png is just an example. GIF's do not work. You could have

# Importing Turtle Again import turtle # Using bgcolor turtle.bgcolor("green")

To reset all the drawings, use turtle.reset(). The background will still be the same, though.

Input

This also does not work for Python(with Turtle)

For a text input, use turtle.textinput("title", "question"). For a number input, use turtle.numinput(). By using minval and maxval, you create a minimum value for the number and a maximum value for the number. For example, you could have:

# Importing Turtle Again import turtle # Making An input turtle.numinput("Number", "Type a number from 1 to 10:", minval=1, maxval=10)

Exiting

If you want to exit very quick, use turtle.bye(). If you want to exit onclick, do turtle.exitonclick(). For example, you can have:

# Importing Turtle Again import turtle # Exiting onclick turtle.exitonclick()

More Window Methods

Setup Size

This does not work in Python(with Turtle)

You can change the setup by using turtle.setup(). There are 4 attributes: width, height, startx, and starty. The width and height are self-explanatory. Using startx will make the starting position align more to the left. The starty attribute does the opposite. You might have:

# Importing Turtle Again import turtle # Using Setup turtle.setup(width=500, height=300, startx=0, starty=0)

Title

This also does not work in Python(with Turtle)

I already talked about title in the very beginning. The syntax is turtle.title("title"). For example, you can have:

# Importing Turtle As Usual import turtle # Using title turtle.title("Something")

Ending

This took me and my friend

@SamiOsman
4 days to write! I hope you found this useful. If you did, make sure to upvote. Anyways, I'm done!

Bye!

P.S If I have any grammar mistakes, please tell me so I can fix them. Also, make sure to check out
@SamiOsman
's post: https://replit.com/talk/learn/Pyhton-Turtle-Tutorial/138758
2 years ago

Voters

Comments

TopNew
3
ahmedo123454
ahmedo123454

Screenshot 2021-05-17 at 9.15.10 AM

this is what it is like bruhhhhhhhhhhh

2 years ago
1
JWZ6
JWZ6

@byronordolol
um ok this is not a rickroll

2 years ago
1
JWZ6
JWZ6

it's quite obvious it's not a rickroll lol

@byronordolol

2 years ago
1
byronordolol
byronordolol

@JWZ6
literally oneperson recognizes the comedy of the 'ok boomer' how do i do strikethrough

2 years ago
1
JWZ6
JWZ6

@byronordolol
lol use ~~ to start and end text in strikethrough

2 years ago
1
byronordolol
byronordolol

@JWZ6
squiggle be like ~~~~~~~~~~~~~

2 years ago
1
byronordolol
byronordolol

@JWZ6
im gonna test strikthru since am new lol xD

2 years ago
1
1
byronordolol
byronordolol

@JWZ6
thanks lol me when week 7 in fnf

2 years ago
1
byronordolol
byronordolol

@JWZ6
when human go bonk: D:

2 years ago
1
1
byronordolol
byronordolol

@JWZ6
beeep boop bap bep bop

2 years ago
1
JWZ6
JWZ6

@byronordolol
boyfriend goes brrrrrrr

2 years ago
1
byronordolol
byronordolol

@JWZ6
me when zZHXYASuhdweiusgudghi7slgweuyuguhs67wrfsrewdsdssadswdsdwsdawsasdsawdadcaswdasdadswsrtfryttegystrtokenuhuegfgesugeugsugsygyqgswdsdQ

2 years ago
1
byronordolol
byronordolol

@JWZ6
basicall my only online friend lol what seems to be odd is that nobody expects replit to be a place where u can chat with people lol

2 years ago
1
byronordolol
byronordolol

@JWZ6
plus i got school and assignments to complete

2 years ago
2
fel1x0u
fel1x0u

This is cool. But I would recommend also teaching turtle.tracer() alongside turtle.speed.

2 years ago
1
JWZ6
JWZ6

@fel1x0u
hmmm, I might do a part 2, though not sure

2 years ago
2

👎 Not working

This is not working I’m doing it but it does not work I can’t make it draw

2 years ago
1
JWZ6
JWZ6

@codermaster8
you must first import turtle

2 years ago
1

I did do that

@JWZ6

2 years ago
1
JWZ6
JWZ6

@codermaster8
which part doesn't work?

2 years ago
1
1
JWZ6
JWZ6

@codermaster8
like turtle.forward or something

2 years ago
1
JWZ6
JWZ6

@codermaster8
this has to be python or python(with Turtle)

2 years ago
1

yeah it says there is an error so what should I do

@JWZ6

2 years ago
1
JWZ6
JWZ6

@codermaster8
well, try ctrl + c, ctrl + v some of my codeblocks i guess

2 years ago
1
JWZ6
JWZ6

@codermaster8
which error r ya getting

2 years ago
1

nevermind i got it to work maybe cause on my tablet it does not work

@JWZ6

2 years ago
1
JWZ6
JWZ6

@codermaster8
hmm could be anyways yay! glad it works

2 years ago
1

I never knew but python (with turtle) doesn’t work but just regular python turtle works on my tablet

@JWZ6

2 years ago
1

Oh now it doesn’t work lol

@JWZ6

2 years ago
1

Lol i dont know why but now it’s working it’s like changing

@JWZ6

2 years ago
1

This is my double square
385AF241-6AE0-4A13-B573-DD64CE2D9948


Hope ya like it
@JWZ6

2 years ago
2
Th3Coder
Th3Coder

NOICE!

I prefer using HTML, CSS, and JS instead of Turtle though
2 years ago
1
Th3Coder
Th3Coder

@JWZ6
to be honest, this is literally the fastest I've seen a post go trending, congrats!

2 years ago
1
JWZ6
JWZ6

@Th3Coder
this went to trending in less than a minute LOL

2 years ago
1
Th3Coder
Th3Coder

@JWZ6
you deserve a place in The Guinness Book of World Records!

2 years ago
1
ch1ck3n
ch1ck3n

@ch1ck3n
wow fastest reply ever

2 years ago
1
JWZ6
JWZ6

@ch1ck3n
just got ya 1,700 cycles

2 years ago
1
JWZ6
JWZ6

@ch1ck3n
glad u like me tutorial also yay too

2 years ago
1
ch1ck3n
ch1ck3n

@JWZ6
ya just got rick rolled

2 years ago
1
JWZ6
JWZ6

@ch1ck3n
lol x-d out before it could

2 years ago
1
Th3Coder
Th3Coder

@ch1ck3n
thank you for blessing me with this link

2 years ago
1
ch1ck3n
ch1ck3n

@Th3Coder

image

at least i have this

2 years ago
1
ch1ck3n
ch1ck3n

@JWZ6
@Th3Coder
give me a coding challenge for a website

2 years ago
1
JWZ6
JWZ6

@ch1ck3n
also y am i gettin 10 notifs every 2 min?

2 years ago
1
Th3Coder
Th3Coder

@ch1ck3n
easy, hard, or impossible?

2 years ago
1
ch1ck3n
ch1ck3n

@JWZ6
this post is this morning's #general for us

2 years ago
1
ch1ck3n
ch1ck3n

@ch1ck3n
WAIT WHO UPVOTED ME

MY 1700 NOOOOOOOOOOOO

2 years ago
1
JWZ6
JWZ6

@ch1ck3n
it 1705 dat also neat

2 years ago
1
ch1ck3n
ch1ck3n

@JWZ6
frick why do these people keep upvoting me

2 years ago
1
ch1ck3n
ch1ck3n

@JWZ6

image


officially the smartest and best AI everrr

2 years ago
1
JWZ6
JWZ6

@ch1ck3n
OF COURSE shoutout to
@CatR3kd
for making it! (sorry for the ping)

2 years ago
1
Th3Coder
Th3Coder

@ch1ck3n

Make a good website without using StackOverFlow.

Difficulty: IMPOSSIBLE

Okay for real though...

Make an equation solver website, using DOGESCRIPT.

2 years ago
1
Th3Coder
Th3Coder

@ch1ck3n
wait you're really making it-

2 years ago
1
ch1ck3n
ch1ck3n

@Th3Coder
im making the stack overflow one

2 years ago
1
CatR3kd
CatR3kd

huh what did I make lmao

@JWZ6

2 years ago
Load more