-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.html
More file actions
379 lines (329 loc) · 10.4 KB
/
index.html
File metadata and controls
379 lines (329 loc) · 10.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<head>
<meta charset="utf-8">
<title>Inspect This Snake</title>
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans&display=swap" rel="stylesheet">
<style>
body {
color: #3500ff;
text-align: center;
overflow: hidden;
margin: 0px;
margin-top: 8px;
font-family: 'IBM Plex Sans', sans-serif;
font-size: 16px;
}
h1 {
font-size: 30px;
font-weight: normal;
margin: 0;
padding: 0;
}
h2 {
font-size: 16px;
font-weight: normal;
margin: 0;
padding: 0;
}
div:first-of-type {
display: inline-block;
margin-left: -20px;
width: 150px;
height: 170px;
background-image: url('snake.gif');
background-position: center;
background-repeat: no-repeat;
background-size: 150px;
}
footer {
position: absolute;
bottom: 5;
right: 5;
text-align: right;
}
footer span {
display: inline-block;
padding: 1px;
}
a {
color: #3500ff;
}
</style>
<script>
const gameWidth = 15;
const gameHeight = 13;
const introAttributeNames = 'clicksnake'.split('');
const gameAttributeNames = 'playsnakegame'.split('');
let firstElement;
const divs = [];
let mode = 'intro';
let snake = [];
let map = {};
let direction;
let newDirection;
let score;
let highScore = 0;
let lastMove = Date.now();
let gameOverTime;
let flipFlop = 0;
function wait(milliseconds) {
return new Promise((resolve, reject) => {
setTimeout(resolve, milliseconds);
});
}
function calculateDistance(xa, ya, xb, yb) {
let a = xa - xb;
let b = ya - yb;
return Math.sqrt(a*a + b*b);
}
async function startGame() {
mode = 'setup';
const midY = Math.floor(gameHeight / 2);
snake = [];
direction = 0;
newDirection = null;
score = 0;
try {
let newHighScore = parseInt(localStorage.getItem('snake-high-score')) || 0;
if (newHighScore > highScore) {
highScore = newHighScore;
}
}
catch (error) {
}
do {
let div = divs.pop();
await wait(50);
div.parentElement.removeChild(div);
}
while (divs.length > 1);
divs[0].removeAttribute(introAttributeNames[0]);
mode = 'game';
while (divs.length < gameHeight+1) {
await wait(50);
div = document.createElement('div');
document.body.insertBefore(div, firstElement);
divs.push(div);
}
for (let x = 0; x < gameWidth; x ++) {
for (let y = 0; y < gameHeight; y ++) {
map[x+','+y] = 0;
}
}
for (let i = 0; i < 4; i ++) {
map[i+','+midY] = 1;
snake.unshift({x: i, y: midY});
}
addApple();
}
function addApple() {
let x;
let y;
do {
x = Math.floor(Math.random() * gameWidth);
y = Math.floor(Math.random() * gameHeight);
}
while (map[x+','+y] != 0);
map[x+','+y] = 2;
}
function gameMove() {
const head = snake[0];
if (!head) {
return;
}
const newHead = {x: head.x, y: head.y};
if (newDirection != null) {
if (newDirection == 0 && direction != 2) {
direction = 0;
}
else if (newDirection == 1 && direction != 3) {
direction = 1;
}
else if (newDirection == 2 && direction != 0) {
direction = 2;
}
else if (newDirection == 3 && direction != 1) {
direction = 3;
}
newDirection = null;
}
if (direction == 0) { // right
newHead.x ++;
}
else if (direction == 1) { // down
newHead.y ++;
}
else if (direction == 2) { // left
newHead.x --;
}
else if (direction == 3) { // up
newHead.y --;
}
if (newHead.x < 0) {
newHead.x = gameWidth - 1;
}
else if (newHead.x > gameWidth - 1) {
newHead.x = 0;
}
if (newHead.y < 0) {
newHead.y = gameHeight - 1;
}
else if (newHead.y > gameHeight - 1) {
newHead.y = 0;
}
const oldTile = map[newHead.x+','+newHead.y];
snake.unshift(newHead);
map[newHead.x+','+newHead.y] = 1;
if (oldTile == 2) {
score ++;
if (score > highScore) {
highScore = score;
try {
localStorage.setItem('snake-high-score', highScore);
}
catch (error) {
}
}
addApple();
}
else if (oldTile == 1) {
gameOverTime = Date.now();
mode = 'gameover';
}
else {
const oldTail = snake.pop();
map[oldTail.x+','+oldTail.y] = 0;
}
}
function renderGame() {
divs.forEach((div, y) => {
if (y == 0) {
div.setAttribute('score', score);
div.setAttribute('high-score', highScore);
return;
}
y -= 1;
let line = '';
for (let x = 0; x < gameWidth; x ++) {
const tile = map[x+','+y];
if (tile == 1) {
line += '🐍';
}
else if (tile == 2) {
line += '🍎';
}
else if (tile == 3) {
line += '💥';
}
else {
line += '⬜';
}
}
if (flipFlop) {
line += ' ';
}
else {
line += '⠀';
}
div.setAttribute(gameAttributeNames[y], line);
});
flipFlop = !flipFlop;
}
function renderGameover() {
const n = Math.floor((Date.now() - gameOverTime) / 100);
if (n < snake.length) {
const a = Math.floor(Date.now()/50) % 2;
if (a) {
const segment = snake[n];
map[segment.x+','+segment.y] = 3;
renderGame();
}
}
else {
const b = Math.floor(Date.now()/1000) % 2;
if (b) {
const midY = Math.floor(gameHeight / 2);
for (let y = 0; y < gameHeight; y ++) {
let div = divs[y+1];
div.setAttribute(gameAttributeNames[y], ' GAME OVER GAME OVER GAME OVER ');
}
}
else {
renderGame();
}
}
}
function renderIntro() {
let i = Math.floor(Math.sin(Date.now()/700) * (divs.length/2) + (divs.length/2));
divs.forEach((div, y) => {
if (y == i) {
div.setAttribute(introAttributeNames[y], ' . OKAY now CLICK the SNAKE! . ');
}
else {
div.setAttribute(introAttributeNames[y], ' . . . . . . . . . . . . . . . ');
}
});
}
function loop() {
setTimeout(loop, 1000/30);
if (mode == 'intro') {
renderIntro();
}
else if (mode == 'game') {
const now = Date.now();
if (now - lastMove > 100) {
lastMove = now;
gameMove();
}
renderGame();
}
else if (mode == 'gameover') {
renderGameover();
}
}
function keydown(event) {
const key = event.which;
if (key == 39) { // right
newDirection = 0;
}
else if (key == 40) { // down
newDirection = 1;
}
else if (key == 37) { // left
newDirection = 2;
}
else if (key == 38) { // up
newDirection = 3;
}
}
window.addEventListener('load', () => {
firstElement = document.body.firstChild;
for (let i = 0; i < 10; i ++) {
const div = document.createElement('div');
document.body.insertBefore(div, firstElement);
divs.push(div);
}
window.addEventListener('keydown', keydown);
const snakeElement = divs[0];
window.addEventListener('blur', () => {
document.getElementsByTagName('style')[0].innerHTML += 'div:first-of-type {cursor: pointer;}';
snakeElement.addEventListener('click', startGame);
});
loop();
});
</script>
</head>
<body>
<!---->
<!---->
<!---->
<!---->
<!---->
<h1>Inspect This Snake</h1>
<h2>( you know.. with right click )</h2>
<footer>
<span>yet</span><br />
<span>another</span><br />
<span>web thing from</span><br />
<span><a href="https://matthewrayfield.com">matthew rayfield world</a></span>
</footer>
</body>