1. Game Programming Fundamentals

If you want to write an iOS 5 game, you’ve come to the right place. But, like any great project, you need to start at the beginning with some understanding of the fundamentals of game development.

This first chapter is about theory and terminology, the pieces that make up all games—from a simple Pong-style clone to the latest and greatest multiplayer masterpiece. Most, if not all, games share the traits that you’ll read about in this chapter.

Looking Under the Hood

When you sit down to write your game, whether it’s a simple single-screen puzzle game or a massive multilevel shooter, you should be aware of some key aspects that apply to almost all games. In this chapter, we’ll look at all aspects of game development, define them, and discuss how they apply to your game.

The Game Loop

Behind every game is a game loop which functions as the coordinator of the game. It constantly runs on a cycle—until the player dies or ends the level—and checks on user input, game state, redrawing the screen, and much more. A simple game loop might look like Figure 1.1.

Figure 1.1 A game loop

image

Typically, your game loop won’t contain much code, but it will call other functions to do the work.

Programming a game is different from other types of programming you may have done. Typically, a modern application is in a waiting state, waiting for events to be triggered, and you, as the programmer, instruct the program how to respond to those events when they happen. In a game, the game loop runs as long as the game is running, performing the same functions over and over regardless of user input or other actions. For example, enemies will still move and update whether the player is shooting at them or not.

Sprites

Sprites are two-dimensional (height and width), pixel-based game objects. They can represent anything from the main player object to enemies, weapons, missiles, and even the fonts in a high score display. In a game you have probably played before, such as Space Invaders, the sprites include the player ship, the enemy ships, the bunkers, the laser shots, the... You get the point?

Sprite Sheets

A sprite sheet is a collection of images stored in a single file from which individual sprites are extracted.

Usually the sprites in a sprite sheet are of a uniform size, so that you can loop through the sprite sheet using an offset, and display the next sprite. Sprite sheets usually have a solid background color called a mask. The game engine substitutes the mask for transparency or whatever is behind the sprite at the time of rendering. In the example in Figure 1.2 the mask/background color is white.

Figure 1.2 Sprite sheet example

image

Pseudo code to “cut” a sprite out of the sheet might look like:

For y = 0 to NUM_SPRITES_HIGH
    For x = 0 to NUM_SPRITES_WIDE
        xpos = x * X_OFFSET
        ypos = y * Y_OFFSET
        sprite = cut_image(xpos, ypos, X_OFFSET, Y_OFFSET)
    Loop x
Loop y

A sprite sheet typically reduces the number of individual graphics files your game needs. A sprite sheet can contain all the frames of an animation in a single file. As a result, sprite sheets are more efficient than individual files. You can load a single large image and store all the images in memory at once, a much faster proposition than loading a lot of small files.

The Game Engine

The game engine is where all the interesting bits of code are executed. The game engine is responsible for user input, graphics rendering, artificial intelligence, collision detection, sound playback, physics, and a lot more. Not every game needs a complex game engine, and several “off the shelf” game engines are available that already have programmed much of the code that a games programmer requires. Two of the more popular game engines for iOS are Unity (www.unity3d.com/unity) and cocos2d (www.cocos2d-iphone.org) (Figure 1.3).

Figure 1.3 Unity and cocos2D are popular iOS Game engines

image
image

The project in this book uses a fairly simple game engine, and you can follow along with the code by downloading it from www.peachpit.com/iosgames. When you create your own games, however, it pays to find out what game engines are available and appropriate for your project. One of the tenets of good programming is code reuse. While it’s a great feeling to program the code yourself, in most cases you will simply be reinventing the wheel. At my day job, for example, we have created several games for clients, and we primarily use cocos2d as our starting point.

But don’t worry! You’ll still have plenty of coding to do!

OpenGL ES

OpenGL (Open Graphics Library) is an open, cross-platform specification API for creating 2D and 3D graphics. The iPhone uses OpenGL ES (Embedded Systems), which is a subset of the full OpenGL spec, but is still very powerful in its own right (Figure 1.4).

Figure 1.4 OpenGL ES is under the hood of most iOS games.

image

iOS devices support two versions of OpenGL: ES 1.1 and 2.0. ES 1.1 is supported on all iOS devices, while ES 2.0 is supported on only iPhone 3GS and later, iPad, and iPod Touch third generation and later. This causes a slight dilemma for the iOS games developer, as you can either use ES 1.1 so the game will work on all devices, or use ES 2.0 and support only the more recent iOS devices. With a little extra work, however, your code can support both ES 1.1 and 2.0.

The choice of OpenGL is something you will definitely have to think about. For 2D games such as the one we’ll program in this book, it isn’t as important a choice, but it is important to be aware of these things every time you start a game project.

Artificial Intelligence

Artificial intelligence (AI) is the study and design of intelligent agents. Or, as it applies to games, trying to make non-player characters (NPC) and game objects not appear too dumb.

In many games, AI may be represented by some standard algorithms. For instance, in strategy games, path finding is an algorithm that allows a game object to find the best and quickest path to reach a point defined by the player while avoiding all obstacles in its way. Game development has many such methods to simulate object intelligence, and you’ll learn a few in this book.

A game can succeed or fail on its AI. If the game is too easy or too hard, or the NPCs do the same things on every play, the game quickly becomes repetitive and boring.

Game Controls

Put simply, game controls allow the human player (or, more specifically, his onscreen representative) to move around in the game. All iOS devices support many ways to control the player, and each has its pros and cons. The type of game you are developing should be a major factor when choosing your game controls.

If you are developing a simple puzzle game, for example, using the gyroscope and accelerometer to move pieces around might not make sense, while it would make perfect sense for a driving game.

Because iOS devices don’t have physical buttons for game control, some care and thought is needed to determine how to control your game.

Wrapping Up

In this chapter, we’ve explored some of the basics of game programming and their associated terms.

In the next chapter, we’ll look at the fundamentals of game design.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset