我正在学习php atm,我决定制作一个简单的游戏,但现在我遇到了一个问题。我有以下代码:
<form action="wolf.php" method="POST">
<input type="submit" value="Attack" name="submit"/> <br />
<?php
$hp = 100;
if(isset($_POST['submit'])) {
$attack = $_POST['submit'];
$damage = mt_rand(5, 30);
$newhp = $hp - $damage;
if ($attkdamage = $hp - $damage ) {
echo "Your HP is: ". $newhp . "<br / >";
echo "You took: " . $damage . " damage!";
}
}
?>
正如你所看到的,我有一个带整数(100)和简单mt rand的变量。我想要的是,在我提交并获得$ newhp(100 - 随机数)后,该数字将替换$ hp。当我下次提交按钮时,我希望从前一个动作中减去$ damage的值,所以基本上将$ newhp保存为$ hp。
答案 0 :(得分:0)
如果可能,将此值存储在将保存在您使用的浏览器中的会话变量中,有关详细信息,请参阅此处$_SESSION
<?php
session_start();
if(!isset($_SESSION["hp"]))
$_SESSION["hp"]=100;
if(isset($_POST['submit'])) {
$attack = $_POST['submit'];
$damage = mt_rand(5, 30);
$newhp = $_SESSION["hp"] - $damage;
if ($attkdamage = $_SESSION["hp"] - $damage ) {
echo "Your HP is: ". $newhp . "<br / >";
echo "You took: " . $damage . " damage!";
}
echo "<pre>previus hp " .$_SESSION["hp"];
$_SESSION["hp"]= $newhp;
echo "next hp " . $_SESSION["hp"]."</pre>";
}
?>