温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用html5实现乒乓球游戏

发布时间:2021-07-23 22:40:12 来源:亿速云 阅读:164 作者:chen 栏目:web开发

这篇文章主要讲解了“怎么用html5实现乒乓球游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用html5实现乒乓球游戏”吧!

怎么用html5实现乒乓球游戏

演示地址

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

简单介绍

小球可以在方框内部自由运动
可以通过方向键控制黑色砖块上下左右移动去与小球发生碰撞

代码实现

代码如下:


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


难点

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

感谢各位的阅读,以上就是“怎么用html5实现乒乓球游戏”的内容了,经过本文的学习后,相信大家对怎么用html5实现乒乓球游戏这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI