Project 4: Playing with objects

The goal of this project is to practice writing a simple class.

1. Introduction

One of the purposes of objects is for them to be components in a software system. Many programs written in an object-oriented language are viewed as collections of interacting objects. In this project, I am providing almost the entire system for a program (including a graphical user interface). Your task is to develop one class to complete the system.

The program is a game which involves a series of levels. In each level, the user controls a character which must collect treasures and avoid monsters. The character is controlled by five buttons: to move up, down, left, and right, and to disappear or reappear. The monsters will move toward you, and if one "gets" you, the game is over. The character collects treasure by moving into a space that current holds a treasure. When all treasures in a level are collected, then the level is cleared and you move on to the next level. When all levels are completed, the game is won.

As mentioned above, one control button makes your character appear or disappear. The advantage of disappearing is that the monsters can't see you (but they can still get you if the happen to move into you-- or if you happen to move into them-- while you are invisible). The disadvantage is that you can't see yourself either, and you can't collect any treasure while you are invisible. In the lower levels, the monsters will stop moving if they don't see you, but in the higher levels, they will remember where you were when you disappeared and keep moving toward that spot.

2. Setup

Make and move into a new directory for this project and copy the following file from the course public directory:

cp /homeemp/tvandrun/pub/235/proj4.jar .
cp /homeemp/tvandrun/pub/235/Player.java .

The extension jar stands for "Java archive" and contains a collection of archives Java classfiles. Since there is a complex set of classes for this project, I've hidden them. Player.java is the source file for the class you need to finish.

You can play the game by compiling Player.java and running

java -cp .:proj4.jar MonsterGame

(The flag -cp .:proj4.jar tells the Java virtual machine to look both in the current directory (".") and the file proj4.jar for classfiles. If you're curious to know more about the -cp flag, jar files, and the like, just ask.)

As you try out the game, you'll see a green T standing for the player, and yellow O standing for the treasure, and a magenta M standing for the monster. The buttons, which are supposed to move the player, don't do anything. The monster moves towards the player and gets it, and the game ends. The net result of what you are supposed to do in this project is that the buttons will work and one can play the game.

3. Finishing the Player class

The Player class's contract with the rest of the system is

Your task is to finish this class, according to this contract. You should think about

You are responsible to make sure that if the user clicks (for example) the left button when the player is already on the left edge of the board, then the player does not actually move any further left. Otherwise the program will crash.

4. Turn in

Email your Player.java file to Joe.R.Michalka@wheaton.edu.

DUE: Friday, Mar 2, at 5:00 PM.


Thomas VanDrunen
Last modified: Mon Feb 26 14:49:56 CST 2007