CSC15 Lab 6

CSC15 Lab 6: Professor and Goblins

Due one week from date assigned



For this assignment you are to write a small video game using animated gifs. You will need to understand the principles of object oriented programming that I have been teaching you, especially the example with the bouncing circles. First, download the file goblinapp.java from the homepage. If you are not on Hofstra's Solaris machines, you'll also need to download the animated gif files (right click on images above). If you are using the Solaris machines, however, the images already reside in the shared directory /shared/csc/local/stuff/.

The program you downloaded runs by itself - you'll see a little human figure ("the professor") in the lower right corner, which you can control using the arrow keys and the space bar. The figure wraps around the edges of the window.

The file itself contains two classes: "goblinapp" is the application class and includes the method "animate", which you will modify. The "human" class is for the "professor" object. You will be creating two additional classes, one for goblins and one for the diamond/treasure, both of which will be similar to the human class. The graphical image of the professor is an animated gif. However, internally the object is represented as a circle, and thus the key attributes of the object are still the x,y coordinates and the radius. STUDY THE HUMAN CLASS FIRST.

Step 1: Create the goblin class. It will be mostly be similar to the human class. However, you should make it so that it bounces off of the edges of the screen instead of wrapping around. You'll also need to add the overlap function, which will return true iff the goblin's imaginary circle overlaps that of the human object (passed to the overlap function as a parameter). This also means you'll have to put the distance function inside your goblin class. You'll need to adjust the value of the radius of the goblin (depends on size of animated gif you chose). Try a value of 30 to start with.

Step 2. Similarly, create a class called "treasure" that represents the diamond. The diamond will move around randomly, so you can choose to change it's position by random amounts inside the move method of the class. In fact, it's possible to write this class without the dx and dy variables.

Step 3. Edit the animate function inside the goblinapp public class. Here, you'll need to create a goblin object and a treasure object. There are a few points to be careful about. First, make sure you distinguish the CLASS from the OBJECT. For example, "professor" is an OBJECT of CLASS human. Secondly, make sure you understand that creating an object is a two-step process: first declare a variable, then assign to the variable a "new" object:

  human professor;
  professor = new human(XBOUND-50, YBOUND-50,brush);
Also, in this program the DECLARATION of the variable professor is inside the CLASS goblinapp, but the actual object is created inside the animate FUNCTION. This is because other functions of the class will be responsible for changing the movement vector of the professor (function "KeyPressed"). For your goblin and diamond object, however, you have the choice of declaring the variables inside the class or inside the function, BUT DON'T DECLARE THEM MORE THAN ONCE!!!

How do you make the goblin chase after the human? Using the objects' getx, gety, getr, getdx, and getdy functions, you can find out where each object is, and in which direction it's moving in. You can then call the setdirection function of your object to move the objects. It's your choice what kind of strategy you want to adopt. In the sample program I showed you, if the x-coordinate of the professor is greater than the x-coordinate of the gremlin, you adjust the gremlin's movement vector to move right (positive dx), otherwise, you adjust it to move left (negative dx). Do the same thing to determine dy. To move the diamond object around randomly, use the randint function I gave you (find it in cicles.java), and call setdirection with two random values.

When either the professor gets the treasure or the goblin catches the professor, the game should end. The while loop in the animate function stops when the stop boolean variable becomes true.

Step 4: After getting the program to work with one goblin, make the game harder by adding another instance of the goblin class. Furthermore, you should modify the constructor function so that you can load a different animated gif file (as well as a new radius value). In other words, you'll need to use additional parameters for the constructor function. The two objects should have different strategies. For example, one can go after the professor and one can protect the treasure.

______ NO TELEPORTATION! ______

You are encouraged to be creative with this assignment, but PLEASE give the professor a chance to win! Don't have your goblins move way too fast or jump right infront of the professor (that's not nice!).

______ EXTRA CREDIT! ______

After completing the assignment, you can make it so that, once the professor gets the treasure, he becomes a superhero (find an appropriate gif), and it'll be the goblins' turn to run away from him. Extra credit will also be given for other, exceptionally creative designs that show your abilities as a programmer.

______ EXTRA EXTRA CREDIT!! ______

After completing the assignment AND the extra credit, you'll notice that the three classes for human, goblin, and treasure are all mostly the same, though not entirely the same (their move() methods will be different). There's a way that all three classes can share much of the same code through a technique called inheritance. In this scheme you'll define a common "superclass" from which the three "subclasses" of your program will inherit a common set of features. I can't explain fully what inheritance is and how to do it in a few lines, so you'll have to do research (text book, web pages, etc...) to figure out how that's done.