forward(87)
right(43)
forward(26)
left(141)
forward(59)
Turtle Move Commands |
forward(steps) |
backward(steps) |
right(steps) |
left(steps) |
circle(size) |
goto( steps, steps ) |
If you type | Python does |
forward(10*5) |
forward(50) |
right(100/3.0) |
right(33.3333...) |
forward(5+5) |
forward(10) |
Colour |
Numbers |
Black |
0, 0, 0 |
White |
1, 1, 1 |
Green |
0, 1, 0 |
Violet |
0.8, 0.2, 0.8 |
Orange |
1, 0.6, 0 |
Blue |
0, 0, 1 |
Red |
1, 0, 0 |
The name of a function is the single word that
you
type to tell python to perform the series of commands in the fuction. You get to make up anything, but you should make one that:
|
>>> def side(): ... forward(100) ... right(165) ... >>> |
Now for a trick. You certianly don't want to spend the rest of you life typeing square() when you could do the same thing typing sq() ( it saves you some typing) You created the function square() using turtle commands. Now you can create a function sq() using the function square().
Write a function sq() that looks like this:
def sq():
square()
Reset the screen with reset() and run sq(). Clear the screen again with reset() and run square(). You should get the same results with both. Now any time you want to draw a square, type either sq() or square().
sq() and square() can also be used in functions any time you wish, and as many times as you wish, just like any of the other commands.
Turtle move commands | |
Go forward | forward ( distance ) |
Go backward | backward ( distance ) |
Turn left | left ( angle ) |
Turn right | right ( angle ) |
Draw a circle whose center-point is left of the turtle | circle ( radius ) |
Go to co-ordinates x, y | goto ( x , y ) |
Change the way the turtle draws | |
Stop drawing | up ( ) |
Start drawing | down ( ) |
Set the line width (Normal width is 1) | width ( width ) |
Set the pen color | color ( red , green , blue ) |
colors can be decimals from 0 to 1 (example 0.5 ) | |
Turtles window | |
Clear the screen | clear ( ) |
Restart everything | reset ( ) |
Turtle cloaking |
|
Make turtle invisible |
tracer ( 0 ) |
Make turtle visible |
tracer ( 1 ) |
Colour |
RGB Numbers |
Black |
0, 0, 0 |
White |
1, 1, 1 |
Green |
0, 1, 0 |
Violet |
0.8, 0.2, 0.8 |
Orange |
1, 0.6, 0 |
Blue |
0, 0, 1 |
Red |
1, 0, 0 |
Consider these three procedures:
def sq():
forward(30)
right(90)
forward(30)
right(90)
forward(30)
right(90)
forward(30)
def super():
for n in range(8):
sq(); right(45)
def strange():
for n in range(4):
sq()
right(45)
for n in range(4):
sq()
Both super() and strange() draw the same design (although they draw the parts of the design in a different order).
Note that last turn in sq(), the one that would turn the turtle back to its origional heading, wasn't put in.
If you edit sq() now to adda right(90) at the end, super() will still draw the same design (in yet a new order), but strange will not.
This may seem odd at first because we have not changed strange(). However, we DID chage the function strange() uses.
To counteract the effect of adding the right(90) at the end of sq(), we would have to insert a left(90) immediatly after the call to sq() in each function that uses it.
This kind of fix is not always to easy. For example, if the newly introduced extra was a line instead of a turn, it would be harder (in some cases impossible) to counteract its effect.
So it is best to leave the turtle heading as it started. This will eliminate many interface bugs (puzzling things that must be fixed in order to use one function after another).
Command |
Equivalent |
forward(100/2) |
forward(50) |
right(300/30) |
right(10) |
backwards(200/4) |
backwards(50) |
left(360/4) |
left(90) |
squaral( 8 )
1. The turtle moves forward 5 for the first side and turns right.
2. Python runs squaral( 8+3).
squaral( 11 )
1. The turtle moves forward 5 for the first side and turns right.
2. Python runs squaral( 5+3). and so on.
design( 3 )
1. python tests times(3) to see if it is zero
2. python runs square() and turns the turtle
3. python calls design(3-1) (same as design(2) )
design( 2 )
1. python tests times(2) to see if it is zero
2. python runs square() and turns the turtle
3. python calls design(2-1) (same as design(1) )
Control is passed back to each level it turn and the function is done. This ispect of recursion will be covered in the next section.design( 1 )
1. python tests times(1) to see if it is zero
2. python runs square() and turns the turtle
3. python calls design(1-1) (same as design(0) )
design( 0 )
1. python tests times(0) to see if it is zero
2. python stops the program. (returns from it to what is was doing before you called on it.)