While Python was not designed to be an educational programming language, it has become a popular choice for teaching beginning programmers. One of the earliest programming languages that was designed specifically for educational use was Logo. The Logo language featured a graphical turtle that could be programmatically moved on the screen. The Python standard library includes a Logo-inspired turtle graphics module. The activity today involves getting comfortable with Python by experimenting with turtle graphics.
The following sequence of commands illustrate the process of importing the turtle module and executing turtle commands.
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import turtle >>> turtle.forward(100) >>> turtle.left(90) >>> turtle.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.
|
square( size )
This function should draw a
square at the current turtle position. The size parameter will
determine the length of the sides of the square.main()
This function must include a single call to
the square
function described above.if __name__ == "__main__": main()
shapes.py
and test it.
You can run your program from the IDLE "Run" menu, or by typing
"python shapes.py" at
the terminal prompt. Try out both methods. Don't panic if you
can't close the turtle window when you run your program from
IDLE. The window will close when you exit IDLE, or re-run your
program.
polygon(sides,
size)
. This function should take two integer parameters:
one indicating the number of sides for the polygon and one
indicating the length of each side. When invoked, it should
draw an appropriate shape at the current turtle position.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. For
example, you might draw a small triangle inside a large octagon
inside an even larger square.
promptAndDraw()
. This function takes no
parameters. It should prompt the user for the number of sides
for a polygon and the length of the polygon sides, and should
then draw the appropriate shape. main
function so that it only includes a single call
to promptAndDraw
. A session with your program should look like the following:
> python shapes.py How many sides should the polygon to have? 3 Please enter the length of each side (in pixels). 30With the result that an equilateral triangle is drawn to the screen.
polygonXY(sides,
size)
that takes an x and y position to begin
drawing. The new version of the function should have the
signature polygonXY(x, y, sides,
size)
. Modify promptAndDraw
so that it requests x and
y positions from the user and calls the new function.
goto
function to accomplish this. You can read about it in the Turtle Documentation. pendown
and penup
functions to
avoid drawing a line when goto
is executed. polygonOfPolygons
that draws a large polygon
composed of smaller polygons. For example, this function might
be used to draw a large triangle composed of small octagons.
Test your function by calling it from main.
There is nothing to hand in for this assignment. Make sure that you save a copy of your code, either on a thumb drive, the N: drive, or by e-mailing it to yourself. If you worked with a partner, make sure both of you get a copy. The next homework assignment will build on this work.