-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
672 lines (527 loc) · 23.7 KB
/
Copy pathindex.html
File metadata and controls
672 lines (527 loc) · 23.7 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<!DOCTYPE HTML>
<html>
<head>
<title>Circular Snake</title>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-38383090-7', 'auto');
ga('send', 'pageview');
</script>
<link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet">
<style>
body {
background-color: #202020;
overflow: hidden;
width: 100vw;
height: 100vh;
}
#ghubmark {
position: absolute;
bottom: 10px; right: 10px;
}
#canvas {
position: absolute;
top: 0; left: 0;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="./ifvisible.min.js"></script>
<script>
// PARAMETERS
//////////////////////////
// Graphics parameters //
//////////////////////////
//Radius factor: field radius = canvas.height * radiusFactor
var radiusFactor = 0.35;
// Radius of snake points
var snakePointRadius = 9;
//Field color: the background color of the circular game field
var fieldColor = "#303030";
// Obstacle color
var obstColor = "";
// Fruit color
var fruitColor = "#FCFFA3";
//Width of the screen shake effect
var screenshakeWidth = 10;
// Score text color
var scoreColor = "#FCFFA3";
// Score text size
var scoreSize = 80;
var bestScoreSize = 24;
//////////////////////////
// Game parameters //
//////////////////////////
var fruitRadius = snakePointRadius;
// Snake base speed, in pixels per second
var snakeBaseSpeed = 50;
// Maximum speed factor
var maxSpeedFactor = 5;
var logMSF = Math.log ( maxSpeedFactor - 1 );
// Speed increase parameter
var speedIncreaseParam = 0.0075;
// Snake curve radius, in pixels
var snakeRotateRadius = 35;
// Rotation speed, in radians per second
var snakeRotateSpeed = snakeBaseSpeed / snakeRotateRadius;
// Length gained for each fruit
var fruitLength = 15;
// Snake initial length, in snake points
var snakeInitialLength = 80;
// Scaling parameters
var initialScaling = 1.75;
var minScaling = 0.5;
var scalingK = 0.03;
var logscaling = Math.log ( initialScaling/minScaling - 1 );
// Combo duration, seconds
var comboDuration = 4;
</script>
<script>
var outElastic = function(t) {
var s = 1.70158;
var p = 0;
var a = 1;
if (t == 0) return 0;
if (t == 1) return 1;
if (!p) p = 0.3;
if (a < 1) {
a = 1;
s = p / 4;
} else s = p / (2 * Math.PI) * Math.asin(1 / a);
return a * Math.pow(2, -15 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1;
}
var canvas = 0, context = 0;
var radius = 0;
var dist = function ( a, b ) {
var dx = a[0] - b[0];
var dy = a[1] - b[1];
return Math.sqrt ( dx*dx + dy*dy );
}
var dist2 = function ( a, b ) {
var dx = a[0] - b[0];
var dy = a[1] - b[1];
return dx*dx + dy*dy;
}
var Snake = function () {
// Color of the snake
this.color = "#FF6000";
// Array of the positions of the extremes of the segments of the
// snake; it is an array of arrays of two elements each, x and y
// coordinates of the point, relative to the center of the field
this.positions = [ ];
// Snake direction : defined as an angle in radians from PI to -PI
this.direction = 0;
this.rotate = -1;
// Snake base speed, in pixels per second
this.speed = snakeBaseSpeed;
// Distance
this.dist = 0;
// Function that moves the snake for the requested time step
this.move = function ( t ) {
var dx = this.speed * t;
var r = snakePointRadius;
var totdx = 0;
var maxdx = 1;
var count = 0;
var C = Math.cos(this.direction);
var S = Math.sin(this.direction);
for ( ; totdx + maxdx < dx; totdx += maxdx ) {
this.positions.unshift ( this.positions[0].slice(0) );
this.positions[0][0] += maxdx * C;
this.positions[0][1] += maxdx * S;
this.positions[0][2] = r;
count += 1;
}
this.positions.unshift ( this.positions[0].slice(0) );
this.positions[0][0] += (dx - totdx) * C;
this.positions[0][1] += (dx - totdx) * S;
this.positions[0][2] = r;
this.positions.splice(-count-1,count+1);
this.direction += snakeRotateSpeed * this.rotate * t;
this.dist += dx;
}
// Function that grows the snake by one element
this.grow = function ( k ) {
for ( var i = 0; i < k; ++i ) {
this.positions.push(this.positions[this.positions.length-1].slice(0));
}
}
// Plays eat animation effect
this.eat = function () {
this.grow ( fruitLength );
}
// Function that draws the snake on a graphic context
this.draw = function ( context ) {
context.strokeStyle = this.color;
context.lineWidth = 2*this.positions[0][2];
context.lineCap = "round";
context.beginPath ();
context.moveTo ( this.positions[0][0], this.positions[0][1] );
for ( var i = 1; i < this.positions.length; i++ ) {
context.lineTo ( this.positions[i][0], this.positions[i][1], this.positions[i][2], 0, 2*Math.PI );
}
context.stroke ();
context.fillStyle = fieldColor;
context.beginPath ();
context.arc ( this.positions[0][0], this.positions[0][1], this.positions[0][2]*0.6, 0, 2*Math.PI );
context.fill();
}
}
var Match = function ( ) {
// Game running flag
this.running = false;
// Game paused flag
this.paused = false;
// Match snake
this.snake = new Snake ( );
// Current fruit position
// Coordinates relative to field center
this.fruits = [ [0,0] ];
// Score
this.score = 0;
this.scoreNoCombo = 0;
this.scoreShow = 0;
this.combo = 0;
this.comboTime = 0;
this.combometerShow = 0;
// Obstacles
this.obstacles = [];
// Time
this.t = 0;
this.targetScaling = initialScaling;
this.scaling = initialScaling;
// Last time of obstacle spawn
this.lastObst = 0;
// Death time
this.deathTime = 0;
// Function that sets up the game
this.setup = function ( ) {
this.snake = new Snake ( );
this.obstacles = [];
this.snake.positions.push ( [0,snakeRotateRadius,snakePointRadius] );
this.snake.grow ( snakeInitialLength );
this.fruits = [];
this.score = 0;
this.scoreShow = 0;
this.scoreNoCombo = 0;
this.combo = 0;
this.comboTime = 0;
this.running = true;
this.t = 0;
this.scaling = initialScaling;
this.targetScaling = initialScaling;
this.spawnFruit ( 0 );
this.lastObst = 0;
ga ( 'send', 'event', 'game', 'start' );
}
// Function that draws the game
this.draw = function ( context ) {
context.fillStyle = fieldColor;
context.beginPath();
context.arc ( 0, 0, radius, 0, 2 * Math.PI );
context.fill();
context.save ();
context.scale ( this.scaling, this.scaling );
this.snake.draw ( context );
context.fillStyle = obstColor;
for ( var i = 0; i < this.obstacles.length; ++i ) {
context.beginPath ( );
context.arc ( this.obstacles[i][0], this.obstacles[i][1], this.obstacles[i][2], 0, 2*Math.PI );
context.fill();
}
context.fillStyle = fruitColor;
for ( var i = 0; i < this.fruits.length; ++i ) if ( this.fruits[i][3] <= 0 ) {
context.beginPath();
context.arc ( this.fruits[i][0], this.fruits[i][1], this.fruits[i][2], 0, 2*Math.PI );
context.fill();
}
context.restore();
if ( this.combo > 0 ) {
context.lineCap = "round";
context.strokeStyle = fieldColor;
context.lineWidth = 10;
context.beginPath();
context.arc ( 0, 0, radius + 10, -Math.PI * this.combometerShow, Math.PI * this.combometerShow );
context.stroke();
}
}
// Function that updates the game
this.update = function ( t ) {
// Animate scaling
this.scaling += (this.targetScaling - this.scaling) * t * 5;
// Animate score
this.scoreShow += (this.score - this.scoreShow) * 10 * t;
if ( !this.running || this.paused ) return;
this.t += t;
// Animate obstacles
for ( var i = 0; i < this.obstacles.length; ++i ) {
var x = (this.t - this.obstacles[i][4]);
this.obstacles[i][2] = this.obstacles[i][3] * outElastic(x);
}
// Animate fruit
for ( var i = 0; i < this.fruits.length; ++i ) {
if ( this.fruits[i][3] <= 0 ) this.fruits[i][2] += ( fruitRadius - this.fruits[i][2] ) * t * 10;
else { this.fruits[i][3] -= t; if ( this.fruits[i][3] <= 0 ) this.spawnFruit(i); }
}
var oldPositions = [];
for ( var i = 0; i < this.snake.positions.length; ++i )
oldPositions.push ( this.snake.positions[i].slice(0) );
var tt = t * ( maxSpeedFactor - Math.exp( -speedIncreaseParam * this.scoreNoCombo + logMSF ) );
this.snake.move ( tt );
// Check if the snake has hit some fruit
var r = fruitRadius + snakePointRadius;
for ( var i = 0; i < this.fruits.length; ++i ) {
if ( this.fruits[i][3] <= 0 && dist2 ( this.snake.positions[0], this.fruits[i] ) < r*r ) {
this.score += 1 + this.combo;
this.scoreNoCombo++;
localStorage.bestScore = Math.max ( localStorage.bestScore, this.score );
this.spawnFruit(i);
if ( Math.random() * Math.exp(3 * this.fruits.length) < this.scoreNoCombo ) this.spawnFruit(this.fruits.length);
this.snake.eat();
this.targetScaling = minScaling + ( initialScaling - minScaling ) * Math.exp ( -this.scoreNoCombo * scalingK );
this.combo += 1;
this.comboTime = this.t;
console.log ( "FRUIT" );
}
}
// Check if the snake has hit the border
r = radius / this.scaling - this.snake.positions[0][2] + 1;
if ( dist2 ( this.snake.positions[0], [0,0] ) > r*r ) {
this.running = false;
this.deathTime = Date.now();
console.log ( "BORDER" );
}
// Check if the snake has hit some parts of itself
r = 2*snakePointRadius-1;
var first = Math.ceil((2 * Math.PI * snakeRotateRadius - 2*snakePointRadius) / (tt * this.snake.speed));
for ( var i = first; i < this.snake.positions.length; ++i )
if ( dist2 ( this.snake.positions[0], this.snake.positions[i] ) < r*r &&
dist2 ( oldPositions[0], oldPositions[i] ) >= r*r ) {
this.running = false;
this.deathTime = Date.now();
console.log ( "SELF " + i );
}
// Check if the snake has hit some obstacle
for ( var i = 0; i < this.obstacles.length; ++i ) {
r = snakePointRadius + this.obstacles[i][2];
if ( dist2 ( this.snake.positions[0], this.obstacles[i] ) < r*r ) {
this.running = false;
this.deathTime = Date.now();
console.log ( "OBSTACLE" );
}
}
// Spawn obstacles
if ( Math.random() * 10000 < this.t - this.lastObst ) {
this.spawnObst();
this.lastObst = this.t;
}
// Combo timer
if ( this.t - this.comboTime > comboDuration ) {
this.combo = 0;
}
else {
targetCombometer = 1 - (this.t - this.comboTime) / comboDuration;
this.combometerShow += ( targetCombometer - this.combometerShow ) * 10 * t;
}
if ( !this.running ) {
ga ( 'send', 'event', 'game', 'end', this.score );
shake();
}
}
// Spawns a fruit in the game
this.spawnFruit = function ( j, delay ) {
if ( delay == undefined ) delay = 0;
if ( j == undefined || this.fruits.length <= j) {
this.fruits.push ( [0,0,0,0] );
j = this.fruits.length - 1;
}
var r = Math.random() * (radius / this.scaling - fruitRadius - 5);
var th = Math.random() * 2*Math.PI;
this.fruits[j][0] = r * Math.cos(th);
this.fruits[j][1] = r * Math.sin(th);
this.fruits[j][2] = 0;
this.fruits[j][3] = delay;
if ( delay > 0 ) return;
for ( var i = 0; i < this.obstacles.length; ++i ) {
var rr = fruitRadius + ( this.obstacles[i][3] + 5 );
if ( dist2 ( this.fruits[j], this.obstacles[i] ) < rr*rr ) {
this.spawnFruit(j,delay);
break;
}
}
var rr = fruitRadius + snakePointRadius + 10;
for ( var k = 0; k < this.snake.positions.length; k++ ) {
if ( dist2 ( this.fruits[j], this.snake.positions[k] ) < rr*rr ) {
this.spawnFruit(j,delay);
break;
}
}
var rr = 2 * fruitRadius + 30;
for ( var i = 0; i < this.fruits.length; ++i ) {
if ( i != j && dist2 ( this.fruits[j], this.fruits[i] ) < rr*rr ) {
this.spawnFruit(j,delay);
break;
}
}
}
// Spawns an obstacle
this.spawnObst = function () {
var r, rho, th, obst;
var again;
var k = 0;
do {
rho = Math.random() * radius / this.scaling;
r = ( Math.random() * 20 + 30 ) / this.scaling;;
th = Math.random() * 2*Math.PI;
obst = [ rho * Math.cos(th), rho * Math.sin(th), 0, r, this.t ];
again = ( dist2 ( this.snake.positions[0], obst ) < (r + snakePointRadius + snakeRotateRadius)*(r + snakePointRadius + snakeRotateRadius) );
// Also check that the obstacle doesn't cover any fruit
for ( var i = 0; i < this.fruits.length && !again; ++i )
again = ( dist2 ( this.fruits[i], obst ) < (r + fruitRadius)*(r + fruitRadius) );
// And also check that it doesn't cover the tail
for ( var i = 0; i < this.snake.positions.length && !again; ++i )
again = ( dist2 ( this.snake.positions[i], obst ) < (r + snakePointRadius)*(r + snakePointRadius) );
// Finally, check that it is not too close to other obstacles
for ( var i = 0; i < this.obstacles.length && !again; ++i ) {
var rr = r + this.obstacles[i][3] + 2*snakePointRadius;
again = ( dist2 ( this.obstacles[i], obst ) < rr*rr );
}
// At last, the first time it does not touch the border, repeat
if ( !again && k == 0 && (rho + 0.5*r) < radius / this.scaling ) { k = 1; again = true; }
} while ( again );
this.obstacles.push ( obst );
}
}
var match = new Match ();
var screenshake = 0;
var screenshakeBegin = -1;
var shake = function () { screenshakeBegin = Date.now(); }
var keys = new Array();
var registerKey = function ( n, k ) { keys.push ( { name: n, key: k, state: undefined } ); };
var key = function ( name ) {
var result = 0;
for ( i = 0; i < keys.length; i++ )
if ( keys[i].name == name ) result = result || keys[i].state;
return result;
}
document.onkeydown = function ( e ) {
for ( var i = 0; i < keys.length; i++ )
if ( e.which == keys[i].key ) keys[i].state = 1;
}
document.onkeyup = function ( e ) {
for ( var i = 0; i < keys.length; i++ )
if ( e.which == keys[i].key ) keys[i].state = 0;
}
var setup = function ( ) {
canvas = document.getElementById ( "canvas" );
context = canvas.getContext ( "2d" );
resize();
document.getElementById("body").onresize = resize;
radius = 640 * radiusFactor;
requestAnimationFrame ( frame );
bgColor = $("body").css("background-color");
obstColor = bgColor;
match.setup ();
document.onkeydown = function ( e ) {
if ( e.which == 82 ) match.setup();
if ( e.which == 32 ) {
if ( match.running && !match.paused ) match.snake.rotate *= -1;
else if ( match.paused ) match.paused = false;
else if ( Date.now() - match.deathTime > 500 ) match.setup();
}
if ( e.which == 80 ) { match.paused = !match.paused; }
}
document.onmousedown = function ( e ) {
e.preventDefault();
if ( match.running && !match.paused ) match.snake.rotate *= -1;
else if ( match.paused ) match.paused = false;
else if ( Date.now() - match.deathTime > 500 ) match.setup();
}
ifvisible.on("blur", function() { match.paused = true; } );
canvas.addEventListener("touchstart", document.onmousedown );
if ( localStorage.bestScore == undefined ) localStorage.bestScore = 0;
}
var resize = function ( ) {
//vport = window.visualViewport;
canvas.width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0 );
canvas.height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
window.overallScaling = Math.min(canvas.width,canvas.height) / 640;
if ( canvas.width < canvas.height ) window.mobile = true;
else window.mobile = false;
}
var frame = function ( ) {
draw ();
update ();
requestAnimationFrame ( frame );
}
var draw = function ( ) {
context.save ( );
context.translate ( screenshake, 0 );
context.fillStyle = bgColor;
context.fillRect ( 0, 0, canvas.width, canvas.height );
context.translate ( canvas.width / 2, canvas.height / 2 );
context.scale ( overallScaling * (mobile ? 0.45/radiusFactor : 1), overallScaling * (mobile ? 0.45/radiusFactor : 1) );
match.draw ( context );
if ( !mobile ) {
context.fillStyle = scoreColor;
context.font = scoreSize + "px Varela Round";
context.textAlign = "end";
context.textBaseline = "top";
context.fillText ( Math.round(match.scoreShow), -radius + 100, -radius - 60 );
context.font = bestScoreSize + "px Varela Round";
context.fillText ( "combo x" + match.combo, -radius + 65, -radius + 20 );
context.fillText ( "best " + localStorage.bestScore, -radius + 40, -radius + 50 );
}
if ( !match.running ) {
context.fillStyle = bgColor;
context.fillRect ( -canvas.width/2, -scoreSize*0.55, canvas.width, scoreSize*1.1 );
context.font = scoreSize + "px Varela Round";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = scoreColor;
context.fillText ( "GAME OVER", 0, 3 );
}
if ( match.paused ) {
context.fillStyle = bgColor;
context.fillRect ( -canvas.width/2, -scoreSize*0.55, canvas.width, scoreSize*1.1 );
context.font = scoreSize + "px Varela Round";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = scoreColor;
context.fillText ( "PAUSED", 0, 3 );
}
context.restore ();
if ( mobile ) {
context.fillStyle = scoreColor;
context.font = scoreSize*2 + "px Varela Round";
context.textAlign = "start";
context.textBaseline = "hanging";
context.fillText ( Math.round(match.scoreShow), 10, 10 );
context.font = bestScoreSize*2 + "px Varela Round";
context.textAlign = "end";
context.fillText ( "combo x" + match.combo, canvas.width - 10, 10 );
context.fillText ( "best " + localStorage.bestScore, canvas.width - 10, 20 + bestScoreSize*2 );
}
}
var update = function ( ) {
var now = Date.now();
if ( window.lastUpdateTime == undefined ) match.update ( 0.01 );
else match.update ( (now - lastUpdateTime) / 1000 );
if ( window.lastUpdateTime )
window.fps = 1000 / (now - lastUpdateTime);
else window.fps = 60;
window.lastUpdateTime = now;
if ( screenshakeBegin > 0 ) {
var t = now - screenshakeBegin;
screenshake = screenshakeWidth * Math.exp(- t / 150 ) * ( Math.sin(t/30) + Math.cos(t/30) );
}
}
</script>
</head>
<body id="body" onload="setup()">
<canvas width="640" height="640" id="canvas"></canvas>
<a href="https://github.com/michelebucelli/csnake" target="_blank"><img src="./github.png" id="ghubmark"></img></a>
</body>
</html>