Game Development Reference
In-Depth Information
constructor is then given as follows:
public
PaintCan(ContentManager Content,
float
positionOffset, Color targetcol)
{
this
.colorRed = Content.Load<Texture2D>("spr_can_red");
this
.colorGreen = Content.Load<Texture2D>("spr_can_green");
this
.colorBlue = Content.Load<Texture2D>("spr_can_blue");
targetcolor = targetcol;
minVelocity = 30;
Color = Color.Blue;
position =
new
Vector2(positionOffset,
−
currentColor.Height);
velocity = Vector2.Zero;
}
Inside the
GameWorld
class, we call this constructor three times to create the three
PaintCan
objects, each with a different
x
-offset and target color.:
can1 =
new
PaintCan(Content, 450.0f, Color.Red);
can2 =
new
PaintCan(Content, 575.0f, Color.Green);
can3 =
new
PaintCan(Content, 700.0f, Color.Blue);
Because the paint cans do not handle any input (only the ball and the cannon do
this), we do not need a
HandleInput
method for this class. However, the paint cans do
need to be updated. One of the things that we want to do is to have the paint cans
fall down at random moments and at random speeds. But how can we do this?
8.3.2 Randomness in Games
One of the most important parts of the paint can behavior is that some aspects of
it should be
unpredictable
. We do not want every can falling down at a predictable
speed or time. We want to add a factor of
randomness
to it, so that every time the
player starts a new game, the game will be different. Of course, we also need to
keep this randomness in control. We do not want one can to take three hours to fall
from top to bottom while another can takes only 1 millisecond. The speed should be
random, but within a
playable range of speeds
.
What does randomness actually mean? Generally, random events or values in
games and other applications are managed by a
random number generator
.In
object-oriented programming languages like C#, this is often implemented as a class
of which we can make an instance. This instance then has methods that generate ran-
dom numbers according to different parameters. But then you might wonder: how
do you actually generate a completely random number? And you may even wonder
if randomness exists at all in reality. After all, is not randomness just a manifestation
of behavior that we cannot yet fully predict and therefore we call it 'random'? Well,
let us not get too philosophical here. In game worlds and in computer programs,