The Elements of Game Programming

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This article is brought to you by Sams Publishing
publisher of Clayton Walnum’s  Sams Teach Yourself Game Programming with Visual Basic in 21 Days

This article is the second part of a two part article. The first part is here.


As you’ve already discovered in part one, a good computer game pushes your computer to its limits. In fact, a game must excel in many areas. To write computer games that people will want to play, then, you must gain some expertise in the related areas of game programming:

  • Game design
  • Graphic design
  • Sound generation
  • Controls and interfaces
  • Image handling
  • Animation
  • Algorithms
  • Artificial intelligence
  • Game testing

These elements overlap to an extent. For example, to learn graphic design for computer games, you need to know how a computer handles graphic images. Moreover, game design draws on all the other elements in the list. After all, you can’t design a game unless you know how the graphics, sound, controls, and computer algorithms fit together to form the final product.

Game Design

Whether your game is a standard shoot-’em-up, in which the player’s only goal is to blast everything on the screen, or a sophisticated war game, requiring sharp wits and clever moves, first and foremost your game must be fun. If a game isn’t fun, it doesn’t matter how great the graphics are, how realistic the sound effects are, or how well you designed the computer player’s algorithms. A boring game will almost certainly get filed away in a closet to gather dust.

Many things determine what makes a game fun. The most important thing, of course, is the game’s concept. Often, a game’s concept is based on some real-world event or circumstance. For example, chess[md]one of the most popular board games of all time[md]is really a war game. Monopoly, on the other hand, is a financial simulation in which players try to bankrupt their competition.

Computer games are no different from their real-world cousins. They too must have some logical goal for the player, and[md]with rare exceptions[md]they must be set in some sort of believable world. This world can be as simple as an onscreen maze or as complex as an entire planet with continents, countries, and cities. In the insanely addictive computer game Tetris, the world is simply a narrow onscreen channel in which the player must stack variously shaped objects. On the other hand, in the fabulous Ultima series of graphic adventures, the player’s world is filled with forests, swamps, cities, monsters, and the other elements that make up a complete fantasy scenario.

No matter what type of world you envision for your game, it must have consistent rules that the player can master. For a game to be fun, the player must be able to figure out how to surmount the various obstacles that you place in his path. When a player loses a computer game, it should be because he hasn’t mastered the subtleties of the rules yet, not because some random bolt out of the blue blasted him into digital bits and pieces.

Of course, to build a logical, fair, and effective gaming world, you must draw on all your skills as a programmer. All the other areas of programming listed earlier come into play here. Graphics, sound, interface design, computer algorithms, and more can make the difference between a fun game and just another dime-a-dozen hack job whose disk will be used as a Frisbee at the next family picnic.

Not only do you need consistent rules for your game, but you also need a consistent game world. Every element of your game[md]fonts, graphics, sound, story–contributes to this goal. For example, if you’re writing a game in which the player must battle zombies and werewolves, you probably won’t need those cute little bunny characters you drew. (That is, unless the bunnies suddenly grow fangs and horns and develop an unquenchable desire to consume human flesh!) Similarly, your zombie game will need suitably eerie sound effects and spooky music. The “Sugarplum Fairies” theme just ain’t gonna cut it.

Graphic Design

There’s a good reason why so many computer game packages are covered with exciting illustrations and awe-inspiring screen shots. In spite of how hard people try to make intelligent buying decisions, everyone is swayed by clever packaging. Although your smart side may tell you to ignore that fabulous wizard on the box cover, your impulsive side sees that wizard as just a hint of the excitement that you’ll find in the box. Of course, reality usually falls far short of packaging. Buyer beware.

The lesson here is not that you should make your games look better than they play, but rather that how a game looks is often as important as how well it performs. You want your gaming screens to be neat and uncluttered, logically laid out, and above all exciting to look at. Your screens should scream “Play me!” to anyone who comes into viewing distance.

