Book Image

Android Game Programming by Example

By : John Horton
Book Image

Android Game Programming by Example

By: John Horton

Overview of this book

Table of Contents (18 chapters)
Android Game Programming by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
7
Platformer – Guns, Life, Money, and the Enemy
Index

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.

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.

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 irregularly-shaped 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.

What you need for this book

Any recent and free version of Eclipse or Android Studio running on any of the major operating systems can use the code in this book.

Android Studio is the recommended development tool, and at time of publication, the minimum system requirements are:

For Windows:

  • Microsoft Windows 8/7/Vista/2003 (32 or 64-bit)

  • 2 GB RAM minimum, 4 GB RAM recommended

  • 400 MB hard disk space

  • At least 1 GB for Android SDK, emulator system images, and caches

  • 1280 x 800 minimum screen resolution

  • Java Development Kit (JDK) 7

  • Optional for accelerated emulator: Intel processor with support for Intel VT-x, Intel EM64T (Intel 64), and Execute Disable (XD) Bit functionality

For Mac OS X:

  • Mac OS X 10.8.5 or higher, up to 10.9 (Mavericks)

  • 2 GB RAM minimum, 4 GB RAM recommended

  • 400 MB hard disk space

  • At least 1 GB for Android SDK, emulator system images, and caches

  • 1280 x 800 minimum screen resolution

  • Java Runtime Environment (JRE) 6

  • Java Development Kit (JDK) 7

  • Optional for accelerated emulator: Intel processor with support for Intel VT-x, Intel EM64T (Intel 64), and Execute Disable (XD) Bit functionality

On Mac OS, run Android Studio with Java Runtime Environment (JRE) 6 for optimized font rendering. You can then configure your project to use JDK 6 or JDK 7.

For Linux:

  • GNOME or KDE desktop

  • GNU C Library (glibc) 2.15 or later

  • 2 GB RAM minimum, 4 GB RAM recommended

  • 400 MB hard disk space

  • At least 1 GB for Android SDK, emulator system images, and caches

  • 1280 x 800 minimum screen resolution

  • Oracle Java Development Kit (JDK) 7

Tested on Ubuntu 14.04, Trusty Tahr (64-bit distribution capable of running 32-bit applications).

Who this book is for

The book is best suited for existing Android or Java programmers, who want to adapt their skills to make exciting Android games.

The book is also for readers who might have no Android, game programming, or even Java experience, but a good understanding of object-oriented programming is assumed.

Also, a determined programming beginner with at least some OOP experience can follow along and build all the projects, because of the step-by-step approach of the book. This book will also be ideally suited for readers who have completed Learning Java By Building Android Games.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We will first add all the classes, and then update LevelManager in the usual three places."

A block of code is set as follows:

if (lm.isPlaying()) {
  // Reset the players location as 
  // the world centre of the viewport
  //if game is playing
  vp.setWorldCentre(lm.gameObjects.get(lm.playerIndex)
    .getWorldLocation().x,
    lm.gameObjects.get(lm.playerIndex)
    .getWorldLocation().y);

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    //Has player fallen out of the map?
    if (lm.player.getWorldLocation().x < 0 ||
      lm.player.getWorldLocation().x > lm.mapWidth ||
      lm.player.getWorldLocation().y > lm.mapHeight) {

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In the Create New Project window shown next, we need to enter some basic information about our app."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/0122OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.