Online Book Reader

Home Category

HTML5 Canvas [155]

By Root 6497 0
sections and Arrays of Logical Display Objects for further details on each:

var player = {};

var rocks = [];

var saucers = [];

var playerMissiles = [];

var particles = []

var saucerMissiles = [];

Level-specific variables

The level-specific variables handle the difficulty settings when the game level increases. See the section Level Knobs for more details on how these are used:

var levelRockMaxSpeedAdjust = 1;

var levelSaucerMax = 1;

var levelSaucerOccurrenceRate = 25

var levelSaucerSpeed = 1;

var levelSaucerFireDelay = 300;

var levelSaucerFireRate = 30;

var levelSaucerMissileSpeed = 1;

The player Object

The player object contains many of the variables we encountered earlier in this chapter when we discussed animating, rotating, and moving the player ship about the game screen. We have also added in three new variables that you have not seen before:

player.maxVelocity = 5;

player.width = 20;

player.height = 20;

player.halfWidth = 10;

player.halfHeight = 10;

player.rotationalVelocity = 5

player.thrustAcceleration = .05;

player.missileFrameDelay = 5;

player.thrust = false;

The new variables are halfWidth, halfHeight, and missileFrameDelay. halfWidth and halfHeight simply store half the width and half the height values, so these need not be calculated on each frame tick in multiple locations inside the code. The missileFrameDelay variable contains the number of frame ticks the game will count between firing player missiles. This way, the player cannot simply fire a steady stream of ordnance and destroy everything with little difficulty.

The player.thrust variable will be set to true when the player presses the up key.

Geo Blaster Game Algorithms

The game source code covers a lot of ground that we did not touch on earlier in this chapter. Let’s discuss some of those topics now; the rest will be covered in detail in Chapter 9.

Arrays of Logical Display Objects


We have used arrays to hold all our logical display objects, and we have an array for each type of object (rocks, saucers, playerMissiles, saucerMissiles, and particles). Each logical display object is a simple object instance. We have created a separate function to draw and update each of our objects.

NOTE

The use of an object class prototype similar to FrameRateCounter can be implemented easily for the various display object types. To conserve space, we have not implemented them in this game. However, these objects would allow us to separate the update and draw code from the current common functions, and then place that code into the individual object prototypes. We have included a Rock prototype at the end of this chapter as an example (see Example 8-13).

You will notice that saucers and rocks are drawn with points in the same manner as the player ship.

Rocks


The rocks will be simple squares that rotate clockwise or counterclockwise. The rock instances will be in the rocks array. When a new level starts, these will all be created in the upper-right corner of the game screen.

Here are the variable attributes of a rock object:

newRock.scale = 1;

newRock.width = 50;

newRock.height = 50;

newRock.halfWidth = 25;

newRock.halfHeight = 25;

newRock.x

newRock.y

newRock.dx

newRock.dy

newRock.scoreValue = bigRockScore;

newRock.rotation = 0;

The rock scale will be set to one of the three rock-scale constants discussed earlier. halfWidth and halfHeight will be set based on the scale, and they will be used in calculations in the same manner as the player object versions. The dx and dy values represent the values to apply to the x and y axes when updating the rock on each frame tick.

Saucers


Unlike Atari’s Asteroids game, which has both small and large saucers, we are only going to have one size in Geo Blaster Basic. It will be stored in the saucers array. On a 28×13 grid (using paths), it looks like Figure 8-6.

Figure 8-6. The saucer design

The variable attributes of the saucer object are very similar to the attributes of a rock object, although without the rock scale attribute. Also, saucers don’t have a

Return Main Page Previous Page Next Page

®Online Book Reader