Jump to content

Recommended Posts

Hey guys, i was wondering if anyone could help me with the following php script that i have created, it is meant to be a game that allows a player sprite to move about a html table. When i click up it allows me to move off the board, it also wont allow me to move down at all and only one space left or right. the If statements that create the boundrys and meant to stop the sprite going off the board although its stopping moving about the board aswell.

 

<?php

$userName = "administrator";

function newGame(){

}

function move(){
global $x, $y, $userName;

//Move up/North
if (isset($_POST['up']))
{
	$y = $y + 10;

	if ($y <= 100)
		$moved = 1;
	else
		{
			$y = $y - 10;
			echo "You cant move any further forward as you have reached the boundry";
		}
}


//Move down/South
elseif (isset($_POST['down']))
{
	$y = $y - 10;

	if ($y > 10)
		$moved = 1;
	else	
		{
			$y = $y + 10;
			echo "You cant move any further south as you have reached the boundry";
		}
}

//Move left/West
elseif (isset($_POST['left']))
{
	$x = $x - 1;

	if ($x >= 0)
		$moved = 1;	
	else	
		{
			$x = $x + 1;
			echo "You cant move any further west as you have reached the boundry";
		}
}

//Move right/East
elseif (isset($_POST['right']))
{
	$x = $x + 1;

	if ($x <= 100)
		$moved = 1;
	else
		{
			$x = $x - 1;
			echo "You cant move any further east as you have reached the boundry";
		}
}

//update user location
if ($moved == 1)
{
	require "connect.php";

	$connection = mysql_connect($hostname, $username, $password) or die ("Cannot connect to the database server.") or die("cannot connect to the game server.");
	mysql_select_db($database, $connection);
	mysql_query("UPDATE location SET x = '$x' AND y = '$y' WHERE username = '$userName'");

}

return $x;
return $y;

}

function location(){
global $x, $y, $userName;
// retrieve user location from database
require "connect.php";

$connection = mysql_connect($hostname, $username, $password) or die ("Cannot connect to the database server.");
mysql_select_db($database, $connection);
$query = mysql_query("SELECT * FROM location WHERE username = '$userName'");
$result = mysql_num_rows($query);
$location = mysql_fetch_assoc($query);

if (!$result == 0)
	{
		$x = $location[0];
		$y = $location[1];
	}
	else
	echo "Cannot find location of user: $userName";

	echo $x;
	echo $y;

}

function tile(){
$tile = "grass";
return $tile;
}

function render($rows = 10 , $cols = 10 , $tileWidth = 50 , $tileHeight = 50 ){

//functions
location();
move();
//global variables
global $userName, $lvl, $x, $y;

$gameWidth = $cols * $tileWidth;
$gameHeight = $rows * $tileHeight;
$game = "<table frame=\"border\" width=\"$gameWidth\" height=\"$gameHeight\" cellpadding=\"0\" cellspacing=\"0\">\n";
// while there is more tiles to display keep looping
for ($i = 0; $i < $rows; ++$i) {
        $game .= "<tr>\n";
        for ($j = 0; $j < $cols; ++$j) {
		//if user location is equal to tile location display user
		if ($i == $y && $j == $x)
            	$game .= "<td width=\"$tileWidth\" height=\"$tileHeight\" background=\"" . tile() . ".gif\">
			<img src=\"mod.gif\" alt=\"$userName\" title=\"$userName\nlevel: $lvl\">\n</td>\n";
		//else display no user
		else 
			$game .= "<td width=\"$tileWidth\" height=\"$tileHeight\" background=\"" . tile() . ".gif\">\n</td>\n";

        }
        $game .= "</tr>\n";
    }
    print $game . "</table>\n";
}

function error()
{
$message = "";
echo $message;
}
?>



 

Thanks allot,

shergold.

Link to comment
https://forums.phpfreaks.com/topic/172469-php-game-help/
Share on other sites

Well a table won't suffice as you'll need layers. Another great advantage is the ability to put objects into your game like trees, npc's, bushes, etc.. So you only need to provide a surface and fill it with those objects. You'll ofcourse also need JavaScript for collision-detection. You can't walk over a tree, can you?

 

<style type="text/css">
.tree { display: block; width: 20px; height: 20px; background: url(gfx-tree.jpg) }
.shrub { display: block width: 5px; height: 5px; background: url(gfx-shrub.jpg) }

.t1 { position: absolute; top: 15px; left: 30px }
.s1 { position: absolute; bottom: 30px; right: 5px }

.character { z-index: 99 }
.neo { background: url(gfx-char-neo.jpg) }

#ground { display: block; position: relative }
#player { display: block; position: absolute; top: $y; left: $x } /* $x and $y are start values */
</style>

<div id="ground">
    <div class="tree t1"></div>
    <div class="shrub s1"></div>
    <div id="player" class="character neo"></div>
</div>

Link to comment
https://forums.phpfreaks.com/topic/172469-php-game-help/#findComment-909303
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.