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

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

    Silentdrummer has a good answer. I'm not entirely sure, but I think it could end up taking up too much memory on typing intensive pages. It's good practice to reset. Either way, here's an alternative.

    // Cheat Codes
    neededkeys = [38,38,40,40,37,39,37,39,66,65], started = false, count = 0;
    $(document).keydown(function(e) {
        key = e.keyCode;
        if (!started) {
            if (key == 38) {
                started = true;
            }
        }
        if (started) {
            if (neededkeys[count] == key) {
                count++;
            } else {
                reset();
            }
            if (count == 10) {
                reset();
                // Do your stuff here
                alert('Cheat Codes Activated');
                $('body').css('background-color', '#FFA8A8');
                // Turn down for what
                var s=document.createElement('script');
                s.setAttribute('src','https://nthitz.github.io/turndownforwhatjs/tdfw.js');
                document.body.appendChild(s);
            }
        } else {
            reset();
        }
    });
    function reset() {
        started = false;
        count = 0;
    }
    

提交回复
热议问题