CS111 Course Intro: Drawing Tinman

Note: To run the code in a cell (a cell is a row that has the label In [ ]: in front of it), select it (by putting the cursor in the cell) and then click the Run button. (It looks like Play in a music player interface). Alternatively, press Shift+Return in your keyboard. (This is a faster way, try to remember it.)

Let's now have some fun drawing with Python.

This example is just a preview into what you can do graphically with Python. Don't panic if the code seems opaque. It will become a lot clearer once we go through it.

Step 1: Import Turtle Graphics

The first thing we need to do is import a package named turtle that allows us to generate images.

You can find documentation on turtle graphics here.

In [1]:
from turtle import * # This line means "from the package turtle import everything"

The CS111 team has also developed another package named turtleBeads that makes drawing simple and concise. Make sure turtleBeads.py is in the same folder as this notebook. Go ahead and import that as well.

In [2]:
from turtleBeads import *

Step 2: Setup the Turtle Screen

Before we draw our picture, we generally want to setup our turtle. Simply execute the code below. We will learn more about what exactly this code does in the coming classes.

In [3]:
setupTurtle()

You should be able to see a window popping up on the screen. If you cannot see it, move around your browser window, because it might be hiding underneath it. It's better to resize your browser to keep them that window on the side.

Note: The center of the window has the coordinates (0,0). Think of that point as the center of the 2D coordinates system.

Step 3: Draw the parts of Tinman one by one

First we will draw the head. Executing the code below will show the turtle drawing a filled circle on the window created on the previous step.

In [4]:
# the head

teleport(0, 130)    # Move turtle to the coordinate (0,130)
fillcolor('grey')   # Use the color 'grey' to fill the shape
begin_fill()        # Ready for filling
drawCircle(30)      # Draw a circle with radius 30
end_fill()          # Stop filling
Start of filled shape.
A 1.5-pensize black circle centered at (0, 130) with radius 30
Filled in shape using grey.

... and add the rest of the body

In [5]:
# the hat

teleport(-30, 150) # Move turtle to the coordinate (-30, 150)
begin_fill()
fd(60)             # Turtle moves forward by 60
lt(120)            # Turtle moves its direction left by 120 degrees
fd(60)
lt(120)
fd(60)
lt(120)
end_fill()
Start of filled shape.
A 1.5-pensize black horizontal line from (-30, 150) to (30, 150).
A 1.5-pensize black 120° line from (30, 150) to (0, 202).
A 1.5-pensize black 60° line from (0, 202) to (-30, 150).
Filled in shape using grey.

Next part to draw is the torso:

In [6]:
# the torso

teleport(0, 0)
begin_fill()
drawRectangle(100, 200)
end_fill()
Start of filled shape.
A black 200 by 100 rectangle centered at (0, 0) with a vertical long axis.
Filled in shape using grey.

Then the legs:

In [7]:
# the legs

teleport(-25, -150)
begin_fill()
drawRectangle(20, 100)
end_fill()

teleport(25, -150)
begin_fill()
drawRectangle(20, 100)
end_fill()
Start of filled shape.
A black 100 by 20 rectangle centered at (-25, -150) with a vertical long axis.
Filled in shape using grey.
Start of filled shape.
A black 100 by 20 rectangle centered at (25, -150) with a vertical long axis.
Filled in shape using grey.

And then the arms:

In [8]:
# the arms

teleport(-75, 50)
begin_fill()
drawRectangle(50, 20)
end_fill()

teleport(75, 50)
begin_fill()
drawRectangle(50, 20)
end_fill()
Start of filled shape.
A black 50 by 20 rectangle centered at (-75, 50) with a horizontal long axis.
Filled in shape using grey.
Start of filled shape.
A black 50 by 20 rectangle centered at (75, 50) with a horizontal long axis.
Filled in shape using grey.

You can also add text to your picture as well.

In [9]:
# the text

teleport(0, 200)
color('deep pink')
style = ('Courier', 40, 'italic')
write('If only I had a heart!', font=style, align='center')
The text 'If only I had a heart!' in 40pt Courier italic font at (0, 200)

Step 4: Decorate the Tinman!

Use what you learned above (drawing circles and triangles) to decorate Tinman's drawing. You can draw buttons, pockets, shoes, arms, and so on. Be creative, have fun!

In [ ]:

In [ ]:

Step 5: Clear the Screen

If at any point, you want to clear the screen and start over, run the code below.

In [ ]:
reset()
setupTurtle()

That's all for now! We will do much more with turtle graphics over the course of the semester.