This project is a simple dice game built with HTML, CSS, and JavaScript. The game allows two players to roll dice and the player with the higher roll wins.
Files:
- index.html: The main HTML file containing the structure and content of the game.
- style.css: The CSS file responsible for styling the game elements.
- code.js: The JavaScript file containing the game logic.
This file contains the HTML structure for the dice game. It includes the following elements:
- DOCTYPE declaration: Specifies the document type as HTML5.
- HTML tag: Defines the root element of the HTML document.
- Head section:
- Meta tags: Provide information about the document, including character encoding (UTF-8) and viewport settings for responsiveness.
- Title tag: Sets the title of the HTML document.
- Link tags: Link the
style.cssfile for styling and Font Awesome for the icon library.
- Body section:
- Heading: Displays the game's title.
- Section: Contains the player's scores and dice images.
- Div with class 'first':
- H2 tag: Displays "Score" for the first player.
- H2 tag with id 'p1': Displays the score for the first player (initially empty).
- Image tag with id 'dice1': Displays the dice image for the first player.
- Div with class 'sec': Similar structure as the first player's div, but for the second player.
- Div with class 'first':
- Div with class 'b':
- Button with id 'btn': The "Let's Play" button that triggers the game logic.
- Script tag: Loads the
code.jsfile containing the JavaScript logic.
This file contains the CSS styles for the game. It includes:
- Body styles: Sets the background color, font family, and text alignment.
- Heading styles: Styles the game title.
- Section styles: Sets the layout and styling for the player's score and dice image sections.
- Div styles: Applies styles to the "Score" and "Let's Play" button containers.
- Button styles: Styles the "Let's Play" button with a background color, border radius, padding, and hover effect.
- Image styles: Sets the height and padding for the dice images.
This file contains the JavaScript code for the game logic:
- Event listener: Attaches an event listener to the "Let's Play" button, triggering the game logic when clicked.
- Dice roll function: Generates random numbers between 1 and 6 to represent the dice rolls for both players.
- Update image function: Updates the dice images based on the generated random numbers.
- Determine winner function: Compares the two dice rolls and displays the winner (or a tie) in the console.
To Run the Game:
- Open the
index.htmlfile in a web browser. - Click the "Let's Play" button to start the game.
Features:
- Simple dice game for two players.
- Random dice roll generation.
- Visual display of dice images.
- Determination and display of the winner (or tie).
Further Improvements:
- Add sound effects for dice rolling.
- Implement a scorekeeping system for multiple rounds.
- Add a visual animation for dice rolling.
- Create a user interface for player names and scores.
- Integrate the game with a server for multiplayer functionality.