濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > html5 乒乓球(碰撞檢測)實(shí)例二

html5 乒乓球(碰撞檢測)實(shí)例二

熱門標(biāo)簽:小朱地圖標(biāo)注 怎么做百度地圖標(biāo)注 智能芯電話機(jī)器人 咸陽穩(wěn)定外呼系統(tǒng)軟件 400開頭的電話好申請(qǐng)不 四川移動(dòng)電銷外呼客戶管理系統(tǒng) 臨海地圖標(biāo)注app 地圖標(biāo)注柱狀圖 百度地圖標(biāo)注為什么總是封號(hào)

演示地址

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

簡單介紹

小球可以在方框內(nèi)部自由運(yùn)動(dòng)
可以通過方向鍵控制黑色磚塊上下左右移動(dòng)去與小球發(fā)生碰撞

代碼實(shí)現(xiàn)

復(fù)制代碼
代碼如下:

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<title>乒乓球游戲</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
var ctx;
var canvas;
var ball_x=10;
var ball_y=10;
var ball_radius=10;
var ball_vx=10;
var ball_vy=8;
var wall_x=30;
var wall_y=40;
var wall_width=30;
var wall_height=60;
var box_x=0;
var box_y=0;
var box_width=300;
var box_height=300;
var bound_left=box_x+ball_radius;
var bound_right=box_x+box_width-ball_radius;
var bound_top=box_y+ball_radius;
var bound_bottom=box_y+box_height-ball_radius;
var unit=10;
function intersect(sx, sy, fx, fy, cx, cy, rad)
{
var dx;
var dy;
var t;
var rt;
dx = fx - sx;
dy = fy - sy;
t = 0.0 - (((sx - cx) * dx + (sy - cy) * dy) / (dx * dx + dy * dy));
if (t < 0.0)
{
t = 0.0;
}
else if (t > 1.0)
t = 1.0;
var dx1 = (sx + t * dx) - cx;
var dy1 = (sy + t * dy) - cy;
var rt = dx1 * dx1 + dy1 * dy1;
if (rt < rad * rad)
return true;
else
return false;
}
function move_ball()
{
ball_x=ball_x+ball_vx;
ball_y=ball_y+ball_vy;
if(ball_x<bound_left)
{
ball_x=bound_left;
ball_vx=-ball_vx;
}
if(ball_x>bound_right)
{
ball_x=bound_right;
ball_vx=-ball_vx;
}
if(ball_y<bound_top)
{
ball_y=bound_top;
ball_vy=-ball_vy;
}
if(ball_y>bound_bottom)
{
ball_y=bound_bottom;
ball_vy=-ball_vy;
}
//撞到上邊
if(intersect(wall_x,wall_y,wall_x+wall_width,wall_y+wall_height,ball_x,ball_y,ball_radius))
{
ball_y=wall_y-ball_radius;
ball_vy=-ball_vy;
}
//撞到左邊
if(intersect(wall_x,wall_y,wall_x,wall_y+wall_height,ball_x,ball_y,ball_radius))
{
ball_x=wall_x-ball_radius;
ball_vx=-ball_vx;
}
//撞到右邊
if(intersect(wall_x+wall_width,wall_y,wall_x+wall_width,wall_y+wall_height,ball_x,ball_y,ball_radius))
{
ball_x=wall_x+wall_width+ball_radius;
ball_vx=-ball_vx;
}
//撞到下邊
if(intersect(wall_x,wall_y+wall_height,wall_x+wall_width,wall_y+wall_height,ball_x,ball_y,ball_radius))
{
ball_y=wall_y+wall_height+ball_radius;
ball_vy=-ball_vy;
}
}
function move_wall(ev)
{
var keyCode;
if(event==null)
{
keyCode=window.event.keyCode;
window.event.preventDefault();
}
else
{
keyCode=event.keyCode;
event.preventDefault();
}
switch(keyCode)
{
case 37://left;
wall_x-=unit;
if(wall_x<bound_left)
wall_x=bound_left;
break;
case 38://up
wall_y-=unit;
if(wall_y<bound_top)
wall_y=bound_top;
break;
case 39://right
wall_x+=unit;
if(wall_x+wall_width>bound_right)
wall_x=bound_right-wall_width;
break;
case 40://down
wall_y+=unit;
if(wall_y+wall_height>bound_bottom)
wall_y=bound_bottom-wall_height;
break;
default:
break;
}
}
function draw_all()
{
ctx.beginPath();
ctx.clearRect(box_x,box_y,box_width,box_height);
ctx.fillStyle="rgb(255,0,0)";
//ctx.lineWidth=ball_radius;
ctx.arc(ball_x,ball_y,ball_radius,0,Math.PI*2,true);
ctx.fill();//note
ctx.fillStyle="rgb(0,0,0)";
ctx.fillRect(wall_x,wall_y,wall_width,wall_height);
ctx.strokeRect(box_x,box_y,box_width,box_height);
}
function init()
{
canvas=document.getElementById('canvas');
ctx=canvas.getContext('2d');
draw_all();
setInterval(draw_all,100);
setInterval(move_ball,50);
window.addEventListener('keydown',move_wall,false);//note
}
</script>
</head>
<body onLoad="init();">
<canvas id="canvas" width="300" height="300"></canvas>
</body>
</html>

難點(diǎn)

小球和磚塊的碰撞檢測以及碰撞處理
將磚塊分解為4條線段
分別對(duì)小球和每條線段進(jìn)行碰撞檢測。

小球和線段的碰撞檢測在另一篇文章https://www.jb51.net/html5/93997.html中有介紹。

標(biāo)簽:山南 平頂山 黃石 南平 黃石 公主嶺 陜西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5 乒乓球(碰撞檢測)實(shí)例二》,本文關(guān)鍵詞  html5,乒乓球,碰撞,檢測,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《html5 乒乓球(碰撞檢測)實(shí)例二》相關(guān)的同類信息!
  • 本頁收集關(guān)于html5 乒乓球(碰撞檢測)實(shí)例二的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    共和县| 峨眉山市| 吴川市| 紫金县| 德钦县| 通海县| 全州县| 寿宁县| 临朐县| 定州市| 天柱县| 金山区| 来宾市| 石河子市| 梁山县| 麻栗坡县| 西乌珠穆沁旗| 加查县| 堆龙德庆县| 巴彦县| 宁安市| 松溪县| 美姑县| 大关县| 噶尔县| 房产| 禄丰县| 霸州市| 庆阳市| 连南| 宁陵县| 颍上县| 华亭县| 桓台县| 罗甸县| 响水县| 屏东县| 许昌县| 中山市| 雅安市| 乐山市|