shergold Posted August 30, 2009 Share Posted August 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172469-php-game-help/ Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 if ($y <= 100) $moved = 1; else { should be: if ($y <= 100) { $moved = 1; } else same for south, west and east. Quote Link to comment https://forums.phpfreaks.com/topic/172469-php-game-help/#findComment-909266 Share on other sites More sharing options...
shergold Posted August 30, 2009 Author Share Posted August 30, 2009 I've added the brackets, although it makes no difference. Quote Link to comment https://forums.phpfreaks.com/topic/172469-php-game-help/#findComment-909290 Share on other sites More sharing options...
shergold Posted August 30, 2009 Author Share Posted August 30, 2009 also if i change the x and y values in the database the sprite still stays in the same location when i refresh the page which is in the first table square. Thanks, shergold. Quote Link to comment https://forums.phpfreaks.com/topic/172469-php-game-help/#findComment-909292 Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 also if i change the x and y values in the database the sprite still stays in the same location when i refresh the page which is in the first table square. Thanks, shergold. Post your code. Edit: nevermind looking at it. Quote Link to comment https://forums.phpfreaks.com/topic/172469-php-game-help/#findComment-909298 Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/172469-php-game-help/#findComment-909303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.