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

前端 未结 10 1280
野性不改
野性不改 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:38

    My own compact and cleaned version inspired by the answers here:

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

    In this case, the activate() function is called when triggered.

提交回复
热议问题