A Crash Course In Python Turtle!
h
JWZ6 (710)

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
You are viewing a single comment. View All
Th3Coder (134)

NOICE!

I prefer using HTML, CSS, and JS instead of Turtle though
Th3Coder (134)

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

JWZ6 (710)

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

Th3Coder (134)

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