我正在尝试设计一款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);
}
}
}
&GT;
其他时间在其他方面。我知道MySQL是折旧的。谢谢!
答案 0 :(得分: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 #5
等,则表示您没有正确地从 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'];
您需要为您获得的每个查询结果执行此操作(或类似的操作)。它将您的查询结果转换为关联数组,然后转换为您需要的变量。
答案 1 :(得分:2)
在你的while循环中你永远不会改变怪物和玩家的HP:
$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;