Implementing Multiple Players
The next little bit of game programming legerdemain is implementing multiple players. Of course, if you want to implement a networked game, that's a whole other story, although DirectPlay makes the communication part easy, at least. However, if all you want to do is let two or more players play your game at the same time or take turns, that requires nothing more than some extra data structures and a bit of housekeeping.
Implementing turn-taking is easy and hard at the same time. It's easy because if you can implement one player, implementing two or more only requires having more than one player record. It's hard because you must save the game for each player when switching players. Hence, usually you need to implement a save game option if you want to have turn-taking. Obviously, the players shouldn't know that the game is being saved as they take turns, but that's what's really going on.
With that in mind, here's a list of the steps that you would follow to allow two players to play, one after another:
As you can see, step 5 is where the action starts happening and the game starts pinging back and forth between players. If you want more than two players, simply play them one at a time until you're at the end of the list, and then start over.
Enabling two or more players to play on the same screen is a little harder than swapping because you have to write the game a little more generally as far as gameplay, collision, and interaction between the players are concerned. Moreover, with multiple players in the game at the same time, you must allocate a specific input device for each player. This usually means a joystick for each player, or maybe one player uses the keyboard and one uses the joystick.
The other problem with putting multiple players in the game at the same time is that some games just aren't good for it. In a scrolling game, for example, one player might want to go one way while the other wants to go another way. This can cause a conflict, and you'll have to plan for this. Thus, the best games for multiple players are single-screen fighting games, or games in which the players stay relatively near each other for one reason or another.
But if you want to allow the players to roam around freely, you can always create a split-screen display, as shown in Figure 11.14.
The only problem with the split-screen display is—the split-screen display! You must generate two or more views of the game, which can be technically challenging. Moreover, there might not be enough room on the screen to fit the two or more views, and the players might find it hard to see what's going on. But the bottom line is, if you can pull it off, it's a cool option…