Objective: Create a small game and practice writing child classes.
Due: Before Wednesday's Class
Recommendation: Complete at least through part of part 3-4 before Monday, for timely completion of the assignment and to identify any issues/questions/comments you may have.
Copy the
directory /csdept/local/courses/cs209/handouts/assign5
and its contents into your cs209
directory.
javac *.java
or javac Game.java.
java Game
To help you with understanding the code, here are the Java docs for the game code. Refer back to them while completing the assignment, as it may help you to figure out what is inherited and who can do what.
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.
GamePiece.java. (This
class contains some less-than-ideal 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.
Human.java. Look at the
class's constructor and the move method. Hopefully, you're
now seeing how the pieces fit together.There should be a lot more thinking than there is coding for this part. The amount of code you need to write is not huge. Think about the behavior of each class and how that should be implemented.
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.
Since you are overriding the move method, the method should have
exact same signature (i.e., the same parameters) as the parent's
move method. How can you make the compiler help enforce that the
child class's method has the same signature as the parent?
The Goblin's move method should look completely
different from the Human's move method.
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's move method
or Game's animate method, but
the move method is the more appropriate place for
easier design.) Also, the top 20 pixels of the window (indicated
by YMIN_DISPLAY) are covered by the menu bar. 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 in the
window.move method to make
them move.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.
Cannot be completed until after Monday's class.
This assignment is an example of a typical design/implementation process. Start with your original code design: inheritance from GamePiece class. You realize it could be designed better, so you change the design/implementation. With the revision, it is now easier to add new functionality to Game.
Now for the refactoring of your code:
@Override annotation on the move
methods of your child classes, you should add that. If you get
errors, fix the errors (most likely caused by not using the
appropriate parameters).GamePiece class so that it is an
abstract class with an abstract move
method.Game class's animate
method to have an array of GamePiece objects, which contains the
GamePiece objects that you just created. Iterate through the
objects, calling the move and draw method on
each object. (If you're
getting errors, you likely didn't override the move method
correctly.) Is there any other way you can make the code more concise? GamePiece class be
final methods. The coding part of this assignment should be straightforward. The explanations in your comments are more important.
GamePiece,
GamePiece abstract is a better
approach to the code's designfinal in
comments.Game,
move method on
a GamePiece object--a method that is abstract--and the
application does the "right" thing 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 gp ). 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.
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 fmovements), ... Be creative, but keep the code neat.
Copy your assign5 directory into your
turnin directory, using the turnin.sh script.
You will be evaluated based on the following criteria: