js实现方块的碰撞检测
2024-09-05 12:47:08
文章地址:https://www.cnblogs.com/sandraryan/
个人感觉。方块的碰撞检测比圆形麻烦~~
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
background-color: lightyellow;
} .wrap {
width: 50px;
height: 50px;
background-color: red;
position: fixed;
top: 0;
left: 0;
} .box {
position: fixed;
width: 50px;
height: 50px;
}
</style>
</head> <body>
<div class="wrap"></div>
<div class="box box1">11</div>
<div class="box box2">22</div>
<div class="box box3">33</div>
<div class="box box4">44</div>
<div class="box box5"></div>
<div class="box box6"></div>
<div class="box box7"></div>
<div class="box box8"></div>
<div class="box box9"></div>
<div class="box box10"></div>
<div class="box box11"></div>
<div class="box box12"></div>
<div class="box box13"></div>
<div class="box box14"></div>
<div class="box box15"></div>
<div class="box box16"></div>
<div class="box box17"></div>
<div class="box box18"></div>
<div class="box box19"></div>
<div class="box box20"></div>
<script>
let wrap = document.querySelector('.wrap');
let box = document.querySelectorAll('.box');
// 封装随机数函数
function rn(a, b) {
return Math.round(Math.random() * (a - b) + b);
} // 循环遍历要被检测的盒子(被检测的盒子也可以用js生成)给他们设置位置和颜色
for (let i = 0; i < box.length; i++) {
// 随机生成box的位置
box[i].style.left = rn(0, 1200) + 'px';
box[i].style.top = rn(0, 500) + 'px';
// 设置随机颜色
let opc = Math.random().toFixed(1);
// 要用box[i].color接受颜色,本来用变量接受,但是在后面每碰撞成功的元素恢复原有颜色时,所以box都会变成相同的颜色
box[i].color = 'rgba(' + rn(0, 254) + ',' + rn(0, 254) + ',' + rn(0, 254) + ',' + opc + ')';
box[i].style.backgroundColor = box[i].color;
}
// 文档注册鼠标移动事件
document.onmousemove = function (ev) {
let e = ev || window.event;
// 获取移动盒子的宽高(相等
// 用变量long接受wrap的宽高,(正方形)
let long = wrap.offsetWidth;
// x y 当前窗口宽高-盒子宽高/2
let x = e.clientX - long / 2;
let y = e.clientY - long / 2;
wrap.style.left = x + 'px';
wrap.style.top = y + 'px';
// 判断上下左右四个方向是否碰撞
for (let j = 0; j < box.length; j++) {
// 顶部:wrap高度 + wrap top值 >= 检测盒子top
let t = long + wrap.offsetTop >= box[j].offsetTop;
let b = wrap.offsetTop <= box[j].offsetHeight + box[j].offsetTop;
let l = long + wrap.offsetLeft >= box[j].offsetLeft;
let r = wrap.offsetLeft <= box[j].offsetLeft + box[j].offsetWidth;
// 如果在四个方向任意方向碰撞成功,变色
if (l && t && r && b) {
box[j].style.backgroundColor = 'red';
} else {
box[j].style.backgroundColor = box[j].color;
}
}
}
</script>
</body> </html>
最新文章
- 字符串 中的split 与数组中的join
- MongoDB-集群搭建
- 【转】轮询、长轮询、iframe长连接、web socket
- windbg sos版本不匹配问题解决
- c#中$.ajax的使用
- The following classes could not be found: - android.support.v7.internal.widget.ActionBarOverlayLayou解决方案
- WINDOWS下安装PHP7出现PHP-CGI无法启动
- SpringBoot(四)SpringBoot中lombok使用
- B20J_3231_[SDOI2014]旅行_树链剖分+线段树
- 通过 URL 打开 Activity
- sw-stream
- K - Super A^B mod C
- Jmeter(二十九)Jmeter-Question之“Ant集成报告模板优化”
- POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)
- JavaOperator小框架制作【精品博客】
- Web API: Security: Authentication and Authority
- MySQL命令行查询结果中文显示乱码
- PHP面试系列 之Linux(五)---- 案例
- Redis+Lua实现限流
- windows7 端口查看以及杀死进程释放端口