-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
485 lines (484 loc) · 14.7 KB
/
snake.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Transcrypt'ed from Python, 2020-06-01 15:19:57
import {AssertionError, AttributeError, BaseException, DeprecationWarning, Exception, IndexError, IterableError, KeyError, NotImplementedError, RuntimeWarning, StopIteration, UserWarning, ValueError, Warning, __JsIterator__, __PyIterator__, __Terminal__, __add__, __and__, __call__, __class__, __envir__, __eq__, __floordiv__, __ge__, __get__, __getcm__, __getitem__, __getslice__, __getsm__, __gt__, __i__, __iadd__, __iand__, __idiv__, __ijsmod__, __ilshift__, __imatmul__, __imod__, __imul__, __in__, __init__, __ior__, __ipow__, __irshift__, __isub__, __ixor__, __jsUsePyNext__, __jsmod__, __k__, __kwargtrans__, __le__, __lshift__, __lt__, __matmul__, __mergefields__, __mergekwargtrans__, __mod__, __mul__, __ne__, __neg__, __nest__, __or__, __pow__, __pragma__, __proxy__, __pyUseJsNext__, __rshift__, __setitem__, __setproperty__, __setslice__, __sort__, __specialattrib__, __sub__, __super__, __t__, __terminal__, __truediv__, __withblock__, __xor__, abs, all, any, assert, bool, bytearray, bytes, callable, chr, copy, deepcopy, delattr, dict, dir, divmod, enumerate, filter, float, getattr, hasattr, input, int, isinstance, issubclass, len, list, map, max, min, object, ord, pow, print, property, py_TypeError, py_iter, py_metatype, py_next, py_reversed, py_typeof, range, repr, round, set, setattr, sorted, str, sum, tuple, zip} from './org.transcrypt.__runtime__.js';
import {fabric} from './com.fabricjs.js';
var __name__ = '__main__';
var __left0__ = tuple ([500, 500]);
export var w = __left0__ [0];
export var h = __left0__ [1];
export var inc = 20;
var __left0__ = tuple ([37, 39, 32]);
export var left = __left0__ [0];
export var right = __left0__ [1];
export var space = __left0__ [2];
window.onkeydown = (function __lambda__ (event) {
return event.keycode != space;
});
var __left0__ = null;
export var player = __left0__;
export var apple = __left0__;
export var obstacle = __left0__;
export var mongoose = __left0__;
export var canvas = __left0__;
export var facing = [1, 2, 3, 4];
export var LEFT = 1;
export var UP = 2;
export var RIGHT = 3;
export var DOWN = 4;
export var Snake = __class__ ('Snake', [object], {
__module__: __name__,
get __init__ () {return __get__ (this, function (self) {
self.alive = true;
self.past = [];
self.tail = [];
self.coords = [];
self.snake_length = 1;
self.loc = tuple ([5 * inc, 5 * inc]);
self.snake = new fabric.Rect (dict ({'left': self.loc [0], 'top': self.loc [1], 'width': inc, 'height': inc, 'originX': 'left', 'originY': 'top', 'fill': 'green'}));
self.facing = 3;
});},
get py_update () {return __get__ (this, function (self) {
canvas.remove (self.snake);
for (var t_box of self.tail) {
canvas.remove (t_box);
}
self.coords = [];
self.tail = [];
self.past.append (self.loc);
if (self.facing == 1) {
self.loc = tuple ([self.loc [0] - inc, self.loc [1]]);
}
else if (self.facing == 2) {
self.loc = tuple ([self.loc [0], self.loc [1] - inc]);
}
else if (self.facing == 3) {
self.loc = tuple ([self.loc [0] + inc, self.loc [1]]);
}
else {
self.loc = tuple ([self.loc [0], self.loc [1] + inc]);
}
self.snake = new fabric.Rect (dict ({'left': self.loc [0], 'top': self.loc [1], 'width': inc, 'height': inc, 'originX': 'left', 'originY': 'top', 'fill': 'green'}));
self.coords.append (self.loc);
self.snake.hasControls = false;
var tail = [];
for (var i = 0; i < self.snake_length - 1; i++) {
self.tail.append (new fabric.Rect (dict ({'left': self.past [len (self.past) - (i + 1)] [0], 'top': self.past [len (self.past) - (i + 1)] [1], 'width': inc, 'height': inc, 'originX': 'left', 'originY': 'top', 'fill': 'green'})));
self.coords.append (self.past [len (self.past) - (i + 1)]);
}
for (var t_box of self.tail) {
canvas.add (t_box);
if (t_box.left == self.snake.left && t_box.top == self.snake.top) {
alert ('You ate yourself! Click OK to continue.');
self.die ();
}
}
canvas.add (self.snake);
});},
get turn () {return __get__ (this, function (self, e) {
if (e.keyCode == 37) {
if (self.facing == 1) {
self.facing = 4;
}
else {
self.facing = facing [facing.index (self.facing) - 1];
}
}
else if (e.keyCode == 39) {
if (self.facing == 4) {
self.facing = 1;
}
else {
self.facing = facing [facing.index (self.facing) + 1];
}
}
else if (e.keyCode == 80) {
alert ('Game Paused. Click OK to continue.');
}
});},
get turn_mobile () {return __get__ (this, function (self, event) {
if (event.clientX < screen.width / 2) {
if (self.facing == 1) {
self.facing = 4;
}
else {
self.facing = facing [facing.index (self.facing) - 1];
}
}
else if (event.clientX > screen.width / 2) {
if (self.facing == 4) {
self.facing = 1;
}
else {
self.facing = facing [facing.index (self.facing) + 1];
}
}
});},
get die () {return __get__ (this, function (self) {
player.alive = false;
canvas.remove (self.snake);
for (var t_box of self.tail) {
canvas.remove (t_box);
}
var __left0__ = false;
var start = __left0__;
var started = __left0__;
gameOver ();
});}
});
export var Apple = __class__ ('Apple', [object], {
__module__: __name__,
get __init__ () {return __get__ (this, function (self) {
self.spawn ();
});},
get spawn () {return __get__ (this, function (self) {
self.loc = tuple ([int ((Math.random () * w) / inc) * inc, int ((Math.random () * h) / inc) * inc]);
self.apple = new fabric.Rect (dict ({'left': self.loc [0], 'top': self.loc [1], 'width': inc, 'height': inc, 'originX': 'left', 'originY': 'top', 'fill': 'red'}));
self.apple.hasControls = false;
self.apple.evented = false;
self.apple.selectable = false;
canvas.add (self.apple);
});},
get eat () {return __get__ (this, function (self) {
canvas.remove (self.apple);
});}
});
export var Obstacle = __class__ ('Obstacle', [object], {
__module__: __name__,
get __init__ () {return __get__ (this, function (self) {
self.in_place = false;
self.block = null;
});},
get generate_obstacle () {return __get__ (this, function (self, apple_loc) {
if (self.block != null) {
canvas.remove (self.block);
}
self.apple_loc = apple_loc;
self.spot = int (Math.random () * 3) + 1;
if (self.spot == LEFT && apple_loc [0] == 0) {
self.spot = RIGHT;
}
if (self.spot == RIGHT && apple_loc [0] == w - inc) {
self.spot = LEFT;
}
if (self.spot == UP && apple_loc [1] == 0) {
self.spot = DOWN;
}
if (self.spot == DOWN && apple_loc [1] == h - inc) {
self.spot = UP;
}
if (self.spot == LEFT) {
self.locs = tuple ([self.apple_loc [0] - inc, self.apple_loc [1]]);
}
else if (self.spot == UP) {
self.locs = tuple ([self.apple_loc [0], self.apple_loc [1] - inc]);
}
else if (self.spot == RIGHT) {
self.locs = tuple ([self.apple_loc [0] + inc, self.apple_loc [1]]);
}
else if (self.spot == DOWN) {
self.locs = tuple ([self.apple_loc [0], self.apple_loc [1] + inc]);
}
});},
get draw_obstacle () {return __get__ (this, function (self) {
if (self.block != null) {
canvas.remove (self.block);
}
self.block = new fabric.Rect (dict ({'left': self.locs [0], 'top': self.locs [1], 'width': inc, 'height': inc, 'originX': 'left', 'originY': 'top', 'fill': 'black'}));
self.block.hasControls = false;
self.block.evented = false;
self.block.selectable = false;
canvas.add (self.block);
self.in_place = true;
});},
get remove_obstacle () {return __get__ (this, function (self) {
canvas.remove (self.block);
self.in_place = false;
});}
});
export var Mongoose = __class__ ('Mongoose', [object], {
__module__: __name__,
get __init__ () {return __get__ (this, function (self) {
self.locs = [0, 0];
self.mongoose = null;
});},
get generate_mongoose () {return __get__ (this, function (self) {
self.direction = int (Math.random () * 3) + 1;
if (self.direction == LEFT) {
self.locs [0] = w;
self.locs [1] = int ((Math.random () * h) / inc) * inc;
}
if (self.direction == UP) {
self.locs [0] = int ((Math.random () * w) / inc) * inc;
self.locs [1] = h;
}
if (self.direction == RIGHT) {
self.locs [0] = -(1) * inc;
self.locs [1] = int ((Math.random () * h) / inc) * inc;
}
if (self.direction == DOWN) {
self.locs [0] = int ((Math.random () * w) / inc) * inc;
self.locs [1] = -(1) * inc;
}
self.hunting = true;
});},
get draw () {return __get__ (this, function (self) {
if (self.mongoose != null) {
canvas.remove (self.mongoose);
}
if (self.hunting) {
if (self.direction == LEFT) {
self.locs = [self.locs [0] - inc, self.locs [1]];
if (self.locs [0] < 0) {
self.die ();
}
}
if (self.direction == UP) {
self.locs = [self.locs [0], self.locs [1] - inc];
if (self.locs [1] < 0) {
self.die ();
}
}
if (self.direction == RIGHT) {
self.locs = [self.locs [0] + inc, self.locs [1]];
if (self.locs [0] >= w) {
self.die ();
}
}
if (self.direction == DOWN) {
self.locs = [self.locs [0], self.locs [1] + inc];
if (self.locs [1] >= h) {
self.die ();
}
}
self.mongoose = new fabric.Rect (dict ({'left': self.locs [0], 'top': self.locs [1], 'width': inc, 'height': inc, 'originX': 'left', 'originY': 'top', 'fill': 'yellow'}));
self.mongoose.hasControls = false;
self.mongoose.evented = false;
self.mongoose.selectable = false;
canvas.add (self.mongoose);
}
});},
get die () {return __get__ (this, function (self) {
self.hunting = false;
canvas.remove (self.mongoose);
});}
});
export var u = null;
export var py_update = function () {
if (u != null) {
window.clearTimeout (u);
}
if (player != null) {
moveCanvas ();
changeColor ();
}
if (!(started) && start) {
startTheGame ();
}
if (start && started && player != null) {
player.py_update ();
if (player.loc [0] == apple.loc [0] && player.loc [1] == apple.loc [1]) {
apple.eat ();
apple.spawn ();
player.snake_length++;
changeScore (player.snake_length - 1);
if (player.snake_length - 1 > 5) {
obstacle.generate_obstacle (apple.loc);
if (!__in__ (obstacle.locs, player.coords)) {
obstacle.draw_obstacle ();
}
}
else if (obstacle.in_place) {
obstacle.remove_obstacle ();
}
}
if (obstacle.in_place && player.loc [0] == obstacle.locs [0] && player.loc [1] == obstacle.locs [1]) {
alert ('You ran into an obstacle! Click OK to continue.');
player.die ();
}
if (player.loc [0] < 0 || player.loc [0] >= w || player.loc [1] < 0 || player.loc [1] >= h) {
alert ('You went out of bounds! Click OK to continue.');
player.die ();
}
if (player.alive) {
var update_time = 350 - 10 * (player.snake_length - 1);
if (update_time <= 0) {
var update_time = 1;
}
u = window.setTimeout (py_update, update_time);
}
}
else {
u = window.setTimeout (py_update, 500);
}
};
export var focus = function () {
var focus = !(focus);
};
export var pauseGame = function () {
alert ('Game Paused. Click OK to continue.');
};
export var startTheGame = function () {
canvas = new fabric.Canvas ('canvas', dict ({'width': w, 'height': h}));
document.getElementById ('canvas').style.border = '5px solid black';
player = Snake ();
apple = Apple ();
obstacle = Obstacle ();
mongoose = Mongoose ();
window.setTimeout (genMongoose, 2000);
window.addEventListener ('keydown', player.turn, true);
document.addEventListener ('click', player.turn_mobile);
started = true;
};
py_update ();
export var d = null;
export var draw_mongoose = function () {
if (d != null) {
window.clearTimeout (d);
}
mongoose.draw ();
checkMongooseOrDead ();
if (mongoose.hunting) {
d = window.setTimeout (draw_mongoose, 250);
}
};
export var c = null;
export var checkMongooseOrDead = function () {
if (c != null) {
window.clearTimeout (c);
}
if (mongoose.hunting) {
var index = 0;
for (var coordinate of player.coords) {
if (coordinate [0] == player.loc [0] && coordinate [1] == player.loc [1]) {
continue;
}
if (mongoose.locs [0] == coordinate [0] && mongoose.locs [1] == coordinate [1]) {
player.snake_length = index + 1;
changeScore (player.snake_length - 1);
break;
}
index++;
}
if (player.loc [0] == mongoose.locs [0] && player.loc [1] == mongoose.locs [1]) {
mongoose.die ();
player.snake_length += 2;
changeScore (player.snake_length - 1);
}
var update_time = 50;
if (update_time <= 0) {
var update_time = 1;
}
c = window.setTimeout (checkMongooseOrDead, update_time);
}
};
export var g = null;
export var genMongoose = function () {
if (g != null) {
window.clearTimeout (g);
}
if (start && started) {
mongoose.generate_mongoose ();
draw_mongoose ();
g = window.setTimeout (genMongoose, ((250 * h) / inc) * 2);
}
};
export var cv_mv = 1;
var left = 0;
export var top = 0;
export var m = null;
export var moveCanvas = function () {
if (m != null) {
window.clearTimeout (m);
}
if (start && started) {
if (player.alive) {
var cv = document.getElementById ('canvas');
if (player.alive && player.snake_length - 1 >= 15) {
if ((left + w) + 20 < window.innerWidth && mv_right) {
left += cv_mv;
if ((left + w) + 20 >= window.innerWidth) {
mv_right = false;
}
}
else if (!(mv_right)) {
left -= cv_mv;
if (left <= 0) {
mv_right = true;
}
}
if ((top + h) + 160 < window.innerHeight && mv_bottom) {
top += cv_mv;
if ((top + h) + 160 >= window.innerHeight) {
mv_bottom = false;
}
}
else if (!(mv_bottom)) {
top -= cv_mv;
if (top <= 0) {
mv_bottom = true;
}
}
cv.style.left = ('' + left) + 'px';
cv.style.top = ('' + top) + 'px';
cv.style.border = '5px solid black';
}
}
}
m = window.setTimeout (moveCanvas, 125);
};
export var mv_right = true;
export var mv_bottom = true;
export var t = null;
export var changeColor = function () {
if (t != null) {
window.clearTimeout (t);
}
if (start && started && player.alive && player.snake_length - 1 >= 10) {
if (r > 0 && rd) {
r--;
if (r == 0) {
rd = false;
}
}
else if (!(rd)) {
r++;
if (r == 255) {
rd = true;
}
}
if (g > 0 && gd) {
g--;
if (g == 0) {
gd = false;
}
}
else if (!(gd)) {
g++;
if (g == 255) {
gd = true;
}
}
if (b > 0 && bd) {
b--;
if (b == 0) {
bd = false;
}
}
else if (!(bd)) {
b++;
if (b == 255) {
bd = true;
}
}
canvas.backgroundColor = ((((('rgb(' + r) + ',') + g) + ',') + b) + ')';
}
t = window.setTimeout (changeColor, 100);
};
var __left0__ = tuple ([int (255 * Math.random ()), int (255 * Math.random ()), int (255 * Math.random ())]);
export var r = __left0__ [0];
var g = __left0__ [1];
export var b = __left0__ [2];
var __left0__ = tuple ([true, true, true]);
export var rd = __left0__ [0];
export var gd = __left0__ [1];
export var bd = __left0__ [2];
//# sourceMappingURL=snake.map