Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make a simple billiard game with HTML5

Shulou Source: shulou.com Published: 2022-06-01 06:56:52 09月14日 Update

In this article, the editor introduces in detail "how to make a simple billiard game with HTML5". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to make a simple billiard game with HTML5" can help you solve your doubts.

Billiard game

There are only two categories in the whole billiard game, one is the ball and the other is the auxiliary line of sight. If you want to make the game more complex, you can also abstract a shape class that can be used to detect the collision of the ball with the corner and the goal. I do this game to take the simplest wall collision detection, so there is no ball and irregular shape collision detection, if you want to play more complex collisions, can poke about simple collision detection Cen an big talk is very good. Okay, let's take it one step at a time:

[ball]

Paste the code first:

[/ code] var Ball = function (x, y, ismine) {

This.x = x

This.y = y

This.ismine = ismine

This.oldx = x

This.oldy = y

This.vx = 0

This.vy = 0

This.radius = ballRadius

This.inhole = false;this.moving = true

}

Ball.prototype = {

Constructor:Ball

_ paint:function () {

Var b = this.ismine?document.getElementById ("wb"): document.getElementById ("yb")

If (b.complete) {

Ctx.drawImage (b, this.x-this.radius, this.y-this.radius, 2*this.radius, 2*this.radius)

}

Else {

B.onload = function () {

Ctx.drawImage (b, this.x-this.radius, this.y-this.radius, 2*this.radius, 2*this.radius)

}

}

}

_ run:function (t) {

This.oldx = this.x

This.oldy = this.y

This.vx = Math.abs (this.vx) 0? This.vx-mcl*t: this.vx+mcl*t)

This.vy = Math.abs (this.vy) 0? This.vy-mcl*t: this.vy+mcl*t)

/ / this.vx + = this.vx > 0?-mcl*t: mcl*t

/ / this.vy + = this.vy > 0?-mcl*t: mcl*t

This.x + = t * this.vx * pxpm

This.y + = t * this.vy * pxpm

If ((this.x490)) {

This.inhole = true

If (this.ismine) {

Var that = this

SetTimeout (function () {

That.x = 202

That.y = canvas.height/2

That.vx = 0

That.vy = 0

That.inhole = false

}, 500)

}

Else {

Document.getElementById ("shotNum") [xss_clean] = parseInt (document.getElementById ("shotNum") [xss_clean]) + 1

}

}

Else {

If (this.y > canvas.height-(ballRadius+tbw) | | this.y

< (ballRadius+tbw)){ this.y = this.y < (ballRadius+tbw) ? (ballRadius+tbw) : (canvas.height - (ballRadius+tbw)); this.derectionY = !this.derectionY; this.vy = -this.vy*0.6; } if(this.x >

Canvas.width-(ballRadius+tbw) | | this.x < (ballRadius+tbw)) {

This.x = this.x < (ballRadius+tbw)? (ballRadius+tbw): (canvas.width-(ballRadius+tbw))

This.derectionX =! this.derectionX

This.vx =-this.vx*0.6

}

}

This._paint ()

If (Math.abs (this.vx))

Tags: Ball collision position speed code mouse that is dotted line detection increment auxiliary two billiards production complexity strength animation article method time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker vpn Shulou Information Linux