原生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>

“`这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
原生 JavaScript 实现三级分销小 Demo 的主要步骤如下: 1. 创建三个用户: 1) 顶级用户:拥有最高级别的分销权力。 2) 一级用户:由顶级用户推广而来,拥有中级分销权力。 3) 二级用户:由一级用户推广而来,拥有最低级别的分销权力。 2. 定义用户类,并设置三个属性:姓名、级别、推广收益。 ```javascript class User { constructor(name, level) { this.name = name; this.level = level; this.profit = 0; } } ``` 3. 创建用户实例,并设置级别和姓名: ```javascript const topUser = new User("顶级用户", "顶级"); const firstLevelUser = new User("一级用户", "一级"); const secondLevelUser = new User("二级用户", "二级"); ``` 4. 实现用户推广的方法,即通过推广链接注册用户,上级用户获得相应的推广收益: ```javascript function promoteUp(user, invitee) { if (user.level === "顶级") { invitee.profit += 100; // 顶级用户推广新用户获得100元推广收益 } else if (user.level === "一级") { invitee.profit += 50; // 一级用户推广新用户获得50元推广收益 } else if (user.level === "二级") { invitee.profit += 30; // 二级用户推广新用户获得30元推广收益 } } ``` 5. 测试推广的过程: ```javascript promoteUp(topUser, firstLevelUser); // 顶级用户推广一级用户 promoteUp(firstLevelUser, secondLevelUser); // 一级用户推广二级用户 ``` 6. 输出用户的推广收益: ```javascript console.log(topUser.profit); // 输出 100 console.log(firstLevelUser.profit); // 输出 50 console.log(secondLevelUser.profit); // 输出 30 ``` 这就是一个简单的原生 JavaScript 实现的三级分销小 Demo。通过不同的级别和推广链接,上级用户可以获得相应的推广收益。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值