Now we have a basic game we can improve it a bit. Let’s give Chilly something to eat!
First download the images for the fish here and add them to the folder resources\fish
.
The Fish class has, just like the Player class, an int x
, int y
, int width
, int height
. Besides that a fish has one more properties:
String type
, that either is tuna, salmon, shrimp, herring.Now create a class fish with these properties and a constructor that looks like this: Fish(int x, int y, String type)
. Every fish has a width of 75 and a height of 50.
Note that the image String can be built up using the type String.
Don’t forget to add a fish to your game. So your init and loop function should look like this:
public void init() {
player = new Player(100,100);
fish = new Fish(400,400, "tuna");
}
@Override
public void loop() {
...
//Draw the player
player.draw();
//Draw the fish
fish.draw();
}
First, check the video about collision detection in the SaxionApp here.
Please note that we simplify the boundingbox a bit by generating them on the fly as they where needed. This is not efficient (because Java creates new boudingboxes every time we check a collision), but it’s a easier to read and understand.
public Rectangle getBoundingbox() {
return new Rectangle(x,y,width, height);
}
public boolean collidesWith(Fish fish) {
return this.getBoundingbox().intersects(fish.getBoundingbox());
}
loop()
method:
//Check collisions
if (player.collidesWith(fish)) {
System.out.println("Yummie!");
}
You should see a lot of Yummie!
in the console as soon as Chilly walks over the fish.
Before the real fun starts we will start using some constants in this game, because this will help us a lot later on.
public static final int SCREEN_WIDTH = 1024;
public static final int SCREEN_HEIGHT = 768;
Update your code! Use the constants at least in the main method where you init the game loop (The main method should look like this: SaxionApp.startGameLoop(new ChillyGame(), SCREEN_WIDTH,SCREEN_HEIGHT, 40);
). For last weeks advanced suggestions you did some calculations with the screen width and height. Update these also.
public Fish generateFish()
in the Fish class. It should like this (fill in the dots youself:
public static Fish generateFish() {
//Generate a random position for the new fish
int posx = SaxionApp.getRandomValueBetween(0, ChillyGame.SCREEN_WIDTH - 75);
int posy = SaxionApp.getRandomValueBetween(0, ChillyGame.SCREEN_HEIGHT - 50);
//Set type of the newly created to fish to tuna, salmon, shrimp or herring.
...
//Create an instance of the fish and return it
...
}
public void init() {
player = new Player(100,100);
fish = Fish.generateFish();
}
Eating the fish is very easy now.
Update your code in the loop()
method so that the fish is removed after you collide to it. Insytead of setting the fish variable to null
we generate a new fish (of another type and at another location) instantly!
if (player.collidesWith(fish)) {
//Enjoy the fish
//Generate a new fish!
fish = Fish.generateFish();
}
To make this a real game we’re going to add health to the player (a.k.a. Chilly). The health goes down over time, but increases when Chilly eats fish.
First update the Player
class:
int health
to the player. The health starts with a value of 100. Also add a method public int getHealth()
.public void decreaseHealth()
which decreases the health of our hero by 1.public void increaseHealth(int amount)
which increases the health of our hero by a given amount.Then, add a healthbar drawing to loop()
method. You can do that by drawing a rectangle. Use player.getHealth() * 5
for the value of the width of the bar.
After that do the following to make sure the health decreseases over time.
Add a constant to the ChillyGame
class: public static final int HEALTH_DECREASE_INTERVAL = 12;
. It represents the number of frames the should pass before the health drops by one. This means that the health drops about every half second. (because we do 25 fps).
int healthDecreaseTimer
in the class ChillyGame
. Add code to the loop()
method to decrease the health everytime the healthDecreaseTimer runs out:
//Decrease health every health decrease interval
healthDecreaseTimer++;
if (healthDecreaseTimer == HEALTH_DECREASE_INTERVAL) {
player.decreaseHealth();
healthDecreaseTimer = 0;
}
if (player.getHealth() == 0) {
SaxionApp.quit();
}
Type | Nutrition |
---|---|
tuna | 10 |
salmon | 5 |
herring | 2 |
shrimp | 1 |
increaseHealth(int amount)
method).After you’ve done this we’re going to add huge upgrade.
We will not spawn only one Fish, but we will spawn a new fish every 5 seconds. Every time a fish is hit it will be removed from the game.
Start by replacing the Fish instance variable with an ArrayList of Fish objects.
ChillyGame
class: public static int FISH_POPUP_INTERVAL = 125;
. It represents the number of frames that should pass between each fish popping up. 125 frames at 25 frames per second means 5 seconds.Create an instance variable int fishPopupTimer
in the class ChillyGame
. Add code to the loop()
method to add a fish to the list everytime the fishPopupTimer runs out.