An Example: Building a Tanks Game
We've built two simple game prototypes on top of the framework described in the previous section: a tanks game and a group jigsaw puzzle.
In this section, we describe how the tanks game was built. We hope that by showing how we built our prototype on this framework, we will show how you can build your game on top of the framework as well. The tanks game, the jigsaw puzzle, and the framework are available in source-code form at the Intel Software Network site.
Tanks Game Client Code
The client-side code for the tanks game deals with player creation, player actions, and game restart.
Player Creation
When a player opens their browser to the player screen, they are presented with a simple HTML form as shown in Figure 4. You can download the HTML source code by clicking here.
Figure 4 - The player creation form.
When the player-create form is POSTed, PHP code extracts the player name and team color and then uses the game framework to both identify a unique number for the player and add a new player instance into the game database, as shown in the code in Figure 5.
public static function createPlayer( $name, $team ) {
// get number for player
$mates = $GLOBALS['db']->select(array(
'table' => 'players',
'fields' => array( 'id', 'teamNumber' ),
'condition' => array( 'team' => $team )
));
$number = 0;
if( !empty($mates) ) {
$next = $mates[0]['teamnumber'];
foreach( $mates as $mate ) {
if( $next < $mate['teamnumber'] ) {
$next = $mate['teamnumber'];
}
}
$number = ( $next + 1 ) % self::$NUM_TANKS;
}
// create player
$_SESSION['me'] = new Player( $name, $team, $number );
// join the game
$_SESSION['me']->join( $GLOBALS['game'] );
}
Figure 5 - Adding a player to the game database.
At this point, the player is created and can begin to input actions (commands) into the game.
Player Actions
Player actions, or commands, are captured via JavaScript running in the client's browser after they have successfully created a player instance. This code, as show in Figure 6, first parses the input and then POSTs a message to the game server containing an indication of the player's command. (This code uses the jQuery JavaScript library to POST the message.)
// Player Controls:
// these are mappings to keyboard keys
var Control = {
Forward: 38, // arrow key up
Back: 40, // arrow key down
RotateLeft: 37, // arrow key left
RotateRight: 39, // arrow key right
Shoot: 32, // space bar
Quit: -1
};
// _execute is called when a user presses a key
var _execute = function( _opts ) {
switch( _opts.keyCode || _opts.which || _opts.other ) {
case Control.Forward: // move forward
_action.type = Command.Move;
_action.message = 'forward';
break;
case Control.Back: // move back
_action.type = Command.Move;
_action.message = 'back';
break;
…
default:
_action.message = '';
}
if( _action.message != '' ) {
// send player command to server
$.post( _url, _action, function( gameID ) { … }
}
Figure 6 - Handing Player commands in JavaScript.
At this point, the client code is nearly complete. We've created a player and passed the player's commands on to the game server. All that is left is to detect when the game has finished or restarted.
Game Restart
The game framework makes it easy to detect when a game has either ended or restarted. Each time a player command is entered, the response from the game server is the current game identifier. We check whether this identifier has changed, and if it has, we simply call createPlayer again (see Figure 5).
|
http://video.google.com/videoplay?docid=-1651221202388387390
ProjectorGames has been almost exactly the same idea for some 5 years now - and we're for hire ;-)
It's hard to tell from the video, but it looks like you have multiple XBox controllers hooked up to a USB hub, is that right? If so, I would say the ability to use any Internet capable device as a controller is an important difference (along with the fact that we're giving away code).
However, I wish we had known about your work while we were working on this article. I'm still interested in comparing notes & sharing work. Is there any place we can learn more about the technology behind your work?
Thanks,
--Scott
We've tried a number of different ways of getting people's input up to the main screen, including DS, PSP, mobile phones + bluetooth. Internet-enabled devices isn't something we tried, simply because people almost never have these at LANs (or they have massive alienware laptops that will singe their laps)
As for technology, well, the PG system is a custom game engine, built around a DirectX renderer, and has support for lots of abstract controllers, player naming, cross-event scoringkeeping, but there's nothing overly original there - all the cool stuff's in the game design. Feel free to grab me at djarcas@hotmail.com if you'd like to ask anything else tho.
Did I mention we were for hire for lan parties and corporate events? ;-)
I think we have a great pairing here. You say "all the cool stuff's in the game design" and we know our games are nothing special...indeed the games we created are just to show the usage model and the technology we used to hook it all together.
With more and more "mobile Internet devices" in the world every day, at some point it might be worth you checking out putting your games on our code (which we're giving away).
And I'll definitely keep you in mind for parties and corporate events. :)
--Scott
I'm trying to get this to work, but I keep getting an error that says that the MDB2.php file was not found and then it gives me a fatal error. I have it uploaded at www.brandonbarnes.us/game/index.php . I noticed in the readme that it required Apache 2, but my server is only running 1.3.41. Could this be the problem, or am I totally wrong?
Thanks!
The reason the README file lists Apache 2 as a requirement is because I had Apache 2 installed when developing this project. I would say it couldn't hurt to try it on Apache 2 if you can. However, the error you are getting seems to be related to the MDB2 PEAR package. What version of MDB2 do you have installed? I used version 2.4.0.
Thanks for trying it out...I would like to help you get this working.
Can you shoot me an email at freelance.brandon@yahoo.com ? I would love to get this working.
I've got the MDB2 PEAR package installed, but it still doesn't recognize it, so I'm wondering what else it could be.
Thanks!