首页 > web前端 > css教程 > 正文

建立一个石头剪刀布游戏网站

WBOY
发布: 2024-08-24 06:41:35
原创
1035 人浏览过

Build a Rock Paper Scissors Game Website

介绍

各位开发者大家好!我很高兴向大家介绍我的最新项目:石头剪刀布游戏。这款经典游戏是练习 JavaScript 技能和创建交互式用户体验的有趣方式。无论您是编码新手还是希望在您的作品集中添加一个简单但引人入胜的游戏,该项目都提供了提高您前端开发能力的绝佳机会。

项目概况

石头剪刀布游戏是一个基于网络的应用程序,用户可以在其中与计算机玩流行的游戏。该项目演示了如何管理用户输入、生成随机计算机动作以及确定游戏结果。这是使用条件逻辑和 DOM 操作的绝佳练习。

特征

  • 互动游戏:用户可以选择石头、剪刀、布并立即看到结果。
  • 分数跟踪:游戏会跟踪玩家和计算机的分数。
  • 响应式设计:确保在不同设备上获得一致且愉快的体验。

使用的技术

  • HTML:构建网页和游戏元素。
  • CSS:设计游戏界面的样式,以实现简洁且响应式的设计。
  • JavaScript:管理游戏逻辑,包括用户交互和分数跟踪。

项目结构

以下是项目结构的快速浏览:

Rock-Paper-Scissors/
├── index.html
├── style.css
└── script.js
登录后复制
  • index.html:包含剪刀石头布游戏的 HTML 结构。
  • style.css:包括 CSS 样式以增强游戏的外观和响应能力。
  • script.js:管理游戏逻辑,包括用户交互和分数跟踪。

安装

要开始该项目,请按照以下步骤操作:

  1. 克隆存储库

    git clone https://github.com/abhishekgurjar-in/Rock-Paper-Scissors.git
    
    登录后复制
  2. 打开项目目录:

    cd Rock-Paper-Scissors
    
    登录后复制
  3. 运行项目:

    • 在网络浏览器中打开index.html文件即可开始玩石头剪刀布游戏。

用法

  1. 在网络浏览器中打开网站
  2. 点击石头、剪刀、布按钮来选择你的动作
  3. 查看比赛结果,并查看分数自动更新。

代码说明

超文本标记语言

index.html 文件提供了游戏的结构,包括石头、剪刀、布的按钮,以及显示结果和分数的元素。这是一个片段:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Rock Paper Scissors Game</title>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js" defer></script>
  </head>
  <body>
    <div class="main">
      <h1>Rock Paper Scissors Game</h1>
      <p>Choose your move:</p>
      <div class="buttons">
        <button id="rock">&#x1F44A;</button>
        <button id="paper">&#x1f590;</button>
        <button id="scissors">&#x270c;</button>
      </div>
      <p id="result"></p>
      <p id="scores">
        Your score: <span id="user-score">0</span> Computer score:
        <span id="computer-score">0</span>
      </p>
    </div>
    <div class="footer">
      <p>Made with ❤️ by Abhishek Gurjar</p>
    </div>
  </body>
</html>
登录后复制

CSS

style.css 文件设计剪刀石头布游戏的样式,提供现代且用户友好的布局。以下是一些关键样式:

body {
  background-color: #f1f1f1;
  font-family: "Arial", sans-serif;
  margin: 0;
  padding: 0;
}

h1 {
  font-size: 2rem;
  text-align: center;
  margin: 100px;
}

p {   
  font-size: 1.5rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 0.5rem;
}

.buttons {
  display: flex;
  justify-content: center;
}

button {
  border: none;
  font-size: 3rem;
  margin: 0 0.5rem;
  padding: 0.5rem;
  cursor: pointer;
  border-radius: 5px;
  transition: all 0.3s ease-in-out;
}

button:hover {
  opacity: 0.7;
}

#rock {
  background-color: #ff0000;
}

#paper {
  background-color: #2196f3;
}

#scissors {
  background-color: #4caf50;
}

#user-score {
  color: #2196f3;
}

#computer-score {
  color: #ff0000;
}

.footer {
  margin-top: 250px;
  text-align: center;
}

.footer p {
  font-size: 16px;
  font-weight: 400;
}
登录后复制

JavaScript

script.js 文件管理剪刀石头布游戏的逻辑,包括处理用户输入、生成计算机动作以及确定获胜者。这是一个片段:

const buttons = document.querySelectorAll("button");
const resultEl = document.getElementById("result");
const playerScoreEl = document.getElementById("user-score");
const computerScoreEl = document.getElementById("computer-score");

let playerScore = 0;
let computerScore = 0;

buttons.forEach((button) => {
  button.addEventListener("click", () => {
    const result = playRound(button.id, computerPlay());
    resultEl.textContent = result;
  });
});

function computerPlay() {
  const choices = ["rock", "paper", "scissors"];
  const randomChoice = Math.floor(Math.random() * choices.length);
  return choices[randomChoice];
}

function playRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "It's a tie!";
  } else if (
    (playerSelection === "rock" && computerSelection === "scissors") ||
    (playerSelection === "paper" && computerSelection === "rock") ||
    (playerSelection === "scissors" && computerSelection === "paper")
  ) {
    playerScore++;
    playerScoreEl.textContent = playerScore;
    return "You win! " + playerSelection + " beats " + computerSelection;
  } else {
    computerScore++;
    computerScoreEl.textContent = computerScore;
    return "You lose! " + computerSelection + " beats " + playerSelection;
  }
}
登录后复制

现场演示

您可以在这里查看剪刀石头布游戏的现场演示。

结论

构建剪刀石头布游戏是一次有趣且具有教育意义的体验,它帮助我练习 JavaScript 和 DOM 操作。我希望这个项目能够激励您探索更多 JavaScript 项目并继续培养您的编码技能。快乐编码!

制作人员

这个项目是我增强前端开发技能之旅的一部分,专注于创建交互式和动态 Web 应用程序。

作者

  • 阿布舍克·古贾尔
    • GitHub 简介

以上是建立一个石头剪刀布游戏网站的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!