Apress Practical Android 4 Games Development (2011)

Apress Practical Android 4 Games Development (2011)

(Parte 1 de 10)


US $39.9

Shelve in Mobile Computing

User level: Intermediatewww.apress.com

Practical Android 4 Games Development guides you through the process of designing and developing game apps that work on both smartphones and tablets. The easy-to-read, concise instructions found in this book will help you get started making a variety of fun, engaging games. The first Android-based game you’l create is a 2D, top-down scroling shooter caled Star Fighter. Once you have mastered the basics, and recorded your top Star Fighter score on your tablet or smartphone, you’l be ready to expand your knowledge and skils by putting together a 3D environment that can be used to create any number of compelling 3D games.

With Practical Android 4 Games Development you’l learn how to:

• Build your own reusable “black box” for game development • Leverage game engine and game specific code

• Design and develop rich 2D and 3D games

• Develop and implement an intriguing storyline for your game

• Display and manipulate graphics with OpenGL ES

• Create a control system to navigate the 3D corridor

• Create rich environments and characters

• Code collision detection

This book is for aspiring Android game app developers who are ready to move beyond beginning level books or tutorials on Android game building. From 2D-based casual games to 3D OpenGL-based first-person shooters, you’l find that Practical Android 4 Games Development teaches you all you need to know to start creating games on the fastest growing mobile platform.

D iMar zio

Andr oid 4 G ames

D e v elopmen t

P r ac tic

Companion eBook Available


Android 4 Games

Development J. F. DiMarzio

Create 2D and 3D Android game apps, using hands-on practical examples

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Download from Wow! eBook <w.wowebook.com> iv

About the Authorx
About the Technical Reviewersxi
About the Game Graphics Designerxii
Part I: Planning and Creating 2D Games1
■Chapter 1: Welcome to Android Gaming3
■Chapter 2: Star Fighter : A 2-D Shooter15
■Chapter 3: Press Start: Making a Menu27
■Chapter 4: Drawing The Environment73
■Chapter 5: Creating Your Character119
■Chapter 6: Adding the Enemies159
■Chapter 7: Adding Basic Enemy Artificial Intelligence177
■Chapter 8: Defend Yourself!207
■Chapter 9: Publishing Your Game243
Part I: Creating 3D Games253
■Chapter 10: Blob Hunter: Creating 3-D Games255
■Chapter 1: Creating an Immersive Environment271
■Chapter 12: Navigating the 3-D Environment287

Contents at a Glance Index ............................................................................................................... 301


Planning and Creating 2D Games

The first part of this book, Chapter 1-9, will take you through the processes of planning and creating a playable 2D Android game – Star Fighter. The creation of this game will follow a distinct and logical path. First you will plan and write the story behind your game. Next, you will create the background for the game. Then you will create the playable and non-playable characters. Finally you will create the weapons systems and collision detection. Before following the steps needed to deploy your game to a mobile device in Chapter 9, at the end of Chapter 8, I provide the complete code listings of the most important 2D files that you either created or modified in Part 1. Use these listings to compare your code and ensure that each game runs properly. This will prepare you for the 3D development phase that follows in Part 2: “Creating 3D Games” (Chapters 10-12).


Welcome to Android Gaming

I began developing on Android in early 2008 on the beta platform. At the time, no phones were announced for the new operating system and we developers genuinely felt as though we were at the beginning of something exciting. Android captured all of the energy and excitement of the early days of open source development. Developing for the platform was very reminiscent of sitting around an empty student lounge at 2:0 a.m. with a Jolt cola waiting for VAX time to run our latest code. It was an exciting platform to see materialize, and I am glad I was there to see it.

As Android began to grow and Google released more updates to solidify the final architecture, one thing became apparent: Android, being based on Java and including many well known Java packages, would be an easy transition for the casual game developer. Most of the knowledge that a Java developer already had could be recycled on this new platform. The very large base of Java game developers could use that knowledge to move fairly smoothly onto the Android platform.

So how does a Java developer begin developing games on Android and what tools are required? This chapter aims to answer these questions and more. Here, you will learn how to block out your game’s story into chunks that can be fully realized as parts of your game. We’l explore some of the essential tools required to carry out the tasks in future chapters

This chapter is very important, because it gives you something that not many other gaming books have—a true focus on the genesis of a game. While knowing how to write the code that will bring a game to life is very important, great code will not help if you do not have a game to bring to life. Knowing how to get the idea for your game out of your head in a clean and clear way will make the difference between a good game and a game that the player can’t put down.

CHAPTER 1: Welcome to Android Gaming 4

Programming Android Games

Developing games on Android has its pros and cons, which you should be aware of before you begin. First, Android games are developed in Java, but Android is not a complete Java implementation. Many of the packages that you may have used for OpenGL and other graphic embellishments are included in the Android software development kit (SDK). “Many” does not mean “all” though, and some very helpful packages for game developers, especially 3-D game developers, are not included. Not every package that you may have relied on to build your previous games will be available to you in Android.

With each release of new Android SDK, more and more packages become available, and older ones may be deprecated. You will need to be aware of just which packages you have to work with, and we’l cover these are we progress through the chapters.

Another pro is Android’s familiarity, and a con is its lack of power. What Android may offer in familiarity and ease of programming, it lacks in speed and power. Most video games, like those written for PCs or consoles, are developed in low-level languages such as C and even assembly languages. This gives the developers the most control over how the code is executed by the processor and the environment in which the code is run. Processors run very low-level code, and the closer you can get to the native language of the processor, the fewer interpreters you need to jump through to get your game running. Android, while it does offer some limited ability to code at a low level, interprets and threads your Java code through its own execution system. This gives the developer less control over the environment the game is run in.

This book is not going to take you though the low-level approaches to game development. Why? Because Java, especially as it is presented for general Android development, is widely known, easy to use, and can create some very fun, rewarding games.

In essence, if you are already an experienced Java developer, you will find that your skills are not lost in translation when applied to Android. If you are not already a seasoned Java developer, do not fear. Java is a great language to start learning on. For this reason, I have chosen to stick with Android’s native Java development environment to write our games.

We have discussed a couple of pros and cons to developing games on Android. However, one of the biggest pros to independent and casual game developers to create and publish games on the Android platform is the freedom that you are granted in releasing your games. While some online application stores have very stringent rules for what can be sold in them and for how much, the Android Market does not. Anyone is free to list and sell just about anything they want. This allows for a much greater amount of creative freedom for developers.

In Chapter 2, you’l create your first Android-based game, albeit a very simple one. First, however, it’s important look behind the scenes to see what inspires any worthwhile game, the story.

CHAPTER 1: Welcome to Android Gaming 5

Starting with a Good Story

Every game, from the simplest arcade game to the most complex role-playing game (RPG), starts with a story. The story does not have to be anything more than a sentence, like this: Imagine if we had a giant spaceship that shot things.

However, the story can be as long as a book and describe every land, person, and animal in the environment of a game. It could even describe every weapon, challenge, and achievement.

NOTE: The story outlines the action, purpose, and flow of a game. The more detail that you can put into it, the easier your job developing the code will be.

Take a look at the game in Figure 1–1, what does it tell you? This is a screen shot from Star Fighter; the game that you will be developing through the beginning chapters of this book. There is a story behind this game as well.

Figure 1–1. Star Fighter screen shot

(Parte 1 de 10)