You are currently viewing Rock Paper Scissors Game using HTML, CSS & JavaScript

Rock Paper Scissors Game using HTML, CSS & JavaScript

Rock Paper Scissors Game using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. Today we’ll learn how to make a game which we have played in our childhood Rock Paper Scissors to make it possible, In Today’s Project we will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements. And contributes the most while creating a structure for the Rock Paper Scissors Project.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment.

At last we will use JS (JavaScript) which will add a logic to make the project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Rock Paper Scissor

<html lang="en">
<head>
    <title>Rock Paper Scissor Game</title>
    <!--Fontawesome-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">
    <!--Google Font-->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="scores">
            <p>Computer : 
                <span id="computer_score">0</span>
            </p>
            <p>
                You :
                <span id="user_score">0</span>
            </p>
        </div>
        <div class="weapons">
            <button onclick="checker('rock')">
                <i class="far fa-hand-rock"></i>
            </button>
            <button onclick="checker('paper')">
                <i class="far fa-hand-paper"></i>
            </button>
            <button onclick="checker('scissor')">
                <i class="far fa-hand-scissors"></i>
            </button>
        </div>
        <div class="details">
            <p id="user_choice"></p>
            <p id="comp_choice"></p>
            <p id="result"></p>
        </div>
    </div>


    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

First we’ll start with creating the structure of the Rock Paper & Scissors project for that as you can see the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags used to create the project.

Create Simple Portfolio Website Using Html Css (Portfolio Website Source Code)

CSS Code for Rock Paper Scissor

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 100vh;
    background: linear-gradient(
        135deg,
        #ffcf1b,
        #ff8b1b
    );
}
.container{
    width: 45%;
    min-width: 500px;
    background-color: #ffffff;
    padding: 40px 30px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    border-radius: 10px;
    box-shadow: 0 15px 25px rgba(0,0,0,0.15);
}
.scores{
    margin-bottom: 50px;
    text-align: right;
}
.weapons{
    width: 90%;
    margin: auto;
    display: flex;
    justify-content: space-around;
}
.weapons button{
    background-color: #ffd51b;
    color: #000000;
    border: none;
    font-size: 50px;
    height: 100px;
    width: 100px;
    border-radius: 50%;
    outline: none;
    cursor: pointer;
}
.details{
    margin-top: 30px;
    text-align: center;
}
.scores,.details{
    font-family: 'Poppins',sans-serif;
    font-weight: 400;
    font-size: 15px;
}
#result{
    width: 180px;
    padding: 10px 0;
    margin: 30px auto;
    font-weight: 600;
    letter-spacing: 0.5px;
}
#user_choice,
#computer_choice{
    font-weight: 400;
    margin-bottom: 10px;
}
span{
    font-weight: 600;
}

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the project so that it is properly situated and doesn’t get messy with suitable CSS elements for Rock Paper Scissors Project. Now, let’s code the JavaScript part to make it responsive.

50+ Html ,Css &#038; Javascript Projects With Source Code

JavaScript Code for Rock Paper Scissor

let [computer_score,user_score]=[0,0];
let result_ref = document.getElementById("result");
let choices_object = {
    'rock' : {
        'rock' : 'draw',
        'scissor' : 'win',
        'paper' : 'lose'
    },
    'scissor' : {
        'rock' : 'lose',
        'scissor' : 'draw',
        'paper' : 'win'
    },
    'paper' : {
        'rock' : 'win',
        'scissor' : 'lose',
        'paper' : 'draw'
    }

}

function checker(input){
    var choices = ["rock", "paper", "scissor"];
    var num = Math.floor(Math.random()*3);

    document.getElementById("comp_choice").innerHTML = 
    ` Computer choose <span> ${choices[num].toUpperCase()} </span>`;

    document.getElementById("user_choice").innerHTML = 
    ` You choose <span> ${input.toUpperCase()} </span>`;

    let computer_choice = choices[num];

    switch(choices_object[input][computer_choice]){
        case 'win':
            result_ref.style.cssText = "background-color: #cefdce; color: #689f38";
            result_ref.innerHTML = "YOU WIN";
            user_score++;
            break;
        case 'lose':
            result_ref.style.cssText = "background-color: #ffdde0; color: #d32f2f";
            result_ref.innerHTML = "YOU LOSE";
            computer_score++;
            break;
        default:
            result_ref.style.cssText = "background-color: #e5e5e5; color: #808080";
            result_ref.innerHTML = "DRAW";
            break;
    }

    document.getElementById("computer_score").innerHTML = computer_score;
    document.getElementById("user_score").innerHTML = user_score;
}

Last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. And defined some conditions when to declare a winner in each scenario when it is clicked on Rock, Paper & Scissors. Let us see the Final Output of the project Rock Paper Scissors using HTML, CSS & JavaScript (Source Code) | Rock Paper Scissor.

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

Final Output Rock Paper Scissors Game using HTML, CSS & JavaScript

We have Successfully created our Rock Paper Scissors Game using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply