Dice Game Using HTML, CSS & JavaScript (Source Code)

How to Build Dice Roll Game Using JavaScript with Free Source Code

Dice Game Using HTML, CSS & JavaScript (Source Code)

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a dice game in which the user will click on the button and the dice will randomly give the value. In Today’s session, 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. 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 logic to make the project responsive from the user end.

In this blog post, we will discuss Dice Game Using HTML, CSS & JavaScript (Source Code) with complete source code for you so that you can just copy and paste it into your own project. Let’s deep dive into the Dice Challenge using JavaScript.  Happy exploring and learning !!

Prerequisites

Before You start building the Dice Roll Game You must have HTML and CSS knowledge and you are just going to work with JavaScript to make the game functional.
This project, however, will be helpful to both code newbies and intermediate programmers who want to brush their JavaScript skills.

I hope you have got an idea about the project.

Step To Build the Dice Roll Game

  1. Create an HTML File for creating the structure of Dice Game
  2. Add the CSS File for providing the interface
  3. Add the JavaScript for building the logic.

HTML Code for Dice Game

<div class="wrapper">
        <div class="column">
                <div id="dice-side-1" class="dice">0</div>
                <div id="dice-side-2" class="dice">0</div>
        </div>
        <div class="column">
                <button type="button" class="dice-roll">roll dice</button>
                <h2 id="status"></h2>
        </div>
</div>

First, we’ll start with creating the structure of the project for that as you can see in 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.

Temperature Convertor using HTML, CSS &JavaScript

CSS Code for Dice Game

Below is the CSS code for Creating the Dice Game Challenge.

body {
    position: relative;
        background: orange;
}

.wrapper {
    width: 400px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
}

.column {
    display: flex;
    align-items: center;
}

#status {
    position: absolute;
    top: 5rem;
        left: 50%;
        transform: translateX(-50%);
    min-width: 400px;
    margin: 0;
        text-align: center;
}

.dice {
    width: 32px;
    float: left;
    background-color: lightcoral;
    border: 1px solid black;
    padding: 10px;
    font-size: 24px;
    text-align: center;
    margin: 5px;
    border-radius: 5px;
}

.dice-roll {
    cursor: pointer;
    text-transform: capitalize;
}

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. Now, let’s code the JavaScript part to make it responsive.

Palindrome Checker using HTML, CSS and JavaScript

JavaScript Code for Dice Game

Velow is JavaScript Code for creating the Dice Roll Game that contains the logic and main working of the Dice Game.

window.addEventListener( 'DOMContentLoaded', function () {
    
        const buttonRoolDice = document.querySelector( '.dice-roll' );

    function rollDice () {

        const diceSide1 = document.getElementById( 'dice-side-1' );
        const diceSide2 = document.getElementById( 'dice-side-2' );
        const status = document.getElementById( 'status' );

        const side1 = Math.floor( Math.random() * 6 ) + 1;
        const side2 = Math.floor( Math.random() * 6 ) + 1;
        const diceTotal = side1 + side2;

        diceSide1.innerHTML = side1;
        diceSide2.innerHTML = side2;

        status.innerHTML = 'You rolled ' + diceTotal + '.';

        if ( side1 === side2 ) {
            status.innerHTML += ' Doubles! You get a free turn!';
        }
    }

    buttonRoolDice.addEventListener( 'click', rollDice, false );

}, false);

The last stage of the project the JavaScript in which we added the logic and coded as per the requirement with some conditions also a function is defined to delete the input in the list. Let us see the Final Output of the project Dice Game Using HTML, CSS & JavaScript (Source Code)

Final Output Dice Game Using HTML, CSS & JavaScript

Below we’ve provided the Free Dice Roll Game Codepen Source Code. This will be given as a preview of the project. You can make changes according to your preference.

We have Successfully created our Dice 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 this Blog helpful, then make sure to search codewithrandom on Google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Conclusion

Congratulations! You just completed another Mini Project using JavaScript. We have looked at how we can use JavaScript to manipulate CSS styles and HTML elements in response to the clicking of buttons. I hope you enjoyed the article and learned something from it.

Thank You And Keep Learning!!!

Written By – Harsh Sawant

Code By – @harshh9

HAPPY CODING!!!



Leave a Reply