碰撞检测
2016-07-13 19:22
281 查看
<!DOCTYPE html>
<html>
<head>
<meta
charset="UTF-8">
<title></title>
<style
type="text/css">
.wrap
{
width:
600px;
height:
400px;
border:
1px solid black;
position:
relative;
}
.snake
{
width:
50px;
height:
50px;
background-color: aquamarine;
position:
absolute;
}
.food
{
width:
50px;
height:
50px;
background-color: pink;
position:
absolute;
left:
200px;
top:
200px;
}
</style>
</head>
<body>
<div
class="wrap">
<div
class="snake"></div>
<div
class="food"></div>
</div>
<script
type="text/javascript">
var
wrap = document.querySelector(".wrap");
var
snake = document.querySelector(".snake");
var
food = document.querySelector(".food");
snake.onmousedown
= function() {
var
l = snake.offsetLeft;
var
t = snake.offsetTop;
var
x = event.clientX;
var
y = event.clientY;
// 移动一个小div块
document.onmousemove
= function() {
var
x1 = event.clientX;
var
y1 = event.clientY;
var
chaX = x1 - x;
var
chaY = y1 - y;
// 原来的位置上面加上鼠标移动了多少就动多少,,实现小div移动
snake.style.left
= l + chaX +
"px";
snake.style.top
= t + chaY +
"px";
// 碰撞检测
var
sL = snake.offsetLeft;
var
sT = snake.offsetTop;
var
sW = snake.offsetWidth;
var
sH = snake.offsetHeight;
var
fL = food.offsetLeft;
var
fT = food.offsetTop;
var
fH = food.offsetHeight;
var
fW = food.offsetWidth;
// 上下左右四个方向都判断
var
leftBol = sL + sW
>= fL;
var
rightBol = sL <= fL
+ fW;
var
topBol = sT + sH
>= fT;
var
bottomBol = sT <= fT
+ fH;
if
(leftBol && rightBol &&
topBol && bottomBol) {
food.style.backgroundColor
= "chartreuse";
}else{
food.style.backgroundColor
= "";
}
// if ((snake.offsetLeft - food.offsetWidth <= food.offsetLeft ) && (snake.offsetLeft+ snake.offsetWidth >= food.offsetLeft) && (snake.offsetTop - food.offsetHeight <= food.offsetTop)
&& (snake.offsetTop + snake.offsetHeight >= food.offsetTop)) {
// food.style.backgroundColor = "chartreuse";
// }else{
// food.style.backgroundColor = "";
// }
}
document.onmouseup
= function() {
document.onmousemove
= null;
}
}
</script>
</body>
</html>
<html>
<head>
<meta
charset="UTF-8">
<title></title>
<style
type="text/css">
.wrap
{
width:
600px;
height:
400px;
border:
1px solid black;
position:
relative;
}
.snake
{
width:
50px;
height:
50px;
background-color: aquamarine;
position:
absolute;
}
.food
{
width:
50px;
height:
50px;
background-color: pink;
position:
absolute;
left:
200px;
top:
200px;
}
</style>
</head>
<body>
<div
class="wrap">
<div
class="snake"></div>
<div
class="food"></div>
</div>
<script
type="text/javascript">
var
wrap = document.querySelector(".wrap");
var
snake = document.querySelector(".snake");
var
food = document.querySelector(".food");
snake.onmousedown
= function() {
var
l = snake.offsetLeft;
var
t = snake.offsetTop;
var
x = event.clientX;
var
y = event.clientY;
// 移动一个小div块
document.onmousemove
= function() {
var
x1 = event.clientX;
var
y1 = event.clientY;
var
chaX = x1 - x;
var
chaY = y1 - y;
// 原来的位置上面加上鼠标移动了多少就动多少,,实现小div移动
snake.style.left
= l + chaX +
"px";
snake.style.top
= t + chaY +
"px";
// 碰撞检测
var
sL = snake.offsetLeft;
var
sT = snake.offsetTop;
var
sW = snake.offsetWidth;
var
sH = snake.offsetHeight;
var
fL = food.offsetLeft;
var
fT = food.offsetTop;
var
fH = food.offsetHeight;
var
fW = food.offsetWidth;
// 上下左右四个方向都判断
var
leftBol = sL + sW
>= fL;
var
rightBol = sL <= fL
+ fW;
var
topBol = sT + sH
>= fT;
var
bottomBol = sT <= fT
+ fH;
if
(leftBol && rightBol &&
topBol && bottomBol) {
food.style.backgroundColor
= "chartreuse";
}else{
food.style.backgroundColor
= "";
}
// if ((snake.offsetLeft - food.offsetWidth <= food.offsetLeft ) && (snake.offsetLeft+ snake.offsetWidth >= food.offsetLeft) && (snake.offsetTop - food.offsetHeight <= food.offsetTop)
&& (snake.offsetTop + snake.offsetHeight >= food.offsetTop)) {
// food.style.backgroundColor = "chartreuse";
// }else{
// food.style.backgroundColor = "";
// }
}
document.onmouseup
= function() {
document.onmousemove
= null;
}
}
</script>
</body>
</html>
相关文章推荐
- javascript制作游戏开发碰撞检测的封装代码
- 你的第一个cocos2d游戏
- 点、圆、矩形之间的简单碰撞检测
- 为什么cocos2d-x3.x版本的碰撞监听不起作用?因为没有设置掩码
- cocos2d-x 碰撞问题详解(上)
- 关于Sprite Kit的碰撞检测
- [Awt]——物体反弹检测
- canvas 记录绘画对象实现交互能力 简单碰撞检测
- 碰撞检测
- unity中的碰撞检测
- unity3d中的碰撞系统
- 【Unity入门】碰撞检测与触发检测
- aabb与oob包围盒 碰撞检测
- iOS动画中的物理知识应用之愤怒的小鸟-碰撞检测
- 【cocos2d-x IOS游戏开发-城市跑酷16】碰撞检测:撞墙 or 从房子上掉下来
- COCOS2DX学习之Box2D物理引擎-------物体和相互作用
- cocos2d-x 3.x 物理碰撞机制
- 物理引擎中不规则物体的碰撞检测
- 【cocos2d-x入门实战】微信飞机大战之九:碰撞检测
- 【cocos2d-x入门实战】微信飞机大战之十:UFO层特殊道具