进步的konami代码

时间:2012-03-12 16:19:29

标签: javascript

我正在尝试为网站创建一个.js文件,一旦输入konami代码Up,Up,Down,Down,Left,Right,Left,Right,B,A,Start(enter)它将嵌入一个视频。 然而,当输入正确的键时,网页应显示“继续”的内容,如果输入了错误的键,则应显示“错误,再试一次”,并允许它们重新开始。

我已经设法让JavaScript工作,在输入正确的代码后会显示警告,输入错误的代码会显示不同的代码。

我已经使用在线资源来获取这么多代码,但没有一个解释如何出错,请再试一次

    if (window.addEventListener) {
    var keys = [],
    konami = "38,38,40,40,37,39,37,39,66,65,13";

    window.addEventListener("keydown", function(e){
    keys.push(e.keyCode);


    if (keys.toString().indexOf(konami) >= 0) 
    {            
       alert('Right');
        keys = [];
    };

    if (keys.toString().indexOf(konami) < 0)
    {
       alert('Wrong');
        keys = [];
    }
}, true);

};

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:7)

if (window.addEventListener) {
    var index = 0;
    var konami = [38,38,40,40,37,39,37,39,66,65,13];

    window.addEventListener("keydown", function(e){
        if (e.keyCode === konami[index])
        {
            index++; //valid key at the valid point

            if (index == konami.length)
            {
                alert("Correct");
            } else {
                alert("Keep going");
            }
        } else {
            // incorrect code restart
            index = 0;
            alert("Wrong"); 
        }
   });
}

答案 1 :(得分:2)

您可以执行类似

的操作
 if (window.addEventListener) {
    var keys = [],
    konami = "38,38,40,40,37,39,37,39,66,65,13".split(',');

    window.addEventListener("keydown", function(e){
    keys.push(e.keyCode);
        console.log(e.keyCode);
    var lengthOfKeys = keys.length -1;


    if (konami[lengthOfKeys] == keys[lengthOfKeys])
    {            
       alert('Right');
        if(konami.length === keys.length){
            alert('complete!');
        }

    }else{
       alert('Wrong');
        keys = [];
    }
}, true);
};

在这里摆弄http://jsfiddle.net/b6kuZ/

答案 2 :(得分:0)

这对我有用:

if (window.addEventListener) {
    var keys = [],
    konami = "38,38,40,40,37,39,37,39,66,65,13";
    konami_arr = konami.split(',');

    window.addEventListener("keydown", function(e){
        keys.push(e.keyCode);

        var position = keys.length-1;

        if(keys[position ] != konami_arr[position])
        {
            alert('Wrong');
            keys = [];
        }
        else if (keys.join(',') == konami) 
        {            
           alert('Right');
            keys = [];
        };
    }, true);
}

​jsFiddle exmaple

答案 3 :(得分:0)

每次敲击键都会发出警报,这非常刺耳。相反,为什么要在DIV中显示正确答案的验证,而仅在答案不正确时使用警报。

     function checker(){

     if (kc==11){

        kc=0;  // This resets the sequence. 

        // The function for what you want to occur goes here. 
      }
     }

     function keyUp(e) {
 var keynum;
        if (window.event){keynum = event.keyCode;}
 else if (e.which){keynum = e.which;}
     for (i=0;i<222;i++){

// The 222 represents all the keys on the keyboard.

     var kx=konamicode[kc]; // kx represents the current position in the code sequence.

     var res=document.getElementById('response');
     var dumb=wrong[kc];

      if (keynum==i){

 // Checks to see if key matches sequence,  and resets sequence if it doesn't.

      if (i!=kx){
                 res.innerHTML='';
                 alert(dumb); // Reprimands user, and resets the sequence.
                 kc=0;
                 } 
             else {
             res.innerHTML=right[kc];   // Congratulates user, and advances the sequence.
             kc++;
             }

       }
     }
   checker();
       }

document.onkeyup = keyUp;

您需要在页面的正文中放置一个DIV,以显示按键被验证为正确的

<div id="response"></div>