1.3-Object-Georienteerd-Programmeren

ChillyGame - Part 3: Global warming issues hits Chilly! :(

Screenshot of the end result

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.

Step 1: Add inheritance

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:

Classdiagram

Now it’s up to you to implement this diagram into your project:

Step 2: Packages

It might be a good idea to split up the project in packages. I’ve choosen the following:

Step 3: Create a Crack gameobject

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:

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

Finish your code so that a new crack appears every now and then.

Step 4: Handle collisions

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.

    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");
    }

Advanced Suggestion 1: Growing cracks

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:

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.