Unable to load the content

Please attempt to reload the page or alternatively, try accessing it using a different browser.

Create Rock Paper Scissors Game with Interactive Animations | HTML CSS JS

Rock Paper Scissors is a simple yet captivating game that has stood the test of time. Whether played casually among friends or as a decision-making tool, it has always been a go-to game for quick, fun interactions. In this tutorial, we will dive into how you can build a fully functional Rock-Paper-Scissors web app using just HTML, CSS and JavaScript.

Also Read: Ultimate Weather Web App using HTML CSS JavaScript | Fully Responsive 

Create-Rock-Paper-Scissors-Game-with-Interactive-Animations-HTML-CSS-JS

By the end of this guide, you'll have learned how to structure a web app, apply styles to make it visually appealing and implement interactive game logic that pits the player against a randomly generated computer choice. We’ll also add a score-tracking system that updates in real-time as the game progresses. Whether you're a beginner or looking to refine your front-end development skills, this project is a great hands-on learning opportunity.

Setting Up the HTML Structure

To start, we need to create the basic structure of the game using HTML. The HTML serves as the backbone of our web app and will contain all the elements we need for the game to function. Here's an outline of what we need to include:

<div class="game-container">
    <h2>Rock Paper Scissors</h2>
    <div class="game">
      <div class="selection-panel">
        <p>Select your Weapon</p>
        <div class="choices-container">
          <div id="rock" class="choice">
            <img src="Assets/rock.png" alt="Rock">
          </div>

  • Game Header: The header will contain the title of the game, "Rock Paper Scissors". This will be displayed at the top of the page so players know exactly what game they are playing.
  • Selection Panel: The player needs to be able to choose between Rock, Paper and Scissors. This section will include three buttons or clickable images that represent each choice. We’ll make these elements interactive, allowing users to click on them to make their selection.
  • Game Result Panel: This is where the outcome of the game will be displayed. After the player makes their choice, the app will show both the player's choice and the computer's choice, followed by the result of the match (win, loss or draw).
  • Scoreboard: A crucial part of the game is tracking the number of wins, losses and draws. We’ll create a small scoreboard that updates in real-time with each round.

Styling the Game with CSS

Next, we need to style the app using CSS to make it visually appealing and user-friendly. Our goal is to create a clean, responsive design that is easy to navigate and fun to interact with.

.game-container {
  width: 100%;
  height: 500px;
  font-family: "Baloo Bhai 2";
  border-radius: 10px;
  overflow: hidden;
  display: grid;
  padding: 15px;
  color: #f6f6f6;
  align-items: center;
  background-color: #191c25;
}

  • Layout and Positioning: The app will be centered on the page with a fixed-width container to ensure it looks good across different screen sizes. We’ll use a grid or flexbox layout to structure the elements neatly. The selection buttons (Rock, Paper and Scissors) will be horizontally aligned for easy access. The game result and scoreboard will be placed below the selection panel, each in its own section.
  • Button Styling and Hover Effects: The buttons for each of the three choices should stand out, so we’ll apply a background color, padding and some border radius to give them a modern, rounded look. When the player hovers over the buttons, we’ll change the background color slightly to give visual feedback that the button is clickable. This hover effect makes the game feel more interactive.
  • Animations and Transitions: For an added layer of polish, we can use CSS transitions to animate elements when the game progresses. For example, when the player makes a choice, we can animate the transition of their selection into place. Additionally, we can apply a fade-in effect to the result section to make it appear smoothly after each round.

JavaScript Game Logic

Now comes the fun part—implementing the logic of the game in JavaScript. JavaScript is used to handle the interaction between the player and the computer, calculate the result of each round and update the scoreboard.

const choices = document.querySelectorAll('.choice');
const playerGesture = document.querySelector('.p-gesture img')
const compGesture = document.querySelector('.c-gesture img');
const finalMove = document.querySelector(".final-move");
const selectionPanel = document.querySelector('.selection-panel');
const msg = document.querySelector(".msg");
const actionPanel = document.querySelector(".action-panel");
const body = document.body;
const wonElem = document.querySelector("#won");
const lostElem = document.querySelector("#lost");

  • Handling Player’s Choice: When the player clicks one of the three buttons, we need to capture that choice and display it on the screen. We can add event listeners to each button to detect when a player makes their selection. Once a choice is made, we display it on the screen and move on to the next step.
  • Computer’s Random Choice: The computer also needs to make a choice, but this time it’s a random one. We’ll generate a random number (0, 1 or 2) corresponding to Rock, Paper or Scissors. Based on that number, we’ll determine the computer’s choice and display it.
  • Result Calculation: After both the player and the computer have made their choices, we compare them to decide the winner:

    • Rock beats Scissors.
    • Scissors beats Paper.
    • Paper beats Rock.
    • If both the player and the computer choose the same option, the game is a draw.
  • Updating the Scoreboard: Every time a round is completed, we need to update the scoreboard. If the player wins, we increment the "Wins" counter. If the computer wins, we increment the "Losses" counter. In the case of a draw, we increment the "Draws" counter.
  • Play Again: Once a round is over, the player can click the "Play Again" button to reset the game and start a fresh round. We’ll need to reset the result display and allow the player to make another selection.

Testing and Improving

After completing the basic implementation of the app, it's important to test it thoroughly. Make sure that:

  • The player’s choices are displayed correctly.
  • The computer’s choice is generated randomly.
  • The result is calculated correctly.
  • The scoreboard updates dynamically after each round.
  • The "Play Again" button resets everything as expected.

Once you've confirmed that everything is working, you can add additional features to improve the game. For instance, you could add sound effects for when the player wins or loses or you could add more advanced animations. 

Conclusion

Building a Rock-Paper-Scissors web app is a great project for beginner web developers to practice using HTML, CSS and JavaScript. Not only does it give you hands-on experience with DOM manipulation and event handling, but it also provides a fun and interactive game that you can share with others.

By following this tutorial, you’ve learned how to set up a basic HTML structure, style it using CSS and implement the game logic with JavaScript. You've also seen how to update the UI dynamically and how to keep track of scores across multiple rounds.

Also Read: Create a User-Friendly and Responsive Sign Up Form using HTML & CSS

Feel free to experiment with the design and functionality of the app, adding new features or customizations to make it your own. Whether you’re a beginner or looking to hone your front-end development skills, this project is a perfect way to practice.

Download