Game Development Reference
You can substitute the mouse's position for the position of any object in a game, such as the player's
position. Using the Psaaj class and its related methods, properties, and events, you have the basis of
an effective enemy AI system without an eb/ahoa statement in sight. (Remember to use op]ca*ikqoaT
and op]ca*ikqoaU if you use this technique in a class other than the document class.)
You've only just scratched the surface of what you can do with Psaaj objects in your games. I hope
this will inspire you to push the boundaries even farther. There's a rich vein of untapped potential here
that can be put to some fantastic use in games. Go forth and conquer!
Case study: Complex mouse-driven player control
To round off this discussion of mouse-driven player control systems, you'll look at a real-world exam-
ple that takes into account the kind of complexity some of your games will demand.
Many of the games you'll be designing won't need this level of detail in player control; you can get
by just fine using some of the systems discussed earlier in the chapter. For those games that require
subtle detail in the way the player character is controlled, however, you'll need to put on your think-
ing cap and work out how you can use a combination of the techniques you've been looking at in the
past two chapters.
When you're just learning programming and game design, it won't be obvious at all. So, to make the
process a little easier for you, you'll take a detailed look at a player control system that's very much
an everything-but-the-kitchen-sink example. I'll throw in as many techniques as can fit, and I'll leave it
up to you to deconstruct the examples to make them work the way you might need them to in your
The example you'll look at is a control system for Button Fairy. It implements a classic spaceship-style
control scheme using a mouse-based control system instead of a keyboard-based one. You'll also
implement a system to allow you to fire bullets in all directions by using some new I]pd class func-
tions, which isn't necessarily the best way for this control scheme to be built. But it's a good starting
point for you to create your own control systems by giving you an inside look at how it's been done.
Take a look at how this control scheme works. Find the ?d]n]_pan?kjpnkh folder in the chapter's
source files, open it as a project, and take Button Fairy for a test drive. You can fly her around the
stage by moving the mouse. The mouse also controls the position of a yellow “wand.” If you click the
left mouse button, the wand fires red stars in the direction of the mouse.
Button Fairy can be tricky to control at first because her movements are not based on easing. She
uses acceleration and friction (discussed in the previous chapter) to regulate her speed. If the mouse
is greater than 75 pixels from Button Fairy's center position, she'll accelerate toward it. If the mouse is
less than 75 pixels away, acceleration stops and friction kicks in. With a bit of practice, you'll find you
can have quite a bit of subtle control. Figure 10-13 illustrates how Button Fairy behaves.