CS 111: Introduction to Computer Science
Winter 2017
HW09: Ocean simulation
Due: Wednesday, 02/15 at 22:00
Prelude
In Lab 06, as a class, we collaboratively coded up
Pacman
, a cool new class using Zelle's graphical library. It
commands more primitive graphical elements such as Circle
and
Polygon
, implements a few standard methods such as
draw
, undraw
, and move
, a few important
methods such as __init__
, makeParts
, and
step
, and a method reverse
just for fun.
I polished it a bit after class and put it as pacman.py. You can download and run it directly, or
use pacmantest.py (requires
pacman.py
to be in the same directory) to run a more extravagant
test. This demonstrates how pacman.py
can be run directly but
also import
ed as a module.
If you compare pacman2.py (what we had in
class) and pacman.py
(polished), you'll see that I mostly used
what you wrote. I added undraw
, reverse
, and lightly
modified step
, draw
, and makeParts
to
make the pacman change direction when it hits the right side of the window.
The program
For this assignment, your job will be to write a program showing an animation of a simple underwater scene. Your final program, when runnning, should display a randomly generated collection of fish and bubbles against a suitably watery background (just blue, for example, is suitably watery). As the animation proceeds, the fish should swim back and forth (switching directions when they hit the sides of the window) and the bubbles should rise. When a bubble hits the top of the screen, it should disappear and be replaced by a new randomly generated bubble at the bottom of the screen.
Put your Fish
class in a file called fish.py
. Put
your Bubble
class in a separate file called
bubble.py
. Your main program should be in a file called
ocean.py
, which will import
both the fish module and
the bubble module.
If you find this program straight-forward, feel free to add extra features--waving plants (that generate bubbles), vicious sharks (that eat fish), submarines (that explode when clicked), self-solving Rubik's cubes, or whatever strikes your fancy. These extra goodies, however, should be pursued only for your own pleasure; a straight implementation of the ocean as described in the previous paragraphs will be eligible for full credit.
Like the previous graphics homework, I may show some especially impressive
work in class. If you do not want your work shown, please mention this in the
comments at the top of your ocean.py
.
More details
-
For the basic motion of the
Fish
andBubble
objects, you should feel free to borrow liberally fromPacman
. Generating random objects, which you'll need to do, is demonstrated in pacmantest.py, so again, borrow and adapt as needed. -
Replacing the bubbles at the top with bubbles below require some care: when
a
Bubble
object disappears at the top of the ocean, make sure to either remove thatBubble
from your bubble list, or reuse it by resetting itsx
andy
coordinates. Don't just build an ever largerbubbleList
: this will gradually slow your program down as it keeps trying to move bubbles that are no longer visible. -
As always, I strongly recommend that you create an iterative development
plan for yourself. What happens in Phase 1, Phase 2, Phase 3,
etc. of your work, and how will you test each phase? Even if you get
only a couple phases done (e.g., your
Fish
swim in only one direction and yourBubble
s don't regenerate), you'll have a functional and partially complete project to hand in, which will be worth more points than if you attempt to do everything specified but your code does not run. -
For good style, follow the example of the
Pacman
module: running the file directly viapython3 pacman.py
shows a simple module demo (test), butimport
ing the module does NOT run anything. This ought to fit well with the iterative development plan from the suggestion above.
Submission
Upload to Moodle three files: named fish.py
,
bubble.py
, and ocean.py
, as described above.
Alternatively, you may use zip
(or tar
) to package
all your project files as a single archive file, and upload that instead. This
is especially applicable if you add a lot of extra features (each in its own
file!), and it becomes cumbersome to upload one file at a time to Moodle.
If you create other features (as optional challenge), each class should be in its own file, where the name of the class is the capitalized version of the file name. Submit those as well, obviously.
Grading
- Style/comments [4 points]
- All the style guidelines from previous assignments, as always.
- When run on the command line,
fish.py
andbubble.py
run simple demos. These demos should not run when they are imported byocean.py
. - Follow naming convention: class names are capitalized; each class is saved in a file with the same name, in lower case. (Class
Shark
should be saved in fileshark.py
.)
- Requirements outlined above [16 points]
- ocean [4 points]
- fish [6 points]
- bubble [6 points]
This is a somewhat long assignment. Start early, have fun, and discuss questions on Moodle.