10

我想使用我已经在我的网站上成功使用的 konami 代码来实现它。我只是无法让它与skifree一起工作。为了快速参考,这里是 konami 代码:

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e) {
 kkeys.push( e.keyCode );
if ( kkeys.toString().indexOf( konami ) >= 0 ){
$(document).unbind('keydown',arguments.callee);
// Launch easter egg here
  }
});

这是从本网站提取的skifree 代码:http: //timelessname.com/canvas/skifree/

    var left;
    var right;
    var faster = false;

    var step = 0;
    var obst = new Array();

    var locX = 430;
    var locY = 100;

    var running = true;

    var guy = new Image();
    var guyLeft = new Image();
    var guyRight = new Image();
    var crash = new Image();
    var rock = new Image();
    var tree = new Image();
    var bush = new Image();
    guy.src = "http://timelessname.com/canvas/skifree/guy_down.png";
    guyLeft.src = "http://timelessname.com/canvas/skifree/guy_left.png";
    guyRight.src = "http://timelessname.com/canvas/skifree/guy_right.png";
    crash.src = "http://timelessname.com/canvas/skifree/crash.png";
    rock.src = "http://timelessname.com/canvas/skifree/rock.png";
    tree.src = "http://timelessname.com/canvas/skifree/tree.png";

    $(window).keydown(function(e){
        if(e.keyCode == 37){
            left = true;
        }
        else if(e.keyCode == 39){
            right = true;
        }
        else if(e.keyCode == 70){
            faster = true;
        }
    });
    $(window).keyup(function(e){
        if(e.keyCode == 37){
            left = false;
        }
        else if(e.keyCode == 39){
            right = false;
        }
        else if(e.keyCode == 70){
            faster = false;
        }
        else if(e.keyCode == 32){
            if(!running){
                step = 0;
                obst = new Array();
                locX = 430;
                locY = 100;
                running = true;
                runSki();
            }
        }
    });




    //TODO: wrap edges (no wall)

    function runSki(){
        if(!running) return;
        var canvas = document.getElementById("can");
        var ctx = canvas.getContext("2d");

        ctx.fillStyle = "rgb(255,255,255)";
        ctx.fillRect (0, 0, canvas.width, canvas.height);


        if(left){
            if(locX > -320){
                locX--;
            }
        }
        if(right){
            if(locX < 640+320){
                locX++;
            }
        }

        ctx.fillStyle = "rgb(0,0,0)";

        ctx.fillRect (-10-locX, 0, 10, canvas.height);
        ctx.fillRect (640*2+20-locX, 0, 10, canvas.height);


        for(var i = 0; i < obst.length;i++){
            var o = obst[i];
            o.y-=2.5;
            if(faster){
                o.y-=2.5;
            }
            ctx.drawImage(o.type,o.x-locX,o.y);
            if(o.y < -30){
              obst.splice(i,1);
              i--;
            }

            var tX = o.x-locX+5;
            var tY = o.y+5;

            var d = Math.sqrt((tX-320+3)*(tX-320+3)+ (tY-100+5)*(tY-100+5));
            if(d < 20){
                ctx.drawImage(crash,320,locY);
                running = false;
            }
        }
        if(running){
        if(left){
            ctx.drawImage(guyLeft,320,locY);
        }
        else if(right){
            ctx.drawImage(guyRight,320,locY);
        }
        else{
            ctx.drawImage(guy,320,locY);
        }
        }

        var randomnumber=Math.floor(Math.random()*641)
        if(Math.floor(step*10)%10==0){
            var type;
            if(Math.floor(Math.random()*2) == 0){
              type = rock;
            }
            else{
              type = tree;
            }
            var obj = {x: Math.floor(Math.random()*641*2), y:480, type: type};
            obst.push(obj);
        }
        step+= 0.1;
        if(running){
            setTimeout("runSki();",1); 
        }
    }
    setTimeout("runSki();",1000);
4

1 回答 1

3

不确定,但您复制的代码看起来不对;看这里:

    if(left){
        if(locX > -320){
            locX--;
        }
    }
    if(right){
        if(locX < 640+320){
            locX++;
        }
    }

你是从 html 复制过来的吗?>&<字符不应该被转义:)

编辑

还要记住更新您的图像路径:D

编辑

我让它在这个页面上工作。刚刚添加了<canvas/>元素,将滑雪 javascript 包装在一个名为setupSki(由 konami 函数调用)的函数中,并替换了两个实例

setTimeout("runSki();"

setTimeout(runSki

于 2012-08-01T12:58:19.087 回答