Objective: Create a small game and practice writing child classes.
Due: Before the next class on Friday, Sep 19.
Create a directory for assign4
within
your cs209
directory.
Copy the contents
of /home/courses/cs209/handouts/assign4
javac *.java
. You
may get a warning about deprecated APIs. Don't worry about that.
java Game
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.
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
Human.java
. Note the
reduction in code size. Look at the class's constructor.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).
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. 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.
Game
's animate
method. Create a
Goblin object and a Treasure object.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.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.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.
Copy your assign4
directory into your
turnin directory.
There is no printed part of this assignment.
You will be evaluated based on the correctness and OO style of your classes.