HTML5 code sharing for circle bubble game
Function Description:
## Within one minute, Use the left mouse button to circle bubbles on the canvas, where the points of bubbles are 10 (white), 20 (light blue), 30 (yellow), -10 (red), -20 (green), -30 (dark blue), which can be used once Circle multiple bubbles and the total score will be calculated when the countdown is over. This game is based on cnGameJS.
Effect preview:
Implementation analysis:
First, each ball Define a ball class. Since the ball needs to use pictures and has a certain size and movement, this classinherits the sprite class of cnGameJS. In addition to having x and y coordinates, the ball class also has a z coordinate , which is used to make the ball have a visual difference in distance from the player.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
updates, then the movement trajectory of the mouse can be represented by a curve, which It is composed of line segments drawn each time, so we can also say that the curve is a curve composed of multiple line segments connected end to end. Therefore, we can first implement a line segment class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Traverse each line segment, traverse the remaining line segments starting from the next line segment and the next line segment, and determine whether any of them intersects with the starting line segment. If they intersect, prove the curve closed. Note that traversing from the next line segment to the next line segment here is to skip the situation where the line segments are connected end to end. (For example, the first line segment must intersect with the second line segment, so start judging from the third line segment and skip the intersection of adjacent line segments.) The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
In this case, the trajectory is not a strict polygon, because it has extra blue and green parts. So we need to perform a correction operation on the circled polygon to turn it into a real closed polygon:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
index, here are i and j (i
1 2 3 4 |
|
判断小球是否在多边形里,可以转化为判断小球的中点是否在多边形里,这里使用的方法叫射线法,意思是从一点向左发射出一条射线,如果射线和多边形有奇数个交点,则证明点在多边形内部。根据该定理实现的isInside方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
另外需要注意的是,由于射线与多边形相交交点个数是通过射线和多边形的每条边是否相交来判断,所以如果射线通过多边形的顶点,我们得出的结果就是相交了两次(通过顶点使射线与两条边都有相交)。因此我们需要记录判断过的交点,每次判断时检查该交点是否已经出现过,若出现过则不纳入计数,这样就基本实现了判断小球是否在鼠标圈选的多边形区域内。
The above is the detailed content of HTML5 code sharing for circle bubble game. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
