forked from jish/contra
/
contra.js
47 lines (39 loc) · 927 Bytes
/
contra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var Contra = (function() {
var a = 65,
b = 66,
left = 37,
up = 38,
right = 39,
down = 40,
handlers = [],
pattern = [up, up, down, down, left, right, left, right, b, a],
position = 0;
function check(evt) {
if (pattern[position] === evt.keyCode) {
position++;
} else {
position = 0;
}
if (position === pattern.length) {
win();
position = 0;
}
}
function win() {
var body = document.getElementsByTagName('body')[0];
body.className = body.className + ' contra';
for(var i = 0, l = handlers.length; i<l; i++) {
handlers[i].call();
}
}
if (document.addEventListener) {
document.addEventListener('keyup', check);
} else if(document.attachEvent) {
document.attachEvent('onkeypress', check);
}
return {
addHandler: function(handler) {
handlers.push(handler);
}
}
}());