Implementation of whack-a-mole game in JavaScript

Implementation of whack-a-mole game in JavaScript

This article shares the specific code for JavaScript to implement the whack-a-mole game for your reference. The specific content is as follows

Game Instructions:

Click the "Start Game" button to randomly generate a mouse in the picture. Click on the mouse to hit it before it disappears. You can get 100 points if you hit it once. If you don't hit the mouse, you will deduct 100 points.

CSS Modules

<style>
    #div0{
     text-align: center;
     width: 1360px;
     height: 600px;
     margin: 60px auto;
     background-image: url("images/bg.jpg");
     position: relative;
    }
    #div_top{
     text-align: left;
     color:brown;
     width: 360px;
     height: 100px;
     position: absolute;
     left: 500px;
    }
    #div_left{
     width: 350px;
     height: 320px;
     position: absolute;
     left: 300px;
     top: 150px;
    }
    #tab_data{
     width:350px;
     height:320px;
    }
    #div_right{
     width: 350px;
     height: 320px;
     position: absolute;
     right: 380px;
     top: 150px;
    }
    #tab{
     width:320px;
     height:320px;
    }
    #tab td{
     background-image:url("images/00.jpg");
     background-repeat:no-repeat;
     background-position:center;
    }
</style>

html module

<div id="div0">
   <div id="div_top">
     Game Instructions:<br/>
    Click the "Start Game" button, and a mouse will be randomly generated in the picture below. Click on the mouse to hit it before it disappears.<br/>
    You will get 100 points if you hit the mouse once, and 100 points will be deducted if you miss. Act quickly and test your reaction and eyesight! <br/>
   </div>
   <div id="div_left">
    <table id="tab_data">
     <tr>
      <th>Game time:</th>
      <td><input type="text" name="text_time" value="1" />minutes</td>
     </tr>
     <tr>
      <th>Countdown:</th>
      <td><input type="text" name="text_CD" value="60" disabled="disabled"/>seconds</td>
     </tr>
     <tr>
      <th>Occurs every:</th>
      <td><input type="text" name="text_hide" value="1" />seconds</td>
     </tr>
     <tr>
      <th>Dwell time:</th>
      <td><input type="text" name="text_show" value="1" />seconds</td>
     </tr>
     <tr>
      <th>Score:</th>
      <td><span id="span_score"></span></td>
     </tr>
     <tr>
      <th colspan="2">
       <input type="button" value="Start the game" id="but_start"/>
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input type="button" value="End Game" id="but_end"/>
      </th>
     </tr>
    </table>
   </div>
   <div id="div_right">
    
   </div>
</div>

js module

