Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!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</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- background-color: #f0f0f0;
- text-align: center;
- padding: 50px;
- }
- h1 {
- color: #333;
- }
- #game-container {
- display: flex;
- justify-content: center;
- margin-top: 50px;
- }
- .choice {
- background-color: #ffffff;
- border: 2px solid #333;
- border-radius: 8px;
- width: 150px;
- padding: 20px;
- margin: 0 20px;
- cursor: pointer;
- transition: 0.3s;
- }
- .choice:hover {
- background-color: #f2f2f2;
- }
- #result {
- margin-top: 30px;
- font-size: 24px;
- color: #555;
- }
- #scoreboard {
- margin-top: 20px;
- font-size: 20px;
- }
- .btn-reset {
- background-color: #ff4d4d;
- color: white;
- padding: 10px 20px;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- transition: 0.3s;
- }
- .btn-reset:hover {
- background-color: #ff3333;
- }
- </style>
- </head>
- <body>
- <h1>Rock Paper Scissors Game</h1>
- <div id="game-container">
- <div class="choice" data-choice="rock">
- <img src="https://img.icons8.com/ios/100/000000/hand-rock.png" alt="Rock">
- <p>Rock</p>
- </div>
- <div class="choice" data-choice="paper">
- <img src="https://img.icons8.com/ios/100/000000/hand.png" alt="Paper">
- <p>Paper</p>
- </div>
- <div class="choice" data-choice="scissors">
- <img src="https://img.icons8.com/ios/100/000000/hand-scissors.png" alt="Scissors">
- <p>Scissors</p>
- </div>
- </div>
- <div id="result"></div>
- <div id="scoreboard">
- <p>Player Score: <span id="playerScore">0</span></p>
- <p>Computer Score: <span id="computerScore">0</span></p>
- </div>
- <button class="btn-reset" onclick="resetGame()">Reset Game</button>
- <script>
- const choices = document.querySelectorAll('.choice');
- const resultDiv = document.getElementById('result');
- const playerScoreDiv = document.getElementById('playerScore');
- const computerScoreDiv = document.getElementById('computerScore');
- let playerScore = 0;
- let computerScore = 0;
- // Add click event listener for each choice
- choices.forEach(choice => {
- choice.addEventListener('click', playGame);
- });
- function playGame(event) {
- const playerChoice = event.currentTarget.getAttribute('data-choice');
- const computerChoice = getComputerChoice();
- const winner = getWinner(playerChoice, computerChoice);
- showResult(playerChoice, computerChoice, winner);
- updateScore(winner);
- }
- function getComputerChoice() {
- const choices = ['rock', 'paper', 'scissors'];
- const randomIndex = Math.floor(Math.random() * choices.length);
- return choices[randomIndex];
- }
- function getWinner(player, computer) {
- if (player === computer) {
- return 'draw';
- } else if (
- (player === 'rock' && computer === 'scissors') ||
- (player === 'paper' && computer === 'rock') ||
- (player === 'scissors' && computer === 'paper')
- ) {
- return 'player';
- } else {
- return 'computer';
- }
- }
- function showResult(playerChoice, computerChoice, winner) {
- let resultText = `Player chose ${playerChoice}, Computer chose ${computerChoice}.`;
- if (winner === 'player') {
- resultText += ' You win!';
- } else if (winner === 'computer') {
- resultText += ' Computer wins!';
- } else {
- resultText += ' It\'s a draw!';
- }
- resultDiv.textContent = resultText;
- }
- function updateScore(winner) {
- if (winner === 'player') {
- playerScore++;
- } else if (winner === 'computer') {
- computerScore++;
- }
- playerScoreDiv.textContent = playerScore;
- computerScoreDiv.textContent = computerScore;
- }
- function resetGame() {
- playerScore = 0;
- computerScore = 0;
- playerScoreDiv.textContent = playerScore;
- computerScoreDiv.textContent = computerScore;
- resultDiv.textContent = '';
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement