当前位置:首页 > 网页特效 > 游戏特效 >

你永远都别想碰到我,Js小游戏

时间:2014-06-08 23:39 来源:互联网 作者:源码搜藏 收藏 推荐

运行代码 保存代码 复制代码 提示:您可以先修改部分代码再运行,保存代码功能在Firefox下无效。
  • 一个有趣JavaScript应用,物理碰撞检测,你的鼠标永远不会碰到屏幕上的小球,你鼠标移动的时候,那些小球会早早就躲开了,神奇的JS,有来头。
  • <script type="text/javascript">
    document.onselectstart = new Function("return false");
    object = new Array();
    N  = 0;
    xm = 1;
    ym = 1;
    nx = 0;
    ny = 0;
    S  = 0;
    Ni = 0;
     
    function CObj(N,x,y,img){
    o = document.createElement("img");
    o.src = img.src;
    document.getElementById("CENTER").appendChild(o);
    this.obj  = o.style;
    this.x  = x;
    this.y  = y;
    this.x0 = x;
    this.y0 = y;
    this.w  = img.width  / 2;
    this.h  = img.height / 2;
     
    this.anim = function () {
    with(this){
    dx = xm - x;
    dy = ym - y;
    d = Math.sqrt(dx * dx + dy * dy);
    x = x - S / d * (dx / d) + (x0 - x) * .15;
    y = y - S / d * (dy / d) + (y0 - y) * .15;
    obj.left   = x - w;
    obj.top    = y - w;
    }
    }
    }
     
    function run(){
    for(i in object)object[i].anim();
    setTimeout(run, 16);
    }
     
    document.onmousemove = function(e){
    if (window.event) e = window.event;
    xm = -nx * .5 + (e.x || e.clientX);
    ym = -ny * .5 + (e.y || e.clientY);
    }
    function resize(){
    nx = document.body.offsetWidth;
    ny = document.body.offsetHeight;
    S  = ny * 20;
    }
    onresize = resize;
     
    onload = function() {
    img = document.getElementById("IMG").getElementsByTagName("img");
    resize();
    Ni=0;
    for(var i=-5;i<=5;i++){
    for(var j=-4;j<=4;j++){
    x = i * nx / 12;
    y = j * ny / 10;
    object[Ni] = new CObj(Ni++,x,y,img[Ni%img.length]);
    }
    }
    run();
    }
    </script>
由源码搜藏网整理,转载请注明出处https://www.codesocang.com/tx-youxi/8629.html

游戏特效下载排行

最新文章