Before we proceed a big change is needed! Let’s add inheritance to our ChillyGame first. After that we will see how global warming impacts the life of Chilly the Penguin.
As you probably noticed last week we did a lot of copy/paste work to create the Fish class. That might be an indication that some redesign is needed. Wouldn’t it be a good idea if there is GameObject class that can be extend to create GameObjects? Check this classdiagram:
Now it’s up to you to implement this diagram into your project:
Fish
or Player
class.Fish
and the Player
extend from GameObject
.Player
and the Fish
so that they use the (common) methodfs from GameObject
.It might be a good idea to split up the project in packages. I’ve choosen the following:
nl.saxion.chillygame
which contains the ChillyGame
class.nl.saxion.chillygame.gameobjects
which contains the GameObject
, Fish
and Player
.Now, we add another gameobject to the game which is a crack in the ice. When Chilly hits a crack in the ice he drowns and returns to his original starting positioning (maintaining his current health level). So, to do this, do the following:
Crack
class that extends from GameObject
. The behaviour of the Crack is in the beginning similar with the fish. Just draw the crack-complete.png image with a size of 50 by 50.Just like Fishes, cracks appear from time to time in the game. Where a fish always appear every 5 seconds, for a crack it’s a bit more unpredictable. To implement this do the following in your ChillyGame
private int crackCreateTimer
. Make sure this increments every the draw()
method is called. The other instance variable is the private int crackCreateTimerMax
and this one is used to determine after how many frames a new crack should be drawn. The value for this one is somewhere between 150 and 300 frames. So make sure you initialize it like this:private int crackCreateTimerMax = SaxionApp.getRandomValueBetween(150, 300);
crackCreateTimer
is equals to crackCreateTimerMax
it’s time to add a crack to a random position on the screen. Just like you did with the fishes. Make sure you reset crackCreateTimer
to 0 and crackCreateTimerMax
to a new random value between 150 and 300. You can also play the sound named cracknew.wav every time a crack appear, use SaxionApp.playSound("resources/cracks/cracknew.wav");
for that.Finish your code so that a new crack appears every now and then.
Now we are going to handle collisions. Every time Chilly collides to a crack his position should be reseted to the center of the screen.
ChillyGame.SCREEN_WIDTH/2
by ChillyGame.SCREEN_HEIGHT/2
).generateCrack()
in the Crack
class): public static Crack generateCrack() {
//Make sure there is a safezone at the center of the screen
int centerx = ChillyGame.SCREEN_WIDTH /2;
int centery = ChillyGame.SCREEN_HEIGHT /2;
int posx = SaxionApp.getRandomValueBetween(0, ChillyGame.SCREEN_WIDTH);
while (posx > centerx - 250 && posx < centerx + 100) {
posx = SaxionApp.getRandomValueBetween(0, ChillyGame.SCREEN_WIDTH);
}
int posy = SaxionApp.getRandomValueBetween(0, ChillyGame.SCREEN_HEIGHT);
while (posy > centery - 250 && posy < centery + 100) {
posy = SaxionApp.getRandomValueBetween(0, ChillyGame.SCREEN_HEIGHT);
}
return new Crack(posx,posy,50,50,"resources/cracks/crack-small.png");
}
Normally cracks won’t appear at once but will grow slightly. We have added 4 images for a crack: crack-small.png, crack-medium.png, crack-large.png, crack-complete.png. You can use these to make your crack expand.
So, how can you do that? This is a bit more advanced so you need to figure out yourself. We have a few clues for you:
Crack
class that determines how far along the crack is. Just like you did in the ChillyGame
. Increase it every time the draw()
method is called.time after appearing | image | size (width and height) |
---|---|---|
100 frames | crack-medium.png | 100x100 |
200 frames | crack-large.png | 200x200 |
300 frames | crack-complete.png | 200x200 |
You can play the sound crackgrow.wav every time the crack grows.