Home >Web Front-end >CSS Tutorial >Build a Dice Roll Simulator Website

Build a Dice Roll Simulator Website

PHPz
PHPzOriginal
2024-08-11 06:34:321051browse

Build a Dice Roll Simulator Website

Introduction

Hello, fellow developers! Today, I'm excited to share a project I recently completed: a Dice Roll Simulator. This project is a fun and interactive way to simulate the rolling of a dice, making it a great way to practice JavaScript, especially in the areas of DOM manipulation and event handling. Whether you're looking to build something playful or need a simple dice roll feature for a game, this project is a perfect start.

Project Overview

The Dice Roll Simulator allows users to simulate the roll of a six-sided dice with a simple click of a button. The result is displayed in a visually appealing way, mimicking the appearance of a real dice. This project is perfect for developers who want to enhance their skills in building interactive web applications while working with animations and event listeners.

Features

  • Dice Roll Simulation: Users can roll a virtual dice by clicking a button, and the result is displayed instantly.
  • Roll History: The application keeps a record of all previous rolls, allowing users to see the outcomes of their past rolls.
  • Animation: The dice roll is accompanied by a smooth rotation animation to add realism to the simulation.
  • Responsive Design: The interface is designed to work well across different devices and screen sizes.

Technologies Used

  • HTML: Used for structuring the content on the webpage.
  • CSS: Applied for styling the webpage and adding animations.
  • JavaScript: Implemented for handling the dice roll logic, updating the DOM, and managing animations.

Project Structure

Here's a quick look at the project structure:

Dice-Roll-Simulator/
├── index.html
├── style.css
└── script.js
  • index.html: Contains the HTML structure of the webpage.
  • style.css: Holds the CSS styles, including animation and responsive design rules.
  • script.js: Manages the dynamic aspects of the page using JavaScript.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Dice-Roll-Simulator.git
    
  2. Open the project directory:

    cd Dice-Roll-Simulator
    
  3. Run the project:

    • You can either run it on a local server or simply open the index.html file in a web browser.

Usage

  1. Open the website in a web browser.
  2. Click the "Roll Dice" button to simulate a dice roll.
  3. View the result on the dice and check the roll history displayed below.

Code Explanation

HTML

The index.html file contains the structure of the webpage, including the dice display, the roll button, and the roll history list. Below is a snippet of the HTML code:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
  93f0f5c25f18dab9d176bd4f6de5d30e
    7c8d9f814bcad6a1d7abe4eda5f773e5
    acd8feeb3a0ea7477b979779de32785a
    26faf3d1af674280d03ba217d87e9421
    b2386ffb911b14667cb8f0f91ea547a7Dice Roll Simulator6e916e0f7d1e588d4f442bf645aedb2f
    90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0
    af75c476cdb7e6c074ca6da9b40841de
  9c3bca370b5104690d9ef395f2c5f8d1
  6c04bd5ca3fcae76e30b72ad730ca86d
    4a249f0d628e2318394fd9b75b4636b1Dice Roll Simulator473f0a7621bec819994bb5020d29372a
    0f518d525528904adc812d7ec5557770⚄16b28748ea4df4d9c2150843fecfba68
    9766fbc41ad5a9efefddfe0fa8af0f49Roll Dice65281c5ac262bf6d81768915a4a77ac0
    46e6f1c63052ca773d4c27bdcfd98f44
      0d9fbdb23b176f720131df826f3e11af929d1f5ca49e04fdcb27f9465b944689
    16b28748ea4df4d9c2150843fecfba68
    ffd6ba4147bda351239915f463e46e38
      e388a4556c0f65e1904146cc1a846beeMade With ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
  36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

The style.css file contains styles that ensure the webpage is visually appealing and includes animations for the dice roll. Here are some key styles:

body {
  font-family: "Open Sans", sans-serif;
  text-align: center;
  margin: 0;
}

h1 {
  font-size: 3rem;
  margin-top: 2rem;
}

.dice {
  font-size: 7rem;
  margin: 5px;
  animation-duration: 1s;
  animation-fill-mode: forwards;
}

.roll-animation {
  animation-name: roll;
}

@keyframes roll {
  0% {
    transform: rotateY(0deg) rotateX(0deg);
  }

  100% {
    transform: rotateY(720deg) rotateX(720deg);
  }
}

button {
  background-color: #47a5c4;
  color: white;
  font-size: 1.5rem;
  padding: 1rem 2rem;
  border: none;
  border-radius: 1rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #2e8baf;
}

.roll-list {
  min-height: 270px;
}

ul {
  list-style: none;
  padding: 0;
  max-width: 600px;
  margin: 2rem auto;
}

li {
  font-size: 1.5rem;
  padding: 0.5rem;
  margin: 0.5rem;
  background-color: #f2f2f2;
  border-radius: 0.5rem;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

li span {
  font-size: 3rem;
  margin-right: 1rem;
}

.footer {
  margin: 50px;
}

JavaScript

The script.js file manages the dice roll logic, updates the roll history, and handles the dice roll animation. Below is a snippet of the JavaScript code:

const buttonEl = document.getElementById("roll-button");
const diceEl = document.getElementById("dice");
const rollHistoryEl = document.getElementById("roll-history");

let historyList = [];

function rollDice() {
  const rollResult = Math.floor(Math.random() * 6) + 1;
  const diceFace = getDiceFace(rollResult);
  diceEl.innerHTML = diceFace;
  historyList.push(rollResult);
  updateRollHistory();
}

function updateRollHistory() {
  rollHistoryEl.innerHTML = "";
  for (let i = 0; i b0ea8d5d87b846bf4a6bd4575a8eff42${getDiceFace(historyList[i])}54bdf357c58b8a65c66d7c19c8e4d114`;
    rollHistoryEl.appendChild(listItem);
  }
}

function getDiceFace(rollResult) {
  switch (rollResult) {
    case 1:
      return "⚀";
    case 2:
      return "⚁";
    case 3:
      return "⚂";
    case 4:
      return "⚃";
    case 5:
      return "⚄";
    case 6:
      return "⚅";
    default:
      return "";
  }
}

buttonEl.addEventListener("click", () => {
  diceEl.classList.add("roll-animation");
  setTimeout(() => {
    diceEl.classList.remove("roll-animation");
    rollDice();
  }, 1000);
});

Live Demo

You can check out the live demo of the Dice Roll Simulator here.

Conclusion

Building this Dice Roll Simulator was a fun and rewarding experience that allowed me to experiment with JavaScript animations and DOM manipulation. I hope this project inspires you to create your own interactive applications. Feel free to explore the code, customize it, and use it in your own projects. Happy coding!

Credits

This project was inspired by the need for a simple and interactive dice roll tool.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Build a Dice Roll Simulator Website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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