The following example illustrates the process of importing and executing turtle commands.
Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from turtle import * >>> forward(100) >>> left(90) >>> forward(100)
Complete documentation for the turtle module can be found in the Python Standard Library Documentation. The following functions should be enough to get you started for today's activities:
forward(distance)backward(distance) |
Move the turtle forward/backward the indicated distance (in pixels). |
left(angle)right(angle) |
Turn the turtle left/right by the indicated angle (in degrees). |
penup()pendown() |
After penup() is called, the turtle will
not draw until pendown() is called. |
goto(x,
y) |
Move the turtle to the indicated position. The orientation is not changed. |
setheading(to_angle) |
Set the orientation of the turtle to the indicated angle. Zero degrees points to the right, and ninety degrees points up. |
exitonclick() |
This command prevents the turtle screen from closing
until it is clicked by the user. This should be the last
command in your main. |
Note that you must be using a version of Python that
has Tk support, because
the turtle module uses
the
tkinter module for graphics output. The labs in
ISAT/CS (143, 248 and 250) should have this software installed.
python3" at the
prompt.shapes.py.import statement at the top of the
file. We will learn about this statement later, but for now
you should know that it allows you to call turtle
module functions without prefacing them with the module
name. In other words, you may simply write
"forward(100)" instead of
"turtle.forward(100)". In general, this is a bad
idea (for reasons we will discuss later), but it will simplify
our code for the purposes of this lab.rectangle(x, y, width, height)
Modify your main so that it calls your newly created function. Test your program by pressing F5 or clicking on "Run Module" under the Run menu.
You can also execute your code by typing "python3 shapes.py" at the terminal prompt. Try out both methods.
polygon(x, y, sides,
size)
size parameter.for loop or a while loop.
The best approach is to use a for loop
along with the range function to control
the number of loop iterations.main so that it includes
several calls to your newly defined
polygon function.row(x, y, count,
size)
This function should draw a horizontal row of squares
with the lower-left corner of the row at position (x,
y). The left edge of each square should be aligned with
the right edge of the previous square.
The count and size parameters
indicate the number of squares in the row and the size
of each square respectively.
Don't copy/paste code from the existing
square function! Your row
function should invoke square with
appropriate parameter values.
main so that it includes a
call to your completed function.grid(x, y, rows, columns,
size).
rows x columns grid of squares. The
lower-left corner of the grid will be at position
(x,y). The size parameter indicates the width of the
individual squares.
filled_square
and checkerboard.
The filled_square function should create a single
colored square, and the checkerboard
function should create a checkerboard-style grid of
alternating filled and un-filled squares. You will need to look through the
documentation for the begin_fill() and
end_fill() functions.
polygon function
you wrote earlier, and you may wish to use the speed() or
tracer()/update() functions to speed up your code
development. Here is an example of what the end result should look like: