各位开发者大家好!我很高兴向大家介绍我的最新项目:石头剪刀布游戏。这款经典游戏是练习 JavaScript 技能和创建交互式用户体验的有趣方式。无论您是编码新手还是希望在您的作品集中添加一个简单但引人入胜的游戏,该项目都提供了提高您前端开发能力的绝佳机会。
石头剪刀布游戏是一个基于网络的应用程序,用户可以在其中与计算机玩流行的游戏。该项目演示了如何管理用户输入、生成随机计算机动作以及确定游戏结果。这是使用条件逻辑和 DOM 操作的绝佳练习。
以下是项目结构的快速浏览:
Rock-Paper-Scissors/ ├── index.html ├── style.css └── script.js
要开始该项目,请按照以下步骤操作:
克隆存储库:
git clone https://github.com/abhishekgurjar-in/Rock-Paper-Scissors.git
打开项目目录:
cd Rock-Paper-Scissors
运行项目:
index.html 文件提供了游戏的结构,包括石头、剪刀、布的按钮,以及显示结果和分数的元素。这是一个片段:
8b05045a5be5764f313ed5b9168a17e6 49099650ebdc5f3125501fa170048923 93f0f5c25f18dab9d176bd4f6de5d30e 7c8d9f814bcad6a1d7abe4eda5f773e5 26faf3d1af674280d03ba217d87e9421 b2386ffb911b14667cb8f0f91ea547a7Rock Paper Scissors Game6e916e0f7d1e588d4f442bf645aedb2f af75c476cdb7e6c074ca6da9b40841de 90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 473a967da286a3c736825b4619e7dc30 4a249f0d628e2318394fd9b75b4636b1Rock Paper Scissors Game473f0a7621bec819994bb5020d29372a e388a4556c0f65e1904146cc1a846beeChoose your move:94b3e26ee717c64999d7867364b1b4a3 cfc30845dcc69087db4670cfb20dbb15 06909a81795c7b20fc7f465c5650f5e5👊65281c5ac262bf6d81768915a4a77ac0 8f650b08ee1baf9097487bb7690c2cfe🖐65281c5ac262bf6d81768915a4a77ac0 c434bb80fc2caadf54d6803b6423b351✌65281c5ac262bf6d81768915a4a77ac0 16b28748ea4df4d9c2150843fecfba68 9369b95c0a54fa14078c71ff87b92a5394b3e26ee717c64999d7867364b1b4a3 9c3cee03555a05e7fda6c3ddcac84285 Your score: d8a54a40a42473022fcf25a18ceeb0b8054bdf357c58b8a65c66d7c19c8e4d114 Computer score: 9777042b948140f7c37ec825c7ba0124054bdf357c58b8a65c66d7c19c8e4d114 94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 ffd6ba4147bda351239915f463e46e38 e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
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; }
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 应用程序。
以上是建立一个石头剪刀布游戏网站的详细内容。更多信息请关注PHP中文网其他相关文章!