Open In App

Whack-a-Mole Game using HTML CSS and JavaScript

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Whack-A-Mole is a classic arcade-style game that combines speed and precision. The game is set in a grid of holes, and the objective is to "whack" or hit the moles that randomly pop up from these holes. In this article, we are going to create Whack-a-Mole using HTML, CSS and JavaScript.

Preview Image:

Screenshot-2023-11-28-164214

Prerequisites:

Approach:

  • Start by creating the HTML structure for your Whack-A-Mole game. Use semantic tags like <header>, <main>, and include elements for game information (score, timer) and the game container with mole holes.
  • Style your game using CSS to create an appealing layout and you have to use this mole image.
  • Use JavaScript to add interactivity to your Whack-A-Mole game.
  • Select DOM elements and initialize game variables.
  • Implement core functions for mole appearance (comeout) and handling clicks (handleMoleClick).
  • Create functions to start the game, initiating intervals, and updating UI elements. Implement the end game function, clearing intervals, and resetting the game state.
  • Add event listeners to buttons, ensuring proper states (disabled/enabled).
HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
           content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Whac-A-Mole</title>
</head>

<body>
    <div class="game-info">
        <div id="score">Score: 0</div>
        <div id="timer">Time: 60s</div>
    </div>
    <button id="startButton">Start Game</button>
    <button id="endButton" disabled>End Game</button>
    <div class="game-container">
        <div class="hole" id="hole1"></div>
        <div class="hole" id="hole2"></div>
        <div class="hole" id="hole3"></div>
        <div class="hole" id="hole4"></div>
        <div class="hole" id="hole5"></div>
        <div class="hole" id="hole6"></div>
        <div class="hole" id="hole7"></div>
        <div class="hole" id="hole8"></div>
        <div class="hole" id="hole9"></div>
    </div>
    <script src="script.js"></script>
</body>

</html>
CSS JavaScript

Output:



Next Article

Similar Reads

three90RightbarBannerImg