Reader small image

You're reading from  Android Game Programming by Example

Product typeBook
Published inJun 2015
Reading LevelIntermediate
Publisher
ISBN-139781785280122
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 8. Platformer – Putting It All Together

Finally, we will make the bullets do some damage. The ricochet sound is very satisfying when the bullets energy is absorbed by a clump of grass. We will add an abundance of new platform types and inanimate scenery objects to make our levels more interesting. We will provide a real sense of motion and immersion by implementing multiple scrolling parallax backgrounds.

We will also add an animated fire tile for the player to avoid, and in addition, a special Teleport class to link levels together into one playable game. Then, we will use all of our game objects and backgrounds to create four, linked, and fully playable levels.

Then, we will add a HUD to keep track of pickups and lives. Finally, we will discuss some of the neat things that couldn't be fitted into this project in just four chapters.

Bullet collision detection


Detecting bullet collisions is fairly straightforward. We loop through all the existing Bullet objects held by our MachineGun object. Next, we convert the points of each bullet into a RectHitBox object and test it using intersects() against each object in our viewport.

If we get a hit, we check to see what type of object it has hit. We then switch to handle each type of object that we care about. If it is a Guard object, we knock it back a bit, if it is a Drone object, we destroy it, and if it is anything else, we just make the bullet disappear and play a kind of thudding/ricochet sound.

We simply place this logic we discussed after our switch block that handles collisions with the player, but before, we call update() on all our unclipped objects as shown next:

default:// Probably a regular tile
    if (hit == 1) {// Left or right
        lm.player.setxVelocity(0);
        lm.player.setPressingRight(false);
    }

   if (hit == 2) {// Feet
        lm.player.isFalling...

Adding some fire tiles


These new GameObject derived objects will mean instant death to Bob. They won't move, but they will be animated. We will see we can achieve this just by setting the already existing properties of GameObject.

Adding this feature into our game is simple because we have already implemented all the features we need. We already have a way to locate and add new tiles, a way to detect and respond to a collision, sprite sheet animation, and so on. Let's do it step-by-step, then we can add these dangerous and life-threatening elements into our world.

We can put the entire functionality of the class into its constructor. All we do is configure the object much like we did our Grass object, but in addition, we configure it with all the animation settings, like we did to the Player and Guard objects. The fire.png sprite sheet has three frames of animation that we want to play over the course of one second.

Create a new class, call it Fire, and add the following code to it:

import android...

Eye candy


The next three sections in this chapter will be purely aesthetic. We will add a whole bunch of different tile graphics with matching classes so that we can use a whole lot more artistic license to make our levels more interesting. The difference between the tiles will be purely visual, but it will be fairly simple to make them more functional than that.

For example, we can easily detect collision with a snow tile and have the player keep moving briefly after stopping to simulate skidding, or perhaps; the concrete tile can allow the player to move faster and therefore change the way we design big jumps and so on. The point is that you don't have to just copy paste the classes as they will be presented here.

We will also add some completely aesthetic props: mine carts, boulders, stalactites, and more. There will be no collision detection for these objects. They will allow the level designer to make the levels more visually interesting.

Tip

It would be simple to make these aesthetics...

Summary


We finished the platform game because that is all there is space for. Why not try to implement some or all of the following improvements and features?

Change the code in the Player class to make Bob gradually accelerate and decelerate instead of always running at full speed. Simply increment the velocity for each frame that the player is holding down left or right, and decrement it for each frame they are not.

Once you have achieved this, add the preceding code to the collision detection switch block in the update method to make the player skid on snow, speed up on concrete, and have a different walking/landing sound effect for each tile type.

Draw a gun on Bob, and adjust the height that the Bullet object is spawned at to appear as if it is coming from the barrel of his machine gun.

Make some objects pushable. Add an isPushable member to GameObject and make the collision detection simply knock the object back a little. Perhaps, Bob could push mine carts into fire to jump over extra...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Game Programming by Example
Published in: Jun 2015Publisher: ISBN-13: 9781785280122
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton