原生js实现移动小球(碰撞检测)


Posted in Javascript onDecember 17, 2020

本文实例为大家分享了js实现移动小球的具体代码,供大家参考,具体内容如下

</head>
<style>
 *{margin: 0;
  padding:0;}
 #main{margin: 0px auto;position: relative;}
 #main div{overflow: hidden;position: absolute;width: 50px;height: 50px;opacity: 0.5;
  -moz-border-radius: 50%;-webkit-border-radius: 50%;border-radius: 50%;}
</style>
<body>
 <div id="main">
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
</div>

 <script>
 var main = document.getElementById('main');
 var circles = main.getElementsByTagName('div');
 var st = [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
 var json = [],arr=[],color = [];
 var maxW = 0;
 var maxH = 0;
 var cwidth = circles[0].offsetWidth;
 var cheight = circles[0].offsetHeight;

 //根据浏览器窗口的大小自动调节小球的运动空间
 window.onresize=function(){
  var main = document.getElementById('main');
  maxW=window.innerWidth-circles[0].clientWidth;
  maxH=window.innerHeight-circles[0].clientHeight;
  main.style.width = window.innerWidth+'px';
  main.style.height = window.innerHeight+'px';

 }
 onresize();
 //数组对象的初始化
 for(var i=0;i<circles.length;i++){
  arr=[];
  for(var j=0;j<6;j++){
   color[j] = st[Math.floor(Math.random()*16)];
  }
  arr.x = Math.floor(Math.random()*(maxW+1));//初始x坐标
  arr.y = Math.floor(Math.random()*(maxH+1));//初始y坐标
  arr.cx = arr.x + circles[0].offsetWidth/2; //圆心x坐标
  arr.cy = arr.y + circles[0].offsetHeight/2;//圆心y坐标
  arr.movex = Math.floor(Math.random()*2);//x轴移动方向
  arr.movey = Math.floor(Math.random()*2);//y轴移动方向
  arr.bgolor = '#'+ color.join('');//随机生成一个6位字符串
  arr.speed = 2+Math.floor(Math.random()*5);
  //随机生成一个2~6之间的移动速度(如果设置的改变速度太大,容易造成小球碰撞时两个小球之间有重合,也有可能小球会出界)
  arr.timer = null;//计时器
  arr.index = i;//索引值
  json.push(arr);
  circles[i].style.left = arr.x + 'px';//小球位置初始化
  circles[i].style.top = arr.y + 'px';//小球位置初始化
  circles[i].style.backgroundColor = arr.bgolor;//小球背景颜色初始化
 }
 //碰撞函数
 function crash(a){
  var ball1x = json[a].cx;
  var ball1y = json[a].cy;
  for(var i= 0;i<json.length;i++){
   if(i!=a){
    var ball2x = json[i].cx;
    var ball2y = json[i].cy;
    //圆心距离的平方
    var len = (ball1x-ball2x)*(ball1x-ball2x)+(ball1y-ball2y)*(ball1y-ball2y);
    if(len <= cwidth*cwidth){
    //小球位置的判断,发生碰撞反应
     if(ball1x >ball2x){
      if(ball1y > ball2y){
       json[a].movex=1;
       json[a].movey=1;
      }else if(ball1y < ball2y){
       json[a].movex=1;
       json[a].movey=0;
      }else{
       json[a].movex=1;
      }
     }else if(ball1x < ball2x){
      if(ball1y > ball2y){
       json[a].movex=0;
       json[a].movey=0;
      }else if(ball1y < ball2y){
       json[a].movex=0;
       json[a].movey=1;
      }else{
       json[a].movex=0;
      }
     }else{
      if(ball1y > ball2y){
       json[a].movey=1;
      }else if(ball1y < ball2y){
       json[a].movey=0;
      }
     }
    }
   }

  }
 }
 //移动函数
 function move(circle){
  circle.timer = setInterval(function () {
   if(circle.movex == 1){
    circle.x+=circle.speed;
    if(circle.x+circle.speed >= maxW){//防止小球出界
     circle.x = maxW;
     circle.movex=0;//小球运动方向发生改变
    }
   }else{
    circle.x-=circle.speed;
    if(circle.x-circle.speed <= 0){
     circle.x = 0;
     circle.movex=1;
    }
   }
   if(circle.movey == 1){
    circle.y += circle.speed;
    if(circle.y+circle.speed >= maxH){
     circle.y = maxH;
     circle.movey=0;
    }
   }else{
    circle.y-=circle.speed;
    if(circle.y-circle.speed <= 0){
     circle.y = 0;
     circle.movey=1;
    }
   }
   circle.cx = circle.x + circles[0].offsetWidth/2;//小球每一次运动圆心都会发生改变
   circle.cy = circle.y + circles[0].offsetHeight/2;
   circles[circle.index].style.left = circle.x + 'px';//小球位置重定位
   circles[circle.index].style.top = circle.y + 'px';
   /*console.log('x'+circle.cx+'y'+circle.cy);*/
   crash(circle.index);
  },16);
 }
 //对每一个小球绑定计时器,让小球动起来
 for(var i=0;i<circles.length;i++){
  move(json[i]);
 }
</script>
</body>

效果图:

原生js实现移动小球(碰撞检测)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
SinaEditor使用方法详解
Dec 28 Javascript
javascript中的__defineGetter__和__defineSetter__介绍
Aug 15 Javascript
jQuery使用hide方法隐藏元素自身用法实例
Mar 30 Javascript
JS针对浏览器窗口关闭事件的监听方法集锦
Jun 24 Javascript
javascript运算符——逻辑运算符全面解析
Jun 27 Javascript
bootstrap栅格系统示例代码分享
May 22 Javascript
jQuery菜单实例(全选,反选,取消)
Aug 28 jQuery
浅谈angular4生命周期钩子
Sep 05 Javascript
第一个Vue插件从封装到发布
Nov 22 Javascript
ES6 Promise对象的含义和基本用法分析
Jun 14 Javascript
Vue引入Stylus知识点总结
Jan 16 Javascript
2019最新21个MySQL高频面试题介绍
Feb 06 Javascript
vue+elementUI动态增加表单项并添加验证的代码详解
Dec 17 #Vue.js
Taro小程序自定义顶部导航栏功能的实现
Dec 17 #Javascript
vue 数据操作相关总结
Dec 17 #Vue.js
Vue 组件注册全解析
Dec 17 #Vue.js
js回到页面指定位置的三种方式
Dec 17 #Javascript
javascript 数组(list)添加/删除的实现
Dec 17 #Javascript
使用webpack5从0到1搭建一个react项目的实现步骤
Dec 16 #Javascript
You might like
十大催泪虐心动漫电影,有几部你还没看
2020/03/04 日漫
用PHP查询域名状态whois的类
2006/11/25 PHP
php Ubb代码编辑器函数代码
2012/07/05 PHP
跨浏览器PHP下载文件名中的中文乱码问题解决方法
2015/03/05 PHP
PHP yii实现model添加默认值的方法(两种方法)
2016/11/10 PHP
基于PHP实现栈数据结构和括号匹配算法示例
2017/08/10 PHP
YII2框架中behavior行为的理解与使用方法示例
2020/03/13 PHP
javascript Select标记中options操作方法集合
2008/10/22 Javascript
Jquery下attr和removeAttr的使用方法
2010/12/28 Javascript
基于jquery完美拖拽,可返回拖动轨迹
2012/03/29 Javascript
深入理解jQuery中live与bind方法的区别
2013/12/18 Javascript
在 Express 中使用模板引擎
2015/12/10 Javascript
ionic由于使用了header和subheader导致被遮挡的问题的两种解决方法
2016/09/22 Javascript
js模仿微信朋友圈计算时间显示几天/几小时/几分钟/几秒之前
2017/04/27 Javascript
jQuery实现的滑块滑动导航效果示例
2018/06/04 jQuery
vue计算属性computed的使用方法示例
2019/03/13 Javascript
在Python中操作日期和时间之gmtime()方法的使用
2015/05/22 Python
python模块smtplib学习
2018/05/22 Python
python3中property使用方法详解
2019/04/23 Python
Python 元组拆包示例(Tuple Unpacking)
2019/12/24 Python
解决python3插入mysql时内容带有引号的问题
2020/03/02 Python
pytorch 中的重要模块化接口nn.Module的使用
2020/04/02 Python
为什么称python为胶水语言
2020/06/16 Python
解决redis与Python交互取出来的是bytes类型的问题
2020/07/16 Python
Matplotlib.pyplot 三维绘图的实现示例
2020/07/28 Python
Python加载数据的5种不同方式(收藏)
2020/11/13 Python
工艺工程师工作职责
2013/11/23 职场文书
实习心得体会
2014/01/02 职场文书
护士自我介绍信
2014/01/13 职场文书
学校招生宣传广告词
2014/03/19 职场文书
护士上岗前培训自我鉴定
2014/04/20 职场文书
2014年征兵标语
2014/06/20 职场文书
村干部任职承诺书
2015/01/21 职场文书
青年教师个人总结
2015/02/11 职场文书
教师党员自我评价范文
2015/03/04 职场文书
调研报告的主要写法
2019/04/18 职场文书