# 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.

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

## 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``````

### 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``

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

## 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.

• ``title('An aquatic scene')  # changes canvas title in title bar``
• ``bgcolor('PeachPuff2')  # changes canvas background color (see color list below)``

Chart of available turtle colors

• ``````setup(400, 300)   # changes canvas size to 400 wide and 300 high.
# Must be done before any drawings occur.
# In general, we will stick with the default turtle canvas size.``````

## 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:

• ``fillcolor('tan3') # sets the fill color``
• ``pencolor('bisque') # sets the pen color (outlines of shapes drawn)``

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

## 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.

## 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:

Simple line hat:

• This hat uses simple lines (forward, backward and left and right turns) with a three-ellipse pom pom on top.

## 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:

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:

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?

You can make the font bigger like this:

``````fontsize(45)
drawText("I love CS111")``````

## [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.