Computer Science 111 - Spring Semester, 2006
Homework #8 programming project
due: by midnight on Wednesday November 8
Part 1.
Read Chapter 5
Part 2. Your task in this week's
programming homework is to implement the Logo Programming
Environment on top of the turtle graphics that
you did in lab.
You'll need to write more than one function definition.
I'll leave it up to you how to do it. But do not
just define one big function for everything.
Also, the inputs are all strings (turtle commands or either the
word 'list' or 'quit'). There should only be
one call to raw_input(), at the end of the while loop <body>
In Logo, the user types in commands and a turtle shown graphically
on the screen moves around and draws shapes, depending on
what the user types in.
You can obtain a copy of a .pyc file for this program by typing
getcopy logo.pyc
Then in python
>>> import logo
>>> logo.main()
Remember that the .pyc file does not contain any Python statements. It
is a binary file generated by the Python interpreter, that can
be run by the interpreter later.
The list of turtle commands is, with n being an integer:
- FD n - moves the turtle forward n steps
- BK n - moves the turtle back n steps
- RT n - turns the turtle right by n degrees (subtracts n to angle)
- LT n - turns the turtle left by n degrees (adds n from angle)
- Home - Returns the turtle to its home position
- PU - lifts the pen while the turtle moves so no line is drawn
- PD - lowers the pen while the turtle moves so a line is drawn
- list - lists all the possible commands
You can see a small demo to get the idea
at this URL:
http://www.klbschool.org.uk/ict/control/logo/logo05.htm
Here is how your program will work:
- Call your program hw8.py
- Set up a while loop that stops if the user enters 'quit'.
Inside the loop:
- Eventually you will place your graphics in here from the lab.
For now leave the <body> of the while loop empty except for
the last part where the program get the next user input.
- Ask the user to enter the next turtle command, or to type list in order
to see all the possible commands. Use the raw_input()
function to get the user's response.
- Use the string.split() on the user's response to
split it into a list of separate strings.
For example, if they type
bk 100
the result will be
['bk', '100']
and if they type
pu
the result will be
['pu']
- The result is now a list of strings and the length of
the list is either one or two.
Assign the first string to a separate variable call comm.
Then find a string function that will change this string
to all lower case characters. So if they type
PU or Pu or pU
the result is the same:
pu
If the result from the user has length two, this means
that it is a command that requires a number. Make sure to call eval() on this
number (because it is currently a string) and assign
it to a separate variable called num.
Make sure this works before working on how to respond.
- Now your program will contain a big if-elif-else
statement. It will check to see if the
command is the first command, for example bk
and if it is, it will print out the word
"backword" and then the number. It checks for every command.
Here is a table with the commands and what your
program should print out.
Command |
What your program prints |
list
|
fd number
bk number
rt number
lt number
pu
pd
home
list
|
fd n
|
forward n
|
bk n | backward n |
rt n | right n |
lt n | left n |
pu | Pen up, move without drawing |
pd | Pen down, draw lines |
anything else | home |
If you want to, use functions for printing. For example, you might
have this function to call in case the user types fd number
def forward():
print "forward",
and this one to print the number
def number(x):
print x
To use these, suppose the user has typed fd 20, and
variable comm contains "fd" and variable num contains 20.
Your program would have
forward()
number(num)
after it has determined that the use has entered fd.
- After getting the command structure to work, for every possible command,
use graphics to actually move the
turtle in GraphWin object:
- Cut and paste the statements from lab8.py
at the top of the main() function definition, into hw8.py.
These are the statements that create a GraphWin object, set the
coordinates, initialize variables, etc.
-
Then cut and paste the statements we used in Lab8 to implement
the commands
fd num
pd
pu
and place them in the correct places for those commands
in the big if-elif-else statement.
Run the program this far and make sure it works when
the user types in these test commands:
fd 100
pu
fd 100
pd
fd 100
(make sure the variable pd is initialized to be True).
-
Make sure this part also works before moving on.
- What is left to do?
- home
This moves the turtle back to the homepoint, stored in
the variable named home.
- bk num
This one is a lot like fd num.
- rt num
This one changes the angle.
It means rotate right by num degrees (e.g. if num is 90 it's rotate clockwise
90 degrees)
- lt num
This one changes the angle.
It means rotate left (counter-clockwise) by num degrees.
- list
This one does not affect the turtle, so leave it the way it is.
- Here's a good test. Make sure angle is initialized to 0
before the while loop.
Run the program and type in the following commands:
pd
fd 150
lt 90
fd 150
lt 90
fd 150
lt 90
fd 150
This should cause the turtle to draw a square.
Try some other shapes out. How about an equilateral triangle?
Try out some backward commands as well.
- Make sure your filename, desc, etc. are done.
Also make sure to place description header comments before
any function definition you use. Make sure to include some
comments through your code to help clarifying it.
But you should not include one comment for every line.
It makes the code too hard to read.
- To submit:
submit homework8 hw8.py
Also submit the image file your program uses. For example, if you
use an image stored in file turtle.gif, you will also
submit homework8 turtle.gif
Do this even if you used franklinTurtleSmall.gif.