HW 7

User-Interface Programming

Introduction to Programming -- CS 140
Spring, 2008

This assignment is due on Friday, May 9(the last day of classes)

 


Introduction.  In this assignment you will take an existing, working program and add some user controls to it.

DebuggingExercises: There are no debugging exercises this week.

Programming Exercise. Examine the program FoxesAndRabbits.py which implements a predator-prey simulation. In the world of this simulation animals live a predictable life -- each year they try to eat to gain energy. If they are old enough and have enough energy they reproduce. When they become too old they die. This cycle is represented by the method oneYear(self) of class Animal. Class Fox and class Rabbit are subclasses of Animal, so they both inherit this method.

Animals are kept in an AnimalList, which is comparable to the AnimationLists that we kept in other animated programs. When animals are first born they are put in a NewAnimalList rather than the main animal list, and only enter the main list after their first year. This prevents a situation in which new rabbits are born into the end of the AnimalList and then have babies themselves which further expand the AnimalList. If left unchecked this could become a cycle in which the AnimationList gets longer and longer and we never return to the beginning of the list.

In the simulation foxes are represensted by red squares, rabbits by blue squares, and grass by green ones. The rabbits eat only grass. The foxes eat only rabbits. Each animal can only eat if its food is in a square that is adjacent to it. The animals eat by moving to the square containing the food. Rabbits, of course, are killed when the foxes eat them. The grass grows back as soon as the rabbit on it mvoes on. Rabbits only move by finding an adjacent grass square and moving to it. If foxes don't find an adjancent rabbit they can still move in a random direction to any empty adjacent square as they search for food.

If you run the simulation you will see that the only user control it provides is a Quit button. The main() function initializes a few variables, class function RandomlySeedGrid() to put an initial assortment of foxes and rabbits only the grid, and then immediately starts running the simulation one year at a time. Your job is to make the following changes:

Program BasicGraphics.py has code for buttons and scale widgets. You can use this as a template for the controls you need to add for this program.

This homework should be handed in by midnight of Friday, May 9. This is the last day of classes. I will accept late work up to the end of Reading Period, but no later.