Like anything else, graphic design is a professional skill that takes many years of study and practice to master. Luckily, though, you don’t have to be a graphic-design whiz to create attractive game screens. You can look at other games to get design ideas, and you can experiment with different screen designs to see which are the most attractive and work best with your game world. Use your favorite paint program to draw different layouts and compare them. Trial and error is not only a powerful technique for devising improved designs, but it’s also a great learning tool. The more you experiment, the more you’ll learn about what looks good on a computer screen and what doesn’t.

Sound Generation

The word we live in is a noisy place indeed. There’s hardly a moment in our lives when we’re not assaulted by hundreds of sounds simultaneously. If your game world is to seem realistic to the player, it too must provide sound. That’s not to say you have to recreate the full spectrum of sounds that a player hears in the real world, though. With today’s computers, that task would be impossible.

Although you shouldn’t fill your player’s ears with unnecessary noise, you should provide as many sound cues as appropriate. When the player selects an onscreen button, she should hear the button click. When she slams a home run, she should hear the crack of the bat and the roar of the crowd.

There’s not a computer game on the planet (or, I’d venture to say, in the universe) that couldn’t be improved by better sound effects. Luckily, thanks to powerful sound cards, many of today’s games include fabulous digitized sound effects.

Although music isn’t as important as sound effects, it can also add a lot to a computer game. The most obvious place for music is at the beginning of the game, usually accompanying a title screen. You might also want to use music when the player advances to the next level or accomplishes some other important goal in the game.

To add music to a computer game, however, you must have some knowledge of music composition. Bad music in a game is worse than none at all. If you have no musical training, chances are that you have a friend who does. You can work together to compose the music for your computer game magnum opus. If you’re lucky, she won’t even ask for a share of the royalties!

Controls and Interfaces

The game programmer must provide some sort of interface to enable the player to play the game. In a computer game, menus and onscreen buttons enable the user to select options and commands. In addition, the player uses the keyboard or mouse to move and otherwise manipulate objects on the screen.

A good game interface makes playing the game as easy as possible. The game commands should be logical and readily available. The more your game works like a real-world game, the easier it will be for the player to learn its controls. For example, in a computer chess game, you might enable the player to move a game piece with her mouse pointer instead of typing in the position of the square where she wants to move the piece.

Image Handling

Every computer game must deal with various types of images. These images may be full-screen background graphics, icons that represent game commands or game pieces, or tiles that you use to create a map or some other complex game screen. When you design your game, you must decide which types of images you need. Should you draw your game’s background screen at runtime? Or should you create the screen with a paint program and just load it in your game? If you need to conserve memory, maybe you should create your game screens from small tiles?

In game-programming lingo, a tile is a small graphical object that can be used with other similar objects to assemble a complex game screen. For example, several tiles depicting trees can be used to create an entire forest. Various types of tiles–trees, grass, water, mountains, and so on[md]can be used to assemble an entire world map.

You must consider questions like these as you design your computer game’s graphics. You want your game to look as professional as possible (which means that you may need to find an artist), but you also must consider the amount of memory the graphics will consume and how long it takes to move graphic images from the disk to the computer’s memory. Most gamers hate to wait for files to load from the disk. On the other hand, keeping too much data in memory may make your game clunky on computers that have smaller amounts of free memory.

Another important issue is the amount of time it takes to create your game’s graphics. You can’t spend the next 10 years drawing detailed graphics for every aspect of your game. You need to use shortcuts (such as tiling) to speed up the graphic design process. In other words, although every tree in the real world looks different, many trees in a computer game look identical.

Animation

Once you’ve learned to design and manipulate computer graphic images, you’re ready to take the next step: animation. This is the process of making objects appear to come to life and move around the computer screen. By using a series of images, you can make a chicken waddle across a road, a rock tumble from a cliff side, or a spaceship blast off from a launch pad.

Animation is the process of moving or changing a graphical game object in some way. For example, a ball that bounces around the screen is an animation, as is a game creature that falls to the ground when shot.

