0

我正在尝试设计一个 PHP 浏览器游戏,只是为了好玩和练习。我目前正在编写战斗脚本,我遇到了一个无限循环。我无法弄清楚它发生的原因。无论如何,这是代码:

<?php
session_start();

// Script PHP pentru simularea luptelor single player

/*
    1 = Magic
    2 = Attack
    3 = Defence
    5 = Maximum HP
    6 = Current HP
    4 = Gold coins
    17 = Experience
*/

require_once("config.php");

$username = $_SESSION['username'];
$query = ("SELECT id FROM users WHERE username = '$username'");
$user_id = mysql_query($query) or die (mysql_error());

// Momentan monstrii sunt alesi in mod aleatoriu
$query = ("SELECT id FROM monsters ORDER BY RAND() LIMIT 1");
$monster_id = mysql_query($query) or die (mysql_error());

$query = ("SELECT value FROM user_stats WHERE stat_id = 6 AND user_id = '$user_id'");
$player_hp = mysql_query($query) or die (mysql_error());

$query = ("SELECT value FROM monster_stats WHERE stat_id = 5 AND monster_id = '$monster_id'");
$monster_hp = mysql_query($query) or die (mysql_error());

if ($player_hp <= 0)
{
    $query = ("SELECT value FROM user_stats WHERE stat_id = 4 AND user_id = '$user_id'");
    $player_gc = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM monster_stats WHERE stat_id = 4 AND user_id = '$monster_id'");
    $monster_gc = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM user_stats WHERE stat_id = 17 AND user_id = '$monster_id'");
    $player_exp = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM monster_stats WHERE stat_id = 17 AND monster_id = '$monster_id'");
    $monster_exp = mysql_query($query) or die (mysql_error());

    $player_gc = $player_gc + $monster_gc;
    $player_exp = $player_exp + $monster_exp;

    // item drop trebuie facut

    echo "Congratulations! You won the battle and you gained " . $monster_gc . " gold coins and " . $monster_exp . " experience points.";
}
elseif ($monster_hp <= 0)
{
    echo "You lost the battle.";
}
else
{
    $query = ("SELECT value FROM user_stats WHERE stat_id = 1 AND user_id = '$user_id'");
    $player_magic = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM monster_stats WHERE stat_id = 1 AND monster_id = '$monster_id'");
    $monster_magic = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM user_stats WHERE stat_id = 2 AND user_id = '$user_id'");
    $player_attack = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM monster_stats WHERE stat_id = 2 AND monster_id = '$monster_id'");
    $monster_attack = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM user_stats WHERE stat_id = 3 AND user_id = '$user_id'");
    $player_defence = mysql_query($query) or die (mysql_error());

    $query = ("SELECT value FROM monster_stats WHERE stat_id = 3 AND monster_id = '$monster_id'");
    $monster_defence = mysql_query($query) or die (mysql_error());

    $turn = rand(0,1);
    while (($player_hp > 0) && ($monster_hp > 0) &&($turn < 6))
    {
        $turn++;

        // Player turn
        if ($turn % 2 == 0)
        {
            echo $monster_hp . "<br>";
            $monster_hp -= ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);
            var_dump ($monster_hp);
        }
        // Monster turn
        else
        {
            echo $player_hp . "<br>";
            $player_hp -= ($monster_attack + $monster_magic / 2 - $player_defence - $player_magic / 2);
            var_dump ($player_hp);
        }
    }
}

?>

时间在其他地方。我知道 MySQL 已经贬值了。谢谢!

4

2 回答 2

4

你是不是在这之后(运营商-=不是-):

// Player turn
if ($turn % 2 == 0)
{
    $monster_hp -= ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);
}
// Monster turn
else
{
    $player_hp -= ($monster_attack + $monster_magic / 2 - $player_defence - $player_magic / 2);
}

-edit-如果您的变量是Resource id #5etc,这意味着您没有正确地从PHP重试 MySQL 数据。尝试这个:

$query = ("SELECT value FROM user_stats WHERE stat_id = 3 AND user_id = '$user_id'");
$player_defence = mysql_query($query) or die (mysql_error());
$player_defence = mysql_fetch_assoc($player_defence);
$player_defence = $player_defence['value'];

您需要为获得的每个查询结果执行此操作(或类似操作)。它将您的查询结果转换为关联数组,然后转换为您需要的变量。

手册: http: //php.net/manual/en/function.mysql-fetch-assoc.php

于 2013-10-14T21:07:24.553 回答
2

在你的 while 循环中,你永远不会真正改变怪物和玩家的生命值:

$monster_hp - ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);

你是不是想改用这样的东西:

$monster_hp -= ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);

-=运算符确实是一种快速的编程方式equals itself minus...

// The following lines are identical in logic:
$var-=1
$var=$var-1;
于 2013-10-14T21:08:11.143 回答