Assignment 4: Inheritance Practice

Objective: Create a small game and practice writing child classes.

Due: Before the next class (Friday).

Set Up

Copy the directory /home/courses/cs209/handouts/assign4 and its contents into your cs209 directory.

Part 1: Compiling and Running the Code

  1. Compile the code using javac *.java or javac Game.java.
  2. Run the game by executing java Game
    You should see a black pop-up window with a little dude--the professor--in the bottom right corner of the screen, walking.
  3. Using the arrow keys, move the professor around the screen. Use the space bar to stop him.
  4. To stop the program, close the window.

Part 2: Understanding the Code

  1. Start by looking at Game.java. There is a lot of code that you don't need to worry about. Focus on the main and animate methods as well as the instance variables.

    The main method creates a new Game object, including setting up the window. main also calls the animate method, which starts the game running.

    The animate method creates the professor and moves the professor. The professor's direction is determined by pressing keys.

  2. Then, look at GamePiece.java. (This class contains some poor coding practices; bear with me--we haven't seen the techniques to fix them yet.)

    Note the instance variables, the constructor, and the available methods--especially the
    move(Game game) method.

  3. Now, look at Human.java. Look at the class's constructor and the move method. Hopefully, you're now seeing how the pieces fit together.

Part 3: Creating Child Classes

  1. Create a Goblin class, which inherits from GamePiece. There are several animated gifs to choose from. A Goblin should "chase" the professor instead of just standing there, which is what the inherited GamePiece's move method does.
  2. Create a Treasure class, whose image is the gem. The gem will move around the window, periodically and randomly. Note that the Treasure should not change positions during every iteration of the animate loop. (You may choose to enforce this restriction either in the Treasure or Game class, but it probably makes more sense in the animate method.) Also, the top 20 pixels of the window (indicated by YMIN_DISPLAY) are covered by the menu bar.

Part 4: Updating the Game Class

  1. Edit Game's animate method. Create a Goblin object and a Treasure object. Start the Goblin in the upper left corner. The Treasure should be at a random spot on the window.
  2. Call the Treasure and Goblin's move method to make them move. (Recall that you probably don't want the Treasure to move constantly because it will be too difficult to catch.)
  3. Call the Treasure and Goblin's draw method to display them. The window "refreshes" each frame, so you'll need to draw each object, even if you don't move it.

Extra Credit

Complete all of the above requirements before doing extra credit.

Add the overlap method to the GamePiece class. The signature for overlap will look something like
public boolean overlaps( GamePiece gob ). It will return true iff the GamePiece overlaps the GamePiece passed to the overlap method as a parameter. Note that, if you haven't done so already, you'll need to adjust the value of the img_width or char_width variables of the respective child classes, depending on the size of animated gif you chose. Try a value of 15 to start.

Clearly, there is a lot more that can be done on this assignment--actually make the game stop, winning or losing, adding goblins (with different types of movements), ... Be creative, but keep the code neat.

Turning in Your Assignment

Copy your assign4 directory into your turnin directory.

There is no printed part of this assignment.

Grading (100 pts)

You will be evaluated based on the correctness and OO style of your classes.