Skip to content

Instantly share code, notes, and snippets.

@dzharii
Created September 18, 2015 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzharii/1fce1ef335d0073c0ded to your computer and use it in GitHub Desktop.
Save dzharii/1fce1ef335d0073c0ded to your computer and use it in GitHub Desktop.
// https://gist.github.com/ejoubaud/7d7c57cda1c10a4fae8c
Podium = {};
Podium.keypress = function(k) {
var oEvent = document.createEvent('KeyboardEvent');
// Chromium Hack
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
return this.keyCodeVal;
}
});
Object.defineProperty(oEvent, 'which', {
get : function() {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, k, k, "", "", false, "");
} else {
oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
}
oEvent.keyCodeVal = k;
if (oEvent.keyCode !== k) {
alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
}
document.body.dispatchEvent(oEvent);
};
var keyb = {
up:38, // Up
right:39, // Right
down:40, // Down
left:37 // Left
};
function Play() {
var gameWrap = document.getElementById("game-wrap");
var ds = gameWrap.dataset;
var eggs = {
downLeft: ds["egg-0"],
topLeft: ds["egg-1"],
topRight: ds["egg-2"],
downRight: ds["egg-3"]
};
console.log(eggs);
if (eggs.topLeft == 5) {
Podium.keypress(keyb.left);
Podium.keypress(keyb.up);
console.log("topLeft");
} else if (eggs.downLeft == 5) {
Podium.keypress(keyb.left);
Podium.keypress(keyb.down);
console.log("downLeft");
} else if (eggs.downRight == 5) {
Podium.keypress(keyb.right);
Podium.keypress(keyb.down);
console.log("downRight");
} else if (eggs.topRight == 5) {
Podium.keypress(keyb.right);
Podium.keypress(keyb.up);
console.log("topRight");
}
}
setInterval(Play, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment