October 28

Lecture Overview

Design

Example

A larger example of classes and objects:

The problem is to simulate the Predator-Prey Relationship using python. The specific example is fish and bears. The following are the rules of the simulation:

The design is bottom up: first we will create classes for Fish and Bears with some useful methods without a clear picture of how they will be used. For example, the Bear class will need properties x and y for its location, a property for how long it has been since the bear has eaten, a method to check if there is a fish to eat, and so on. After listing these properties and methods for the Fish and Bear class, we notice that there are several similarity (e.g. both store x and y and have to move randomly). So bottom up design say create a class for this similarity: we create an Animal class and then make both the Fish and Bear class inherit from the Animal class. Note that in bottom up design, we made the Animal class after we initially designed the fish and bear classes. The results of this design are in day27_animals.py. Note that I created day27_animals.py without a clear picture of where these classes will exactly be used.

The next thing we need is a World class to track the positions of the Fish and Bears. Note that the World class can take advantage of the pre-existing classes without having to care about the details of Fish and Bears, just that each class implements the rules for Fish and Bears. We then give the world some useful methods like adding and killing animals, drawing the animals on the screen. This is day27_world.py.

Finally, in day27_simulation.py, we combine all of these tools into our program.

Exercises

No homework, finish up the project.