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 that makes drawing simple and concise. Make sure turtleShapes.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]:

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 [5]:
# 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
A 1.5-pensize black circle centered at (0, 130) with radius 30

... and add the rest of the body

In [6]:
# the hat

teleport(-30, 150) # Move turtle to the coordinate (-30, 150)
fd(60)             # Turtle moves forward by 60
lt(120)            # Turtle moves its direction left by 120 degrees

Next part to draw is the torso:

In [7]:
# the torso

teleport(0, 0)
drawRectangle(100, 200)
A black 200 by 100 rectangle centered at (0, 0) with a vertical long axis.

Then the legs:

In [8]:
# the legs

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

teleport(25, -150)
drawRectangle(20, 100)
A black 100 by 20 rectangle centered at (-25, -150) with a vertical long axis.
A black 100 by 20 rectangle centered at (25, -150) with a vertical long axis.

And then the arms:

In [ ]:
# the arms

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

teleport(75, 50)
drawRectangle(50, 20)

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')

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 [ ]:

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