This chapter presented Slapshot!, a fast-paced hockey game where the computer plays against you, the user. Slapshot! sends a dozen pucks whizzing around a hockey rink, and it's your job to keep them out of your goal while the computer tries to do the same with its goal. The pucks bounce off the walls of the rink, each other, and the blockers in front of the two goals. The computer player is not infallible by any means, but when those speeds get up there, it's a hard player to beat. This game built on the Aquarium project of the previous chapter, adding a lot of interactive power. It uses many of the techniques you saw in the Aquarium project, such as starting a worker thread, using double buffering to avoid flickering, handing images with media trackers, and so on. Slapshot! has a menu system to let the user start and stop a game, as well as to exit the game, using Boolean flags to communicate with the worker thread. In the old days, you could use the suspend, resume, and stop methods to handle threads, but these days, the Java people want you to interact with the thread using flags. The user can also use a menu item, Set Speed…, to set the speed (1 to 100) at which the pucks move. When the user selects that item, the application displays a dialog box with a text field and OK and Cancel buttons. The user can enter the new speed, and if the value entered is between 1 and 100, inclusive, the application uses that new speed. Slapshot! also intercepts mouse eventsmouse pressed, released, and dragged eventsin order to let the user move his blocker at will (as long it's in the Y direction). There's a lot of Java power here; all in all, Slapshot! is a cool game that'll keep users on their toes, fending off those pucks. |