Game Development Reference
In-Depth Information
Chapter 4
Beginning C++ Game
Development with Arrays
We will begin to create our first C++ game in this chapter. So far this topic has looked at the various
types supplied by the C++ programming language as well as the operators that can be used to
alter and compare these types. The next major area of the C++ programming language that you
should understand to begin constructing complex programs is data storage. The individual types in
C++, such as int and char , can hold single values, but often we will want to be able to remember
multiple values all relating to similar things. We could achieve this by creating a unique variable for
every object with a value that we wish to remember; however, C++ provides us with arrays to store
multiple values together. This chapter takes a look at how arrays are used in C++ and also explores
how we can use arrays to begin our text adventure game.
The C++ Array
We looked at the various types supplied by C++ in Chapter 2, but each of the examples involved
looking at only a single variable at a time. When writing programs, we would rather work with
multiple pieces of data, as this is the type of task that computers are exceptionally good at.
The following line of code shows how we can define an array that contains five integers.
int intArray[5] = { 0, 1, 2, 3, 4 };
This code creates an array of five integers each containing the values 0 through 4. The individual
variables contained within an array are often referred to as the elements of the array. Listing 4-1
shows how we can use this array in a simple program.
37
 
Search WWH ::




Custom Search