Constants, Functions, and Types in Invaderz

[ LiB ]

Constants, Functions, and Types in Invaderz!!!

Let's take the time to go over each of the variables , constants, functions, and types used in Invaderz!!! First off, let's check out all of the constants.

There are only a few constants, but they are very important. Table 13.1 lists all of the constants and their descriptions.

Table 13.1. Constants in Invaderz!!!

Constant

Value

Description

ESCKEY

1

The key code for the Esc key.

SPACEBAR

57

The key code for the Spacebar.

LEFTKEY

203

The key code for the left arrow.

RIGHTKEY

205

The key code for the right arrow.

CHANGEENEMYDIRECTION

700

The time (in milliseconds ) between velocity changes for enemy UFOs.

TIMEBETWEENENEMYBULLETS

1200

The time (in milliseconds) between enemy bullet fire.


I am sure you understand how the first four key codes work (in case you need a refresher, they are used with KeyDown() and KeyHit() ), but you might not know what CHANGEENEMYDI- RECTION and TIMEBETWEENENEMYBULLETS do. Well, let's go over both of them.

In this program, the enemies' movement is random. We need to move those UFOs in a random direction at the beginning. We don't want it to appear random, however, so we adjust their direction variables every once in a while to make it appear that they are moving. CHANGEENEMYDIRECTION determines the time between those direction changes. Each UFO changes direction every 700 milliseconds (or every 7/10 of a second).

TIMEBETWEENENEMYBULLETS does the same sort of thing. This constant determines how long a break there is between the bullets shot by the enemy. Instead of 700 milliseconds like the CHANGEENEMYDIRECTION constant, this constant has the enemies fire bullets every 1,200 milliseconds (1 and 1/5 of a second, or every 1.2 seconds).

Cool? Let's examine the functions. There are a lot of them!

Table 13.2 lists each function in Invaderz!!!

Table 13.2. Functions in Invaderz!!!

Function

Description

InitializeLevel()

Resets the level with the proper number of enemies and resets all the starting variables.

DrawHUD()

Draws the health points remaining and displays the bullets fired /hit in the top of the window.

CreateNewEnemy()

Creates a new enemy ship onscreen.

DrawShips()

Draws the enemy and player spaceships.

EnemyAI()

Updates the directions and bullet fire of the enemy spaceships.

CreateBullet()

Creates a new bullet onscreen.

UpdateBullets()

Moves bullets and checks to see if they've collided against any opposing ships.

CreateExplosion()

Creates an explosion after a ship collision.

UpdateExplosions()

Rotates explosions through its frames and deletes the explosion when it is over.

GameOver()

Prepares the game for exit and quits to desktop.


And those are the functions that are used in the program. Of course, not all of these functions are called by the main loop, so I drew a function outline that explains how the functions interact with one another (see Figure 13.4).

Figure 13.4. The function outline for Invaderz!!!


That's all for functions. Last, we have the types used in Invaderz!!!

There are four types used in Invaderz!!! They are

  • The ship type

  • The user type

  • The bullet type

  • The explosion type

The ship type refers to all of the enemies that are created during the game, the user type is the player spaceship that is onscreen, the bullet type describes each bullet that is fired during the game (both enemy and player bullets), and the explosion type refers to explosions that occur after a ship dies for either enemy or player.

I am going to list tables with all of the types' fields, in order to give you a feel for the Invaderz!!! program.

Table 13.3 lists the ship type's fields.

Table 13.3. The Ship Type's Fields

Field

Description

x

The x coordinate of the Ship.

y

The y coordinate of the Ship.

hits

The hit points remaining on the Ship.

xv

The x direction variable that governs how far left and right the Ship moves per frame.

yv

The y direction variable that governs how far up and down the Ship moves per frame.

frame

The frame of the animated image that will be drawn. (See Figure 13.2.)


Table 13.4 lists the user type's fields, which are used to make the player.

Table 13.4. The User Type's Fields

Field

Description

x

The x coordinate of the User.

y

The y coordinate of the User.

hits

The remaining hit points of the User.

frame

The frame of the animated image that will be drawn. (See Figure 13.3.)

draw

Determines whether the user should be drawn on the screen or not. The User should be drawn if set to 1, and should not be drawn if set to 0.


Notice, if you will, that the ship and user types are very similar to one another. This is because they are both spaceships, and although they are opposing forces, both of them have to move in the same way. Their similarities remind me of comic-book heroes and villains: the villains are almost exactly the same as the heroes, and in fact, they are often friends growing up.

Table 13.5 lists the bullet type's fields.

Table 13.5. The Bullet Type's Fields

Field

Description

x

The x coordinate of the Bullet.

y

The y coordinate of the Bullet.

draw

Determines whether the Bullet should be drawn on the screen or not. The Bullet should be drawn if set to 1, and should not be drawn if set to 0.

from

Determines who fired the Bullet. This is set to 1 if it was fired by User and set to 2 if it was fired by an enemy.

frame

The frame of the animated image that will be drawn.


And last but not least, Table 13.6 lists the explosion type's fields.

Table 13.6. The Explosion Type's Fields

Field

Description

x

The x coordinate of the Explosion.

y

The y coordinate of the Explosion.

from

Determines who is exploding. This is set to 1 if the User exploded, and set to 2 if the enemy exploded.

frame

The frame of the animated image that will be drawn.


Woo-hoo! Now, let's move on to actually playing the game.

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net