that pen can move and draw on a two dimensional plane
the pen is called a turtle!
it's essentially a Python implementation of another language called Logo
Some History
So, what's this Logo thing about?
Logo is an educational language
one of its most well-known features is turtle graphics
built on theory of constructionist learning
learning by experimentation
learning by making tangible things!
created in the mid 60's(!) by a group of computer scientists: Daniel G. Bobrow, Wally Feurzeig, Seymour Papert and Cynthia Solomon
Seymour Papert
Papert, in particular, is well known for his work in education and computing
developed an influential theory on learning called constructionism
was the director of the MIT Artificial Intelligence Laboratory
besides inventing Logo, also collaborated with Lego (it's not confusing that that's one vowel away from Logo) on a robotics kit called Mindstorms
(And Like Many Great Computer Scientists, He Has a Beard)
Great, So… Why Turtle?
Imagine you have a turtle hanging out on the beach…
Turtles Drawing Stuff
imagine further that it's a robotic turtle (AWESOME!)
…that you can give commands to
move forward
turn around
as it moves, it leaves tracks on the ground
turtle graphics simulates this (seriously)
your window is a sandy beach
the turtle, is… well… um… a turtle (a virtual robotic one)
What Does That Mean for Us?
So… in Python, we now have access to our own drawing turtle
we can draw by writing code
that code is analogous to the commands that we would give the turtle (or pen, or pointer, or whatever)
move forward
turn around
but in addition, we can also
change colors
ask for user input
etc.
Hello Turtle
This draws a line (that's exactly 200 pixels). (exciting). Let's try running it.
About the Drawing Environment
obviously, we're drawing on a two-dimensional plane
the turtle starts at the center
the turtle is facing right (imagine that it's looking east)
can you guess what the coordinates (x and y values) are at the center?
where are the positive x values… and the positive y values?
About the Drawing Environment Continued
you can use leo.forward(200) as a clue!
if that drew a 200 pixel line, then, maybe
the center is at (0, 0)
positive x values are to the right of the origin, positive y, above
(yeah, maybe that's obvious, but some graphics packages have a different coordinate plane)
A Few Tips for Running Programs
Running these programs (from IDLE or from Terminal!) cause a new window to pop up. You may encounter some minor annoyances:
the window usually opens up behind the interactive shell (or Terminal) →
sometimes an extra Python process shows up as a rocket icon on the dock →
even after you've closed your actual drawing's window
you can get around this by closing the console
(btw, you can also exit the shell using CTRL-d)
More Tips for Running Turtle
if there's an error, the window of your program may hang →
close the interactive shell to get rid of it
…or force quit the window
output from print still shows up in the shell →
there won't be any ouptut in your drawing's window for print
you'll have to juggle two windows
Let's Dissect That Code
bring in the turtle module
create a Screen object (this provides a canvas to draw on, and some window related commands)
create a Turtle object to draw with
Let's Dissect That Code Continued
tell the turtle to move forward 200 pixels
start the program!
Objects
So… I used the word object there a few times. What does that actually mean?
object - a thing that a variable name can refer to, like a screen or a turtle
…in fact, all of the values in Python are things
they're objects too: "hello" is a str object, 42 is an int object
an object can have attributes …data associated with an object
an object can have methods …which are basically things that the object can do
Methods
a method is essentially a function that's associated with a particular object
you can call a method just like a function… but you have to use the dot operator
object.method() - it's similar to using a method in a module!
for example: leo.forward(200)
…means I'm calling the forward() function on the turtle object called leo
in fact… we can see some methods on objects that we've used before!
Let's Look at That Code Again…
The Basic Steps Are…
What did we have to do?
bring in the turtle module
create a Screen object
create at least one Turtle object
tell the Screen object to start the program
So, That's Some Boilerplate Stuff
We should probably convert our hello program into a template. You'll need to write this stuff every time you create a program with turtle:
A Note On Names
in the template, I use t as the variable name for my turtle.
it's just a variable name; it can be anything you want (same with wn, but you have to change wn everywhere you see it)
in fact, in my previous programs, I called the turtle leo, in honor of one of these guys
BTW, I Definitely Encourage You to Follow Along When I Code Up Examples!
Basic Turtle Methods
These are all methods that you can call on your Turtle object.
forward(distance) - move the turtle forward by the specified distance
right(angle) - turn the turtle right by angle degrees
left(angle) - turn the turtle left by angle degrees
back(distance) - move the turtle back by the specified distance
Forward, Right, Left, Back - Code
BTW… what do you think this draws? →
Screen and Pen Drawing Attributes
Methods you can call on your Turtle object:
color(colorstring) - change the color of your pen to colorstring, which can be "red", "green", etc.
pensize(size) - change the size of your pen to size
Methods you can call on your Screen object
setup(width, height) - window dimensions (default is 50% and 75% of screen)
bgcolor(colorstring) - change the background color of your window to colorstring
Color, Background and Pen Size
Moving Without Drawing
Methods you can call on your Turtle object:
up() - pick the pen up so that the turtle object doesn't draw when it moves
down() - put the pen down so that the turtle object draws when it moves
Pen Up, Pen Down
BTW… what do you think this draws? →
Going Somewhere?
A method you can call on your Turtle object:
goto(x, y) - move the turtle to the specified coordinates …x and y. Note that if the pen is down, it will draw up to that coordinate.
Goto
BTW… what do you think this draws? →
A Confused Turtle
A quick demo using goto: let's try incorporating random elements to our drawings! →
Let's Use What We Know to Create a Square!
How would we tell the turtle to create a square with the upper left corner at the origin? Each side should be 200px long. We just learned goto, so let's try that.→
Another Square!
Same thing, but this time, just use forward or back and either left or right. →
For a Square….
How can we simplify the previous version? There was a lot of repeated code! →
Clearly, this calls for a for loop!
Loops for Squares?
Ugh… so, every time we want a square, we have to write another loop? That seems a bit cumbersome.
What can we do to package up a drawing of a square so that we we don't have to explicitly worry about looping? →
How about we write a function?
every time we want a square, we just call draw_square()
all of the implementation details of drawing a square are hidden within that function
easy enough →
draw_square Function
Write a function to draw a square with a side of length 200. →
parameters: 0 (no parameters)
processing: draws a square with side of length 200
return value: None (does not return anything)
Cool Function, Bro… But…
So, our function worked pretty well for drawing a square, but there's a major shortcoming. What's not so great about the draw_square function that we created? →
For every different sized square, we'd have to create another function. How do we get around this? →
Parameterize the side length!
Ok, Now What? Using draw_square(…)
Eh? That was a lot of work, but for what. Let's try our new draw square_function by drawing a lot of squares! →
Y So Slow?
It's fun watching the turtle draw stuff for the first couple of times you work on your program, but it gets super annoying immediately after that.
Just like cooking shows where the food magically appears all cooked, we can get to the drawing results faster by adding these two lines of code: →
hideturtle, tracer, update
Ok… so those two special methods for drawing more quickly were:
t.hideturtle() - makes the turtle invisible (this actually speeds up drawing, somehow!?)
wn.tracer(0) - turns animation off
wn.update() - to remind Python to actually refresh the window with the new drawing
Enough With Squares
Long live pentagons! More sides equals more better, right!?
But first. Some geometry
sum of interior angles of a polygon are:
(number of sides - 2) × 180°
to get each interior angle of a polygon, divide the sum of interior angles by number of sides:
sum of interior angles / sides
so, the turtle has to turn 180 - the interior angle
Planning for a Pentagon
So, for a pentagon, when we apply the calculations from the previous slide… →
sum_interior_angles = (5 - 2) * 180 = 540
interior_angle = 540 / 5 = 108
turtle_angle = 180 -108 = 72
Pentagons
Ok, so that means we have 5 sides, and the turtle has to turn 72 degrees. Let's create a draw_pentagon function →
Same but Different
Let's check out our draw_square and draw_pentagon functions. Can anyone find another thing to parameterize? →
The number of sides!
Why. Stop. At. Squares and Pentagons. Polygonz!
So many shapes. Let's create one function that could draw a square, pentagon or even a tetradecagon. →
Create a function called draw_poly that has two parameters:
number of sides
length of a side…
Remember the following calculations:
sum interior angles = (num sides - 2) × 180°
interior angle = sum interior angles / num sides
turtle's turn angle = 180 - interior angle
draw_poly
Polygonz!
Let's draw a bunch of polgons, starting with a rectangle, going up to and including a 10-gon. →
Circle
We were kind of approaching a circle at the end there. This is a pretty decent approximation. →
Buuuut… of course there's also a circle(radius method:
ontimer
Let's bring time into the mix! →
The Screen object function, ontimer, allows a function to be executed some specified time (in milliseconds) later. For example, the following code would call a function called my_draw 500 milliseconds later.
Notice that the function name is passed in as you would any other variable name.
On to an example… →
ontimer Example
What will happen when this program is run? →
ontimer Example Continued
Let's modify our draw_stuff method ever so slightly. What will happen when this program is run? →
clear
To reset the drawings on the screen, just call clear on your turtle object!
This is actually kind of handy because… we can do clear the screen before drawing each square (which essentially has the effect of… ???) →
Animation!
Clearing the screen before we draw a square at a new position has the effect of animation! →
Putting Everything Together
Ok … so this is a bit ambitious. Let's try creating a bouncing circle. Starting with some setup code: →
Putting Everything Together
Aaaand… filling in our draw function →
Reviewing Objects and Methods
What's an object and what's a method? →
object - a thing that a variable name can refer to, like a Screen, Turtle, int or str
an object can have methods … things that the object can do
a method is a function that you can call from a particular object
Again a Few Tips for Running Programs
Running these programs (from IDLE or from Terminal!) cause a new window to pop up.
the window usually opens up behind the interactive shell (or Terminal)
if there's an error, the window of your program may hang. demo →
close the interactive shell to get rid of it
…or force quit the window
your prints still show up in the shell, but you'll have to juggle two windows. demo →