javascript implements web version of pinball game

javascript implements web version of pinball game

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:
  • JS implements a simple brick-breaking pinball game
  • js implements a small pinball game with points
  • Example of using native JS to achieve the effect of multiple balls colliding and rebounding
  • JS realizes the elastic collision effect of the ball
  • Non-html5 implementation of js version of pinball game sample code
  • Native js to realize bouncing ball

<<:  Detailed tutorial on replacing mysql8.0.17 in windows10

>>:  Understand the implementation of Nginx location matching in one article

Recommend

Dealing with the problem of notes details turning gray on web pages

1. In IE, if relative positioning is used, that is...

Native JavaScript to achieve slide effects

When we create a page, especially a homepage, we ...

CentOS7 deployment Flask (Apache, mod_wsgi, Python36, venv)

1. Install Apache # yum install -y httpd httpd-de...

Docker exec executes multiple commands

The docker exec command can execute commands in a...

Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

Keepalived+Nginx+Tomcat to achieve high availabil...

How to modify the sources.list of Ubuntu 18.04 to Alibaba or Tsinghua mirror

1. Backup source list The default source of Ubunt...

How to install openssh from source code in centos 7

Environment: CentOS 7.1.1503 Minimum Installation...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

MySQL database query performance optimization strategy

Optimize queries Use the Explain statement to ana...

Summary of MySQL date and time functions (MySQL 5.X)

1. MySQL gets the current date and time function ...

mysql replace part of the field content and mysql replace function replace()

[mysql] replace usage (replace part of the conten...

Vue+Echart bar chart realizes epidemic data statistics

Table of contents 1. First install echarts in the...

MySQL concurrency control principle knowledge points

Mysql is a mainstream open source relational data...

How to fix the width of table in ie8 and chrome

When the above settings are used in IE8 and Chrome...