Python's built-in turtle library, and the turtleBeads library that we've provided, allow you to use code to draw things. They operate around the metaphor of a turtle that crawls around, drawing a line behind it, so you issue commands to do things like move forward or turn left or right. The turtleBeads library adds some shortcuts for things like drawing simple shapes, centered on the turtle's location. A few simple functions that we'll use today are:
forward (or fd for short) which makes the turtle move forwards, drawing a line behind it.
left and right (or lt and rt for short) which make the turtle turn left or right (without moving).
drawCircle which draws a circle around the turtle's current position, and then puts the turtle right back where it started.
drawRectangle works like drawCircle, drawing a rectangle centered on the turtle.
teleport which moves the turtle instantly to a specific x/y coordinate position, without drawing a line behind it as it moves.
begin_fill and end_fill which mark the start and end of a filled shape.
pencolor and fillcolor to set the pen or fill colors.
For every function you use, you will need to know what kind of arguments it expects. Arguments customize what a function does, and are written in parentheses after the function name when we call a function, like this: forward(20). A full reference for turtle functions can be found on our website's reference page here. For now, try to follow along with the examples, and just consult the reference if you get stuck. The arguments for each function listed above are:
forward needs to know how far to go. The default window is about 600-800 units wide, and the turtle starts in the middle.
left and right need to know how far to turn. Values are given in degrees, so a value of 90 would make a right angle turn.
drawCircle needs to know what radius to use (same units as for forward).
drawRectangle needs to know what length and width to use, so it needs two arguments. When there are multiple arguments, we separate them with commas, like this: drawRectangle(100, 60).
teleport needs to know two numbers, specifying the x and y coordinates to go to. As stated above, the whole window is about 600-800 units wide (and the height is similar), with (0, 0) right in the middle. Positive x values move to the right, and positive y values move up, just like in math. So to go to the top-right area of the window, you could do something like teleport(200, 200).
begin_fill and end_fill don't need any arguments. You still need parentheses to call them, but the parentheses should be empty, like this: begin_fill().
pencolor and fillcolor need to be given the name of a color, as a piece of text (i.e., surrounded by quotation marks). So you could do something like pencolor('blue'). This turtle colors reference page has examples of all of the available colors.
Before you start programming, you should download the starter files from here. You should write your code in the smile.py file using Thonny.
For this part of the lab, our goal will be to draw a simple smiley face.
To start with, we need to draw a circle to form the head. Write code in Thonny which calls the drawCircle function with a radius of 250. Don't move the turtle: this will center the circle right in the center of the window, which is where the turtle starts out. If you've got it right, it should print out this text:
A 1-pensize black circle centered at (0, 0) with radius 250
Run the cell afterwards to check for that. That "testing code" is how you will know if you've gotten an exercise correct, and it also submits the exercise when you run it. The image below shows what the circle should look like.
Now that we have a head, we could start adding features, but first, we should probably add some color first. Add three more lines of code:
Call the fillcolor function before you draw the circle.
Call the begin_fill function before you draw the circle as well. This will tell the turtle which part to fill in (but it won't fill in anything until end_fill is called).
Call end_fill after the circle is drawn. This will fill in everything that has been drawn since begin_fill was called, with whatever color was set using fillcolor.
You can pick whatever color you want to use for the face, but the radius should still be 250.
The image below shows what things should look like (assuming you like pink as much as we do ;)
Now it's time to add features. This time, add two eyes, using circles filled with white (or another color of your choosing). Place them at (100, 100) and (-100, 100), and give them radii of 25.
Use teleport to get to where you want to put the eyes, and remember to draw them after the head is drawn, so that they are drawn on top of it.
The image below shows what things should look like (although your colors might be different).
To finish off the face, we need a mouth. Drawing a smile would be tricky, so let's just draw a shocked/hungry face using a rectangle. Add a rectangle, filled with whatever color you want, that's centered at (0, -120) and which has a length of 180 and a width of 40. The picture to the right shows what things should look like.
Note that this time, to speed things up, we can add a call to noTrace to the provided code. Things should draw pretty much instantly, but the tradeoff is that you must call showPicture when you are done, so make sure to add a call to showPicture (without any arguments) to the end of your code here.