Tino Intro To Java

Last modified: January 21, 2024

Lab A4.2 Banana Bat

In this lab you will create little game starring both bats and bananas. Click here to download a demo of this lab.

Creating the Bat Class

In order to create the objects for this game you need to download these images.

Create a new class called Bat and set its image to "bat_01.png"

Create two new instance variables, one to track the current image and the other for its speed (intialize the speed to 3).

Every 10 frames the bat changes between "bat_01.png" and "bat_02.png" images. (You can do this by looking into the Actor API for a method to set an image of an actor).

Mouse User Input

For mouse user input, you need to use some methods from the Greenfoot Class.

In the Greenfoot class API there is the getMouseInfo() method that returns a MouseInfo object.

As shown in the image, the method's access modifier is static.

A static method must be called on the class rather than an instance of the class. This means that you would call it like the following:

ClassName.method()

The return type of the getMouseInfo() method is MouseInfo, which is another class in the greenfoot package. If you click on the getMouseInfo() method, you will see more details about how the method works.

When reading the detailed description for getMouseInfo(), you will see that it sometimes returns null because there is no mouse event. If you call a method on null, the program will throw a NullPointerException, so it is important to check if the mouse info is null before trying to call methods on it.

Putting all this together, we can get a MouseInfo object:

MouseInfo mouse = Greenfoot.getMouseInfo();

Process the mouse information by first checking if mouse is not null and then calling methods on mouse. Examine the MouseInfo class documentation to see what you can do with a MouseInfo object

Finishing the Bat Class

Every frame, the bat should move towards the mouse by an amount equal to its speed property. (Hint: Use the turnTowards method inherited from the Actor class).

Additionally, when you turn the bat towards the mouse, make sure you turn the bat back to its original orientation after moving so the user never sees the bat image rotate.

Creating the Banana Class

Create a new class called Banana, and set the image to "bananas.png". When the banana touches a bat, remove the banana from the world.

Hint: When you use the keyword this in an instance method, it refers to the object the method was called on. So if an actor wants to remove itself, it should pass this to the removeObject method.

Creating BananaWorld Class

Rename the MyWorld class to BananaWorld and update the constructor accordingly (the constructor name has to match the class name).

In the constructor, add a new Bat in the center of the World.

Just like Actor, the World class also has an act() method which can be overridden to perform actions such as spawning actors. Override the act() method in BananaWorld so that every frame, there is a 1% chance that a banana will spawn in a random position fully on screen.

You can generate a random number by using the getRandomNumber() method in the Greenfoot Class or use the random() method in the Math class. Note that both of these methods are static, so you will need to call them on the class like you did with the getMouseInfo() method.

Examples of these methods being used:

int n = Greenfoot.getRandomNumber(5); // an integer from 0 (inclusive) to 5 (exclusive)
int offsetN = n + 2; // an integer from 2 to 7
double num = Math.random(); // a decimal from 0.0 (inclusive) to 1.0 (exclusive)
double bigNum = num * 5; // a decimal from 0.0 (inclusive) to 5.0 (exclusive)
double offsetNum = bigNum + 2; // a decimal from 2.0 (inclusive) to 7.0 (exclusive)

Gold Version (Optional)

Optional Challenge: Make the full Banana Bat game

You can find the full game here.

It will take significant time and effort to make the full game. You can keep working on it and adding features over time.

Creating the TRex

The TRex wanders around slowly and randomly.

Every frame, there is a random chance that the TRex suddenly charges at the bat. You can do this by using the turnToward method in the actor class.

The bat will lose health when contact is made with the TRex.

The text on the left-hand side of the canvas shows the health of the bat, and the right side shows the number of frames the bat has survived for. When the bat eats a banana, its health increases by 1. You can add the text to the canvas by using the showText method in the World class.

Also, when the bat is hit by anything (initially just the TRex) add an explosion and play a sound.

The explosions are animated by changing the image every frame (or couple frames depending on the speed of the animation). When they have gone through all the images for the explosion, they remove themselves.

The TRex fires missiles randomly, however, as the bat survives longer, the chance increases. The missile moves toward the bat (using the turnTowards method).

Eventually (after a while), the TRex starts firing missiles that split into more missiles on impact. These missiles travel straight in all directions from the point where the missile exploded.

Finally, after the player has survived for a long time, the TRex starts firing arrow missiles that lock on the player and travel directly at them (in a straight line).

The buttons in the game are actors that respond to mouse events.

Submission

Zip your entire project folder and submit it below.

  • Name your project folder PX_LastName_FirstName_BananaBat.
  • Right click on your project folder and Send To -> Compressed (zipped) folder. On a mac compressing is similar, using a right click, but has a slightly different option.
    • You should end up with a zip file named PX_LastName_FirstName_BananaBat.zip. For example if you were in 3rd period and named Michael Wang, then you would name the file P3_Wang_Michael_BananaBat.zip
    • Upload the zip file.

You must Sign In to submit to this assignment

Dark Mode

Outline