Home Web Front-end CSS Tutorial Build a Rock Paper Scissors Game Website

Build a Rock Paper Scissors Game Website

Aug 24, 2024 am 06:41 AM

Build a Rock Paper Scissors Game Website

Introduction

Hello, fellow developers! I'm excited to introduce my latest project: a Rock Paper Scissors Game. This classic game is a fun way to practice your JavaScript skills and create an interactive user experience. Whether you're new to coding or looking to add a simple yet engaging game to your portfolio, this project offers a great opportunity to improve your front-end development abilities.

Project Overview

The Rock Paper Scissors Game is a web-based application where users can play the popular game against a computer. The project demonstrates how to manage user input, generate random computer moves, and determine the outcome of the game. It's an excellent exercise in working with conditional logic and DOM manipulation.

Features

  • Interactive Gameplay: Users can select Rock, Paper, or Scissors and see the result instantly.
  • Score Tracking: The game keeps track of the player's and computer's scores.
  • Responsive Design: Ensures a consistent and enjoyable experience across different devices.

Technologies Used

  • HTML: Structures the web page and game elements.
  • CSS: Styles the game interface for a clean and responsive design.
  • JavaScript: Manages the game logic, including user interactions and score tracking.

Project Structure

Here's a quick look at the project structure:

Rock-Paper-Scissors/
├── index.html
├── style.css
└── script.js
Copy after login
  • index.html: Contains the HTML structure for the Rock Paper Scissors game.
  • style.css: Includes CSS styles to enhance the appearance and responsiveness of the game.
  • script.js: Manages the game logic, including user interactions and score tracking.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Rock-Paper-Scissors.git
    
    Copy after login
  2. Open the project directory:

    cd Rock-Paper-Scissors
    
    Copy after login
  3. Run the project:

    • Open the index.html file in a web browser to start playing the Rock Paper Scissors game.

Usage

  1. Open the website in a web browser.
  2. Select your move by clicking on the Rock, Paper, or Scissors buttons.
  3. View the result of the game, and see the scores update automatically.

Code Explanation

HTML

The index.html file provides the structure of the game, including the buttons for Rock, Paper, and Scissors, and the elements that display the result and scores. Here’s a snippet:

<!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">&#x1F44A;</button>
        <button id="paper">&#x1f590;</button>
        <button id="scissors">&#x270c;</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>
Copy after login

CSS

The style.css file styles the Rock Paper Scissors game, providing a modern and user-friendly layout. Here are some key styles:

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;
}
Copy after login

JavaScript

The script.js file manages the logic for the Rock Paper Scissors game, including handling user input, generating computer moves, and determining the winner. Here’s a snippet:

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;
  }
}
Copy after login

Live Demo

You can check out the live demo of the Rock Paper Scissors game here.

Conclusion

Building the Rock Paper Scissors game was a fun and educational experience that helped me practice JavaScript and DOM manipulation. I hope this project inspires you to explore more JavaScript projects and continue building your coding skills. Happy coding!

Credits

This project was developed as part of my journey to enhance my front-end development skills, focusing on creating interactive and dynamic web applications.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Build a Rock Paper Scissors Game Website. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

The Three Types of Code The Three Types of Code Apr 11, 2025 pm 12:02 PM

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to

See all articles