2015-08-26 55 views
2

我有一个基于php文本的游戏,我正在处理,但有些不对劲。我无法真正描述它,但是如果我在位置A,并且我输入了一个命令“north”以前往位置B,它会打印出位置B的详细信息以及位置B的适当命令,但行为与我在位置A时相似所以如果我在地点A输入“北”并前往地点B并在命令框中输入“西”,则它将移到地点A的西部而不是地点B。为了更好地理解这一点,请看my (not-working) game。任何帮助将不胜感激。PHP游戏无法识别位置

这里是游戏的代码。

<?php 
include_once 'index.php'; 
print($input); 
$World = simplexml_load_file("gameworld.xml"); 
$CurrentPos = 0; 
$Done = 0; 
print "<br>"; 
printplace(); 
function printplace() { 
    GLOBAL $World, $CurrentPos; 
    $Room = $World->ROOM[$CurrentPos]; 
    $Name = $Room->NAME; 
    $Desc = wordwrap((string)$Room->DESC); 
    print "$Name<br>"; 
    print str_repeat('-', strlen($Name)); 
    print "<br>$Desc<br>"; 
    if ((string)$Room->NORTH != '-') { 
     $index = (int)$Room->NORTH; 
     print "North: {$World->ROOM[$index]->NAME}<br>"; 
    } 
    if ((string)$Room->SOUTH != '-') { 
     $index = (int)$Room->SOUTH; 
     print "South: {$World->ROOM[$index]->NAME}<br>"; 
    } 
    if ((string)$World->ROOM[$CurrentPos]->WEST != '-') { 
     $index = (int)$Room->WEST; 
     print "West: {$World->ROOM[$index]->NAME}<br>"; 
    } 
    if ((string)$World->ROOM[$CurrentPos]->EAST != '-') { 
     $index = (int)$Room->EAST; 
     print "East: {$World->ROOM[$index]->NAME}<br>"; 
    } 
    print "<br>"; 
} 

$input = explode(' ', $input); 
print "<br>"; 
foreach ($input as $command) { 
    switch ($command) { 
     case 'north': 
      if ((string)$World->ROOM[$CurrentPos]->NORTH != '-') { 
       $CurrentPos = (int)$World->ROOM[$CurrentPos]->NORTH; 
       printplace() ; 
      } else { 
       print "You cannot go north!<br>"; 
      } 
      break; 
     case 'south': 
      if ((string)$World->ROOM[$CurrentPos]->SOUTH != '-') { 
       $CurrentPos = (int)$World->ROOM[$CurrentPos]->SOUTH; 
       printplace() ; 
      } else { 
       print "You cannot go south!<br>"; 
      } 
      break; 
     case 'west': 
      if ((string)$World->ROOM[$CurrentPos]->WEST != '-') { 
       $CurrentPos = (int)$World->ROOM[$CurrentPos]->WEST; 
       printplace() ; 
      } else { 
       print "You cannot go west!<br>"; 
      } 
      break; 
     case 'east': 
      if ((string)$World->ROOM[$CurrentPos]->EAST != '-') { 
       $CurrentPos = (int)$World->ROOM[$CurrentPos]->EAST; 
       printplace() ; 
      } else { 
       print "You cannot go east!<br>"; 
      } 
      break; 
     case 'look': 
      printplace() ; 
      break; 
     default: 
      print "not a valid command... <br>"; 
      break; 
    } 
} 
print "<br>Thanks for playing!<br>"; 
?> 
+1

它不会保留调用的页面之间的$ CurrentPos。 – Pieter21

+1

^这。每当页面加载时,$ CurrentPos被设置为0。使用会话/ cookie /获取参数 – Christian

+0

这是一个多人或单人游戏? – hanshenrik

回答

2

你应该阅读有关PHP会话处理:http://php.net/manual/en/intro.session.php

你的游戏参数(如$CurrentPos)被重置为每个新的请求。您需要在请求之间保持游戏状态。会议是为了做到这一点。

尝试这样:

if (!isset($_SESSION['CurrentPos'])) { 
    $_SESSION['CurrentPos'] = 0; 
} 
$CurrentPos = $_SESSION['CurrentPos']; 
+0

我不知道我是否做错了,但其中任何一个会话都不适用于我,或者我将它应用于错误的变量。我所做的是,我用'session_start();'开始了会话,然后使用了'$ CurrentPos = $ _SESSION [“CurrentPos”] = 0;'将$ CurrentPos设置为会话变量。不过,我认为我没有做到这一点。 – dumpong

+0

有关如何设置当前位置的示例,请参阅我的更新答案。 – bspellmeyer