<div class="game">
  <div class="score">
    <div class="player-score">
      <h2>Player</h2>
      <p>0</p>
    </div>
    <div class="comp-score">
      <h2>Machine</h2>
      <p>0</p>
    </div>
  </div>

  <div class="intro">
    <h1>Rock, Paper and Scissors</h1>
    <button>Let's Play</button>
  </div>

  <div class="match fadeOut">
    <h2 class="winner">Choose an option</h2>
    <div class="hands">
      <img src="assets/rock.png" alt="" class="player-hand">
      <img src="assets/rock.png" alt="" class="comp-hand">
    </div>
    <div class="options">
      <button class="rock">rock</button>
      <button class="paper">paper</button>
      <button class="scissors">scissors</button>
    </div>
  </div>
</div>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.game {
  height: 100vh;
  background: rgb(39, 41, 68);
  font-family: sans-serif;
}

.score {
  color: #ddd;
  height: 20vh;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.score h2 {
  font-size: 30px;
}

.score p {
  text-align: center;
  padding: 10px;
  font-size: 25px;
}

.intro {
  color: #ddd;
  height: 50vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  transition: opacity 0.5s ease;
}

.intro h1 {
  font-size: 50px;
}

.intro button,
.match button {
  width: 150px;
  height: 50px;
  border: none;
  color: #ddd;
  font-size: 20px;
  background: rgb(45, 117, 96);
  border-radius: 3px;
  cursor: pointer;
}

.match {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: opacity 0.5s ease 0.5s;
}

.winner {
  color: #ddd;
  text-align: center;
  font-size: 50px;
}

.hands,
.options {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.player-hand {
  transform: rotateY(180deg);
}

div.fadeOut {
  opacity: 0;
  pointer-events: none;
}

div.fadeIn {
  opacity: 1;
  pointer-events: all;
}

@keyframes shakePlayer {
  0% {
    transform: rotateY(180deg) translateY(0px);
  }

  15% {
    transform: rotateY(180deg) translateY(-50px);
  }

  25% {
    transform: rotateY(180deg) translateY(0px);
  }

  35% {
    transform: rotateY(180deg) translateY(-50px);
  }

  50% {
    transform: rotateY(180deg) translateY(0px);
  }

  65% {
    transform: rotateY(180deg) translateY(-50px);
  }

  75% {
    transform: rotateY(180deg) translateY(0px);
  }

  85% {
    transform: rotateY(180deg) translateY(-50px);
  }

  100% {
    transform: rotateY(180deg) translateY(0px);
  }
}

@keyframes shakeComp {
  0% {
    transform: translateY(0px);
  }

  15% {
    transform: translateY(-50px);
  }

  25% {
    transform: translateY(0px);
  }

  35% {
    transform: translateY(-50px);
  }

  50% {
    transform: translateY(0px);
  }

  65% {
    transform: translateY(-50px);
  }

  75% {
    transform: translateY(0px);
  }

  85% {
    transform: translateY(-50px);
  }

  100% {
    transform: translateY(0px);
  }
}
const game = () => {
  let pScore = 0;
  let cScore = 0;

  const startGame = () => {
    const playBtn = document.querySelector(".intro button");
    const introScreen = document.querySelector(".intro");
    const matchScreen = document.querySelector(".match");

    playBtn.addEventListener("click", () => {
      introScreen.classList.add("fadeOut");
      matchScreen.classList.add("fadeIn");
    });
  };

  const playMatch = () => {
    const options = document.querySelectorAll(".options button");
    const playerHand = document.querySelector(".player-hand");
    const compHand = document.querySelector(".comp-hand");
    const hands = document.querySelectorAll(".hands img");

    hands.forEach((hand) => {
      hand.addEventListener("animationend", function () {
        this.style.animation = "";
      });
      hand.addEventListener("animationstart", function () {
        this.src = "https://harryheman.github.io/rockPaperScissors/assets/rock.png";
      });
    });

    const compOptions = ["rock", "paper", "scissors"];

    options.forEach((option) => {
      option.addEventListener("click", function () {
        const compNumber = Math.trunc(Math.random() * 3);
        const compChoice = compOptions[compNumber];
        setTimeout(() => {
          compareHands(this.textContent, compChoice);

          playerHand.src = `https://harryheman.github.io/rockPaperScissors/assets/${this.textContent}.png`;
          compHand.src = `https://harryheman.github.io/rockPaperScissors/assets/${compChoice}.png`;
        }, 2000);

        playerHand.style.animation = "shakePlayer 2s ease";
        compHand.style.animation = "shakeComp 2s ease";
      });
    });
  };

  const updateScore = () => {
    const playerScore = document.querySelector(".player-score p");
    const compScore = document.querySelector(".comp-score p");
    playerScore.textContent = pScore;
    compScore.textContent = cScore;
  };

  const compareHands = (playerChoice, compChoice) => {
    const winner = document.querySelector(".winner");
    if (playerChoice === compChoice) {
      winner.textContent = "It's a tie";
      return;
    }
    // check for rock
    if (playerChoice === "rock") {
      if (compChoice === "scissors") {
        winner.textContent = "Player Wins";
      } else {
        winner.textContent = "Machine Wins";
      }
    }
    // check for paper
    if (playerChoice === "paper") {
      if (compChoice === "scissors") {
        winner.textContent = "Machine Wins";
      } else {
        winner.textContent = "Player Wins";
      }
    }
    // check for scissors
    if (playerChoice === "scissors") {
      if (compChoice === "rock") {
        winner.textContent = "Machine Wins";
      } else {
        winner.textContent = "Player Wins";
      }
    }
    if (winner.textContent === "Player Wins") {
      pScore++;
    } else if (winner.textContent === "Machine Wins") {
      cScore++;
    }
    updateScore();
  };

  startGame();
  playMatch();
};

game();
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.