Game Development Reference
In-Depth Information
Fig. 4.2
A screenshot of the
SpriteDrawing
program
running
spriteBatch.Begin();
spriteBatch.Draw(balloon, Vector2.Zero, Color.White);
spriteBatch.End();
Before we start drawing anything, we have to make sure that the
Begin
method is
called. In that method, the graphics device is prepared for drawing things on the
screen. The
End
method tells the graphics device that we are done drawing, so things
can be cleaned up for the next drawing batch. Neither the
Begin
nor the
End
methods
need any parameters, therefore we leave the parentheses empty. The
Draw
method
takes three parameters. The first parameter is the sprite that needs to be drawn. The
second parameter is the position of the sprite on the screen. This position is given
as a two-dimensional coordinate, where the coordinate
(
0
,
0
)
corresponds to the top
left of the game screen. Just like
Color.Chocolate
is an expression that gives the color
chocolate, the expression
Vector2.Zero
gives the two-dimensional vector
(
0
,
0
)
.Ifwe
provide the top left point on the screen as a parameter to the
Draw
method, then the
top left point
of the sprite is drawn there. Finally we can provide a 'tint' to optionally
change the color of the sprite. If the color
Color.White
is supplied, no tinting happens,
which is what we want in this case. Figure
4.2
shows a screenshot of the program
when running.
Just like with the
Color
class, we could also make a
Vector2
variable that contains
a position depending on the current time. Let us call this variable
balloonPosition
, and
we declare it as follows:
Vector2 balloonPosition;
Where should we place this declaration? If you think about it, the balloon position
is a part of the game world. We update the game world in the
Update
method, and
we draw it in the
Draw
method. Because multiple methods need to access this vari-
able, we therefore need to declare it at the class body level, and not in one of the
methods. Now that is done, we can write the following instructions in the
Update
method: