Introduction to Turtle

Turtle is a graphics library in Python. What that means is basically that someone wrote a bunch of code that other people can use in their projects. Specifically, Turtle is library that you can use to draw basic images and graphics, and its a really simple library to start using as a beginner. Think of turtle as a little robot that you can direct to move around the screen, and as it moves, it draws lines.

Today, you are going to write a program that uses turtle graphics to make a spiral. Below is the code to do this, you can copy and paste it into a new python file and then I will explain what each line does.


import turtle
window = turtle.Screen()
window.title("Turtle Example")
window.setup(400, 400)
window.bgcolor("blue")

t = turtle.Turtle()
t.pencolor("white")
t.speed(0)
t.width(5)

for x in range(100):
	t.forward(x+2)
	t.left(360/5 + 3)

turtle.mainloop()
							

This is what the output should look like:

The first line, import turtle is an import statement that just tells Python to use turtle graphics in the program.

The next four lines set up different aspects of the turtle window.
window = turtle.Screen() creates a window.
window.title("Turtle Example") sets the name of the window, so when you run the program, the turtle window has a name at the top. If you don't set this, it defaults to "Turtle Graphics".
window.setup(400, 400) specifies the height and width of the window in pixels. In this case, the height and width are both 400 pixels.
window.bgcolor("blue") sets the background color of the window, which is blue here. Make sure to put the color in parenthasis so that the program knows its an argument. Here is a list of colors that Turtle recognizes. If you want to use a color not listed here, you can use hex code also, just put those in quotes in the parenthesis.

Next, there are 4 lines that set up different aspects of the turtle pen.
t = turtle.Turtle() makes a new Turtle object.
t.pencolor("white") set the color of the turtle pen, which is the color that your art is going to be drawn in.
t.speed(0) tells the pen how fast or slow to go. 0 is the fastest speed and as the number increases, the speed slows down.
t.width(5) tells Turtle how thick the pen strokes should be(in pixels). In this case, the width is 5 pixels.

The next 3 lines, starting with for x in range(100): make a for loop that keeps repeating the code inside it. Don't worry too much about this right now, for loops will be covered in later classes.

The last line, turtle.mainloop() just keeps the window open after the program is executed. If you don't include this, the program will run and the window will close right after the program is done.

Here are some other commands you should know:

  • t.forward(10) - tells turtle to move forward a specified number of pixels
  • t.left(3) - tells turtle to turn left a specified number of degrees
  • t.right(3) - tells turtle to turn right a specified number of degrees

If you are ever having trouble figuring out how to do something, always look at the documentation. It will tell you how to use the functions in the library.

Now it is your turn to practice! Your goal is to make a hexagon spiral. Below are the instructions:

  1. Create a new Python file in your replit and name it "hexagon_spiral.py"
  2. Set up a turtle window. Name it spiral, make the height 200 pixels and the width 300 pixels, and set the background color to "black".
  3. Set up a turtle pen. Set the width to 3 pixels, set the color to "white" and set the speed to 0.
  4. Make the spiral. Turtle should make 50 lines and the last one should be 50 pixels long. The angle of rotation should be 66 degrees.

This is what it should look like:

Here are some resources if you need help with anything