Lab 1: Part 5: Intro to Drawing with Turtles

Introducing Turtle graphics

This semester, we'll make frequent use of a Python library called turtle which draws lines and shapes in Python.

Today, we're going to start by constructing a face. Below is one possibility. You and your partner have creative freedom in this task. The goal is to learn how to make shapes appear; their precise color/location/dimensions are up to you.

final face

...but to get you inspired with what else you'll be able to create, here are some scenes created by past CS111 students:

(Hover a thumbnail to see a larger view)

An image of a sunset with a large orange semicircle in the background with a small navy blue silhouette or a person in a boat with a fishing rod in hand. The text in the lower right hand corner says My dad likes to fish, created by Brigitte Lee.An image of a green plant in a terra cotta pot with text reading 6CO2 + 6h20+sunlight = C6H1206 = 602!! created by Shaylie Xie and Bianey Douglas.An image of Mike Wazowski from Monsters, Inc with text saying There is more to life than scaring! created by Faith Carbajals.An image of SpongeBob Squarepants, standing in front of his pineapple house with text saying Ahoy Mateys created by Mattie Lyons.An image of mountains with a sunset in the background created by Ayla Kurdak.An image of a shark created by Carson Dennis & Cassia Schuler.An image of a bee carrying honey by Sooyeon Park.An image of the Golden Gate Bridge labeled 'Welcome to the Bay Area' by Claire Moreland & Danika HeaneyAn image of a panda's face by Toshali Goel & Katherine Guo. Text below says: 'CS 111 student/Po: How did I do? ... Professors/Master Shifu: There is now a level 0.'An image of an elephant by Laura Chin & Mari Kramer. Text below reads 'Ernst the Elephant.'

Reference

These resources will be useful whenever you're working with turtles:

Step 0: Import python's turtle module

Python's turtle module allows us to move the turtle forward, backward, turn left and turn right, as well as change the pen size and color, and pick the pen up and put the pen down.

Create a new file named smiley.py (make sure to save it in your lab01 folder, or else importing turtleBeads.py will not work!). Add the following line at the top of your smiley.py file so that it imports all (as indicated by the *) the features in the turtle library:

from turtle import *

Step 1: See how the turtle draws

Note that the turtle's default starting position is in the center of the canvas, facing East.

Step 1a: Try out some turtle commands, like this:

fd(50)    # forward 50 units
lt(90)    # 90 degree left turn
fd(100)   # forward 100 units
rt(45)    # 45 degree right turn
bk(80)    # backwards 80 units
The results of the code above: a line extending 50 units to the East, then going North 100 units, and finally Southwest (at a 45-degree angle) 100 units, with an arrow at the end of that line pointing back along its length indicating that the turtle is at the end of that final line facing towards the line it just drew. The results of the code above: a line extending 50 units to the East, then going North 100 units, and finally Southwest (at a 45-degree angle) 100 units, with an arrow at the end of that line pointing back along its length indicating that the turtle is at the end of that final line facing towards the line it just drew.

Step 1b: You can also change the pen size and color like this:

pensize(20)               # must be a positive number
pencolor('DodgerBlue4')   # changes pen ink to DodgerBlue4

The turtle draws whenever its pen is down. Note you won't see the thick blue line until the turtle moves again. Try this:

bk(20)            # move backwards 20 units
The same image, with an additional thick and rounded blue line extending farther Southwest from the end of the diagonal line where the turtle ended up before. The turtle is no longer visible

The same image, annotated to show
how the pen moved with a think blue line diagonally extending toward the lower left corner of the
canvas.

Note: You can also lift the turtle's pen up, thereby moving the turtle without leaving a trail.

pu()  # pen up

and put it back down later with:

pd()   # pen down

Step 2: Import our cs111-specific turtle module (turtleBeads.py)

Peter Mawhorter wrote turtleBeads.py (in your lab01 folder) that allows us to draw shapes such as circles, ellipses, rectangles and polygons by using these commands: drawCircle(radius), drawEllipse(radius, ratio), drawRectangle(width, height), and drawPolygon(sideLength, numSides). We can also move the turtle around with these commands: teleport(x,y) and leap(dist). teleport moves the turtle to the specified location without drawing a line, and leap moves the turtle forward dist without drawing a line. Add this line just below your other import line so we have access to all these turtle commands:

