Game Development Reference
In-Depth Information
Humans aren't really any better than computers at understanding “true” randomness. This is why
your MP3 player in shuffle mode sometimes seems to keep playing the same songs over and over.
You perceive naturally occurring streaks to be non-random when in fact they are random. This
means programmers sometimes have to create a function that appears random to humans—even
though it's not truly random.
In games, you have to deal very carefully with randomness. A wrongly designed mechanism to
spawn random units may spawn units of a certain type more often for certain players, giving them
an unfair (dis)advantage. Furthermore, when you design your games, make sure random events
don't have too much influence over the outcome. For example, don't have the player roll a die
after completing 80 levels of your highly challenging platform game and let the outcome of the roll
determine whether the player dies.
Calculating a Random Velocity and Color
Each time a can falls, you want to create a random velocity and color for it. You can use the
Math.random method to help you do this. Let's first look at creating a random velocity. For neatness,
you do this in a separate method in the PaintCan class called calculateRandomVelocity . You can
then call this method when you want to initialize the velocity of the can. Here you use the member
variable minVelocity to define the minimum velocity that paint cans have when they fall. This
variable is given an initial value in the reset method, which is called from the constructor:
PaintCan.prototype.reset = function () {
this.moveToTop();
this.minVelocity = 30;
};
You use this minimum velocity value when you calculate a random velocity, which you do in the
calculateRandomVelocity method:
PaintCan.prototype.calculateRandomVelocity = function () {
return { x : 0, y : Math.random() * 30 + this.minVelocity };
};
The method contains only a single instruction, which returns an object representing a velocity.
The velocity in the x-direction is zero, because cans aren't moving horizontally—they only
fall down. The y-velocity is calculated using the random-number generator. You multiply this random
value by 30 and add the value stored in the member variable minVelocity in order to get a positive
y-velocity between minVelocity and minVelocity+30 .
To calculate a random color, you also use the random-number generator, but you want to choose
among a few discrete options (red, green, or blue). The problem is that Math.random returns a real
number between zero and one. What you would like is to generate a random integer number of 0,
1, or 2. Then you can handle the different cases using an if instruction. Fortunately, the Math.floor
method can help. Math.floor returns the highest integer number that is less than the value passed
along as a parameter. For example:
var a = Math.floor(12.34); // a will contain the value 12
var b = Math.floor(199.9999); // b will contain the value 199
var c = Math.floor(-3.44); // c will contain the value -4
 
Search WWH ::




Custom Search