Game Development Reference
In-Depth Information
And accessing this variable is then done through a static property, just like we did
for the game world:
public static Vector2 Screen
{
get { return screen; }
}
Now that we have done that preparatory work, we can go back to the IsOutsideWorld
method, and use the static Screen property. The body of the method consists of a
single instruction using the keyword return to calculate a boolean value. The logical
'or' operation is used to cover the different cases in which the position is outside of
the game world:
return position.X < 0 || position.X > Painter.Screen.X || position.Y > Painter.Screen.Y;
As you can see, we do not mind if the y coordinate is smaller than zero.
This allows us to have the ball ending up above the screen and falling back in
again.
Let us return to the Update method in the Ball class. The second if -instruction calls
the IsOutsideWorld method in its condition, and if this method returns the value true ,
then the Reset method is executed. Or in simpler terms: if the ball flies out of the
screen, it is placed at the cannon, ready to be shot again by the player. Here we
see another advantage of grouping instructions in methods: methods such as Reset
can be reused in different parts of the program, which saves development time and
results in shorter, better readable programs.
Finally, in order to call the Update method of the ball, we add an instruction to
the (until now empty) GameWorld.Update method:
public void Update(GameTime gameTime)
{
ball.Update(gameTime, this );
}
8.2.5 Drawing the Ball on the Screen
Finally, for drawing the ball on the screen, we also add a Draw method. Here, we do
not have to do anything in particular, we simply draw the ball sprite in the current
color on the screen at the desired position. For the complete example, see the Painter5
program and the other classes in the project.
Search WWH ::




Custom Search