from turtleBeads import *

Step 2a: What if we want to draw something that is disconnected from our current drawing?

We can use leap or hop to move the turtle without leaving a trace.

leap moves up forward or backward in the turtle's current direction, and 'hop' moves us left or right.

leap(170) 
drawCircle(30) 

The same image, with an additional thick
and rounded blue circle off the end of the diagonal line. The turtle
is visible in the center of the circle. The same image, but annotated to show
that the the command leap(170) moved the turtle from the lower tip of
the blue line to the center of the circle without leaving a pen
trail.

Step 3: Where did that turtle canvas come from?

Note that when you execute turtle commands, the canvas automatically pops up. The canvas has a default size (700x650), default color (white) and default title ('Python Turtle Graphics'). We can change these canvas properties.

Step 4: Draw using turtle commands

Part A: Basic shapes: Circle face

Turtle graphics can create common shapes like circles, squares, rectangles, polygons and lines. You can also add text as well.

Let's start simple with a circle— draw a new circle like this:

drawCircle(250)

The radius is 250. Where is the circle centered?

Fill in the circle

You can add begin_fill() before you draw the circle and end_fill() after you draw the circle, and that will fill the circle with color.

Don't like the default fill color? Choose your own fill color

Note that you'll need to specify your color before any drawing or filling happens. To specify the fill color, you can use these commands:

At this point, your face should look something like this:

The start of the smiley face: a brown circle with a lighter tan edge, on a gray background.

Part B: Add eyes (you decide how many and what shape/color)

Using what you learned above, add eyes on the face.

You can refer to the available turtle shapes in the canonical cs111 turtle reference here.

You can refer to the available turtle colors page to pick your eye color(s) of choice.

Face with eyes added: two blue ellipes are now present in the top half of the brown circle, with different shades of blue and different thicknesses for their black borders.

Part C: Make a hat (use rectangles or lines)

Using the turtle shapes documentation, and what you learned about Circles above, create a hat made of one or more rectangles.

Rectangular hat examples:

Smiley Hat: The same face with a hat on top composed of two rectangles: a long thin one and a shorter thicker one that overlaps the thin one above it in the middle. Smiley Hat 2: The same image but now with the hat tilted slightly clockwise.

Simple line hat:

Part D: Make a mouth

To create a curved “half moon” shape for the mouth, we'll get creative by overlapping two circles like so:

mouth1: a full red circle. mouth2: The same circle but now there's a brown circle on top, offset slightly to the north. The brown circle is the same color as the background of the face. mouth3: The brown circle is now filled in, leaving a crescent of the red circle underneath visible.

We start will a full red circle. Then we overlap another circle on top of the first circle, and we fill the second circle with the face color. This makes our top circle blend in nicely with the face as if it's not even there, like this:

The same brown face with eyes described above, now with the addition of an orange circle in the middle for a nose and a red crescent in the bottom half for a smile.

To make the smile, add the following code to the bottom of your file:

Note that you need to add the nose after the mouth, otherwise, the "cover up" mouth circle will hide the nose. The most recently drawn turtle shapes will appear on top of previously drawn shapes.

Part E: Add some text

For our finishing touches, let's add some text to our smiley face. Position your turtle where you would like the text to appear, and then use the drawText command like this:

drawText("I love CS111")

Can't see your text? What color is it? Where was your turtle positioned when you wrote? Is it showing up tiny, like this?

text1: Very tiny white text below the face described above. It's so tiny that only someone who zooms in very far or who can read this alt text will know that it says: 'i love cs111'.

You can make the font bigger like this:

fontsize(45)
drawText("I love CS111")
text2: The same text ('i love cs111') in a larger font centered below the face desicrbed above.

[OPTIONAL] Part G: Still have some time? How about some hair?

You have lots of option for head and/or facial hair: thick or thin lines, filled circles, or overlapping ellipses. Have some fun and add some hair to your face.

hair1: The same face described above, now with two overlapping angled blue-green ellipses on the top to represent hair. hair2: The same face described above, excpet instead of the two ellipses, there are many overlapping pink circles placed around the entire top half of the circle to represent fuzzy pink hair.

Table of Contents