OSdata.com |
Building a game open source code This is the actual source code from a new web game. See the game at thissideofsanity.com and read how this was built starting at example code.
This is example code from the SlamZee project and This Side of Sanity, released under Apache License 2.0.
Copyright 2013 Milo (for software), Distribution and website handled by Strazbick.com
Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
This narrative uses anchor links so that you can follow the building of this software in chronological order. Go to software explanation to start reading. Go to thissideofsanity.com to see the working software.
This is example code from the SlamZee project and This Side of Sanity, released under Apache License 2.0.
This version simply echoes out that it exists for debugging purposes.
<?php echo '<p align="center">the game goes here</p>'; ?>
Start the code for the game server dispatcher.
<?php /* FILE: /gameserver.php */ // include function files for this application require_once('./php/accounts.php'); require_once('./php/databasefunctions.php'); require_once('./php/gamefunctions.php'); /******************************/ /* GLOBALS */ /******************************/ /******************************/ /* FUNCTIONS */ /******************************/ /******************************/ /* StartGame */ /* start the designated game */ /******************************/ function StartGame($startcode,$reporteduser) { echo '<p align="center">Starting a '.$startcode.' game for player '.$reporteduser.'</p>'; $playernumber = GetAccountNumberFromEmail($reporteduser); DisplayVilliansRound($playernumber); /* located in gamefunctions.php */ } /* StartGame */ /******************************/ /* Main Page */ /* serve various game functions */ /******************************/ SessionManagement(); /* located in accounts.php */ $reporteduser = CheckValidUser(); /* located in accounts.php */ /* INITIALIZATIONS */ if(empty($_GET)) { /*go away if no valid requests */ return; } else { /*collect input from user */ $startcode = $_GET['start']; /* have a request to start a specific game */ } /* END if on GET */ /* dispatch based on what game activity requested */ if ( $startcode != '' ) StartGame($startcode,$reporteduser); ?>
Accept game input.
The new function:
/******************************/ /* NewRound */ /* accept input for new round */ /******************************/ function NewRound($gamechoice,$reporteduser) { echo '<p align="center">New round with input '.$gamechoice.' for player '.$reporteduser.'</p>'; } /* NewRound */
And the change to the dispatcher:
$startcode = $_GET['start']; /* have a request to start a specific game */ $roundchoice = $_GET['choice']; /* have an input for a specific game */ /* dispatch based on what game activity requested */ if ( $startcode != '' ) StartGame($startcode,$reporteduser); elseif ( $roundchoice != '' ) NewRound($roundchoice,$reporteduser);
/******************************/ /* NewRound */ /* accept input for new round */ /******************************/ function NewRound($gamechoice,$reporteduser) { $playernumber = GetAccountNumberFromEmail($reporteduser); echo '<p align="center">New round with input '.$gamechoice.' for player '.$reporteduser.'</p>'; DisplayVilliansRound($playernumber); /* located in gamefunctions.php */ } /* NewRound */
Change to StartGame.
/******************************/ /* StartGame */ /* start the designated game */ /******************************/ function StartGame($startcode,$reporteduser) { echo '<p align="center">Starting a '.$startcode.' game for player '.$reporteduser.'</p>'; $playernumber = GetAccountNumberFromEmail($reporteduser); $gamenumber = CreateVillainsGame($playernumber); /* located in gamefunctions.php */ DisplayVilliansRound($playernumber,$gamenumber); /* located in gamefunctions.php */ } /* StartGame */
Start building the round scoring function.
UpdateRoundResults($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */
Now add the check for the end of game.
if ( $newround == 11 ) DisplayEndGame($playernumber,$gamenumber); /* located in gamefunctions.php */ else DisplayVillainsRound($playernumber,$gamenumber); /* located in gamefunctions.php */
Add code to create the first round.
/******************************/ /* StartGame */ /* start the designated game */ /******************************/ function StartGame($startcode,$reporteduser) { $playernumber = GetAccountNumberFromEmail($reporteduser); $gamenumber = CreateVillainsGame($playernumber); /* located in gamefunctions.php */ IncrementVillainsRound($gamenumber); /* located in gamefunctions.php */ DisplayVillainsRound($playernumber,$gamenumber); /* located in gamefunctions.php */ } /* StartGame */
Add a little bit of code to check to see if the cookie timed out. If so, ask them to log back in rather than let them see a game with lots of SQL errors.
/******************************/ /* Main Page */ /* serve various game functions */ /******************************/ SessionManagement(); /* located in accounts.php */ $reporteduser = CheckValidUser(); /* located in accounts.php */ if ( $reporteduser == '' ) { echo '<h1 align="center">You must be logged in to play.</h1>'; echo '<h3 align="center">Your previous cookie timed out.</h3>'; DisplayLoginForm(); }
Process the trivia answer.
Collect input from user.
$triviachoice = $_GET['trivia']; /* have a trivia input for a specific game */
Dispatch to the handler.
elseif ( $triviachoice != '' ) ProcessTrivia($triviachoice,$reporteduser,$gamenumber);
Function to handle the trivia input.
/******************************/ /* ProcessTrivia */ /* accept input for trivia */ /******************************/ function ProcessTrivia($triviachoice,$reporteduser,$gamenumber) { $playernumber = GetAccountNumberFromEmail($reporteduser); $roundnumber = GetRoundNumber($gamenumber); // echo '<p align="center">New trivia with input '.$triviachoice.' for player '.$reporteduser.' and game number '.$gamenumber.' and round '.$roundnumber.'</p>'; /* DEBUG */ /* STORE THE PLAYER TRIVIA CHOICE */ $title = 'rd'.$roundnumber.'trivia1'; $query = "UPDATE games SET ".$title." = ".$triviachoice." WHERE gamenumber=".$gamenumber.";"; $updateresult = mysql_query($query); if(!$updateresult) { echo "<p>failed data base insert with error message ".mysql_error()."</p>"; } /* FIND CORRECT TRIVIA ANSWER */ $selecttitle = 'rd'.$roundnumber.'trivia'; $selectquery = "SELECT ".$selecttitle." FROM games WHERE gamenumber=".$gamenumber." "; $selectresult = mysql_query($selectquery); if(!$selectresult) { echo "<p>failed data base insert with error message ".mysql_error()."</p>"; } $row = mysql_fetch_array($selectresult); $correctanswer = $row[$selecttitle]; /* REPORT TRIVIA RESULT */ if ($correctanswer == $triviachoice) echo '<h1 align="center"><span style="color:green">Correct answer!</span>'; else echo '<h1 align="center"><span style="color:red">Wrong answer!</span>'; echo '<br>Slam or love a celebrity!</h1>'; } /* ProcessTrivia */
Add the love or like button.
$slamchoice = $_GET['slam']; /* have a celebrity slam input for a specific game */ $lovechoice = $_GET['love']; /* have a celebrity love input for a specific game */ elseif ( $slamchoice != '' ) NewRoundAfterSlam($slamchoice,$reporteduser,$gamenumber); elseif ( $lovechoice != '' ) NewRoundAfterLove($lovechoice,$reporteduser,$gamenumber);
/******************************/ /* NewRoundAfterLove */ /* accept input for new round */ /******************************/ function NewRoundAfterLove($gamechoice,$reporteduser,$gamenumber) { $playernumber = GetAccountNumberFromEmail($reporteduser); UpdatePlayerLove($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */ if ( $newround == 11 ) DisplayEndGame($playernumber,$gamenumber); /* located in gamefunctions.php */ else DisplayVillainsRound($playernumber,$gamenumber); /* located in gamefunctions.php */ } /* NewRoundAfterLove */
Create the game object and use it to store the game number.
Include:
require_once('./php/gameobject.php');
Creation code:
$currentgame = new GameObject; $currentgame->SetGameNumber($gamenumber);
Replace all reads of the $gamenumber variable with an object method.
$currentgame->GetGameNumber($gamenumber)
Have the server provide information on a celebrity.
$celebrityinfo = $_GET['celebrityinfo']; /* have a celebrity information request */ elseif ( $celebrityinfo != '' ) { require_once('./php/celebrityinfo.php'); ProcessCelebrity($celebrityinfo); }
Realized that we werent recording the slams or loves in the game database (just which celebrity the action was on, not what the action was). So some quick updates to record that information.
S = SLAM
L = LOVE
In the function NewRoundAfterSlam
.
$newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'S'); /* located in gamefunctions.php */
In the function NewRoundAfterLove
.
$newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'L'); /* located in gamefunctions.php */
Add new game segment for slots.
elseif ( $slotchoice != '' ) ProcessSlots($slotchoice,$reporteduser,$gamenumber);
Add new game segment for slots.
Move new round stuff in NewRoundAfterSlam and NewRoundAfterLove.
/******************************/ /* NewRoundAfterSlam */ /* accept input for new round */ /* NOTE: used to be end of round - now slots follow */ /******************************/ function NewRoundAfterSlam($gamechoice,$playernumber,$gamenumber) { //echo '<p align="center">New round with input '.$gamechoice.' for player '.$reporteduser.' and game number '.$gamenumber.'</p>'; /* DEBUG */ UpdatePlayerSlam($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'S'); /* located in gamefunctions.php */ DisplaySlotStart($playernumber,$gamenumber); /* located in gameserver.php */ AddRoundTokens($playernumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'S'); /* located in gamefunctions.php */ if ( $newround == 11 ) DisplayEndGame($playernumber,$gamenumber); /* located in gamefunctions.php */ else DisplayCelebrityRound($playernumber,$gamenumber); /* located in gamefunctions.php */ } /* NewRoundAfterSlam */ /******************************/ /* NewRoundAfterLove */ /* accept input for new round */ /* NOTE: used to be end of round - now slots follow */ /******************************/ function NewRoundAfterLove($gamechoice,$playernumber,$gamenumber) { //echo '<p align="center">New round with input '.$gamechoice.' for player '.$reporteduser.' and game number '.$gamenumber.'</p>'; /* DEBUG */ UpdatePlayerLove($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'L'); /* located in gamefunctions.php */ DisplaySlotStart($playernumber,$gamenumber); /* located in gameserver.php */ AddRoundTokens($playernumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'L'); /* located in gamefunctions.php */ if ( $newround == 11 ) DisplayEndGame($playernumber,$gamenumber); /* located in gamefunctions.php */ else DisplayCelebrityRound($playernumber,$gamenumber); /* located in gamefunctions.php */ } /* NewRoundAfterLove */
Modify dispatcher:
/* INITIALIZATIONS */ if(empty($_GET)) { /*go away if no valid requests */ return; } else { /*collect input from user */ $startcode = $_GET['start']; /* have a request to start a specific game */ $slamchoice = $_GET['slam']; /* have a celebrity slam input for a specific game */ $lovechoice = $_GET['love']; /* have a celebrity love input for a specific game */ $triviachoice = $_GET['trivia']; /* have a trivia input for a specific game */ $gamenumber = $_GET['game']; /* have a game number */ $celebrityinfo = $_GET['celebrityinfo']; /* have a celebrity information request */ $slotchoice = $_GET['slotchoice']; /* finish up slot round */ $advanceround = $_GET['advanceround']; /* advance to new round */ } /* END if on GET */ /* dispatch based on what game activity requested */ if ( $startcode != '' ) StartGame($startcode,$playernumber); elseif ( $slamchoice != '' ) NewRoundAfterSlam($slamchoice,$reporteduser,$gamenumber); elseif ( $lovechoice != '' ) NewRoundAfterLove($lovechoice,$reporteduser,$gamenumber); elseif ( $triviachoice != '' ) ProcessTrivia($triviachoice,$reporteduser,$gamenumber); elseif ( $slotchoice != '' ) ProcessSlots($reporteduser,$slotchoice); /* slot choice is game number */ elseif ( $advanceround != '' ) AdvanceNewRound($reporteduser,$advanceround); /* advance round is game number */ elseif ( $celebrityinfo != '' ) { require_once('./php/celebrityinfo.php'); ProcessCelebrity($celebrityinfo,$reporteduser,$gamenumber); }
Add tokens with any slam or love.
/******************************/ /* NewRoundAfterSlam */ /* accept input for new round */ /* NOTE: used to be end of round - now slots follow */ /******************************/ function NewRoundAfterSlam($gamechoice,$playernumber,$gamenumber) { //echo '<p align="center">New round with input '.$gamechoice.' for player '.$reporteduser.' and game number '.$gamenumber.'</p>'; /* DEBUG */ UpdatePlayerSlam($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'S'); /* located in gamefunctions.php */ DisplaySlotStart($playernumber,$gamenumber); /* located in gameserver.php */ // AddRoundTokens($playernumber); /* located in gamefunctions.php */ } /* NewRoundAfterSlam */ /******************************/ /* NewRoundAfterLove */ /* accept input for new round */ /* NOTE: used to be end of round - now slots follow */ /******************************/ function NewRoundAfterLove($gamechoice,$playernumber,$gamenumber) { //echo '<p align="center">New round with input '.$gamechoice.' for player '.$reporteduser.' and game number '.$gamenumber.'</p>'; /* DEBUG */ UpdatePlayerLove($gamechoice,$playernumber,$gamenumber); /* located in gamefunctions.php */ $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'L'); /* located in gamefunctions.php */ DisplaySlotStart($playernumber,$gamenumber); /* located in gameserver.php */ // AddRoundTokens($playernumber); /* located in gamefunctions.php */ // $newround = UpdateRoundResults($gamechoice,$playernumber,$gamenumber,'L'); /* located in gamefunctions.php */ // if ( $newround == 11 ) // DisplayEndGame($playernumber,$gamenumber); /* located in gamefunctions.php */ // else // DisplayCelebrityRound($playernumber,$gamenumber); /* located in gamefunctions.php */ } /* NewRoundAfterLove */
Move the calculation of the account number to before the dispatcher.
$playernumber = GetAccountNumberFromEmail($reporteduser); /* dispatch based on what game activity requested */ if ( $startcode != '' ) StartGame($startcode,$playernumber); elseif ( $slamchoice != '' ) NewRoundAfterSlam($slamchoice,$playernumber,$gamenumber); elseif ( $lovechoice != '' ) NewRoundAfterLove($lovechoice,$playernumber,$gamenumber); elseif ( $triviachoice != '' ) ProcessTrivia($triviachoice,$playernumber,$gamenumber); elseif ( $slotchoice != '' ) ProcessSlots($playernumber,$slotchoice); /* slot choice is game number */ elseif ( $advanceround != '' ) AdvanceNewRound($playernumber,$advanceround); /* advance round is game number */ elseif ( $celebrityinfo != '' ) { require_once('./php/celebrityinfo.php'); ProcessCelebrity($celebrityinfo,$playernumber,$gamenumber); }
OSdata.com is used in more than 300 colleges and universities around the worldFind out how to get similar high web traffic and search engine placement. |
OSdata.com is used in more than 300 colleges and universities around the world |
Tweets by @osdata |
A web site on dozens of operating systems simply cant be maintained by one person. This is a cooperative effort. If you spot an error in fact, grammar, syntax, or spelling, or a broken link, or have additional information, commentary, or constructive criticism, please e-mail Milo. If you have any extra copies of docs, manuals, or other materials that can assist in accuracy and completeness, please send them to Milo, PO Box 1361, Tustin, CA, USA, 92781.
Click here for our privacy policy.
previous page | next page |
This web site handcrafted on Macintosh computers using Tom Benders Tex-Edit Plus and served using FreeBSD .
Names and logos of various OSs are trademarks of their respective owners.
Copyright © 2013 Milo
Last Updated: December 20, 2013
Created: September 30, 2013
previous page | next page |