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"/> <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:
|
>>: Docker deploys nginx and mounts folders and file operations
Table of contents Preface: 1. Introduction to Nav...
Introduction Memcached is a distributed caching s...
1. Float: The main purpose is to achieve the effe...
Table of contents 1.sleep function 2. setTimeout ...
Problem phenomenon: [root@localhost ~]# docker im...
One-click execution To install Python 3.8 in a vi...
Recently, I want to build a hadoop test cluster i...
A common suggestion is to create indexes for WHER...
I summarized the previous notes on installing MyS...
Preface Nginx is an HTTP server designed for perf...
Introduction: When I looked at interview question...
Download Nginx image in Docker docker pull nginx ...
After many difficult single-step debugging late a...
need: The backend returns an array object, which ...
Solution Abandon the Linux virtual machine that c...