Android Game Programming by Example - Sample Chapter
Android Game Programming by Example - Sample Chapter
Android Game Programming by Example - Sample Chapter
ee
$ 44.99 US
29.99 UK
P U B L I S H I N G
John Horton
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Sa
m
John Horton
Preface
Making games is addictive and very rewarding, it can be hard to stop once you get
started. The problem comes when we reach a stumbling block because we don't
know how to implement a feature, or perhaps integrate it into our game. This book
is a whirlwind tour of as many Android 2D gaming features that can possibly be
squeezed into 11 chapters.
Every line of the code used to build three games of increasing difficulty is shown in
the text of the book and explained in a straightforward manner.
Steadily build up to implement a flexible and advanced game engine that uses
OpenGL ES 2 for fast smooth frame rates. This is achieved by starting with a simple
game and gradually increasing the complexity of the three complete games built step
by step.
Implement cool features like sprite sheet character animation and scrolling parallax
backgrounds. Design and implement genuinely challenging and playable platform
game levels.
Learn to code both basic and advanced collision detection. Make simple the math
behind 2D rotation, velocity, and collision. Run your game designs at 60 frames per
second or better.
Process multi-touch screen input. Implement a multitude of other game features like
pickups, firing weapons, HUDs, generating and playing sound FX, scenery, level
transition, high scores, and more.
Preface
Chapter 2, Tappy Defender First Step, is about planning the game project and getting
the code for our first game engine up and running. We will implement a main game
loop, control the frame rate, and draw to the screen.
Chapter 3, Tappy Defender Taking Flight, teaches us to add lots of new objects and
some features like player controls, enemies, and scrolling stars in the background.
In the Things that go bump collision detection section, we will discuss our collision
detection options and implement an efficient solution for this game.
Chapter 4, Tappy Defender Going Home, completes the game, including adding high
scores, victory conditions, sound FX, and more.
Chapter 5, Platformer Upgrading the Game Engine, provides a good understanding of
what is needed in a simple game engine. We can quickly learn about and build a more
advanced and flexible engine, suitable for a really tough, retro 2D platform game.
Chapter 6, Platformer Bob, Beeps, and Bumps, uses our new game engine to add a class
to manage the sound FX and a class to implement the more complex player controls
that are required by a game of this type. We can then make Bob, our playable
character, an animated running, jumping hero.
Chapter 7, Platformer Guns, Life, Money, and the Enemy, continues the subject of the
previous two chapters; we add a ton of features in this one. We will add collectible
pick-ups and power-ups, a deadly homing enemy, and a patrolling guard. Of course
with all this, Bob is going to need a machine gun to defend himself, and he gets one!
Chapter 8, Platformer Putting It All Together, is where our platform game comes
to life. We will add lots of new platform tile types and scenery objects, multiple
scrolling parallax backgrounds, collision detection, and a teleporting system so that
Bob can travel between the levels of the game. Using our range of tile types, scenery
objects, and backgrounds, we will implement four playable levels linked together by
the teleporting system.
Chapter 9, Asteroids at 60 FPS with OpenGL ES 2, contains the final project of this book,
which is an introduction to 2D games with the super fast OpenGL graphics library.
In this chapter, we will quickly learn how to draw with OpenGL ES 2 and integrate
the drawing system into our game engine. By the end of the chapter, we will have a
working engine that draws an Asteroids-style spaceship to the screen.
Chapter 10, Move and Draw with OpenGL ES 2, is where we will quickly integrate
our sound and control systems from the previous project. Then, we can add a game
border, twinkling star system, spinning asteroids, a neat HUD, progressively difficult
levels, and a rapid fire gun to the player's spaceship.
Preface
Chapter 11, Things That Go Bump Part II, completes the Asteroids game by adding
the collision detection. The math required to detect collisions with the irregularlyshaped spinning asteroids is made simple and implemented into the game engine.
By the end of this chapter, you will have the third and final fully playable game.
Player 1 UP
The terminology used by old arcade and pinball machines "1 UP" was a kind of
notice to the players that they were playing (up) now. It was also used to indicate
earning an extra life. Are you ready to build three great games?
We will build three cool games together. Every line of code for these three games
is shown in this book; you will never have to refer to the code files to see what is
going on. Also, the entire file set required to build all three games is included in the
download bundle that can be obtained from the books page on the Packt website.
All the code, Android manifest files, and the graphical and audio assets are included
in the download as well. The three cool games are progressively more challenging to
implement.
The first project uses a simple but functional game engine that clearly demonstrates
the essentials of a main game loop. The game will be fully working with the home
screen, high scores, sound, and animation. But by the end of the project, as we add
features and try to balance the game play, we will soon see that we need more
flexibility in order to add features.
In the second project, a hard retro platformer, we will see how we can use a simple
and flexible design to build a relatively fast and very flexible game engine, which is
extendable and reusable. This flexibility will allow us to make quite a complex and
well-featured game. This game will have multiple levels, different environments,
and more. This in turn will highlight the need for being able to draw graphics more
quickly. That leads us on to the third project.
In the third project, we will build an Asteroids-like game called Asteroids simulator.
Although the game won't have as many features as the previous project, it will
feature the super-smooth drawing of hundreds of animated game objects running
at over 60 frames per second. We will achieve this by learning about and using the
Open Graphics Library for Embedded Systems (OpenGL ES 2).
[1]
Player 1 UP
By the end of this book, you will have a whole repertoire of design ideas, techniques,
and code templates that you can use in your future games. By seeing the strengths
and weaknesses of the different ways of making games on Android, you will be able
to successfully design and build games in the most appropriate way for your next
big game.
Tappy Defender
Fly Flappy Bird-style with one finger to reach your home planet, while avoiding
multiple enemies. Features include:
Basic animation
Home screen
Collision detection
High scores
Simple HUD
[2]
Chapter 1
[3]
Player 1 UP
Pickups
An upgradeable gun
[4]
Chapter 1
Asteroids simulator
This is a classic shooter with retro vector-graphics style visuals. It involves clearing
waves of smoothly animated spinning asteroids with a rapid fire gun. Features
include:
An introduction to OpenGL ES 2
[5]
Player 1 UP
The first thing we need to do is prepare your PC to develop for Android using Java.
Fortunately, this is made quite simple for us.
[6]
Chapter 1
If you are learning on Mac or Linux everything in this book will still
work. The next two tutorials have Windows-specific instructions
and screenshots. However, it shouldn't be too difficult to vary the
steps slightly to suit Mac or Linux.
2. Find the three buttons shown here and click on the one that says JDK that
is highlighted in the following image. They are on the right-hand side of the
web page. Then, click on the Download button under the JDK option:
[7]
Player 1 UP
3. You will be taken to a page that has multiple options to download the JDK.
In the Product/File Description column, you need to click the option that
matches your operating system. Windows, Mac, Linux, and some other less
common options are all listed.
4. A common question asked here is, do I have 32- or 64-bit windows? To find
out, right-click on your My Computer icon (This PC on Windows 8), click
on the Properties option, and look under the System heading at the System
type entry:
[8]
Chapter 1
8. In the first of several install dialogs, click on the Next button and you will see
the following dialog box:
9. Accept the defaults shown in the previous image by clicking on Next. In the
next dialog box, you can accept the default install location by clicking on
Next.
10. Next up is the last dialog of the Java installer; for this click on Close.
The JDK is now installed. Next, we will make sure that Android
Studio is able to use the JDK.
[9]
Player 1 UP
12. Type JAVA_HOME for Variable name: and enter C:\Program Files\
Java\jdk1.8.0_05 for the Variable value: field. If you installed the JDK
somewhere else, then the file path you enter in the Variable value: field
will need to point to wherever you put it. Your exact file path will likely
have a different ending to match the latest version of Java at the time you
downloaded it.
13. Click on OK to save your new settings.
14. Now under System variables, click on Path and then click on the Edit...
button. At the very end of the text in the Variable value: field, enter the
following text to add our new variable to the file paths that Windows will
use, ;JAVA_HOME. Be sure not to miss the semicolon from the beginning.
15. Click on OK to save the updated Path variable.
16. Now, click on OK again to clear the Advanced system settings dialog box.
The JDK is now installed on our PC.
Chapter 1
6. In the next dialog, leave the default settings and then click on Next.
7. On the Choose start menu folder dialog box leave the defaults and click on
Install.
8. On the Installation complete dialog, click on Finish to run Android Studio
for the first time.
9. The next dialog is for users who have already used Android Studio, so
assuming you are first-time user, select the I do not have a previous version
of Android Studio or I do not want to import my settings checkbox. Then
click on OK:
That was the last piece of software we needed. We will begin to use Android Studio
straight away in the next chapter.
[ 11 ]
Player 1 UP
Summary
This chapter was deliberately kept as short as possible, so we can get on with
building some games. We will do this now.
[ 12 ]
www.PacktPub.com
Stay Connected: