Assignment 4: Inheritance Practice

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

Due: Before the next class on Friday, Sep 19.

Set Up

Create a directory for assign4 within your cs209 directory.

Copy the contents of /home/courses/cs209/handouts/assign4

Part 1: Compiling and Running the Code

  1. Compile the code using javac *.java. You may get a warning about deprecated APIs. Don't worry about that.
  2. Run the game by running 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.

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.

    The animate method creates the professor and moves the professor. The professor's direction is determined by key presses.

  2. Then, look at GameObject.java. (This class contains some bad 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

  3. Now, look at Human.java. Note the reduction in code size. Look at the class's constructor.

Part 3: Creating Child Classes

  1. Create a Goblin class, which inherits from GameObject. There are several animated gifs to choose from. Ideally, a Goblin should bounce off of the edges of the screen instead of wrapping around (which is what the GameObject's move method does).
  2. Create a Treasure class, whose image is the gem. The gem will move around randomly, so you can choose to change its position by random amounts inside the move method of the class.

Extra Credit

Complete the next part before doing extra credit.

Add the overlap method to the GameObject class. The signature for overlap will look something like
public boolean overlaps( GameObject gob ). It will return true iff the GameObject's imaginary circle (the radius instance variable) overlaps that of the GameObject passed to the overlap method as a parameter. Note that you'll need to adjust the value of the radius of the respective child classes, depending on the size of animated gif you chose. Try a value of 30 to start with.

Part 4: Updating the Game Class

  1. Edit Game's animate method. Create a Goblin object and a Treasure object.
  2. Make the Goblin chase the professor by determining the direction the goblin should go (using the objects' getter methods) and calling the Goblin's setDirection method. It's your choice what kind of strategy you want to adopt.
  3. Call the Treasure and Goblin's move method to make them move. Note that you probably don't want the Treasure to move constantly because it will be too difficult to catch.

Extra Credit

Clearly, there is a lot more that can be done on this assignment--actually make the game stop, winning or losing, adding additional goblins, ... 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 (50 pts)

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