The web pinball game implemented using javeScript objects and methods is for your reference. The specific content is as follows <!DOCTYPE html> <html> <head> <tilie>Huhuhahei's Web Pinball</title> </head> <body> <canvas id="canvas"width="400"height="400"></canvas> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> <script> var canv = document.getElementById("canvas"); var ctx = canv.getContext("2d"); //Create a ball object var ball={ x: 100, y: 100, xSpeed: -5, ySpeed: -5 }; //Define the method for drawing the ball ball.draw=function(){ ctx.beginPath(); ctx.arc(this.x,this.y,10,0,Math.PI*2,false); ctx.fill(); }; //Define the method of ball movement ball.move=function(){ this.x =this.x+this.xSpeed; this.y =this.y+this.ySpeed; }; //Boundary judgment ball.checkCanvas=function(panelStart,panelEnd) { if(this.x<0||this.x>400) this.xSpeed=-this.xSpeed; if(this.y<0) this.ySpeed=-this.ySpeed; if(this.y>390){ if(this.x>panelStart && this.x<panelEnd) this.ySpeed=-this.ySpeed; else{ alert("GAME OVER!!!"); this.x = 50; this.y=100; } } }; //The timing function makes the ball move setInterval(function() { ctx.clearRect(0,0,400,400); ball.draw(); panel.draw(); ball.move(); ball.checkCanvas(panel.x,panel.x+panel.xSize); ctx.strokeRect(0,0,400,400); },30); // Timing function, execute the code in the curly brackets every 30ms; //Create the baffle object; var panel = { x:200, y:390, xSize: 50, ySize: 5 }; //Baffle movement method panel.draw=function(){ ctx.fillRect(this.x,this.y,this.xSize,this.ySize); }; //Use jQuery to implement key interaction; $("body").keydown(function(event) { console.log(event.keyCode); if(event.keyCode==37){ panel.x=panel.x-5; if(panel.x<0){ panel.x=0; } } if(event.keyCode==39){ panel.x=panel.x+5; if(panel.x>400-50){ panel.x=350; } } } ); </script> </body> </html> 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 tutorial on replacing mysql8.0.17 in windows10
>>: Understand the implementation of Nginx location matching in one article
By default, /etc/default/docker configuration wil...
1. In IE, if relative positioning is used, that is...
When we create a page, especially a homepage, we ...
1. Install Apache # yum install -y httpd httpd-de...
The docker exec command can execute commands in a...
Keepalived+Nginx+Tomcat to achieve high availabil...
1. Backup source list The default source of Ubunt...
Environment: CentOS 7.1.1503 Minimum Installation...
Table of contents How to deploy MySQL service usi...
Optimize queries Use the Explain statement to ana...
1. MySQL gets the current date and time function ...
[mysql] replace usage (replace part of the conten...
Table of contents 1. First install echarts in the...
Mysql is a mainstream open source relational data...
When the above settings are used in IE8 and Chrome...