🗣 Wd提问: 程序PHP游戏开发
🤖 Ai回答: PHP游戏开发:猜数字游戏
我将创建一个基于PHP的猜数字游戏,它结合了简单的游戏逻辑和美观的界面设计。这个游戏会随机生成一个数字,玩家需要猜测这个数字是多少。
设计思路
创建一个简洁现代的游戏界面
实现游戏状态管理(开始、进行中、胜利)
添加游戏反馈机制(提示太大/太小)
记录玩家尝试次数
添加响应式设计确保移动设备兼容
完整代码实现
php
<?php
session_start();
// 初始化游戏
if (!isset($_SESSION['game_started']) isset($_GET['reset'])) {
$_SESSION['target'] = rand(1, 100);
$_SESSION['attempts'] = 0;
$_SESSION['game_started'] = true;
$_SESSION['game_won'] = false;
$_SESSION['message'] = 我已经想好了一个1-100之间的数字,开始猜吧!;
}
// 处理用户猜测
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['guess']) && !$_SESSION['game_won']) {
$guess = (int)$_POST['guess'];
$_SESSION['attempts']++;
if ($guess < 1 $guess 100) {
$_SESSION['message'] = 请输入1-100之间的数字!;
} elseif ($guess < $_SESSION['target']) {
$_SESSION['message'] = 太小了!再试一次(你猜了{$_SESSION['attempts']}次);
} elseif ($guess $_SESSION['target']) {
$_SESSION['message'] = 太大了!再试一次(你猜了{$_SESSION['attempts']}次);
} else {
$_SESSION['message'] = 恭喜!你猜对了!数字是{$_SESSION['target']},你用了{$_SESSION['attempts']}次猜中!;
$_SESSION['game_won'] = true;
}
}
?
<!DOCTYPE html
<html lang=zh-CN
<head
<meta charset=UTF-8
<meta name=viewport content=width=device-width, initial-scale=1.0
<titlePHP猜数字游戏</title
<style
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, 1a2a6c, b21f1f, fdbb2d);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.game-container {
background: rgba(255, 255, 255, 0.9);
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 500px;
padding: 30px;
text-align: center;
}
h1 {
color: 2c3e50;
margin-bottom: 20px;
font-size: 2.5rem;
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}
.game-info {
background: 3498db;
color: white;
padding: 15px;
border-radius: 10px;
margin: 20px 0;
font-size: 1.2rem;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.attempts {
background: 2ecc71;
color: white;
padding: 8px 15px;
border-radius: 20px;
display: inline-block;
margin: 10px 0;
font-weight: bold;
}
.game-form {
margin: 25px 0;
}
.guess-input {
padding: 12px 15px;
width: 100%;
max-width: 200px;
border: 2px solid 3498db;
border-radius: 8px;
font-size: 1.2rem;
text-align: center;
margin-bottom: 15px;
transition: all 0.3s;
}
.guess-input:focus {
outline: none;
border-color: e74c3c;
box-shadow: 0 0 10px rgba(231, 76, 60, 0.5);
}
.submit-btn {
background: e74c3c;
color: white;
border: none;
padding: 12px 30px;
font-size: 1.1rem;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s;
font-weight: bold;
display: block;
margin: 0 auto;
width: 200px;
}
.submit-btn:hover {
background: c0392b;
transform: translateY(-3px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.submit-btn:active {
transform: translateY(1px);
}
.reset-btn {
background: 2c3e50;
color: white;
border: none;
padding: 10px 20px;
font-size: 1rem;
border-radius: 8px;
cursor: pointer;
margin-top: 20px;
transition: all 0.3s;
}
.reset-btn:hover {
background: 1a252f;
}
.instructions {
background: f8f9fa;
border-left: 4px solid 3498db;
padding: 15px;
margin: 25px 0;
text-align: left;
border-radius: 0 8px 8px 0;
}
.instructions h3 {
color: 2c3e50;
margin-bottom: 10px;
}
.instructions ul {
padding-left: 20px;
}
.instructions li {
margin-bottom: 8px;
line-height: 1.5;
}
.win-message {
animation: celebrate 1.5s ease infinite;
font-size: 1.5rem;
font-weight: bold;
color: e74c3c;
margin: 20px 0;
}
@keyframes celebrate {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
.history {
margin-top: 20px;
padding: 15px;
background: ecf0f1;
border-radius: 8px;
max-height: 200px;
overflow-y: auto;
text-align: left;
}
.history h3 {
color: 2c3e50;
margin-bottom: 10px;
text-align: center;
}
.history-item {
padding: 8px;
border-bottom: 1px solid bdc3c7;
}
@media (max-width: 500px) {
.game-container {
padding: 20px;
}
h1 {
font-size: 2rem;
}
.guess-input {
max-width: 100%;
}
}
</style
</head
<body
<div class=game-container
<h1🎮 PHP猜数字游戏</h1
<div class=game-info
<?php echo $_SESSION['message']; ?
</div
<div class=attempts
尝试次数: <?php echo $_SESSION['attempts']; ?
</div
<?php if (!$_SESSION['game_won']): ?
<form class=game-form method=POST
<input type=number name=guess class=guess-input min=1 max=100
placeholder=1-100 required autofocus
<br
<button type=submit class=submit-btn提交猜测</button
</form
<?php else: ?
<div class=win-message
🎉 恭喜获胜! 🎉
</div
<?php endif; ?
<a href=?reset=1 class=reset-btn开始新游戏</a
<div class=instructions
<h3游戏说明</h3
<ul
<li我已经随机生成了一个1到100之间的数字</li
<li你需要猜测这个数字是多少</li
<li每次猜测后,我会告诉你猜的数字是太大还是太小</li
<li尽可能用最少的次数猜中数字</li
📣 商家广告:





0
IP地址: 56.13.153.218
搜索次数: 0
提问时间: 2025-11-05 23:25:01
🛒 域名购买:
v.jl.cn
61.chat
rq.gs
qg.biz
887.net.cn
72.gold
pai.cx
ltkj.net
wddd.com.cn
kfdy.cn
❓️ 热门提问:
从大陆发货到香港
编程网站开发
模块化机房排名
液冷服务器
Schema与课程SEO
机房一体化建设方案
顺丰香港官网
二手域名 交易
电脑云
网络游戏挂机
豌豆Ai站群搜索引擎系统
🌐 域名评估:
aifair.ren
10.cn
musk.beer
3q.js.cn
uavtower.tech
aaaa.sc.cn
pxia.cn
su7.mo.cn
6-6.com.cn
oppo.sc.cn
⛏ 最新挖掘:
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜
🖌 热门作画:
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊:
🔗 友情链接:
Ai站群
網頁搜尋引擎
ai提问
🧰 站长工具:
Ai工具 whois查询 搜索
📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。