-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.js
73 lines (73 loc) · 1.57 KB
/
keyboard.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
document.onkeydown = function(event) {
e = event.keyCode;
if (e == 65) { e = 37; }
if (e == 87) { e = 38; }
if (e == 68) { e = 39; }
if (e == 83) { e = 40; }
if (e >= 37 && e <= 40 && directionqueue[directionqueue.length -1] != e) {
directionqueue.push(e);
}
if (e == 32) {
togglepause();
}
}
function mouseclicked(x2, y2) {
console.log("Clicked " + x2.toString + ", " + y2.toString)
if (paused || board[y2][x2] == SNAKE) {
togglepause();
}
else {
var theta = (180/Math.PI) * Math.atan2(snake[snake.length - 1][1] - y2, x2 - snake[snake.length - 1][0]);
switch (direction) {
case LEFT:
theta = theta + 180;
break;
case UP:
theta = theta + 270;
break;
case DOWN:
theta = theta + 90;
break;
}
theta = (theta + 360) % 360;
if (theta > 0 && theta < 180) {
switch (direction) {
case UP:
directionqueue.push(LEFT);
break;
case DOWN:
directionqueue.push(RIGHT);
break;
case LEFT:
directionqueue.push(DOWN);
break;
case RIGHT:
directionqueue.push(UP);
break;
}
}
else if (theta > 180) {
switch (direction) {
case UP:
directionqueue.push(RIGHT);
break;
case DOWN:
directionqueue.push(LEFT);
break;
case LEFT:
directionqueue.push(UP);
break;
case RIGHT:
directionqueue.push(DOWN);
break;
}
}
}
}
cells = document.getElementsByClassName("cell");
for (var i = 0; i < cells.length; i++) {
cells[i].onclick = function(event) {
split = event.target.id.split('-');
mouseclicked(parseInt(split[2]), parseInt(split[1]));
};
}