PHP开发【石头剪刀布小游戏】

石头剪刀布小游戏

玩法超级简单,你只需要在下面选择石头、剪刀或者布,然后提交,系统就会随机生成电脑的选择,告诉你最终的结果哦!
在这里插入图片描述

游戏规则:

  1. 如果你的选择和电脑一样,那么就是平局。
  2. 如果你赢了,会有相应的提示告诉你“你赢了”。
  3. 如果你不幸输了,系统也会显示电脑的选择,让你知道差距在哪里。

使用指南:

  1. 代码如下,保存到一个忘记中:如 index.php。
  2. 代码部署到PHP服务器,比如 phpstudy。
  3. 运行网站,访问 index.php 文件即可。

代码

<?php
session_start();

// 服务器端生成的随机选择对应的图片
$computer_choices_images = array(
    '石头' => 'stone.png',
    '剪刀' =>'scissors.png',
    '布' => 'paper.png'
);

// 生成随机选择
$computer_choice = array_rand($computer_choices_images);
$computer_choice_image = $computer_choices_images[$computer_choice];

// 检查是否有用户输入
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user_choice = $_POST['user_choice'];
    if ($user_choice === $computer_choice) {
        $_SESSION['result'] = '平局';
    } elseif (($user_choice === '石头' && $computer_choice === '剪刀') ||
              ($user_choice === '剪刀' && $computer_choice === '布') ||
              ($user_choice === '布' && $computer_choice === '石头')) {
        $_SESSION['result'] = '你赢了';
    } else {
        $_SESSION['result'] = '你输了';
        $_SESSION['computer_choice'] = $computer_choice;
    }
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>石头、剪刀、布游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f7f7f7;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #333;
        }
        form {
            margin: 20px 0;
        }
        label {
            margin-right: 10px;
        }
        input[type="radio"] {
            margin-right: 5px;
        }
        button {
            padding: 10px 20px;
            background-color: #5cb85c;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #4cae4c;
        }
        p {
            margin-top: 20px;
            font-size: 24px;
            font-weight: bold;
        }
        img {
            max-width: 100px;
            height: auto;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>石头、剪刀、布游戏</h1>
    <p>请猜出我选择的是哪个。</p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
        <div>
            <input type="radio" id="stone" name="user_choice" value="石头">
            <label for="stone">石头</label>
        </div>
        <div>
            <input type="radio" id="scissors" name="user_choice" value="剪刀">
            <label for="scissors">剪刀</label>
        </div>
        <div>
            <input type="radio" id="paper" name="user_choice" value="布">
            <label for="paper"></label>
        </div>
        <button type="submit">提交</button>
    </form>
    <?php
    if (isset($_SESSION['result'])) {
        echo '<p>结果:'. $_SESSION['result']. '</p>';
        if ($_SESSION['result'] === '你输了') {
            echo '<p>电脑选择了:'. $_SESSION['computer_choice']. '</p>';
        }
    }
   ?>
</body>
</html>

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员潘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值