Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Created July 2, 2012 15:56
Show Gist options
  • Save codeincontext/3033933 to your computer and use it in GitHub Desktop.
Save codeincontext/3033933 to your computer and use it in GitHub Desktop.
Konami code using the Gamepad API
<!DOCTYPE html>
<html>
<head>
<title>Gamepad Shit</title>
<script type="text/javascript"></script>
<style type="text/css"></style>
</head>
<body>
<h1>Gamepad Shit</h1>
<p>
This is my boring website
</p>
<div id="gamepads"></div>
<script>
function gamepadConnected(e) {
var gamepads = document.getElementById("gamepads"),
gamepadId = e.gamepad.id;
gamepads.innerHTML += " Gamepad Connected (id=" + gamepadId + ")";
}
function buttonHandler(e) {
var buttonLookup = ['x', 'a', 'b', 'y'];
setButton(buttonLookup[e.button]);
}
function axisHandler(e) {
if (e.value < 1 && e.value > -1) return;
if (e.axis == 5 && e.value < -0.5) setButton('up');
else if (e.axis == 5 && e.value > 0.5) setButton('down');
else if (e.axis == 4 && e.value < -0.5) setButton('left');
else if (e.axis == 4 && e.value > 0.5) setButton('right');
}
var correctPattern = ['up','up','down','down','left','right','left','right','b','a'];
var currentPosition = 0;
function setButton(value) {
if (value == correctPattern[currentPosition]) {
currentPosition++;
} else {
currentPosition = 0;
}
if (currentPosition == correctPattern.length-1) {
success();
};
}
function success() {
window.location.href = 'http://www.youtube.com/watch?v=uPHgojJE0dI';
};
window.addEventListener("MozGamepadConnected", gamepadConnected, false);
window.addEventListener("MozGamepadButtonDown", buttonHandler);
window.addEventListener("MozGamepadAxisMove", axisHandler);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment