Appendix A. The Final Virtual Zoo
This appendix presents the final code for the virtual zoo program, which is covered in Part I. This final version uses the techniques we studied in Part II to add graphics and interactivity to the program.
Tip
You can download the code for the virtual zoo program at http://www.moock.org/eas3/examples.
Note that the code in this version of the virtual zoo has been updated structurally to reflect real-world design practices. In particular, two new classes have been added: FoodButton, which represents a simple clickable-text button, and VirtualPetView, which displays a VirtualPet instance graphically.
The VirtualZoo class has changed in the following significant ways:
It now creates an instance of VirtualPetView, used to render the pet to the screen.
It waits for the VirtualPetView instance to load the required images before starting the pet simulation.
The VirtualPet class has changed in the following significant ways:
The following constants represent the pet’s physical condition:
VirtualPet.PETSTATE_FULL,VirtualPet.PETSTATE_HUNGRY,VirtualPet.PETSTATE_STARVING, andVirtualPet.PETSTATE_DEAD.The instance variable
petStatekeeps track of the pet’s current physical condition.Event listeners are notified of changes in the pet’s physical condition via the
VirtualPet.STATE_CHANGEevent.Event listeners are notified of changes in the pet’s name via the
VirtualPet.NAME_CHANGEevent.To change the number of calories in a pet’s stomach, the VirtualPet class uses the ...