Computer Studies‎ > ‎Python‎ > ‎

Turtle Graphics


The following lessons have been tested in Python 3 only.


Getting a Turtle!

Open Python 3 and type in the following code:

import turtle

t = turtle.Pen()

You should get a screen that looks something like this (click to enlarge):


Note!

Python isn't very friendly about closing your Turtle window when you try, but this can easily be solved by ending your program with the following command:

turtle.mainloop()

Assignment - Studying the Basic Startup Code

Run the program with each of the lines commented out one-at-a-time to understand its effect, then answer and discuss the questions below with a classmate.

Questions

Will the program work if you comment out the import statement? What if you comment out the "t =" line? What happens differently? 

Let's Move the Turtle!

Add the following line to your turtle:

t.forward(50)

Your screen should now look like this:

Questions

What happened when you ran this program? 

What does the "50" represent? Experiment with this number and see how large you can make it before it goes off the screen.

Let's Turn the Turtle!

If you experimented with the length parameter in the previous program, set it back to 50.

Add the following line to your program and run it:

t.left(90)

Notice that the turtle is now facing up:

Questions

What do you think the "90" in the left() command designates?

What if you make it 45? 180? 270? 720? 1E3?

Is there a corresponding right() command? Try it.

Assignment - Square

Only using the forward() and left() commands, write a program that produces a square that looks like this:


Save in the relevant handin folder as "square.py".

Assignment - Square 2

You've probably realized in the above program that you had two lines of code that repeated four times. A shortcut to repeat the two lines would be to use a for loop, as in:

for i in range(0,4):
    some command (note the indent!)
    another command


Rewrite your program from above to create a square using a for loop. Save as "square2.py" and submit to the relevant handin folder.

Absolute Positioning

We don't always want our turtle to start in the middle. Conveniently, Python has a goto() command which will position the turtle at the coordinates we desire:

t.goto(-100,200)

It's important to remember that [0,0] is the middle of the screen.

Assignment - Two Squares

Two additional commands will be useful for this assignment:

t.up()
t.down()

The up() command stops drawing when you move the turtle, and down() will continue drawing.

Using for loops and your newly-learned up() and down() commands, write a program that reproduces the following output. Save as "twosquares.py" and submit to the relevant handin folder.


Addtitonal Assignments

  • Draw an arrow. Save as "arrow.py" and submit to the relevant handin folder.
  • Draw a star. Save as "star.py" and submit to the relevant handin folder.
  • Draw a character (Drunk Bob? Bart Simpson?). Save as "character.py" and submit to the relevant handin folder.
  • Draw a polygon (triangle, square, pentagon, etc.) based on the number of sides input by the user. Save as "polygon.py" and submit to the relevant handin folder.
  • Draw a circle. Save as "circle.py" and submit to the relevant handin folder.
  • Draw a square wave. Save as "squarewave.py" and submit to the relevant handin folder.
  • Draw a sine wave. Save as "sinewave.py" and submit to the relevant handin folder. Hint: the math library may come in handy for this. 
  • Recreate this image. Save as "pattern1.py" and submit to the relevant handin folder.


    (based on this video)




Comments