Open In App

Design Hit the Mouse Game using HTML, CSS and Vanilla Javascript

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

In this article, we are going to create a game in which a mouse comes out from the holes, and we hit the mouse with a hammer to gain points. It is designed using HTML, CSS & Vanilla JavaScript.

HTML Code:

  • First, we create an HTML file (index.html).
  • Now, after creating the HTML file, we are going to give a title to our webpage using the <title> tag. It should be placed between the <head> tag.
  • Then we link the CSS file that provides all the animation effects to our HTML. It is also placed inside the <head> section.
  • Coming to the body section of our HTML code.
    • We have to create a div to give the main heading to our game.
    • In the second div, we place points for our game.
    • In the third div which is the most interesting one, we place 5 holes and assign a particular class to them.
    • In the next one, we place the 2 buttons to start and stop our game as per the user interest.
    • In the final div, we place a hammer image, which later on we convert to a cursor.
  • At the end of our body section, we place a link to our JS file in the <script> tag.

CSS Code: CSS is used to give different types of animations and effects to our HTML page so that it looks interactive to all users. In CSS, we have to remember the following points -

  • Restore all the browser effects.
  • Use classes and IDs to give effects to HTML elements.
  • Use @keyframes{} to give the animation to HTML elements.

JavaScript Code: In this section, we write the code for -

  1. Hitting effects of the hammer.
  2. Changing the cursor to the hammer.
  3. Start/stop our game.
  4. Calculating the number of hits
HTML
CSS JavaScript

Steps to play the Game:

  • Click on the Start button to play the game.
  • After clicking the start button, the object comes out from the hole.
  • Hit the mouse over the object to gain more and more points.
  • Click on the Stop button to pause your game.

Output


Next Article

Similar Reads