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

How to use Navicat to operate MySQL

Table of contents Preface: 1. Introduction to Nav...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...

Use of JavaScript sleep function

Table of contents 1.sleep function 2. setTimeout ...

Solve the problem of docker's tls (ssl) certificate expiration

Problem phenomenon: [root@localhost ~]# docker im...

Use shell script to install python3.8 environment in CentOS7 (recommended)

One-click execution To install Python 3.8 in a vi...

Detailed tutorial on deploying Hadoop cluster using Docker

Recently, I want to build a hadoop test cluster i...

Advantages of MySQL covering indexes

A common suggestion is to create indexes for WHER...

MySQL 5.7.17 winx64 installation and configuration graphic tutorial

I summarized the previous notes on installing MyS...

How to use environment variables in nginx configuration file

Preface Nginx is an HTTP server designed for perf...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Beginners understand MySQL deadlock problem from source code

After many difficult single-step debugging late a...

Vue+element implements drop-down menu with local search function example

need: The backend returns an array object, which ...

Solve the problem that Docker cannot ping the host machine under Mac

Solution Abandon the Linux virtual machine that c...