Chapter 14: Build Your Own Game in C

Download CD Content

Games programming can be the most exciting, and the most challenging area in programming. Unlike business programming, where you carefully guide the user through a narrow range of choices, in games programming you want to give the user the most freedom possible. This makes games programming especially difficult. However, learning to write your own games is an excellent learning activity, and it’s also a lot of fun!

Games programming is a rather extensive topic. There have been rather large tomes written on the topic. A single chapter is unlikely to make you a game- programming guru. The purpose of this chapter is to introduce you to the concepts of games programming and to show you a few simple command-line-based games. The end of this chapter briefly discusses some ideas behind graphics games programming, and Windows-based games programming. You will, however, see a few complete command-line-based games. If you are interested in games programming, this will hopefully encourage you to explore this area in more depth.

The Essentials of Games Programming

Games programming is not only a fascinating arena of programming but also challenging and educational. Many of the concepts introduced throughout this book such as loops, nested if statements, classes, algorithms, and more are used in games programming. Certain concepts that were introduced earlier in this book will take on more significance now. Loops are one such example. It is virtually impossible to write a game without some sort of looping structure. In a game, you want the code to continue executing until the user wishes to end the game. The usual way to do this is to have some loop surround the primary function of the game and to keep calling it until such time as the user enters some key to stop the game:

while (!stopgame) {    startgame(); }

You can see that loops can be quite essential to games programming. They are key to keeping the game operating until the user is done playing. Loops are also often used to draw graphics. Loops can be quite useful in any graphics programming, but they are absolutely critical for command-line-based graphics. When you are trying to use basic cout statements to display simple graphics, loops are almost the only way to accomplish this. In the example of a tic-tac-toe game that you will see later in this chapter, for loops are used to draw the three rows of the Tic Tac Toe board.

Another familiar programming technique, that is of vital importance to games programming, is the if statement. Clearly any game, even a very simplistic one, must take different courses of action depending on what decisions the user makes. if statements are critical for branching action, depending on the user’s choices. Of course, with more complex choices a switch statement will be needed; however, the premise is the same. Responding to user input in an interactive manner is useful in any field of programming; it is especially critical to games programming.

Classes are also frequently used in all types of games programming. In simpler games, you may need an object to represent a deck of cards, a tic-tac-toe board, or dice. With more complex, graphics-oriented games, classes play an even bigger role. For example, in a flight simulator, you probably have a class that represents the airplane, the parts of the plane, other objects in the sky, and so on.

The real essence of games programming is to take the standard programming techniques you have already learned in this book and apply them in new and creative ways. The real secret to games programming is creative thinking.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: