所以我决定尝试拿起PHP,在完成一些在线课程后,我有一定程度的基础知识。
我一直试图通过制作简单的游戏来测试自己。
在游戏中你会下注一定数量的积分和一个介于1和100之间的随机数,如果数字高于60,你得到的回报是你的两倍,如果低于60你就会失去你下注的金额。
我坚持创造一个跑步总数,并非常感谢任何建议。
下面的代码(对于编写错误的代码道歉:P)
<?php
$rand = rand(1,100);
$usrBet = $_POST['usrBet'];
$usrCredit = 1000;
if ($rand > 60) {
$usrCredit = ($usrCredit - $usrBet) + ($usrBet * 2);
echo "Your Bet : " ,$usrBet;
echo "<br>";
echo "The Banker Rolled : " ,$rand;
echo "<br>";
echo "You Win";
echo "<br>";
echo "Current Credits : " ,$usrCredit;
} else {
$usrCredit = $usrCredit - $usrBet;
echo "Your Bet : " ,$usrBet;
echo "<br>";
echo "Aww Snap! you loose!";
echo "<br>";
echo "The Banker Rolled : " ,$rand;
echo "<br>";
echo "Current Credits : " ,$usrCredit;
}
?>
<form action="dicegame.php" method="post">
<INPUT TYPE = "Text" VALUE ="Please Enter Your Bet" NAME = "usrBet" placeholder="Please Enter Your Bet">
<p><input type="submit" /></p>
</form>
答案 0 :(得分:2)
您必须使用会话来存储数据,以便分数不会重置
对于使用会话,您必须像这样开始会话
session_start();
之后你必须检查是否在会话中分配了值,如果没有意味着用户第一次尝试,那么我们将在会话开始时将这一点归功于此
if(!isset($_SESSION['userCredits'])){
$_SESSION['userCredits'] = 1000;
}
当更新用户信用时,我们必须在会话中更新它,以便像这样
$_SESSION['userCredits'] = $usrCredit;
如果用户首先出现在页面上我们不想先给出结果,那么请检查表单是否在这样的条件开始时发布。
if(isset($_POST['usrBet'])){
// PHP CODE
}
在所有这些之后,最终的代码将如下所示
<?php
session_start();
if(!isset($_SESSION['userCredits'])){
$_SESSION['userCredits'] = 1000;
}
if(isset($_POST['usrBet'])){
$rand = rand(1,100);
$usrBet = $_POST['usrBet'];
$usrCredit = $_SESSION['userCredits'];
if ($rand > 60) {
$usrCredit = ($usrCredit - $usrBet) + ($usrBet * 2);
echo "Your Bet : " ,$usrBet;
echo "<br>";
echo "The Banker Rolled : " ,$rand;
echo "<br>";
echo "You Win";
echo "<br>";
echo "Current Credits : " ,$usrCredit;
} else {
$usrCredit = $usrCredit - $usrBet;
echo "Your Bet : " ,$usrBet;
echo "<br>";
echo "Aww Snap! you loose!";
echo "<br>";
echo "The Banker Rolled : " ,$rand;
echo "<br>";
echo "Current Credits : " ,$usrCredit;
}
$_SESSION['userCredits'] = $usrCredit;
}
?>
<form action="dicegame.php" method="post">
<INPUT TYPE = "Text" NAME = "usrBet" placeholder="Please Enter Your Bet">
<p><input type="submit" /></p>
</form>
您可以为其添加更多条件。希望这会有所帮助
答案 1 :(得分:0)
我尝试添加一些功能,以便您可以通过这种方式进行搜索。如上所述$ _SESSION将解决pbm。
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<script>
<!-- Some few lines of JS - to disable characters input -->
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
</head>
<body>
<!-- U'll to re-edit the target of the form + changed type to number for input -->
<form action="7.php" method="post">
<input type="number" name="usrBet" value="Plz Enter Your Bet"
placeholder="Plz Enter Your Bet" onkeypress="return isNumberKey(event)">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
// Check if user entered a value
if (isset($_POST['usrBet']))
{
if($_POST['usrBet'] == '')
$usrBet = 0;
else
$usrBet = $_POST['usrBet'];
$rand = rand(1,100);
if(!isset($_SESSION['usrCredit']))
$_SESSION['usrCredit'] = 1000;
if($rand > 60)
{
$_SESSION['usrCredit'] = ($_SESSION['usrCredit'] - $usrBet) + ($usrBet * 2);
echo "Your Bet : " . $usrBet;
echo "<br>";
echo "The Banker Rolled : " . $rand;
echo "<br>";
echo "You Won";
echo "<br>";
echo "Current Credits : " . $_SESSION['usrCredit'];
}
else
{
$_SESSION['usrCredit'] = $_SESSION['usrCredit'] - $usrBet;
echo "Your Bet : " . $usrBet;
echo "<br>";
echo "Aww Snap! you lost!";
echo "<br>";
echo "The Banker Rolled : " . $rand;
echo "<br>";
echo "Current Credits : " . $_SESSION['usrCredit'];
}
}
?>