CSC 111: Intro to Computer Science through Programming

Homework 9

Due: Thursday, Apr. 21 at 11:59pm on Moodle

Our last homework assignment is on classes. The goal is two write two separate classes, then use them to create an animation. For this assignment, there are less specific instructions - part of the goal is to be able to break down a complex task into small steps while thinking about the larger organizational structure of your code.

For this assignment, you are welcome to discuss your work with other students, but all code you submit should be your own original work. It should be produced and understood by you, without looking at anyone else's assignment.

Credit for this assignment: Joe O'Rourke

Quiz 9

Please take the short Week 9 quiz on Moodle (also due by Tuesday night). This is to help me understand what areas need work, and also help you understand what concepts might need extra practice. It is not a large part of the overall homework grade. Just for the quiz part, do not discuss the questions with other students, TAs, etc. (Note: quiz usually posted Wednesday afternoon.)

Swimming Fish

The high-level goal of the assignment is to create an aquarium of swimming fish, like the video shown below. You will have a Fish class and a Bubble class (+ other classes if you like). Pick one fish and watch its movement - when it reaches one side of the tank it wraps around and comes back on the other side, continuing to swim in the same pattern indefinitely.

There are not specific steps, but the required functionality is described below.

Requirements

  • Fish class

    Your program should contain a Fish class with the following:

    • The constructor __init__.

    • Instance variables: up to you, but they could include: x, y, radius, color, speed, etc

    • The method draw(window).

    • The method move(dx, dy).

    • Maybe a wrap(width) method, or this could be done in move(..).

  • Fish properties

    Each fish should have at least three components: body, eye, and tail. Not all the fish should be moving at the same speed, and the fish should be different colors.

  • Fish wrapping

    Each fish should swim in a "circle" around the tank, wrapping around one side and emerging on the other. This wrapping functionality should be done within the Fish class, not main. You could either put this functionality in the move(..) method, or in a separate wrap(..) method that is called along with move(..). Either way, one of your methods should take in the width of the window as an argument, so it can check whether or not the fish needs to be wrapped around.

  • Fish bobbing

    The fish should move horizontally, but should also bob up and down while moving as shown in the video (this is a minor detail which will not be worth very much, so I might leave it for last).

  • Main function

    In the main function, you should create a list of Fish. Then using a while loop, continue to loop over the fish indefinitely, moving the fish one by one (see Day 30 notes for more details). If the user clicks the window, the animation should stop.

  • Bubble class

    Lastly, create a Bubble class that will send bubbles floating upwards (they can also wrap on the bottom of the window). If you would like to replace this with another class in the spirit of the assignment (swaying kelp, another type of fish, etc), that's fine as long as it moves in some systematic way.

Extensions

Optional variations:

  1. Write another class (different type of sea creature or vegetation) and include this in your animation.

  2. Make the background more interesting (one suggestion is to include a large cat face...) you could also load an image as the background, similar to the map image from Homework 8.

  3. Make the animation interactive (i.e. something happens when you click on a fish).
If you do a variation, email me a video (QuickTime for Mac is built-in, and here are some Windows options) - I would love to show some examples!

Submit

You do not have to submit a transcript for this homework. Instead, take a representative screenshot of your animation and submit that with your code. Make sure that you've thought about variable names and code structure, and that your code is well-commented. For each method and helper function, create a docstring describing the functionality and input/output.

TO SUBMIT ON MOODLE:

  • hw9.py

  • fish_swim.png