Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make a simple billiard game with HTML5

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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))

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report