首頁 > 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學習者快速成長!