PHP做猜数字游戏<?php
session_start();
if (empty($_COOKIE['num']) || empty($_GET['num'])) {
$num = rand(0, 100);
$_SESSION['num'] = $num;
} else {
$count = empty($_SESSION['count']) ? 0 : (int)$_SESSION['count'];
if ($count < 10) {
$result = (int)$_GET['num'] - (int)$_SESSION['num'];
if ($result == 0) {
$message = '恭喜猜对了';
unset($_SESSION['num']);
unset($_SESSION['count']);
} elseif ($result > 0) {
$message = '猜⼩了';
} else {
$message = '猜⼤了';
}
$_SESSION['count'] = $count + 1;
}else{
php游戏源码$message = '游戏结束';
unset($_SESSION['num']);
unset($_SESSION['count']);
}
}
>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>猜数字</title>
<style>
body {
padding: 100px 0;
background-color: #2b3b49;
color: #fff;
text-align: center;
font-size: 2.5em;
}
input {
padding: 5px 20px;
height: 50px;
background-color: #3b4b59;
border: 1px solid #c0c0c0;
box-sizing: border-box;
color: #fff;
font-size: 20px;
}
button {
padding: 5px 20px;
height: 50px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>猜数字游戏</h1>
<p>Hi,我已经准备了⼀个0~100的数字,你需要在仅有的10机会之内猜对它。</p>
<?php if (isset($message)): ?>
<p><?php echo $message; ?></p>
<?php endif ?>
<form action="index.php" method="get">
<input type="number" min="0" max="100" name="num" placeholder="随便猜">
<button type="submit">试⼀试</button>
</form>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。