建立一個石頭剪刀布遊戲網站
介紹
各位開發者大家好!我很高興向大家介紹我的最新項目:石頭剪刀布遊戲。這款經典遊戲是練習 JavaScript 技能和創建互動式使用者體驗的有趣方式。無論您是編碼新手還是希望在您的作品集中添加一個簡單但引人入勝的遊戲,該專案都提供了提高您前端開發能力的絕佳機會。
項目概況
石頭剪刀布遊戲是一個基於網絡的應用程序,用戶可以在其中與計算機玩流行的遊戲。此專案示範如何管理使用者輸入、產生隨機電腦動作以及確定遊戲結果。這是使用條件邏輯和 DOM 操作的絕佳練習。
特徵
- 互動遊戲:使用者可以選擇石頭、剪刀、布並立即看到結果。
- 分數追蹤:遊戲會追蹤玩家和電腦的分數。
- 響應式設計:確保在不同裝置上獲得一致且愉快的體驗。
使用的技術
- HTML:建立網頁和遊戲元素。
- CSS:設計遊戲介面的樣式,以實現簡潔且響應式的設計。
- JavaScript:管理遊戲邏輯,包括使用者互動和分數追蹤。
專案結構
以下是專案結構的快速瀏覽:
Rock-Paper-Scissors/ ├── index.html ├── style.css └── script.js
- index.html:包含剪刀石頭布遊戲的 HTML 結構。
- style.css:包含 CSS 樣式以增強遊戲的外觀和反應能力。
- script.js:管理遊戲邏輯,包括使用者互動和分數追蹤。
安裝
要開始該項目,請按照以下步驟操作:
-
複製儲存庫:
git clone https://github.com/abhishekgurjar-in/Rock-Paper-Scissors.git
-
開啟專案目錄:
cd Rock-Paper-Scissors
-
運行項目:
- 在網頁瀏覽器中開啟index.html檔案即可開始玩石頭剪刀布遊戲。
用法
- 在網頁瀏覽器中開啟網站。
- 點選石頭、剪刀、布鈕來選擇你的動作。
- 查看比賽結果,並查看分數自動更新。
程式碼說明
超文本標記語言
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">👊</button> <button id="paper">🖐</button> <button id="scissors">✌</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中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Autoprefixer是一個根據目標瀏覽器範圍自動為CSS屬性添加廠商前綴的工具。 1.它解決了手動維護前綴易出錯的問題;2.通過PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設置browserslist、在構建流程中啟用;4.注意事項有不手動加前綴、保持配置更新、非所有屬性都加前綴、建議配合預處理器使用。

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

創建CSS加載旋轉器的方法有三種:1.使用邊框的基本旋轉器,通過HTML和CSS實現簡單動畫;2.使用多個點的自定義旋轉器,通過不同延遲時間實現跳動效果;3.在按鈕中添加旋轉器,通過JavaScript切換類來顯示加載狀態。每種方法都強調了設計細節如顏色、大小、可訪問性和性能優化的重要性,以提升用戶體驗。

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

要創建內在響應式網格佈局,核心方法是使用CSSGrid的repeat(auto-fit,minmax())模式;1.設置grid-template-columns:repeat(auto-fit,minmax(200px,1fr))讓瀏覽器自動調整列數並限制每列最小和最大寬度;2.使用gap控制格子間距;3.容器應設為相對單位如width:100%、配合box-sizing:border-box避免寬度計算錯誤並用margin:auto居中;4.可選設置行高與內容對齊方式提升視覺一致性,如row

要讓整個網格佈局在視口中居中顯示,可通過以下方法實現:1.使用margin:0auto實現水平居中,需設定容器固定寬度,適用於固定佈局;2.利用Flexbox在外層容器設置justify-content和align-items屬性,結合min-height:100vh可實現垂直和水平居中,適合全屏展示場景;3.直接使用CSSGrid的place-items屬性在父容器上快速居中,簡潔且現代瀏覽器支持良好,同時需確保父容器有足夠高度。每種方式均有適用場景和限制,根據實際需求選擇合適的方案即可。

prainuredetectionIncsssusissuse@supportScheckSifabRowsEsuppecifortSupecifortEfeatureBeforeApplyingReplyingStyles.1.itusesconditionalcsssssbasssbasedonproperty-valueperty-valuepairs,suessas@supports@supports@supports@supports(display:grid)

處理CSS瀏覽器兼容性和前綴問題需理解瀏覽器支持差異並合理使用廠商前綴。 1.了解常見問題如Flexbox、Grid支持不一,position:sticky失效,動畫表現不同;2.查閱CanIuse確認特性支持情況;3.正確使用-webkit-、-moz-、-ms-、-o-等廠商前綴;4.推薦使用Autoprefixer自動添加前綴;5.安裝PostCSS並配置browserslist指定目標瀏覽器;6.構建時自動處理兼容性;7.老項目可用Modernizr檢測特性;8.不必追求所有瀏覽器一致,確
