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

Analyzing the four transaction isolation levels in MySQL through examples

Preface In database operations, in order to effec...

Vue+Openlayer uses modify to modify the complete code of the element

Vue+Openlayer uses modify to modify elements. The...

Example code for implementing random roll caller in html

After this roll call device starts calling the ro...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

Getting the creation time of a file under Linux and a practical tutorial

background Sometimes we need to get the creation ...

Advertising skills in the Baidu Union environment (graphic tutorial)

Recently, students from the User Experience Team o...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...

Detailed tutorial on deploying Apollo custom environment with docker-compose

Table of contents What is the Apollo Configuratio...

How to implement data persistence using the vuex third-party package

Purpose: Allow the state data managed in vuex to ...

Basic knowledge of MySQL database

Table of contents 1. Understanding Databases 1.1 ...

Solution for front-end browser font size less than 12px

Preface When I was working on a project recently,...

Public free STUN servers

Public free STUN servers When the SIP terminal us...

js native waterfall flow plug-in production

This article shares the specific code of the js n...

How to control the proportion of Flex child elements on the main axis

background Flex layout achieves alignment and spa...