I was asked to implement the Konami Code in a website I'm currently working on. It should do the following:
Change Background Image
Play sound
Bring some pop-up
What's the easiest way to achieve this using javascript?
I was asked to implement the Konami Code in a website I'm currently working on. It should do the following:
Change Background Image
Play sound
Bring some pop-up
What's the easiest way to achieve this using javascript?
compact version:
Place the code below in a file
js/konami.js
and reference it in the body of your html file like this:<script src="js/konami.js"></script>
EDIT: changed the sequence to b, a instead of a, b. Thanks for the comment!
EDIT 2: reset the konamiCodePosition to 0 after activateCheats was called. Thanks for the comment!
My own compact and cleaned version inspired by the answers here:
In this case, the
activate()
function is called when triggered.I really liked Peter's answer, so I made it namespaced and made the callback optional. I also used jquery because I like it ¯\_(ツ)_/¯
This is a solution I came up with around 3 or 4 years ago. In my case I chose keyUp to keep it separate from any actions that occur from keyDown events. Also there is no need to specify what keys are allowable since the for loop checks which key was released against all the keys on the keyboard.
as a typescript module
implementation:
Konami.start(() => { alert("konami sequence entered!"); });
notes: SEQUENCE is an array of the expected inputs. by using the
head
var, the order checking and number of correct inputs is maintained. it also provides a simple way to restart if input deviates from the sequence. it also eliminates the needs for a "count" var.