-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
161 lines (161 loc) · 4.63 KB
/
snake.js
File metadata and controls
161 lines (161 loc) · 4.63 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var ui = document.getElementById("info-panel");
var ui_ctx = ui.getContext("2d");
var panel = document.getElementById("snake-panel");
var ctx = panel.getContext("2d");
var game_started = false;
var snake = {
head: { x: 10, y: 10 },
size: 1,
trail: []
};
var apple = { x: 15, y: 15 };
var movement = { x: 0, y: 0 };
var direction = 0;
var move = {
top: function () {
movement.x = 0;
movement.y = -1;
return 38 /* Top */;
},
right: function () {
movement.x = 1;
movement.y = 0;
return 39 /* Right */;
},
left: function () {
movement.x = -1;
movement.y = 0;
return 37 /* Left */;
},
bottom: function () {
movement.x = 0;
movement.y = 1;
return 40 /* Bottom */;
}
};
var tile_size = 20;
var score = 0;
function through_walls(allowed) {
if (allowed) {
if (snake.head.x < 0)
snake.head.x = tile_size - 1;
if (snake.head.x > tile_size - 1)
snake.head.x = 0;
if (snake.head.y < 0)
snake.head.y = tile_size - 1;
if (snake.head.y > tile_size - 1)
snake.head.y = 0;
}
else {
if (snake.head.x < 0 || snake.head.x > tile_size - 1 || snake.head.y < 0 || snake.head.y > tile_size - 1) {
gameover();
}
}
}
var gameLoop = window.setInterval(game, 1000 / 10);
function gameover() {
if (game_started) {
window.clearInterval(gameLoop);
ui_ctx.fillStyle = "gray";
ui_ctx.font = "60px Arial";
ui_ctx.fillText("GameOver", 60, 50);
}
}
function game() {
snake.head.x += movement.x;
snake.head.y += movement.y;
through_walls(false);
// gamespace
ctx.fillStyle = "#aaa";
ctx.fillRect(0, 0, panel.width, panel.height);
// snake
ctx.fillStyle = "green";
for (var _i = 0, _a = snake.trail; _i < _a.length; _i++) {
var pos = _a[_i];
ctx.fillRect(pos.x * tile_size, pos.y * tile_size, tile_size - 2, tile_size - 2);
// caught yourself
if (pos.x == snake.head.x && pos.y == snake.head.y) {
gameover();
}
}
snake.trail.push({ x: snake.head.x, y: snake.head.y });
// remove last bit of the tail
while (snake.trail.length > snake.size) {
snake.trail.shift();
}
// apple
if (apple.x == snake.head.x && apple.y == snake.head.y) {
snake.size++;
score++;
apple.x = Math.floor(Math.random() * tile_size);
apple.y = Math.floor(Math.random() * tile_size);
}
ctx.fillStyle = "red";
ctx.fillRect(apple.x * tile_size, apple.y * tile_size, tile_size - 2, tile_size - 2);
ctx.fillStyle = "black";
ctx.font = "30px Arial";
ctx.fillText(score.toString(), 10, 30);
}
var gamepad = function (idx) { return navigator.getGamepads()[idx]; };
var axes = function (pad, idx) { return pad && pad.axes && pad.axes[idx] ? pad.axes[idx].toFixed(2) : null; };
function inputloop() {
var pad = gamepad(0);
if (pad) {
var axis = axes(pad, 9);
var btn = pad.buttons;
if (axis) {
if (axis < -0.7)
move.top();
if (axis > 0.4 && axis < 1)
move.left() && (game_started = true);
if (axis < -0.2 && axis > -0.6)
move.right();
if (axis < 0.3 && axis > -0.1)
move.bottom();
}
else {
if (btn[12].pressed)
move.top();
if (btn[14].pressed)
move.left() && (game_started = true);
if (btn[15].pressed)
move.right();
if (btn[13].pressed)
move.bottom();
}
requestAnimationFrame(inputloop);
}
}
;
requestAnimationFrame(inputloop);
window.addEventListener("gamepadconnected", function (e) {
requestAnimationFrame(inputloop);
console.log(e, "inputloop started");
});
document.addEventListener("keydown", keyStroke);
function keyStroke(e) {
switch (e.keyCode) {
case 37 /* Left */:
if (direction === 39 /* Right */)
break;
direction = move.left();
break;
case 38 /* Top */:
if (direction === 40 /* Bottom */)
break;
direction = move.top();
break;
case 39 /* Right */:
if (direction === 37 /* Left */)
break;
direction = move.right();
break;
case 40 /* Bottom */:
if (direction === 38 /* Top */)
break;
direction = move.bottom();
break;
}
if (direction)
game_started = true;
}