<script>
   var collTd=[];//Record all td
   var oTdMouse; //Record the selected td
   //Define the label elements in the variable record page var oButStart, oButEnd;
   var oTextTime,oTextHide,oTimeShow,oTimeCD;
   var oSpanScore;
   //Define variables to record time parameters:
   var iAll,iCD,iShow,iHide,iCount,iGet;
   var iCDId,iRandomId,iShow,iChangeId;
   window.onload = function(){
    //Create table createTable();
    //Assign values ​​to the tag element variables init();
    //Register event for the button oButStart.onclick=startGame;
    oButEnd.onclick=endGame;
   }
   //Start the game function startGame(){
    iAll=parseInt(oTextTime.value)*60;
    iCD=iAll;
    //Execute countdown every 1 second iCDId=window.setInterval(setCD,1000);
    iShow = parseInt (oTextShow.value);
    iHide=parseInt(oTextHide.value);
    iCount=0;
    iGet=0;
    //Randomly display a td every iShow+Hide
    randomId();
    iRandomId=window.setInterval(randomId,(iShow+iHide)*1000);
    oButStart.disabled="disabled";
    oButEnd.disabled="";
   }
   //Random td
   function randomId(){
    iCount++;
    var index = parseInt (Math.random() * collTd.length);
    oTdMouse=collTd[index];
    //Change the background image oTdMouse.style.backgroundImage="url('images/01.jpg')";
    //Wait for the show time to end and then set the background image back iShowId=window.setTimeout("oTdMouse.style.backgroundImage='url(images/00.jpg)';",iShow*1000);
   }
   //Set countdown function setCD(){
    iCD--;
    oTextCD.value=iCD;
    //Record the score every second var message="Total "+iCount+" pieces, hit "+iGet+" pieces!";
    oSpanScore.innerHTML=message.fontcolor("blue").bold();
    if(iCD<=0){
     endGame();
    }
   }
   //End the game function endGame(){
    oButEnd.disabled="disabled";
    oButStart.disabled="";
    window.clearInterval(iCDId);
    window.clearInterval(iRandomId);
   }
   //Assign values ​​to label element variables function init(){
    oButStart=document.getElementById("but_start");
    oButEnd=document.getElementById("but_end");
    
    oTextTime=document.getElementsByName("text_time")[0];
    oTextCD=document.getElementsByName("text_CD")[0];
    oTextHide=document.getElementsByName("text_hide")[0];
    oTextShow = document.getElementsByName("text_show")[0];
    
    oSpanScore = document.getElementById("span_score");
    oTextCD.value=60;
    oButEnd.disabled="disabled";
   }
   //Dynamically generate table function createTable(){
    var oDivRight = document.getElementById("div_right");
    var oTab=document.createElement("table");
    oTab.id="tab";
    for(var i=0;i<6;i++){
     var oTr=document.createElement("tr");
     for(var j=0;j<5;j++){
      var oTd = document.createElement("td");
      oTr.appendChild(oTd);
      collTd.push(oTd);
      //Add click events to all td oTd.onclick=function(){
       if(this==oTdMouse){
        //Judge whether the background image of the current cell is 01.jpg
        if(this.style.backgroundImage.indexOf("01.jpg")!=-1){
         //Get one point iGet++;
         //Change the background image to 02.jpg
         oTdMouse.style.backgroundImage="url('images/02.jpg')";
         //Wait 0.2 seconds to change to 00.jpg
         iChangeId=window.setTimeout("oTdMouse.style.backgroundImage='url(images/00.jpg)';",200);
         var message="Total "+iCount+" pieces, hit "+iGet+" pieces!";
         oSpanScore.innerHTML=message.fontcolor("blue").bold();
        }
        
       }
      }
     }
     oTab.appendChild(oTr);
    }
    oDivRight.appendChild(oTab);
   }
</script>

More interesting classic mini-game implementation topics to share with you:

C++ Classic Mini Games Collection

Python classic games summary

Python Tetris game collection

Play classic JavaScript games

Java classic games collection

JavaScript classic games summary

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the logic flow of whack-a-mole game based on JavaScript+HTML5 (with complete code)
  • JavaScript implements whack-a-mole game
  • js to realize the whack-a-mole game
  • JavaScript whack-a-mole game code explanation
  • Implementing a simple whack-a-mole game in JavaScript

<<:  How to prevent iframe from jumping to the page in HTML and use iframe to embed WeChat web version in the page

>>:  Docker deploys nginx and mounts folders and file operations

Recommend

Vue implements DingTalk's attendance calendar

This article shares the specific code of Vue to i...

Steps for Django to connect to local MySQL database (pycharm)

Step 1: Change DATABASES in setting.py # Configur...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

Detailed explanation of Linux less command examples

less file name View File less file name | grep -n...

CocosCreator Typescript makes Tetris game

Table of contents 1. Introduction 2. Several key ...

Summary of Creating and Using Array Methods in Bash Scripts

Defining an array in Bash There are two ways to c...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

How to create a database in navicat 8 for mysql

When developing a website, you often need to use ...

Native JS to implement drag position preview

This article shares with you a small Demo that ad...

MySQL 5.7.21 winx64 installation and configuration method graphic tutorial

This article summarizes the notes for installing ...

Example of how to quickly build a Redis cluster with Docker

What is Redis Cluster Redis cluster is a distribu...

Detailed explanation of the basic usage of the img image tag in HTML/XHTML

The image tag is used to display an image in a we...

How to use jsonp in vue

Table of contents 1. Introduction 2. Installation...

CSS element hiding principle and display:none and visibility:hidden

1. CSS element hiding <br />In CSS, there ar...