Modified code:
<?php
session_start();
$title = "The Grid"; include("header.php"); ?>
<?php
if (empty($action)) {
print "You walk into a large silver-plated room. Suddenly, a large thud like a giant switch was being thrown booms in your ears. Welcome to the Grid.<br>";
print "Explore the Grid? <a href=grid.php?action=explore>Yup</a>.";
} elseif ($action == "attack") {
$monster_health = rand(1,$monster_max_health);
$monster_armour = rand(1,3);
$ptgain = rand(1,5);
print "Before you attack the monster, it has $monster_health energy, and $monster_armour armour.\n<BR>\n";
$energy = ($_SESSION['energy'] / $monster_armour)
if ($energy <= $monster_health) {
print ("The monster overpowers you. It kills you and steals your money and platinum.");
} else {
print ("You killed the monster and found that it had $ptgain platinum.");
$_SESSION['platinum'] = ($_SESSION['PLATINUM'] + $ptgain)
}
} else {
if ($_SESSION['energy'] <= .3) {
print "You don't have enough energy to keep exploring the Grid.";
} else {
$chance = rand(1,8);
$_SESSION['energy'] = $_SESSION['energy'] - 0.3;
if ($chance == 1) {
print "You take a few steps, and whirl around, but nothing is behind you...";
}
elseif ($chance == 2) {
print "Nothing here.";
}
elseif ($chance == 3) {
$crgain = rand(1,100);
print "A bag of credits! <b>$crgain</b> credits were inside.";
$_SESSION['credits'] = $_SESSION['credits'] + $crgain;
}
elseif ($chance == 4) {
print "Nothin' interesting can be seen.";
}
elseif ($chance == 5) {
print "What was that?";
}
elseif ($chance == 6) {
$plgain = rand(1,3);
print "You stumbled into platinum ore! Found <b>$plgain</b> platinum.";
$_SESSION['platinum'] = $_SESSION['platinum'] + $plgain;
}
elseif ($chance == 7) {
$roll = rand(1,20);
if ($roll == 15) {
print "You reached a spring... and drink the water. Gained <b>.3</b> MAX energy!";
$_SESSION['max_energy'] = $_SESSION['max_energy'] + .3;
} else {
print "You found a spring. <b>1</b> energy restored!";
$_SESSION['energy'] = $_SESSION['energy'] + 1;
}
}
elseif ($chance == 8) {
print "Nothing worthwhile here.";
}
$_SESSION['energy'] = ($_SESSION['energy'] - .3);
print "<br><br>... <a href=grid.php?action=explore>explore</a> again. ($_SESSION['energy'] energy left.)";
elseif ($chance == 9) {
$roll = rand(1,40);
print "<form action='grid.php' method='post'>\n";
print "<input type='hidden' name='action' value='attack'>\n";
print "<input type='hidden' name='monster_max_health' value='$roll'>\n";
print "You meet a monster. You recognise it as a type that can have up to $roll health energy<BR><BR>\n";
print "<input type='button' onclick='location.href=grid.php?action=explore' value='Run Away!'>\n";
print "<input type='submit' value='Attack The Beast!'>\n";
print "</form>\n";
}
$_SESSION['energy'] = ($_SESSION['energy'] - .3);
print "<br><br>... <a href=grid.php?action=explore>explore</a> again. ($_SESSION['energy'] energy left.)";
print "<br><br>... <a href=save.php>save the game</a>\n<br><br>\n";
print "<br><br>... <a href=load.php>reload the game</a>\n<br><br>\n";
}
}
include("footer.php");
?>
For the load game PHP file, you need to do the following:
<?php
session_start();
// connect to SQL and get table row, then...
$_SESSION['platinum'] = $SQL->platinum
// repeat the line of code above for each variable that you want
// to load, you will need to get the ID of the table row as well.
// you might want to let people choose a username that cannot
// be duplicated, so that people can't overwrite other people's
// games with their saves (prevent accidents before they happen)
?>
And to save...
<?php
session_start();
mysql_query ("update `players` set `energy` = '$_SESSION['energy']', `platinum` = '$_SESSION['platinum'];")
// repeat the "set `field` = '$_SESSION['field']," piece of code
// above for each variable that you want
// to save, you will need to save the ID of the table row as well.
// you might want to let people choose a username that cannot
// be duplicated, so that people can't overwrite other people's
// games with their saves (prevent accidents before they happen)
?>
And for a new game, you need to do the following:
<?php
session_start();
// insert a new row into the table with the player's chosen name
// and password, if necessary as the only thing in the
// row. if you leave the default values of the row in the table to
// the new game settings, then you will automatically have a new
// game set up when you execute the next code.
mysql_query("insert into `players` (`username`,`password`) VALUES ('$username',PASSWORD('$password')")
// connect to SQL and get table row, then...
$_SESSION['platinum'] = $SQL->platinum
// repeat the piece of code
// above for each variable that you want
// to set, you will need to save the ID of the table row as well.
// you might want to let people choose a username that cannot
// be duplicated, so that people can't overwrite other people's
// games with their saves (prevent accidents before they happen)
?>
I really hope that this helps you.