PHP 游戏无法识别位置

标签 php input

我正在开发一个基于 php 文本的游戏,但出了点问题。我无法真正描述它,但如果我在位置 A 并输入命令“向北”前往位置 B,它会打印出位置 B 的详细信息以及位置 B 的相应命令,但表现得就像我在位置 A 中一样因此,如果我在位置 A 中输入“north”,然后转到位置 B,然后在命令框中输入“west”,它将转到位置 A 的西边,而不是位置 B。为了更好地理解这一点,请查看 my (not-working) game 。任何帮助将不胜感激。

这是游戏的代码。

<?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>";
?>

最佳答案

您应该阅读有关 PHP session 处理的内容:http://php.net/manual/en/intro.session.php

您的游戏参数(例如$CurrentPos)会针对每个新请求而重置。您需要在请求之间保留游戏的状态。 session 就是为了做到这一点。

尝试这样的事情:

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

关于PHP 游戏无法识别位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32228370/

相关文章:

PHP file_get_contents 错误 503

php - 上传不保存大文件

php - 正在准备语句,但未执行

C++ 将串行输入读取到字符串

c - K&R 第 5.11 节中的排序程序

php - 如果 columrow 包含名称,则样式表行

PHP 正则表达式 (PCRE) - 查找一组所有子字符串

c - C 控制台底部的输入栏

使用两个输入字段的 PHP 搜索引擎

c++ - C++ 中的递归函数、 boolean 语句和输入