Game Development Reference
In-Depth Information
The number for the symbol '0' is not 0, but 48. Also the space (' ') does not have
code 0, but code 32. The symbol that does have code 0 is a special symbol that does
not have any visible representation. You can find out what the code of a char is by
assigning the char value to an int value:
char c; int i;
c='
';
int i=c;
Or even directly:
';
int i='
This is always possible. After all, there are only 65,536 different symbols, while the
largest int value is more than 2 billion. This assignment trick also works the other
way around, but then you have to tell the compiler explicitly that you agree with any
unexpected conversions in case the int value is too big. You do this by indicating
explicitly that you want to convert the int value to a char value as follows:
c=( char )i;
This allows you to perform calculations with symbols. The symbol after 'x' is
( char )('x' + 1) , and the capital letter c is ( char )(c
'A'+1) . This conversion notation is
called a cast, as we have also seen in Sect. 4.2.6 . Casts are very useful. For example,
you can also use a cast to remove the decimals:
double d; int i;
d = 3.14159;
i=( int )d; // now contains the value 3
11.6.2 String Operations
In the Painter game, we use string values in combination with the DrawString method
to draw text of a certain color in a desired font somewhere on the screen. In our
case we want to write the current score in the top left of the screen. The score is
maintained in an integer member variable called score . This variable is increased or
decreased in the Update method. So how can we construct the text that should be
printed on the screen, since a part of this text (the actual score) is changing all the
time? The solution is called string concatenation , or: gluing one piece of text after
another. In C# (and in many other programming languages as well), this is done
using the plus sign. For example, the expression "Hi, my name is "+ "Arjan" results in
the string "Hi, my name is Arjan" . In this case, we concatenated two pieces of text. It
is also possible to concatenate a piece of text and an integer value. For example,
Search WWH ::




Custom Search