我试图制作更高/更低的数字游戏。所以基本上你得到一个数字和一个开始,然后你点击一个按钮 - 更高或更低。因此,如果你点击更高,下一个数字更高,你赢了,否则你输了。同样可以降低。当我点击一些按钮给出一个新数字时出现问题。那我怎么能阻止这个呢?这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title> Gamble </title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include('config.php');
if(!$_SESSION['username']) {
header("Location: index.php");
}
Menu();
isOnline();
$number = rand(0, 100);
echo $number;
if(isset($_POST['higher']) || isset($_POST['lower'])) {
$num = $number;
$newNumber = rand(0, 100);
if($_POST['higher']) {
if($newNumber > $num) {
echo 'You win!<br>'.$newNumber;
}
}
}
?>
<form method="POST">
<input type="Submit" name="higher" value="Higher">
<input type="Submit" name="lower" value="Lower">
</form>
</body>
</html>
*注意:我知道下方的按钮不起作用,我只是想让它更高,因为它会以相同的方式工作更低的..
编辑: 工作版:
<!DOCTYPE html>
<html>
<head>
<title> Gamble </title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include('config.php');
if(!$_SESSION['username']) {
header("Location: index.php");
}
Menu();
isOnline();
$num1 = rand(1, 100);
if(isset($_POST)) {
if($_POST['higher']) {
$num2 = rand(1, 100);
$oldNum = intval($_POST['num1']);
if($num2 > $oldNum) {
echo "You win! Your number:".$oldNum." , and the new number is: ".$num2;
}
}
}
?>
<form method="POST">
<input type="Hidden" value="<?= $num1 ?>" name="num1"><?= $num1 ?><br>
<input type="Submit" name="higher" value="Higher">
<input type="Submit" name="lower" value="Lower">
</form>
</body>
</html>