Chapter2.Slapshot The Interactive Hockey Game

Chapter 2. Slapshot! The Interactive Hockey Game

The Slapshot! application.

Let's get interactive. Chapter 1, "Making Fish Swim in the Multithreaded Aquarium," was all about the Aquarium application, which is nice to watch but doesn't really let you get in on the action. This chapter is on Slapshot!, an interactive hockey game, where you actually have to do something active, or you'll lose. The code developed in Chapter 1 is going to be leveraged for this chapter, because Slapshot! uses hockey puck sprites, much like the fish in the aquariumonly the pucks move faster.

You can see what this game looks like in Figure 2.1. The pucks shown in this figure are zooming around the ice at the speed you choose (varies from very slow to just about unbeatably fast). Your job is to use the mouse with the blocker on the left, as you see in the figure, to keep the pucks out of the goal (represented as a thick red vertical line in the app). The computer's going to play against you on this onethe blocker in front of the goal on the right is run by the code. The score appears near the bottom of the game's window. Can you beat the game? Probably. Until you start increasing the speed anyway….

Figure 2.1. The Slapshot! game in action.

Here are some of the things this application does:

  • Creates a sprite class (called Puck) whose objects can move and draw themselves on the screen as needed

  • Animates the sprites, moving them around and bouncing them off both each other and the walls of the rink as needed

  • Uses double buffering to avoid making the onscreen image flicker

  • Uses the mouse to let you block pucks from your goal

  • Uses a menu system to let users start and stop games, set the speed, and exit

  • Displays a dialog box on command to let you set the speed

  • Uses a separate thread to move the pucks around

So how do you create this game and get it running? The first step, as you might expect, is to build the rink.

    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: