Don’t forget to play games on Chinese Valentine’s Day~ The following editor has prepared a Valentine’s Day gift for everyone, a pure JavaScript imitating WeChat masturbation game to share with lovers all over the world.
First, let me show you the renderings:
View the demo Source code download
Pure JavaScript imitates the WeChat masturbation game and uses it as a reference for making web mini-games. The interface design is vertically shaped to imitate the mobile phone screen style, and the game effect is smooth. It has score statistics. The JS package class inside includes creating aircraft classes, aircraft movement behavior control, creating bullet classes, generating random numbers between min and max, and judging whether the own aircraft moves out of the boundary. If it moves out of the boundary, cancel the mousemove. event, on the contrary, add the mousemove event, add a pause event for the continue button of the pause interface, create the enemy aircraft class, determine the collision, and complete the initialization of the interface, one for the enemy small aircraft and one for our aircraft.
//Get the main interface
var mainDiv=document.getElementById("maindiv");
//Get the start interface
var startdiv=document.getElementById("startdiv");
// Obtain in-game score display interface
var scorediv=document.getElementById("scorediv");
//Get points interface
var scorelabel=document.getElementById("label");
//Get the pause interface
var suspenddiv=document.getElementById("suspenddiv");
//Get the game end interface
var enddiv=document.getElementById("enddiv");
//Get the score statistics interface after the game ends
var planscore=document.getElementById("planscore");
//Initialize score
var scores=; <span style="color: #0000ff">/* 创建飞机类 */ </span>function plan(hp,X,Y,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc){ this.planX=X; this.planY=Y; this.imagenode=null; this.planhp=hp; this.planscore=score; this.plansizeX=sizeX; this.plansizeY=sizeY; this.planboomimage=boomimage; this.planisdie=false; this.plandietimes=; this.plandietime=dietime; this.plansudu=sudu; <span style="color: #0000ff">//行为 /* 移动行为 */ </span> this.planmove=function(){ if(scores<=){ this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+"px"; } else if(scores>&&scores<=){ this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu++"px"; } else if(scores>&&scores<=){ this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu++"px"; } else if(scores>&&scores<=){ this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu++"px"; } else if(scores>&&scores<=){ this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu++"px"; } else{ this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu++"px"; } } this.init=function(){ this.imagenode=document.Element("img"); this.imagenode.style.left=this.planX+"px"; this.imagenode.style.top=this.planY+"px"; this.imagenode.src=imagesrc; mainDiv.appendChild(this.imagenode); } this.init(); } <span style="color: #0000ff">/* 创建子弹类 */ </span>function bullet(X,Y,sizeX,sizeY,imagesrc){ this.bulletX=X; this.bulletY=Y; this.bulletimage=null; this.bulletattach=; this.bulletsizeX=sizeX; this.bulletsizeY=sizeY; <span style="color: #0000ff">//行为 /* 移动行为 */ </span> this.bulletmove=function(){ this.bulletimage.style.top=this.bulletimage.offsetTop-+"px"; } this.init=function(){ this.bulletimage=document.Element("img"); this.bulletimage.style.left= this.bulletX+"px"; this.bulletimage.style.top= this.bulletY+"px"; this.bulletimage.src=imagesrc; mainDiv.appendChild(this.bulletimage); } this.init(); } <span style="color: #0000ff">/* 创建单行子弹类 */ </span>function oddbullet(X,Y){ bullet.call(this,X,Y,,,"image/bullet.png"); } <span style="color: #0000ff">/* 创建敌机类 */ </span> function enemy(hp,a,b,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc){ plan.call(this,hp,random(a,b),-,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc); } <span style="color: #0000ff">//产生min到max之间的随机数 </span> function random(min,max){ return Math.floor(min+Math.random()*(max-min)); } <span style="color: #0000ff">/* 创建本方飞机类 */ </span> function ourplan(X,Y){ var imagesrc="image/我的飞机.gif"; plan.call(this,,X,Y,,,,,,"image/本方飞机爆炸.gif",imagesrc); this.imagenode.setAttribute('id','ourplan'); } <span style="color: #0000ff">/* 创建本方飞机 */ </span> var selfplan=new ourplan(,); <span style="color: #0000ff">//移动事件 </span> var ourPlan=document.getElementById('ourplan'); var yidong=function(){ var oevent=window.event||arguments[]; var chufa=oevent.srcElement||oevent.target; var selfplanX=oevent.clientX-; var selfplanY=oevent.clientY; ourPlan.style.left=selfplanX-selfplan.plansizeX/+"px"; ourPlan.style.top=selfplanY-selfplan.plansizeY/+"px"; // document.getElementsByTagName('img')[].style.left=selfplanX-selfplan.plansizeX/+"px"; // document.getElementsByTagName('img')[]..style.top=selfplanY-selfplan.plansizeY/+"px"; } <span style="color: #0000ff">/* 暂停事件 */ </span>var number=; var zanting=function(){ if(number==){ suspenddiv.style.display="block"; if(document.removeEventListener){ mainDiv.removeEventListener("mousemove",yidong,true); bodyobj.removeEventListener("mousemove",bianjie,true); } else if(document.detachEvent){ mainDiv.detachEvent("onmousemove",yidong); bodyobj.detachEvent("onmousemove",bianjie); } clearInterval(set); number=; } else{ suspenddiv.style.display="none"; if(document.addEventListener){ mainDiv.addEventListener("mousemove",yidong,true); bodyobj.addEventListener("mousemove",bianjie,true); } else if(document.attachEvent){ mainDiv.attachEvent("onmousemove",yidong); bodyobj.attachEvent("onmousemove",bianjie); } set=setInterval(start,); number=; } } <span style="color: #0000ff">//判断本方飞机是否移出边界,如果移出边界,则取消mousemove事件,反之加上mousemove事件 </span>var bianjie=function(){ var oevent=window.event||arguments[]; var bodyobjX=oevent.clientX; var bodyobjY=oevent.clientY; if(bodyobjX<||bodyobjX>||bodyobjY<||bodyobjY>){ if(document.removeEventListener){ mainDiv.removeEventListener("mousemove",yidong,true); } else if(document.detachEvent){ mainDiv.detachEvent("onmousemove",yidong); } } else{ if(document.addEventListener){ mainDiv.addEventListener("mousemove",yidong,true); } else if(document.attachEvent){ mainDiv.attachEvent("nomousemove",yidong); } } }
//Pause interface restart event
//function chongxinkaishi(){ // location.reload(true); // startdiv.style.display="none"; // maindiv.style.display="block"; //} var bodyobj=document.getElementsByTagName("body")[]; if(document.addEventListener){ <span style="color: #0000ff">//为本方飞机添加移动和暂停 </span> mainDiv.addEventListener("mousemove",yidong,true); <span style="color: #0000ff">//为本方飞机添加暂停事件 </span> selfplan.imagenode.addEventListener("click",zanting,true); <span style="color: #0000ff">//为body添加判断本方飞机移出边界事件 </span> bodyobj.addEventListener("mousemove",bianjie,true); <span style="color: #0000ff">//为暂停界面的继续按钮添加暂停事件 </span> suspenddiv.getElementsByTagName("button")[].addEventListener("click",zanting,true); // suspenddiv.getElementsByTagName("button")[].addEventListener("click",chongxinkaishi,true); <span style="color: #0000ff">//为暂停界面的返回主页按钮添加事件 </span> suspenddiv.getElementsByTagName("button")[].addEventListener("click",jixu,true); } else if(document.attachEvent){ <span style="color: #0000ff">//为本方飞机添加移动 </span> mainDiv.attachEvent("onmousemove",yidong); <span style="color: #0000ff">//为本方飞机添加暂停事件 </span> selfplan.imagenode.attachEvent("onclick",zanting); <span style="color: #0000ff"> //为body添加判断本方飞机移出边界事件 </span> bodyobj.attachEvent("onmousemove",bianjie); <span style="color: #0000ff">//为暂停界面的继续按钮添加暂停事件 </span> suspenddiv.getElementsByTagName("button")[].attachEvent("onclick",zanting); // suspenddiv.getElementsByTagName("button")[].attachEvent("click",chongxinkaishi,true); <span style="color: #0000ff">//为暂停界面的返回主页按钮添加事件</span> suspenddiv.getElementsByTagName("button")[].attachEvent("click",jixu,true); } <span style="color: #0000ff">//初始化隐藏本方飞机 </span>selfplan.imagenode.style.display="none"; <span style="color: #0000ff">/* 敌机对象数组 */ </span>var enemys=[]; <span style="color: #0000ff">/* 子弹对象数组 */ </span>var bullets=[]; var mark=; var mark=; var backgroundPositionY=; <span style="color: #0000ff">/* 开始函数 */ </span>function start(){ mainDiv.style.backgroundPositionY=backgroundPositionY+"px"; backgroundPositionY+=.; if(backgroundPositionY==){ backgroundPositionY=; } mark++; <span style="color: #0000ff">/* 创建敌方飞机 */ </span> if(mark==){ mark++; <span style="color: #0000ff"> //中飞机 </span> if(mark%==){ enemys.push(new enemy(,,,,,,,random(,),"image/中飞机爆炸.gif","image/enemy_fly_.png")); } <span style="color: #0000ff"> //大飞机 </span> if(mark==){ enemys.push(new enemy(,,,,,,,,"image/大飞机爆炸.gif","image/enemy_fly_.png")); mark=; } <span style="color: #0000ff">//小飞机 </span> else{ enemys.push(new enemy(,,,,,,,random(,),"image/小飞机爆炸.gif","image/enemy_fly_.png")); } mark=; } <span style="color: #0000ff">/* 移动敌方飞机 */ </span> var enemyslen=enemys.length; for(var i=;i<enemyslen;i++){ if(enemys[i].planisdie!=true){ enemys[i].planmove(); } <span style="color: #0000ff">/* 如果敌机超出边界,删除敌机 */ </span> if(enemys[i].imagenode.offsetTop>){ mainDiv.removeChild(enemys[i].imagenode); enemys.splice(i,); enemyslen--; } <span style="color: #0000ff">//当敌机死亡标记为true时,经过一段时间后清除敌机 </span> if(enemys[i].planisdie==true){ enemys[i].plandietimes+=; if(enemys[i].plandietimes==enemys[i].plandietime){ mainDiv.removeChild(enemys[i].imagenode); enemys.splice(i,); enemyslen--; } } } <span style="color: #0000ff">/* 创建子弹 */ </span> if(mark%==){ bullets.push(new oddbullet(parseInt(selfplan.imagenode.style.left)+,parseInt(selfplan.imagenode.style.top)-)); } <span style="color: #0000ff">/* 移动子弹 */ </span> var bulletslen=bullets.length; for(var i=;i<bulletslen;i++){ bullets[i].bulletmove(); <span style="color: #0000ff">/* 如果子弹超出边界,删除子弹 */ </span> if(bullets[i].bulletimage.offsetTop<){ mainDiv.removeChild(bullets[i].bulletimage); bullets.splice(i,); bulletslen--; } } <span style="color: #0000ff">/* 碰撞判断 */ </span> for(var k=;k<bulletslen;k++){ for(var j=;j<enemyslen;j++){ <span style="color: #0000ff">//判断碰撞本方飞机</span> if(enemys[j].planisdie==false){ if(enemys[j].imagenode.offsetLeft+enemys[j].plansizeX>=selfplan.imagenode.offsetLeft&&enemys[j].imagenode.offsetLeft<=selfplan.imagenode.offsetLeft+selfplan.plansizeX){ if(enemys[j].imagenode.offsetTop+enemys[j].plansizeY>=selfplan.imagenode.offsetTop+&&enemys[j].imagenode.offsetTop<=selfplan.imagenode.offsetTop-+selfplan.plansizeY){ <span style="color: #0000ff">//碰撞本方飞机,游戏结束,统计分数</span> selfplan.imagenode.src="image/本方飞机爆炸.gif"; enddiv.style.display="block"; planscore.innerHTML=scores; if(document.removeEventListener){ mainDiv.removeEventListener("mousemove",yidong,true); bodyobj.removeEventListener("mousemove",bianjie,true); } else if(document.detachEvent){
The above code is simple. It uses pure javascript to imitate the WeChat masturbation game. There were many methods at that time. Everyone is welcome to share it.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software