Game Development Reference
In-Depth Information
And here is how you load the relevant sounds, using the Sound class that you just created:
var loadSound = function (sound, looping) {
return new Sound("../../assets/Painter/sounds/" + sound, looping);
};
sounds.music = loadSound("snd_music");
sounds.collect_points = loadSound("snd_collect_points");
sounds.shoot_paint = loadSound("snd_shoot_paint");
Now it's very easy to play sounds during the game. For example, when the game is initialized,
you begin playing the background music at a low volume, as follows:
sounds.music.volume = 0.3;
sounds.music.play();
You also want to play sound effects. For example, when the player shoots a ball, they want to hear it!
So, you play this sound effect when they start shooting the ball. This is dealt with in the handleInput
method of the Ball class:
Ball.prototype.handleInput = function (delta) {
if (Mouse.leftPressed && !this.shooting) {
this.shooting = true;
this.velocity = Mouse.position.subtract(this.position)
.multiplyWith(1.2);
sounds.shoot_paint.play();
}
};
Similarly, you also play a sound when a paint can of the correct color falls out of the screen.
Maintaining a Score
Scores are often a very effective way to motivate players to continue playing. High scores work
especially well in that regard, because they introduce a competitive factor into the game: you want to
be better than AAA or XYZ (many early arcade games allowed only three characters for each name in
the high-score list, leading to very imaginative names). High scores are so motivating that third-party
systems exist to incorporate them into games. These systems let users compare themselves against
thousands of other players around the world. In the Painter game, you keep it simple and add a
member variable score to the PainterGameWorld class in which to store the current score:
function PainterGameWorld() {
this.cannon = new Cannon();
this.ball = new Ball();
this.can1 = new PaintCan(450, Color.red);
this.can2 = new PaintCan(575, Color.green);
this.can3 = new PaintCan(700, Color.blue);
this.score = 0;
this.lives = 5;
}
 
Search WWH ::




Custom Search