For example, when a player moves a game piece, instead of simply having the piece disappear from its current location and reappear at its new one, you might make the piece dissolve and then reform itself. Or, if the playing piece represents a human being or an animal, you could make the piece saunter over to its new location.

Such animation effects can make your game much more interesting and even more fun to play. Although animation requires a lot of work on the programmer’s part, it’s well worth the effort.

Algorithms

Although the term algorithm sounds like the most horrid technobabble, it’s really a simple word. An algorithm is nothing more than a series of steps that solves a problem. You use algorithms every day of your life. When you make pancakes for breakfast, you must follow an algorithm. When you drive to work, you must follow another algorithm. Algorithms enable you to solve all of life’s simple (and sometimes not-so-simple) tasks.

Computer algorithms enable you to solve computing problems. In other words, to write computer games, you need to figure out how to get your computer to do things that you may not have tried to do on a computer before. For example, how can you determine who has the best hand in a poker game? Or how do you create a smart computer player? You must write an algorithm. Once you know how to solve a problem with your computer, you can write the specific code in whatever programming language you’re using. Throughout this book, you’ll see many algorithms for solving game problems.

A computer algorithm is a set of program steps that solves a programming problem. For example, a function that determines whether a player has a full house in a computer card game uses an algorithm to analyze the cards in the player’s hand.

Artificial Intelligence

Artificial intelligence routines are algorithms that make computers seem smart. By “smart,” I don’t mean the ability to calculate the player’s score or process his input. I mean the computer’s ability to act as an opponent. If you want to write a computer game that features computer-generated players, you must create algorithms that enable the computer to compete with human players. How involved this algorithm turns out to be depends on how complex the game is and how well you want the computer to play.

For example, it can be difficult to write good algorithms for creating a computer chess player because winning a game of chess requires a great deal of strategy. You could simply have the computer choose a random move each turn, but such a computer player would be easy to beat. The algorithm that you write can determine the difficulty of your game.

Game Testing

After learned how to design and program your computer game, you’ll get to work on your own masterpiece (I hope). However, after you write your game, you then must test it extensively to ensure that it works properly.

The best way to test a game is to give it to a few trusted friends and watch as they play, taking notes about things that don’t work quite the way you expected. Remember to watch for not only program bugs that make the program do unexpected things and may even crash the computer, but also interface bugs that may make your program confusing to use.

After your friends have played the game for a while, ask them what they liked or didn’t like. Find out how they think the game could be improved. You don’t have to agree with everything they say, but always be polite, taking their suggestions seriously and writing them down so that you can review them later. Don’t be defensive. Your friends aren’t criticizing your work so much as helping you to make it better. Remember: There’s no such thing as a perfect computer program. There’s always room for improvement. After the testing is complete, implement those suggestions that you think are valuable.

The only way to test a game is to have several people play it repeatedly. Of course, before you pass the game on to a few close friends, you should have already played the game so much that you would rather read a phone book from cover to cover than see your opening screen again!

Summary

Writing a computer game requires you to bring the best of your programming skills into play. To create a successful game, you must first design it. You must think about the game’s graphic design and interface, experiment with it, and finally implement it. As you design your game, you need to consider the types of images and sounds that will bring the game to life. Animation and smart algorithms can also make your game the next bestseller.

In my book, Sams Teach Yourself Game Programming with Visual Basic in 21 Days, I cover
the details of creating games with Visual Basic starting with drawing graphics with Visual Basic. With a few basic techniques and tools at your disposal, you may be surprised at how easy it is to create competent computer graphics for games–or for any other computer application.

About the Author

Clayton Walnum is an award winning author and game programmer who holds a degree in computer science and has written or co-authored more than 40 books covering everything from comptuer gaming to 3D graphics programming.


This article is brought to you by Sams Publishing
publisher of Sams Teach Yourself Game Programming with Visual Basic in 21 Days
) Copyright Sams Publishing, All Rights Reserved

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read