Game Development Reference
In-Depth Information
n
Age
: The age of a person, which changes from year to year.
n
Playing time
: The amount of time that a person has played a level, the
game today, or the total time.
In C# and in other languages, you have to tell the computer what variables you
want to create. The computer will then create a space in memory to store any
values that it might hold. You can also place values in those variables. To do this
in C#, you can program the following:
int score;
int score = 42;
int health;
int totalInventory;
string name;
name = "Jason";
The first example uses the keyword
int
to create a variable called
score
(you will
learn about keywords shortly). Elsewhere in the program, you could then use
score
within the code. The second example automatically assigns the value 42 to
the score. The third and fourth examples have again just told the computer that
you are creating two memory slots for
health
and
totalInventory
with an
int
keyword.
The example
string name;
tells the computer that you want to reserve a space in
memory for a string variable called
name
. The next line tells the computer to put
the value
“Jason�
into the string variable called
name
.
Notice that you need to complete the lines using a semicolon. This tells the
programming language that the line is complete. This is very important because
programming languages need to know when a particular set of instructions has
finished. Code can be quite long; in some cases, it may run over a number of
lines.
You can see some of these examples in code in Figure 14.2.
Note
In Figure 14.2, you will notice that there is a green line under the words
score
and
name
. This is
because the variables have been declared and their values set, but the variables are not used
anywhere in the application. You can ignore this for the purpose of the example here.
Search WWH ::
Custom Search