search
HomeWeb Front-endH5 TutorialHTML5 table tennis (collision detection) example two_html5 tutorial skills

Demo address

http://koking.8u.hanmandarin.com/html5/1.html

A brief introduction

The ball can move freely inside the box
You can use the direction keys to control the black bricks to move up, down, left and right to collide with the ball

Code implementation

Copy code
The code is as follows:





乒乓球游戏

<script> <br />var ctx; <br />var canvas; <br />var ball_x=10; <br />var ball_y=10; <br />var ball_radius=10; <br />var ball_vx=10; <br />var ball_vy=8; <br />var wall_x=30; <br />var wall_y=40; <br />var wall_width=30; <br />var wall_height=60; <br />var box_x=0; <br />var box_y=0; <br />var box_width=300; <br />var box_height=300; <br />var bound_left=box_x ball_radius; <br />var bound_right=box_x box_width-ball_radius; <br />var bound_top=box_y ball_radius; <br />var bound_bottom=box_y box_height-ball_radius; <br />var unit=10; <br />function intersect(sx, sy, fx, fy, cx, cy, rad) <br />{ <br />var dx; <br />var dy; <br />var t; <br />var rt; <br />dx = fx - sx; <br />dy = fy - sy; <br />t = 0.0 - (((sx - cx) * dx (sy - cy) * dy) / (dx * dx dy * dy)); <br />if (t < 0.0) <br />{ <br />t = 0.0; <br />} <br />else if (t > 1.0) <br />t = 1.0; <br />var dx1 = (sx t * dx) - cx; <br />var dy1 = (sy t * dy) - cy; <br />var rt = dx1 * dx1 dy1 * dy1; <br />if (rt < rad * rad) <br />return true; <br />else <br />return false; <br />} <br />function move_ball() <br />{ <br />ball_x=ball_x ball_vx; <br />ball_y=ball_y ball_vy; <br />if(ball_x<bound_left) <br />{ <br />ball_x=bound_left; <br />ball_vx=-ball_vx; <br />} <br />if(ball_x>bound_right) <br />{ <br />ball_x=bound_right; <br />ball_vx=-ball_vx; <br />} <br />if(ball_y<bound_top) <br />{ <br />ball_y=bound_top; <br />ball_vy=-ball_vy; <br />} <br />if(ball_y>bound_bottom) <br />{ <br />ball_y=bound_bottom; <br />ball_vy=-ball_vy; <br />} <br />//撞到上边 <br />if(intersect(wall_x,wall_y,wall_x wall_width,wall_y wall_height,ball_x,ball_y,ball_radius)) <br />{ <br />ball_y=wall_y-ball_radius; <br />ball_vy=-ball_vy; <br />} <br />//撞到左边 <br />if(intersect(wall_x,wall_y,wall_x,wall_y wall_height,ball_x,ball_y,ball_radius)) <br />{ <br />ball_x=wall_x-ball_radius; <br />ball_vx=-ball_vx; <br />} <br />//撞到右边 <br />if(intersect(wall_x wall_width,wall_y,wall_x wall_width,wall_y wall_height,ball_x,ball_y,ball_radius)) <br />{ <br />ball_x=wall_x wall_width ball_radius; <br />ball_vx=-ball_vx; <br />} <br />//撞到下边 <br />if(intersect(wall_x,wall_y wall_height,wall_x wall_width,wall_y wall_height,ball_x,ball_y,ball_radius)) <br />{ <br />ball_y=wall_y wall_height ball_radius; <br />ball_vy=-ball_vy; <br />} <br />} <br />function move_wall(ev) <br />{ <br />var keyCode; <br />if(event==null) <br />{ <br />keyCode=window.event.keyCode; <br />window.event.preventDefault(); <br />} <br />else <br />{ <br />keyCode=event.keyCode; <br />event.preventDefault(); <br />} <br />switch(keyCode) <br />{ <br />case 37://left; <br />wall_x-=unit; <br />if(wall_x<bound_left) <br />wall_x=bound_left; <br />break; <br />case 38://up <br />wall_y-=unit; <br />if(wall_y<bound_top) <br />wall_y=bound_top; <br />break; <br />case 39://right <br />wall_x =unit; <br />if(wall_x wall_width>bound_right) <br />wall_x=bound_right-wall_width; <br />break; <br />case 40://down <br />wall_y =unit; <br />if(wall_y wall_height>bound_bottom) <br />wall_y=bound_bottom-wall_height; <br />break; <br />default: <br />break; <br />} <br />} <br />function draw_all() <br />{ <br />ctx.beginPath(); <br />ctx.clearRect(box_x,box_y,box_width,box_height); <br />ctx.fillStyle="rgb(255,0,0)"; <br />//ctx.lineWidth=ball_radius; <br />ctx.arc(ball_x,ball_y,ball_radius,0,Math.PI*2,true); <br />ctx.fill();//note <br />ctx.fillStyle="rgb(0,0,0)"; <br />ctx.fillRect(wall_x,wall_y,wall_width,wall_height); <br />ctx.strokeRect(box_x,box_y,box_width,box_height); <br />} <br />function init() <br />{ <br />canvas=document.getElementById('canvas'); <br />ctx=canvas.getContext('2d'); <br />draw_all(); <br />setInterval(draw_all,100); <br />setInterval(move_ball,50); <br />window.addEventListener('keydown',move_wall,false);//note <br />} <br /></script>






难点

小球和砖块的碰撞检测以及碰撞处理
将砖块分解为4条线段
分别对小球和每条线段进行碰撞检测。

小球和线段的碰撞检测在另一篇文章http://www.jb51.net/html5/93997.html中有介绍。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering Microdata: A Step-by-Step Guide for HTML5Mastering Microdata: A Step-by-Step Guide for HTML5May 14, 2025 am 12:07 AM

MicrodatainHTML5enhancesSEOanduserexperiencebyprovidingstructureddatatosearchengines.1)Useitemscope,itemtype,anditempropattributestomarkupcontentlikeproductsorevents.2)TestmicrodatawithtoolslikeGoogle'sStructuredDataTestingTool.3)ConsiderusingJSON-LD

What's New in HTML5 Forms? Exploring the New Input TypesWhat's New in HTML5 Forms? Exploring the New Input TypesMay 13, 2025 pm 03:45 PM

HTML5introducesnewinputtypesthatenhanceuserexperience,simplifydevelopment,andimproveaccessibility.1)automaticallyvalidatesemailformat.2)optimizesformobilewithanumerickeypad.3)andsimplifydateandtimeinputs,reducingtheneedforcustomsolutions.

Understanding H5: The Meaning and SignificanceUnderstanding H5: The Meaning and SignificanceMay 11, 2025 am 12:19 AM

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

H5: Accessibility and Web Standards ComplianceH5: Accessibility and Web Standards ComplianceMay 10, 2025 am 12:21 AM

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment