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.
|
goto(x, y)
|
Move the turtle to the indicated position. The orientation is not changed. |
rectangle(x, y, width, height)
This function should draw a rectangle with the indicated width and height. The lower-left corner of the rectangle should be at position (x, y). main()
This function must include several calls to
the rectangle
function described above.if __name__ == "__main__": main()
faces.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.
Add a new function to your program with the
signature face(x, y)
.
This function should draw a face composed of four rectangles.
The lower-left corner of the face should be located at position
(x, y), and the overall size of the face should be 50x50 pixels.
The result should look something like the following:
Don't write this function by copying and pasting the code from your rectangle
function! This function should include four calls to rectangle
.
Update your main
so that it includes at least one
call to your newly defined face
function.
promptAndDraw()
. This function takes no
parameters. It should prompt the user for a location and should
draw a face at the indicated position. main
function so that it only includes a single call
to promptAndDraw
. A session with your program should look like the following:
> python faces.py X Position? 20 Y Position? 30With the result that a face is drawn at position (20, 30).
promptAndDraw
in an infinite loop. The user
should now be able to create any number of faces.
faceGrid(x, y, size)
.
This function should create a size x size
grid of
faces beginning at position (x,y). In other words,
calling faceGrid(0, 0, 2)
would create a grid of
four faces with the bottom-left corner of the bottom-left face
at position (0,0). Adjacent faces should touch at their edges.
scaledFace(x, y,
size)
. This function should work exactly
like face(x, y)
, except it will allow the user to
select a size for the face in pixels. In other words, the
call scaledFace(0, 0, 100)
would create a 100x100
pixel face at position (0, 0). All of the facial features
should scale appropriately.
There is nothing to hand in for this assignment. Make sure that you save a copy of your code, either on a thumb 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.