Game Development Reference
In-Depth Information
MousePosition
from the
InputHelper
class:
−
position;
velocity = inputHelper.MousePosition
By calculating the velocity in this way, we will also have the desired effect that when
we click further away from the cannon, the velocity will be bigger, because then the
difference between the mouse position and the ball position will also be bigger.
However, if we were to play the game now, the ball would move a bit slowly. There-
fore, we multiply this velocity with a constant value that gives the ball a velocity
that is usable in the context of this game:
−
position)
∗
1.2f;
velocity = (inputHelper.MousePosition
The constant value of 1.2 is chosen after testing the gameplay with different values.
Each game will have a number of these
gameplay parameters
that will need to be
tweaked while play-testing the game to determine their optimal value. Finding the
right values for these parameters is crucial for a balanced game that plays well, and
you need to make sure that the values that you choose for these parameters do not
make the game overly easy or difficult. For example, if we would choose a constant
value of 0.3 instead of 1.2, the ball would move much slower. This would make the
game much more difficult, and it might even make the game unplayable because the
ball might never be able to reach the furthest can.
If we add the
HandleInput
method, it will not automatically be called. We need to
do that explicitly in the
GameWorld
class. Therefore, we add an extra instruction to
the
HandleInput
method of that class:
public void
HandleInput(InputHelper inputHelper)
{
cannon.HandleInput(inputHelper);
ball.HandleInput(inputHelper);
}
8.2.4 Updating the Ball
A big advantage of grouping related member variables and behavior together in
classes is that we can keep each class relatively small and clear. We have chosen to
design a class structure that more or less reflects the various kinds of game objects in
the game. In our case, we have a class for the cannon, as well as a class for the ball.
Our goal is that each of these game objects deals with player input relevant for that
object. We also want the game objects to update and draw themselves. Therefore,
we add an
Update
method and a
Draw
method to the
Ball
class, so that we can call this
method from the
GameWorld
class.
Inside the
Update
method of the ball, we need to define what the behavior of the
ball is. The ball behavior is different depending on whether it is currently shooting
or not. This is the complete method: