Lab 5: Animation and Strings
Goals
After the lab, you should be proficient at
- solving problems with a graphics API
- solving basic string problems
- creating basic web pages to display images
Linux
- As usual, create a directory for the programs and output you develop
in this lab.
- Copy graphics.py in
/home/courses/cs111/handouts/lab5/
into your lab5
directory.
Objective: Programming in Python
Reference Material for Graphics Programming
We'll practice writing several Python programs, each in their own text
file. Name the files lab5.1.py through lab5.5.py.
Your programs will be graded on correctness, style, efficiency, and
how well you tested them. Make sure you adhere to the good
development and testing practices we discussed in class. Your code
should be readable and your output should be useful and
well-formatted.
After you've developed a correct solution to each program, restart
IDLE or close and reopen the IDLE "shell" by running the program
again (using F5), demonstrate that the program works using several good
test cases, and save the output to a file named lab5.x.out, where x is
the problem number.
- (35) Create a program that draws "something significant", such as a
scene, a house, or a face, using the graphics library. Here are some guidelines
to follow:
- Your program should keep the drawn face/house/scene on the
graphics window until the user clicks the mouse. Then the graphics
window should close and your program should end.
- Use a variety of colors and several different types of shapes.
- For symmetrical features (such as eyes or ears), remember to
use the clone method to make a copy of the original shape. Then
draw the cloned shape in the window and move it to the desired
location.
- You may want to use the
setCoords
method of
the GraphWin
object to adjust the coordinates of the
window. (See the reference for more information
about setCoords
.)
Screen Capture:
- To save the image you created, in the terminal, open the
program
xv
, using the command xv &
.
- Right click on the window with the fishes that pops up.
- You should get an "xv controls" window that looks like:
- Click the "Grab" option.
- You should get a window that describes how to use "Grab".
- Make sure no other windows are covering your image window. Click
"Grab" and then use the left mouse button to click on the window that
your image is displayed in. Your best bet is probably to click in the
"header" at the top of the image.
- You should get another "copy" of your window, but this time, the
window is actually an image and its label reads something about "xv".
- Back in the "xv controls" window, click "Save". You should get an
"xv save" window that looks something like this:
- In the "Save file:" box at the bottom of the screen, give the
image an appropriate name, with a ".gif" extension (if that's the
format you selected for the file). Then click "OK".
You've saved the image!
Some previously created images to inspire you:
Images From Last Semester

- (20) Add some sort of animation to the picture you created above.
For example, you could make the entire face move around the window or
you could have one feature of the face move (such as a blinking eye).
If you want to animate a group of objects, moving together, you can
use the GraphObjectGroup class. There is an example of using the
GraphObjectGroup class in the example.py
file in the
/home/courses/cs111/handouts/lab5 directory
.
- (10 pts) Write a program that takes as input 3 strings and prints
out the alphabetically first word of the 3 strings. Example output:
Enter the first word: zebra
Enter the second word: monkey
Enter the third word: pig
The alphabetically first word is monkey
Enter the first word: dog
Enter the second word: black cat
Enter the third word: 13
The alphabetically first word is 13
Enter the first word: black
Enter the second word: black cat
Enter the third word: blacky
The alphabetically first word is black
- (15 pts) Write a program that removes spaces from a string that is
input by the user. Since strings are immutable and given our current
knowledge, you'll have to create a new string that contains all the
characters except spaces from the original string. For full credit,
only go through the string removing spaces if the string
contains at least one space.
Example output:
What is your string? The Beatles
Your string without spaces is TheBeatles
What is your string? Help!
Your string without spaces is Help!
- (10 pts) Write a program that prints a string (input by a user) backwards.
Example output:
What is your string? The Beatles
Your string without spaces is selteaB ehT
What is your string? Help!
Your string without spaces is !pleH
Objective: Creating a New Web Page
It's time to revisit our web pages. (You knew we'd get back to this sooner or later!)
- Go into your
public_html
directory.
- Copy your
index.html
file into a file
called lab5.html
(in the public_html
directory).
- Copy the screen capture image from your lab into your
public_html
directory.
- Open
lab5.html
in jEdit.
- Modify the Lab 5 web page to have an appropriate, title, header, and information.
- Modify your Lab 5 web page to display the image you created.
- Modify your index.html page to link to your Lab 5 web page.
Finishing up: What to turn in for this lab
- IDLE and jEdit may create backup files with the "~" extension.
Delete these files from your lab directory to save paper when you
print.
- Copy your lab5 directory into the
turnin
directory.
(Review the UNIX handout if you don't
remember how to do that.)
- Before printing, move the graphics.py, graphics.pyc (actually,
this one can be deleted rather than moved), and your screenshot out of
your
labs/lab5
directory; otherwise, the print out will
be long and screwed up. You can move those files back into
your lab5
directory after you've printed. In other
words, you should only have the .py files you wrote and the .out files
you created in your directory when you print.
- Turn in your printed lab assignment, using the
printLab.sh
command. You should probably print from the
labs
directory.
Before you print, view the file to make sure it's not too long
or has weird characters in it from the .pyc
file. One
command to view the file is gv lab5.ps
Print the file using the lpr
command introduced in the
first lab.
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 1:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your
assignment!
Grading (100 pts)
- Python programs: 90 pts; see above for breakdown
- Web pages: 10 pts (both your index.html page and the lab5.html page)