How to add konami code in a website based on html?

前端 未结 10 1276
野性不改
野性不改 2020-12-13 01:21

I was asked to implement the Konami Code in a website I\'m currently working on. It should do the following:

  1. Change Background Image

  2. Play s

10条回答
  •  执笔经年
    2020-12-13 01:48

    Piggybacking off Ehsan Kia,

    I haven't seen anyone handling cases where the up key could be pressed 3+ times, and technically the code would have been input correctly.

    Minified it a bit because the conditionals got long.

    let c = 0;
    const kCode = [38,38,40,40,37,39,37,39,66,65];
    document.addEventListener('keydown', (e) => {
         c = (e.keyCode == kCode[c] ? c + 1 : (e.keyCode-38 ? 0 : (c ? (kCode[c-1] == 38 ? c : 0) : 0)));
         if(c == kCode.length) activate();
    });
    

提交回复
热议问题