




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Part
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).
I
Chapter
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:00 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’ll 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.
1
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
Most of us never get to read the stories that helped create some of our favorite games, because the stories are really only important to the people who are creating the game. And assuming the developers and creators do their jobs well, the gamer will absorb the story playing the game.
In small, independent development shops, the stories might never be read by anyone other than the lead developer. In larger game-development companies, the story could be passed around and worked on by a number of designers, writers, and engineers before it ends up in the hands of the lead developers. Everyone has a different way to write and handle the creation of the story for the games that they want to make. There is no right or wrong way to handle a game’s story other than to say that it needs to exist before you begin to write any code. The next section will explain why the story is so important.
Why Story Matters
Admittedly, in the early days of video gaming, stories may not have been looked upon as importantly as they are now. It was much easier to market a game that offered quick enjoyment without needing to get very deep into its purpose. This is definitely not the case anymore. People, whether they are playing Angry Birds or World of Warcraft , expect a defined purpose to the action. This expectation may even be on a subconscious level, but your game needs to hook the players so that they want to keep playing. This hook is the driving purpose of the story. The story behind your game is important for a few different reasons. Let’s take a look at exactly why you should spend the time to develop your story before you begin to write any code for your game. The first reason why the story behind your game is important is because it gives you a chance to fully realize your game, from beginning to end, before you begin coding. No matter what you do for a living, whether you are a full-time game developer or are just doing this as a hobby, your time is worth something. In the case of a full-time game developer, there will be an absolute dollar value assigned to each hour you spend coding and create a game. If you are creating independent games in your spare time, your time can be measured in the things you could be doing otherwise: fishing, spending time with others, and so on. No matter how you look at it, your time has a definite and concrete worth, and the more time you spend coding your game, the more it costs. If your game is not fully realized before you begin working on your code, you will inevitably run into problems that can force you to go back to tweak or completely rewrite code that was already finished. This will cost you in time, money, or sanity.
NOTE: To be fully realized an idea must be complete. Every aspect of the idea has been though out and carefully considered.
As a game developer, the last thing that you want is to be forced to go back and change code that is finished and possibly even tested. Ideally, your code should be extensible enough that you can manipulate it without much effort—especially if you want to add levels or bosses onto your game later. However, you may have to recode something
No doubt you have a game in mind that you want to develop as soon as you learn the skills in this book. However, you may not have ever really considered what the story for that game would be. Give some thought to that story.
TIP: Take some time now to write down a quick draft of your game, if you have one in mind. When you finish, compare it to the mock story that follows.
Let’s look at a quick example of a story that can be used to develop a game.
In that quick story, even though there are few details, you still have enough for one casual developer to start working on fairly simple game. What can you get out of this paragraph? The first concept that comes to mind from this short story would be a top-down, arcade- style driving game; think original Spy Hunter. The driver, or the car, could have a gun to fire at enemy vehicles. The game could end when the player reaches the edge of the town, or possibly a safe house or garage of some sort. This short story even has enough details to make the game a bit more enjoyable to play. The main character has a name, John Black. There are two sets of enemies to avoid: the police and the gang. The environment is made up of the streets of Villiansburg, and the majority of the enemy vehicles travel faster than the main character’s. There is definitely enough good material here to make a quick, casual game. Already the metaphoric wheels in your brain should be turning out ideas for this game. A fair amount of good, arcade-style action is described in this one short paragraph. If you can describe the game that you want to make in a short paragraph like this, than as a single, casual developer, you are well on your way to making a fairly enjoyable game. Where one short paragraph might have enough detail for a fairly convincing casual game, imagine what a longer story could provide. The more detail that you can put into your story now, the easier your job will be as you are coding, and the better your game will be. Take some extra time with your story to get the details just right. Sure a short paragraph, like the one in this section, is enough to go on, but more details could definitely help you as you are coding. Here is a list of questions that you should already be asking yourself after reading this story:
What kind of car does John steal and drive? Why did he steal the money? What kind of weapon does he have? What kind of weapons, if any, are on the car? Is Villiansburg a city or country environment? Is there a boss battle at the end? How is scoring accumulated, if at all?
If we go back and answer some of these questions, the story may look like this.
Now, let’s take a look at the story again. We have a lot more to go on now, and clearly, the more detailed story would make for more interesting game play. Anyone coding this game would now be able to discern the following game play details.
The main character’s car is a black sedan. The car has two machine guns, missiles, and oil slicks as weapons. The environment is a crowded city street lined with rundown, boarded up buildings.
Figure 1–2. Eclipse.org
This book will not dive into the download or setup of Eclipse. There are many resources, including those on Eclipse’s own site and the Android Developer’s Forum, that can help you set up your environment should you require assistance.
TIP: If you have never installed Eclipse, or a similar IDE, follow the installation directions carefully. The last thing you want is an incorrectly installed IDE impeding your ability to write great games.
You will also need the latest Android SDK. As with all of the Android SDKs, the latest can be found at the Android developer site (http://developer.android.com/sdk/index.html), as shown in Figure 1–3:
Figure 1–3. The Android developer site
As with the IDE, many resources are available to help you download and install the SDK (and the corresponding Java components that you may need) if you need help doing so. Finally, you should possess at least a basic understanding of development, specifically in Java. While I do my best to explain many of the concepts and practices used in creating the code for this book, I will not be able to explain the more basic development concepts. In short, my explanations alone should be enough to get you through the code in this book if you are a novice, but a more advanced Java developer will be able to easily take my examples and expand on them.
Installing OpenGL ES
Arguably one of the most important items you’ll be using is OpenGL ES, a graphics library that was developed by Silicon Graphics in 1992 for use in computer-aided design (CAD). It has since been managed by the Khronos Group and can be found on most platforms. It is very powerful and an invaluable tool to anyone who wants to create games.
platforms, it will be available to the most devices and will have been extensively tested. Finally, it is just plain easy to pick up and learn. Also, picking up 1.1 and maybe even 2. after you already know 1.0 will be a lot easier.
One of the appeals of developing for Android is that it is so widely used across many different devices, such as mobile phones, tablets, and MP3 players. The games that you develop have a chance to run one dozens of different cell phones, tables, and even e- readers. From different wireless carriers to different manufacturers, the hardware exposure that your game could get is quite varied. Unfortunately, this ubiquity can also be a tough hurdle for you to jump through. At any given time, there could be up to 12 different versions of Android running on dozens of different pieces of hardware. The latest tablets and phones will be running version 2.3.3, 3.0, 3.1, or 4.0—the most recent versions, which are run on the most powerful devices. Therefore, these will be the versions that we are going to target in this book.
NOTE: If you do not have an Android device to test on you can use the PC emulator. However, I highly recommend that you try to use an actual Android phone or tablet to test your code. In my testing, I have noticed some minor discrepancies when running my code in an emulator versus running it on my phone or tablet.
Most importantly, have fun as you work through the process of creating games. Games, after all, are fun, and you should have fun making them!
In this chapter, we discussed what you should expect to get out of this book. You learned the importance of story to the creation of a game and how sticking to that story can help you create better code. You also learned about the process of creating games on the Android platform, the versions of Android, and Android’s development environment. Finally, you discovered the key to creating games on the Android platform, OpenGL ES, and we covered a few pertinent details about Android version releases.
Chapter
Star Fighter :
A 2-D Shooter
The game you will be creating as you work your way through this book is Star Fighter. Star Fighter is a 2-D, top-down, scrolling shooter. Even though the action is fairly limited, the story is surprisingly detailed. In this chapter, you’ll get a sneak preview of the game and the story behind it. You will also learn about the different parts of the game engine and what the game engine does.
The story for Star Fighter is as follows; we will be referring to it periodically as we progress through this book:
Captain John Starke is a grizzled galactic war veteran. He fought his way out of every battle the Planetary Coalition has been involved in. Now, on his way back to earth and ready to retire from years of service to a quiet little farm in western Massachusetts, he finds himself caught in the middle of a surprise enemy invasion force.
Captain Starke prepares for battle. But this is no ordinary Kordark invasion fleet; something is different.
Starke cranks up the thrusters on his AF-718 and sets his guns to automatically fire. Luckily, the AF-718 is light and nimble. As long as he can avoid enemy guns and the occasional collision, the autofire cannons should make short work of the smaller Kordark fighters.
Unfortunately, what the AF-718 has in agility and autofire capabilities, it lacks in shields. Captain Starke is best served by avoiding the enemy craft altogether. If he does sustain any damage, after three strikes, he is out. The AF-718 can’t take very many direct blaster hits without good shields. As for a direct collision from an enemy, unfortunately, it is “one and done” for Captain Starke.
2
Interceptors, but they also have forward facing guns, like the Scouts. They can maneuver in a random pattern and should give Captain Starke his greatest challenge. Luckily for him, there are relatively few of these War Ships, giving him time to recoup between appearances.
The computer of the AF-718 will track how many ships are in the invasion force. It will notify Captain Starke when he has eliminated all of the potential enemies. These statistics will be sent to the forward command on Earth to let them know how he is ranking against the invasion.
Help Captain Starke eliminate as many invasion force waves as possible and reach Earth alive.
So there it is, the story that you will be referring to as you code Star Fighter. What game details can you get out of this story? Let’s list them, in the same way we did for the sample story in Chapter 1:
The main character Captain John Starke will be piloting an AF-718 spaceship. The player will not have to operating any firing mechanism, because the ship has an autofire feature. The player can power up by obtaining more shields and guns. If the player sustains three hits from an enemy cannon without repair, the game will end. If the play sustains a direct hit from an enemy craft, the game will end. There are three different types of enemy ships: Scouts move quickly in a predictable pattern and fire a single cannon. Interceptors have no cannons but can take four direct blaster hits from the player. They cannot change their course once they have locked on to the player’s position The War Ships have cannons and can take four direct blaster hits. They move in a random pattern The game will track the number of enemies in each wave. Every time the player destroys one, the counter will be decreased by one until the wave is finished. The scores will be uploaded to a central area.
This sounds like it is going to be a very fun, exciting, and detailed game to play. The best part is that the code needed to create this game will not be that complicated, or at least not as complicated as you might expect.
In the next section, you will learn about the game engine for Star Fighter. You will learn what the different parts of the game engine are, and what the engine as a whole does for your game. Finally, you will begin to stub out some basic engine functionality and begin to build your game.
Now that you know what Star Fighter is going to be about, we can begin to look at the different pieces necessary to build the game. Many pieces will all have to fit together in a very tight and cohesive way to create the end product that is a playable, enjoyable Android game. When you think about everything a game has to do to deliver a truly enjoyable experience, you will begin to appreciate the time and effort it takes to create even the simplest of games. A typical game will do the following: Draw a background. Move the background as needed. Draw any number of characters. Draw weapons, bullets, and similar items. Move the characters independently. Play sound effects and background music. Interpret the commands of an input device. Track the characters and the background to make sure none move where they should not be able to move. Draw any predefined animation. Make sure that when things move (like a ball bouncing), they do so in a believable way. Track the player’s score. Track and manage networked or multiple players. Build a menu system for the player to select to play or quit the game. This may not be a comprehensive list, but it is a fairly good list of all of the things that most games do. How does a game accomplish all of the things in this list? For the purposes of this book, we can divide all of the code in a game into two categories: the game engine and the game-specific code. Everything in the previous list is handled in one or both of these categories of code. Knowing which is handled where is critical to understanding the skills in this book. Let’s begin examining these two categories of code with a look at the game engine.
Understanding the Game Engine
At the core of every video game is the game engine. Just as the name suggests, the game engine is the code that powers the game. Every game, regardless of its type—