Game Development Reference
In-Depth Information
Game structure
The game is made up of four classes:
I]ej[Ol]_aOdkkpan: The document class that handles all the game logic. It stores all the bullet
objects in an array and checks whether any of them are hitting the player or robot. It updates
the scores and removes bullets when they hit either object. It also removes the bullets if they
reach the stage boundary.
Lh]uan: The player's class. It's completely self-contained but uses the >qhhap class to fire
Nk^kp: Self-contained, but also uses the >qhhap class to fire bullets.
>qhhap: When a new bullet is created, it dispatches an event to tell the game, “Hey, I've been
created!” The document class listens to these events and uses them to add the new bullets to
an array.
Figure 10-25 illustrates how these classes work together.
Main_SpaceShooter (Document Class)
Adds bullets to an array when they're created.
It uses the bullet objects in the array to check for
collisions with the player and robot. It removes them
from the stage when they hit an object or reach the
stage boundary.
Robot Class
Bullet Class
Player Class
Moves the robot.
Makes star and circle bullets.
Moves the player.
Requests circle bullets.
Dispatches an event when a
bullet has been created.
Requests star bullets.
Figure 10-25. Space Shooter game structure
There are quite a few ways that this game could have been structured. The version presented here is
not the best for keeping the classes entirely encapsulated or for code efficiency, but it's the clearest
demonstration of how dispatching events works with the simplest code. I intentionally opted for some
redundant code so that you can clearly see what's happening. Once you see how it works, I'm sure
you'll have some clever ideas for improving the code even further.
Search Nedrilad ::

Custom Search