Here is a tutorial with a remake of the game my friend made. Note, this is not his source code. I made it right on the spot. Also, my mic was off the first time, so this is a voice over.
This three-part tutorial goes over using arrays, loops and sessions to create a simple grid based game. There is no true object to the game other than to move around and hit your head on the boundaries (the edges of the grid).
The Game Part 2
The Game Part 3
Here are the sources used in this tutorial:
tut025.php
<?php session_start(); $grid = array(); $width = 20; $height = 10; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $grid[$x][] = "_"; } } if (!isset($_SESSION['x'])) { $_SESSION['x'] = (int)($width / 2); } if (!isset($_SESSION['y'])) { $_SESSION['y'] = (int)($height / 2); } //Determine new location based on relative location and given direction if (isset($_REQUEST['d'])) { //they gave us a direction so now we will process it. if ($_REQUEST['d'] == "l" && $_SESSION['x'] > 0) { $_SESSION['x'] -= 1; } if ($_REQUEST['d'] == "r" && $_SESSION['x'] < $width - 1) { $_SESSION['x'] += 1; } if ($_REQUEST['d'] == "d" && $_SESSION['y'] < $height - 1) { $_SESSION['y'] += 1; } if ($_REQUEST['d'] == "u" && $_SESSION['y'] > 0) { $_SESSION['y'] -= 1; } //the opposite $error = "you bonked your head, therefore you are dead.(not really.)"; if ($_REQUEST['d'] == "l" && $_SESSION['x'] <= 0) { echo $error; } if ($_REQUEST['d'] == "r" && $_SESSION['x'] >= $width - 1) { echo $error; } if ($_REQUEST['d'] == "d" && $_SESSION['y'] >= $height - 1) { echo $error; } if ($_REQUEST['d'] == "u" && $_SESSION['y'] <= 0) { echo $error; } } ?><html><head><title><?php echo "You are at: (".$_SESSION['x'].",".$_SESSION['y'].")"; ?></title></head><body><?php //PHP Tutorial 025 -- The Game! $grid[$_SESSION['x']][$_SESSION['y']] = "X"; echo '<table border="1">' . "\n"; for ($y = 0; $y < $height; $y++) { echo '<tr>'; for ($x = 0; $x < $width; $x++) { echo '<td>' . $grid[$x][$y] . '</td>'; } echo '</tr>' . "\n"; } echo '</table>'; ?><a href="?d=l">Left</a> <a href="?d=r">Right</a> <a href="?d=u">Up</a> <a href="?d=d">Down</a></body></html>
November 13th, 2009 at 7:46 am
[...] that Grid game back in tutorial 025? In this tutorial I convert it to a Command Line Interface game instead of a web server game with a [...]