Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
// 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);
var strategies = {
one: breakHead,
two: destroyEnemy,
three: forgetFriends
}, strategyNotFound = function() {
throw new Error("Strategy not found");
}
var proceedWith = strategies[value] || strategyNotFound;
proceedWith();
Игра знакомая с детства в реализации на JavaScript