Game Development Reference
In-Depth Information
called a
parameter
of the method. A method can have more than one parameter,
as we will see later on. The parameters are written between parentheses. When a
method is called, we always write parentheses behind it, and within the parentheses
are the parameters (if required).
Using methods—
Do we need to know which instructions are grouped to-
gether in the
Clear
method in order to use it? No, we do not! This is one of
the nice things of grouping instructions together in methods. You (or other
programmers) can use the method without knowing how it works. By smartly
grouping instructions in methods, and methods in classes, it is possible to
write reusable pieces of program that can be used in many different contexts.
The
Clear
method is a good example of this. It can be used for a lot of different
applications and you do not need to know how the method works in order to
use it. The only thing you need to know is that it takes a
Color
as a parameter.
3.3.11 Update and Draw
Because the
BasicGame
class is a special version of the
Game
class, our application
should have an
Update
method and a
Draw
method. Because a method is basically a
group of instructions, every time the
Update
method is called, the instructions inside
that method are executed. And the same goes for
Draw
.
As an example, imagine that we want a simple game where a balloon is drawn at
the position of the mouse pointer. By moving the mouse around, the balloon moves
along with the mouse. In terms of the
Update
and
Draw
methods, we could do this
as follows. In the
Update
method we need to execute an instruction that retrieves
the current position of the mouse pointer and that stores it in the memory. In the
Draw
method, we need to execute an instruction that displays a balloon image at the
stored position. Of course, we do not yet know if these instructions exist (spoiler:
they do!) and we do not yet know what these instructions look like. Also, you might
wonder why this would work. We are not moving around the balloon, we are simply
drawing it at a position that was stored in the
Update
method. Recall that in an XNA
game, the
Update
and
Draw
methods are executed at a very high rate (sixty times per
second). Because of this very high rate, drawing the balloon at different positions
makes it look like the balloon moves (but it actually does not). This is how all game
worlds are drawn and how the player is lured into thinking that there is movement in
the world. In reality, we are just drawing the images very fast at different positions.
Stay tuned, we are going to return to this example and actually make it work later
on!