我正在尝试设计一个 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 已经贬值了。谢谢!