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.
The owners wanted a slot machine effect. The visual presentation is based on the little CSS3 experiment by Jonathan Shook, located at http://snook.ca/technical/slots/. Please compare the original to see how easy it is to avoid reinventing the wheel. There are many great examples of starter code that you can use to expand into real projects. Look for things that are published as examples or tutorials and follow the license restrictions imposed by the original author. And always give appropriate credit, as I have done in this case.
The slot machine effect involves a picture that is a vertical strip of eight pictures. We use this file to generate a random picture on the fly. If you do not send any GET parameters, then the picture is a random one using the celebrity database. But you can also specify each of the pictures and their order, which is great for coordinating the slot machine with otheer game elements.
<?php /* FILE: /makepicture.php */ // include function files for this application require_once('./php/databasefunctions.php'); require_once('./php/gamefunctions.php'); require_once('./php/gameobject.php'); /* INITIALIZATIONS */ if(empty($_GET)) { /*go away if no valid requests */ $part1 = rand(1,100); $part2 = rand(1,100); while ($part2 == $part1) $part2 = rand(1,100); $part3 = rand(1,100); while ( ($part3 == $part1) OR ($part3 == $part2) ) $part3 = rand(1,100); $part4 = rand(1,100); while ( ($part4 == $part1) OR ($part4 == $part2) OR ($part4 == $part3) ) $part4 = rand(1,100); $part5 = rand(1,100); while ( ($part5 == $part1) OR ($part5 == $part2) OR ($part5 == $part3) OR ($part5 == $part4) ) $part5 = rand(1,100); $part6 = rand(1,100); while ( ($part6 == $part1) OR ($part6 == $part2) OR ($part6 == $part3) OR ($part6 == $part4) OR ($part6 == $part5) ) $part6 = rand(1,100); $part7 = rand(1,100); while ( ($part7 == $part1) OR ($part7 == $part2) OR ($part7 == $part3) OR ($part7 == $part4) OR ($part7 == $part5) OR ($part7 == $part6) ) $part7 = rand(1,100); $part8 = rand(1,100); while ( ($part8 == $part1) OR ($part8 == $part2) OR ($part8 == $part3) OR ($part8 == $part4) OR ($part8 == $part5) OR ($part8 == $part6) OR ($part8 == $part7) ) $part8 = rand(1,100); } else { /*collect input from user */ $part1 = $_GET['part1']; /* have a request to start a specific game */ $part2 = $_GET['part2']; /* have a request to start a specific game */ $part3 = $_GET['part3']; /* have a request to start a specific game */ $part4 = $_GET['part4']; /* have a request to start a specific game */ $part5 = $_GET['part5']; /* have a request to start a specific game */ $part6 = $_GET['part6']; /* have a request to start a specific game */ $part7 = $_GET['part7']; /* have a request to start a specific game */ $part8 = $_GET['part8']; /* have a request to start a specific game */ } /* END if on GET */ if ($part1 != '') $pict1url = GetCelebritySmallPictString($part1); else $pict1url = './cpict/sh/charliesheen90.png'; if ($part2 != '') $pict2url = GetCelebritySmallPictString($part2); else $pict2url = './cpict/lo/lindsaylohan90.png'; if ($part3 != '') $pict3url = GetCelebritySmallPictString($part3); else $pict3url = './cpict/bi/justinbieber90.png'; if ($part4 != '') $pict4url = GetCelebritySmallPictString($part4); else $pict4url = './cpict/cy/mileycyrus90.png'; if ($part5 != '') $pict5url = GetCelebritySmallPictString($part5); else $pict5url = './cpict/ga/ladygaga90.png'; if ($part6 != '') $pict6url = GetCelebritySmallPictString($part6); else $pict6url = './cpict/li/snooplion90.png'; if ($part7 != '') $pict7url = GetCelebritySmallPictString($part7); else $pict7url = './cpict/mi/nickiminaj90.png'; if ($part8 != '') $pict8url = GetCelebritySmallPictString($part8); else $pict8url = './cpict/we/kanyewest90.png'; $image = imageCreateTrueColor(100, 640); $white = imageColorAllocate($image, 255, 255, 255); imageFill($image, 0, 0, $white); $person1 = imageCreateFromPNG($pict1url); imageCopyMerge($image, $person1, 10, 0, 0, 0, 80, 80, 100); $person2 = imageCreateFromPNG($pict2url); imageCopyMerge($image, $person2, 10, 81, 0, 0, 80, 80, 100); $person3 = imageCreateFromPNG($pict3url); imageCopyMerge($image, $person3, 10, 162, 0, 2, 80, 80, 100); $person4 = imageCreateFromPNG($pict4url); imageCopyMerge($image, $person4, 10, 243, 0, 0, 80, 80, 100); $person5 = imageCreateFromPNG($pict5url); imageCopyMerge($image, $person5, 10, 324, 0, 0, 80, 80, 100); $person6 = imageCreateFromPNG($pict6url); imageCopyMerge($image, $person6, 10, 405, 0, 0, 80, 80, 100); $person7 = imageCreateFromPNG($pict7url); imageCopyMerge($image, $person7, 10, 486, 0, 0, 80, 80, 100); $person8 = imageCreateFromPNG($pict8url); imageCopyMerge($image, $person8, 10, 567, 3, 0, 80, 80, 100); header('Content-Type: image/png'); imagePNG($image, '', 0, PNG_NO_FILTER); imageDestroy($image); imageDestroy($person1); imageDestroy($person2); imageDestroy($person3); imageDestroy($person4); imageDestroy($person5); imageDestroy($person6); imageDestroy($person7); imageDestroy($person8); /* TESTING echo 'picture 1 url is '.$pict1url.'<br>'; echo 'picture 2 url is '.$pict2url.'<br>'; echo 'picture 3 url is '.$pict3url.'<br>'; echo 'picture 4 url is '.$pict4url.'<br>'; echo 'picture 5 url is '.$pict5url.'<br>'; echo 'picture 6 url is '.$pict6url.'<br>'; echo 'picture 7 url is '.$pict7url.'<br>'; echo 'picture 8 url is '.$pict8url.'<br>'; TESTING */ ?>
Reworked code.
<?php /* FILE: /makepicture.php */ // include function files for this application require_once('./php/databasefunctions.php'); require_once('./php/gamefunctions.php'); require_once('./php/gameobject.php'); /* INITIALIZATIONS */ if(empty($_GET)) { /*go away if no valid requests */ $part1 = rand(1,100); $part2 = rand(1,100); while ($part2 == $part1) $part2 = rand(1,100); $part3 = rand(1,100); while ( ($part3 == $part1) OR ($part3 == $part2) ) $part3 = rand(1,100); $part4 = rand(1,100); while ( ($part4 == $part1) OR ($part4 == $part2) OR ($part4 == $part3) ) $part4 = rand(1,100); $part5 = rand(1,100); while ( ($part5 == $part1) OR ($part5 == $part2) OR ($part5 == $part3) OR ($part5 == $part4) ) $part5 = rand(1,100); $part6 = rand(1,100); while ( ($part6 == $part1) OR ($part6 == $part2) OR ($part6 == $part3) OR ($part6 == $part4) OR ($part6 == $part5) ) $part6 = rand(1,100); $part7 = rand(1,100); while ( ($part7 == $part1) OR ($part7 == $part2) OR ($part7 == $part3) OR ($part7 == $part4) OR ($part7 == $part5) OR ($part7 == $part6) ) $part7 = rand(1,100); $part8 = rand(1,100); while ( ($part8 == $part1) OR ($part8 == $part2) OR ($part8 == $part3) OR ($part8 == $part4) OR ($part8 == $part5) OR ($part8 == $part6) OR ($part8 == $part7) ) $part8 = rand(1,100); } else { /*collect input from user */ $part1 = $_GET['part1']; /* have a request to start a specific game */ $part2 = $_GET['part2']; /* have a request to start a specific game */ $part3 = $_GET['part3']; /* have a request to start a specific game */ $part4 = $_GET['part4']; /* have a request to start a specific game */ $part5 = $_GET['part5']; /* have a request to start a specific game */ $part6 = $_GET['part6']; /* have a request to start a specific game */ $part7 = $_GET['part7']; /* have a request to start a specific game */ $part8 = $_GET['part8']; /* have a request to start a specific game */ } /* END if on GET */ if ($part1 != '') $pict1url = GetCelebritySmallPictString($part1); else $pict1url = './cpict/sh/charliesheen90.png'; if ($part2 != '') $pict2url = GetCelebritySmallPictString($part2); else $pict2url = './cpict/lo/lindsaylohan90.png'; if ($part3 != '') $pict3url = GetCelebritySmallPictString($part3); else $pict3url = './cpict/bi/justinbieber90.png'; if ($part4 != '') $pict4url = GetCelebritySmallPictString($part4); else $pict4url = './cpict/cy/mileycyrus90.png'; if ($part5 != '') $pict5url = GetCelebritySmallPictString($part5); else $pict5url = './cpict/ga/ladygaga90.png'; if ($part6 != '') $pict6url = GetCelebritySmallPictString($part6); else $pict6url = './cpict/li/snooplion90.png'; if ($part7 != '') $pict7url = GetCelebritySmallPictString($part7); else $pict7url = './cpict/mi/nickiminaj90.png'; if ($part8 != '') $pict8url = GetCelebritySmallPictString($part8); else $pict8url = './cpict/we/kanyewest90.png'; $image = imageCreateTrueColor(100, 640); $white = imageColorAllocate($image, 255, 255, 255); imageFill($image, 0, 0, $white); $person1 = imageCreateFromPNG($pict1url); imageCopyMerge($image, $person1, 10, 0, 0, 0, 80, 80, 100); $person2 = imageCreateFromPNG($pict2url); imageCopyMerge($image, $person2, 10, 81, 0, 0, 80, 80, 100); $person3 = imageCreateFromPNG($pict3url); imageCopyMerge($image, $person3, 10, 162, 0, 2, 80, 80, 100); $person4 = imageCreateFromPNG($pict4url); imageCopyMerge($image, $person4, 10, 243, 0, 0, 80, 80, 100); $person5 = imageCreateFromPNG($pict5url); imageCopyMerge($image, $person5, 10, 324, 0, 0, 80, 80, 100); $person6 = imageCreateFromPNG($pict6url); imageCopyMerge($image, $person6, 10, 405, 0, 0, 80, 80, 100); $person7 = imageCreateFromPNG($pict7url); imageCopyMerge($image, $person7, 10, 486, 0, 0, 80, 80, 100); $person8 = imageCreateFromPNG($pict8url); imageCopyMerge($image, $person8, 10, 567, 3, 0, 80, 80, 100); header('Content-Type: image/png'); imagePNG($image, '', 0, PNG_NO_FILTER); imageDestroy($image); imageDestroy($person1); imageDestroy($person2); imageDestroy($person3); imageDestroy($person4); imageDestroy($person5); imageDestroy($person6); imageDestroy($person7); imageDestroy($person8); /* TESTING echo 'picture 1 url is '.$pict1url.'<br>'; echo 'picture 2 url is '.$pict2url.'<br>'; echo 'picture 3 url is '.$pict3url.'<br>'; echo 'picture 4 url is '.$pict4url.'<br>'; echo 'picture 5 url is '.$pict5url.'<br>'; echo 'picture 6 url is '.$pict6url.'<br>'; echo 'picture 7 url is '.$pict7url.'<br>'; echo 'picture 8 url is '.$pict8url.'<br>'; TESTING */ ?>
Shrink the size of the slots to fit in portrait format on smart phones.
$image = imageCreateTrueColor(90, 640); $white = imageColorAllocate($image, 255, 255, 255); imageFill($image, 0, 0, $white); $person1 = imageCreateFromPNG($pict1url); imageCopyMerge($image, $person1, 5, 0, 0, 0, 80, 80, 100); $person2 = imageCreateFromPNG($pict2url); imageCopyMerge($image, $person2, 5, 81, 0, 0, 80, 80, 100); $person3 = imageCreateFromPNG($pict3url); imageCopyMerge($image, $person3, 5, 162, 0, 2, 80, 80, 100); $person4 = imageCreateFromPNG($pict4url); imageCopyMerge($image, $person4, 5, 243, 0, 0, 80, 80, 100); $person5 = imageCreateFromPNG($pict5url); imageCopyMerge($image, $person5, 5, 324, 0, 0, 80, 80, 100); $person6 = imageCreateFromPNG($pict6url); imageCopyMerge($image, $person6, 5, 405, 0, 0, 80, 80, 100); $person7 = imageCreateFromPNG($pict7url); imageCopyMerge($image, $person7, 5, 486, 0, 0, 80, 80, 100); $person8 = imageCreateFromPNG($pict8url); imageCopyMerge($image, $person8, 5, 567, 3, 0, 80, 80, 100);
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 27, 2013
Created: November 18, 2013
previous page | next page |