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.
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.
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.
from turtleBeads import *
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.
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.
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.
# 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
... and add the rest of the body
# 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()
Next part to draw is the torso:
# the torso
teleport(0, 0)
begin_fill()
drawRectangle(100, 200)
end_fill()
Then the legs:
# the legs
teleport(-25, -150)
begin_fill()
drawRectangle(20, 100)
end_fill()
teleport(25, -150)
begin_fill()
drawRectangle(20, 100)
end_fill()
And then the arms:
# the arms
teleport(-75, 50)
begin_fill()
drawRectangle(50, 20)
end_fill()
teleport(75, 50)
begin_fill()
drawRectangle(50, 20)
end_fill()
You can also add text to your picture as well.
# the text
teleport(0, 200)
color('deep pink')
style = ('Courier', 40, 'italic')
write('If only I had a heart!', font=style, align='center')
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!
If at any point, you want to clear the screen and start over, run the code below.
reset()
setupTurtle()
That's all for now! We will do much more with turtle graphics over the course of the semester.