Game Development Reference
In-Depth Information
the expression
"Score: "+ 200
results in the string
"Score: 200"
. Instead of using a fixed
integer value, we can even use a variable. So if the variable
score
contains the value
175, then the expression
"Score: "+ score
evaluates to
"Score: 175"
. By writing this
expression as a parameter of the
DrawString
method, we always draw the current
score on the screen. The final call to the
DrawString
method then becomes (see the
GameWorld
class):
spriteBatch.DrawString(gameFont, "Score: " + score,
new
Vector2(20, 18), Color.White);
Watch out: concatenation only makes sense when we are dealing with text. For
example, it is not possible to 'concatenate' two numbers: the expression
1+2
will
result in
3
and not
12
. Of course, we can concatenate
numbers represented as text
:
"1"+ "2"
will result in
"12"
. Making the distinction between text and numbers is done
by using the double quotes.
In fact, what we have secretly done in the expression
"Score: "+ 200
is a
cast
.The
integer value 200 was casted to the
string
“200” but we did not have to write this
explicitly as
"Score: "+ (
string
)200
. The reason is that it is clear to the compiler we
want to convert the integer value to a string value. Similarly, an implicit cast is also
performed if we execute the instruction
i='
∗
'
. As soon as a cast is no longer trivial, or
there are special cases that the compiler has to deal with, we have to state explicitly
that we want to perform a cast:
c=(
char
)i;
If we want to convert a
string
value to a
double
or an
int
value, things get a bit
more complicated. This is not an easy operation to perform by the compiler, and not
all strings can be converted to a numerical value. For this, the primitive types have a
special method associated with them called
Parse
. Here is an example of how to use
this special method:
int
result =
int
.Parse("10");
double
otherResult =
double
.Parse("3.14159");
Who plays games?—
You might think that games are primarily played by
young males, but that is not true at all. A huge percentage of the people play
games. In 2012 in the US there were 157,000,000 active gamers, which is half
of its total population (including babies). They play games on many different
devices. For example, more than 70 % played casual games on websites. Most
time though was spent on console games.