Android Game Programming by Example - Sample Chapter

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19
At a glance
Powered by AI
The key takeaways are setting up the development environment with Java JDK and Android Studio, and then building three games of increasing complexity using various Android game programming techniques.

The steps to set up the development environment covered in the book are installing the Java JDK, setting environment variables, installing Android Studio, and creating a new Android project.

The main components covered in the book include building game engines, implementing game objects, collision detection, enemy AI, animations, levels, and more advanced features like OpenGL ES 2.

Fr

ee

Android gaming is a hot topic these days, but one of the


few areas of technology that does not have an abundance
of clear and useful documentation online. However, there is
an ever-increasing demand for Android games.
This book will help you get up to speed with the essentials
of game development with Android. The book begins by
teaching you the setup of a game development environment
on a fundamental level. Moving on, the book deals
with concepts such as building a home screen UI,
implementing game objects, and painting the scene at a
fixed resolution. Gradually, it builds up to the implementation
of 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.
By the end of the book, you will have successfully built
three exciting games over the course of three engrossing
and insightful projects.

What you will learn from this book


Build simple to advanced game engines for
different types of game, with cool features
such as sprite sheet character animation
and scrolling parallax backgrounds
Design and implement genuinely challenging
and playable levels
Implement the critical main game loop
Implement basic and advanced collision
detection mechanics
Bring to life a challenging enemy AI
Make the math behind 2D rotation, velocity,
and collisions simple
Run your game designs at 60 frames per
second or better

Who this book is written for

Process multitouch screen input effectively


and efficiently

The book is best suited for existing Android or Java


programmers who want to adapt their skills to make exciting
Android games.

Implement a multitude of other game features


such as pickups, firing weapons, HUDs,
generating and playing sound FX, scenery,
level transition, high scores, and more
Implement a flexible and advanced game
engine that uses OpenGL ES 2 for fast,
smooth frame rates

$ 44.99 US
29.99 UK

community experience distilled

P U B L I S H I N G

John Horton

The book is also for determined readers who might have


no Android, game programming or even Java experience,
but a reasonable understanding of object oriented
programming knowledge is assumed.

Android Game Programming by Example

Android Game Programming


by Example

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

ANDROID GAME Programming


by Example
Harness the power of the ANDROID SDK by building three
immersive and captivating games

Prices do not include


local sales tax or VAT
where applicable

Visit www.PacktPub.com for books, eBooks,


code, downloads, and PacktLib.

Sa
m

John Horton

In this package, you will find:

The author biography


A preview chapter from the book, Chapter 1 'Player 1 UP'
A synopsis of the books content
More information on Android Game Programming by Example

About the Author


John Horton is a technology enthusiast based in the UK. When he is not creating

apps and writing books or blog articles for http://www.gamecodeschool.com,


he can be found playing or making video games with his two sons.

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.

What this book covers


Chapter 1, Player 1 UP, is an introduction to the three cool games that we will build.
We will also get the development environment set up.

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.

A closer look at the games


Here is a quick glimpse at the three projects.

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

One-finger touch screen controls

Tough retro platformer


This is a genuinely tough-to-beat retro style platform game. We have to guide
Bob from the underground fire caves through the city, forest, and finally to the
mountains. It has four challenging levels. Features include:

A more advanced, flexible game engine

More advanced "sprite sheet" character animation

A level builder engine to design your levels in text format

Multiple scrolling parallax backgrounds

Transition between levels

[3]

Player 1 UP

A more advanced HUD

Add loads of extra diverse levels

Sound manager to easily manage sound FX

Pickups

An upgradeable gun

Seek-and-destroy enemy drones

Simple AI scripting for patrolling enemy guards

Hazards such as fire pits

[4]

Chapter 1

Scenery objects to create atmosphere

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:

60 frames per second or better, even on old hardware

An introduction to OpenGL ES 2

Shooter with waves of progressive difficulty

[5]

Player 1 UP

Advanced multiphase collision detection

Setting up your development


environment
All the code in this book and the download bundle will work in your favorite
Android IDE. However, I found the latest version of Android Studio exceptionally
friendly to use and the code was written and tested in it as well.
If you don't currently use Android Studio, I encourage you to give it a try. Here is a
quick overview of how to get up and running quickly. This guide includes steps to
install the Java JDK in case you are completely new to Android development.
If you already have your preferred development environment ready
to go then jump straight to Chapter 2, Tappy Defender First Step.

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.

All we need to do is:


1. Install the Java Development Kit (JDK), which allows us to develop in Java.
2. Then install Android Studio to make Android development fast and easy.
Android Studio uses the JDK and some other Android-specific tools that get
automatically installed when we install Android Studio.

Installing the JDK


The first thing we need to do is get the latest version of the JDK. To complete this
guide, perform the following instructions:
1. We need to be on the Java website, so visit:
http://www.oracle.com/technetwork/java/javase/downloads/index.
html.

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:

5. Click on the somewhat hidden Accept License Agreement checkbox:

6. Now, click on download for your OS and type as previously determined.


Wait for the download to finish.
7. In your downloads folder, double-click on the file you just downloaded. The
latest version at the time of writing for a 64-bit Windows PC was jdk-8u5windows-x64. If you are using Mac/Linux or have a 32-bit OS, your filename
will vary accordingly.

[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.

11. Right-click on your My Computer icon (This PC on windows 8) and click on


Properties | Advanced system settings | Environment Variables... | New...
(under System variables, not under User variables). Now, you can see the
New System Variable dialog box:

[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.

Installing Android Studio


Without delay, let's get Android Studio installed, and then we can begin our first
game project. Visit:
https://developer.android.com/sdk/index.html

1. Click on the button labeled DOWNLOAD ANDROID STUDIO FOR


WINDOWS to start the Android Studio download. This will take you to
another web page with a very similar looking button to the one you just
clicked on.
2. Accept the license by checking the checkbox and commence the download
by clicking the button labeled DOWNLOAD ANDROID STUDIO FOR
WINDOWS and wait for the download to complete.
3. In the folder you just downloaded Android Studio to, right-click on the
android-studio-bundle-135.12465-windows.exe file and click on Run as
administrator. The end of your filename will vary depending on the version
of Android Studio and your operating system.
4. When asked if you want to allow the following program from an unknown
publisher to make changes to your computer, click on Yes. On the next
screen, click on Next.
5. On the screen pictured here, you can choose which users of your PC can use
Android Studio. Choose which is right for you as all options will work, and
then click on Next:
[ 10 ]

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 ]

Get more information Android Game Programming by Example

Where to buy this book


You can buy Android Game Programming by Example from the
Packt Publishing website.
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet
book retailers.
Click here for ordering and shipping details.

www.PacktPub.com

Stay Connected:

You might also like