-
Notifications
You must be signed in to change notification settings - Fork 4
/
FlappyLearning.html
947 lines (841 loc) · 30.3 KB
/
FlappyLearning.html
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
<!DOCTYPE html>
<html>
<head>
<title>NeuroEvolution : Flappy Bird</title>
<style>
body {
background: #2d577b;
}
button {
font-family: Oswald;
transition: all 1s;
border-radius: 20px;
}
.flap, div {
width: 500;
height: 500;
aspect-ratio: auto 500 / 512;
}
button:hover {
filter: blur(0.4px) brightness(0.9);
border-radius: 5px;
}
canvas, .flap {
border-radius: 30px;
transition: all 1s;
}
canvas:hover, .flap:hover {
border-radius: 10px;
opacity: 0.7;
}
canvas:active, .flap:active {
border-radius: 5px;
opacity: 0.5;
filter: blur(0.4px) brightness(0.9);
}
</style>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
</head>
<body>
<canvas id="flappy" width="500" height="512"></canvas> <br/>
<button onclick="speed(60)">x1</button>
<button onclick="speed(120)">x2</button>
<button onclick="speed(180)">x3</button>
<button onclick="speed(300)">x5</button>
<div id="flap" width="500" height="512"></div>
<button onclick="speed(0)">MAX</button>
<br/>
<script id = "Neuroevolution.js">
/**
* Provides a set of classes and methods for handling Neuroevolution and
* genetic algorithms.
*
* @param {options} An object of options for Neuroevolution.
*/
var Neuroevolution = function (options) {
var self = this; // reference to the top scope of this module
// Declaration of module parameters (options) and default values
self.options = {
/**
* Logistic activation function.
*
* @param {a} Input value.
* @return Logistic function output.
*/
activation: function (a) {
ap = (-a) / 1;
return (1 / (1 + Math.exp(ap)))
},
/**
* Returns a random value between -1 and 1.
*
* @return Random value.
*/
randomClamped: function () {
return Math.random() * 2 - 1;
},
// various factors and parameters (along with default values).
network: [1, [1], 1], // Perceptron network structure (1 hidden
// layer).
population: 50, // Population by generation.
elitism: 0.2, // Best networks kepts unchanged for the next
// generation (rate).
randomBehaviour: 0.2, // New random networks for the next generation
// (rate).
mutationRate: 0.1, // Mutation rate on the weights of synapses.
mutationRange: 0.5, // Interval of the mutation changes on the
// synapse weight.
historic: 0, // Latest generations saved.
lowHistoric: false, // Only save score (not the network).
scoreSort: -1, // Sort order (-1 = desc, 1 = asc).
nbChild: 1 // Number of children by breeding.
}
/**
* Override default options.
*
* @param {options} An object of Neuroevolution options.
* @return void
*/
self.set = function (options) {
for (var i in options) {
if (this.options[i] != undefined) { // Only override if the passed in value
// is actually defined.
self.options[i] = options[i];
}
}
}
// Overriding default options with the pass in options
self.set(options);
/*NEURON**********************************************************************/
/**
* Artificial Neuron class
*
* @constructor
*/
var Neuron = function () {
this.value = 0;
this.weights = [];
}
/**
* Initialize number of neuron weights to random clamped values.
*
* @param {nb} Number of neuron weights (number of inputs).
* @return void
*/
Neuron.prototype.populate = function (nb) {
this.weights = [];
for (var i = 0; i < nb; i++) {
this.weights.push(self.options.randomClamped());
}
}
/*LAYER***********************************************************************/
/**
* Neural Network Layer class.
*
* @constructor
* @param {index} Index of this Layer in the Network.
*/
var Layer = function (index) {
this.id = index || 0;
this.neurons = [];
}
/**
* Populate the Layer with a set of randomly weighted Neurons.
*
* Each Neuron be initialied with nbInputs inputs with a random clamped
* value.
*
* @param {nbNeurons} Number of neurons.
* @param {nbInputs} Number of inputs.
* @return void
*/
Layer.prototype.populate = function (nbNeurons, nbInputs) {
this.neurons = [];
for (var i = 0; i < nbNeurons; i++) {
var n = new Neuron();
n.populate(nbInputs);
this.neurons.push(n);
}
}
/*NEURAL NETWORK**************************************************************/
/**
* Neural Network class
*
* Composed of Neuron Layers.
*
* @constructor
*/
var Network = function () {
this.layers = [];
}
/**
* Generate the Network layers.
*
* @param {input} Number of Neurons in Input layer.
* @param {hidden} Number of Neurons per Hidden layer.
* @param {output} Number of Neurons in Output layer.
* @return void
*/
Network.prototype.perceptronGeneration = function (input, hiddens, output) {
var index = 0;
var previousNeurons = 0;
var layer = new Layer(index);
layer.populate(input, previousNeurons); // Number of Inputs will be set to
// 0 since it is an input layer.
previousNeurons = input; // number of input is size of previous layer.
this.layers.push(layer);
index++;
for (var i in hiddens) {
// Repeat same process as first layer for each hidden layer.
var layer = new Layer(index);
layer.populate(hiddens[i], previousNeurons);
previousNeurons = hiddens[i];
this.layers.push(layer);
index++;
}
var layer = new Layer(index);
layer.populate(output, previousNeurons); // Number of input is equal to
// the size of the last hidden
// layer.
this.layers.push(layer);
}
/**
* Create a copy of the Network (neurons and weights).
*
* Returns number of neurons per layer and a flat array of all weights.
*
* @return Network data.
*/
Network.prototype.getSave = function () {
var datas = {
neurons: [], // Number of Neurons per layer.
weights: [] // Weights of each Neuron's inputs.
};
for (var i in this.layers) {
datas.neurons.push(this.layers[i].neurons.length);
for (var j in this.layers[i].neurons) {
for (var k in this.layers[i].neurons[j].weights) {
// push all input weights of each Neuron of each Layer into a flat
// array.
datas.weights.push(this.layers[i].neurons[j].weights[k]);
}
}
}
return datas;
}
/**
* Apply network data (neurons and weights).
*
* @param {save} Copy of network data (neurons and weights).
* @return void
*/
Network.prototype.setSave = function (save) {
var previousNeurons = 0;
var index = 0;
var indexWeights = 0;
this.layers = [];
for (var i in save.neurons) {
// Create and populate layers.
var layer = new Layer(index);
layer.populate(save.neurons[i], previousNeurons);
for (var j in layer.neurons) {
for (var k in layer.neurons[j].weights) {
// Apply neurons weights to each Neuron.
layer.neurons[j].weights[k] = save.weights[indexWeights];
indexWeights++; // Increment index of flat array.
}
}
previousNeurons = save.neurons[i];
index++;
this.layers.push(layer);
}
}
/**
* Compute the output of an input.
*
* @param {inputs} Set of inputs.
* @return Network output.
*/
Network.prototype.compute = function (inputs) {
// Set the value of each Neuron in the input layer.
for (var i in inputs) {
if (this.layers[0] && this.layers[0].neurons[i]) {
this.layers[0].neurons[i].value = inputs[i];
}
}
var prevLayer = this.layers[0]; // Previous layer is input layer.
for (var i = 1; i < this.layers.length; i++) {
for (var j in this.layers[i].neurons) {
// For each Neuron in each layer.
var sum = 0;
for (var k in prevLayer.neurons) {
// Every Neuron in the previous layer is an input to each Neuron in
// the next layer.
sum += prevLayer.neurons[k].value *
this.layers[i].neurons[j].weights[k];
}
// Compute the activation of the Neuron.
this.layers[i].neurons[j].value = self.options.activation(sum);
}
prevLayer = this.layers[i];
}
// All outputs of the Network.
var out = [];
var lastLayer = this.layers[this.layers.length - 1];
for (var i in lastLayer.neurons) {
out.push(lastLayer.neurons[i].value);
}
return out;
}
/*GENOME**********************************************************************/
/**
* Genome class.
*
* Composed of a score and a Neural Network.
*
* @constructor
*
* @param {score}
* @param {network}
*/
var Genome = function (score, network) {
this.score = score || 0;
this.network = network || null;
}
/*GENERATION******************************************************************/
/**
* Generation class.
*
* Composed of a set of Genomes.
*
* @constructor
*/
var Generation = function () {
this.genomes = [];
}
/**
* Add a genome to the generation.
*
* @param {genome} Genome to add.
* @return void.
*/
Generation.prototype.addGenome = function (genome) {
// Locate position to insert Genome into.
// The gnomes should remain sorted.
for (var i = 0; i < this.genomes.length; i++) {
// Sort in descending order.
if (self.options.scoreSort < 0) {
if (genome.score > this.genomes[i].score) {
break;
}
// Sort in ascending order.
} else {
if (genome.score < this.genomes[i].score) {
break;
}
}
}
// Insert genome into correct position.
this.genomes.splice(i, 0, genome);
}
/**
* Breed to genomes to produce offspring(s).
*
* @param {g1} Genome 1.
* @param {g2} Genome 2.
* @param {nbChilds} Number of offspring (children).
*/
Generation.prototype.breed = function (g1, g2, nbChilds) {
var datas = [];
for (var nb = 0; nb < nbChilds; nb++) {
// Deep clone of genome 1.
var data = JSON.parse(JSON.stringify(g1));
for (var i in g2.network.weights) {
// Genetic crossover
// 0.5 is the crossover factor.
// FIXME Really should be a predefined constant.
if (Math.random() <= 0.5) {
data.network.weights[i] = g2.network.weights[i];
}
}
// Perform mutation on some weights.
for (var i in data.network.weights) {
if (Math.random() <= self.options.mutationRate) {
data.network.weights[i] += Math.random() *
self.options.mutationRange *
2 -
self.options.mutationRange;
}
}
datas.push(data);
}
return datas;
}
/**
* Generate the next generation.
*
* @return Next generation data array.
*/
Generation.prototype.generateNextGeneration = function () {
var nexts = [];
for (var i = 0; i < Math.round(self.options.elitism *
self.options.population); i++) {
if (nexts.length < self.options.population) {
// Push a deep copy of ith Genome's Nethwork.
nexts.push(JSON.parse(JSON.stringify(this.genomes[i].network)));
}
}
for (var i = 0; i < Math.round(self.options.randomBehaviour *
self.options.population); i++) {
var n = JSON.parse(JSON.stringify(this.genomes[0].network));
for (var k in n.weights) {
n.weights[k] = self.options.randomClamped();
}
if (nexts.length < self.options.population) {
nexts.push(n);
}
}
var max = 0;
while (true) {
for (var i = 0; i < max; i++) {
// Create the children and push them to the nexts array.
var childs = this.breed(this.genomes[i], this.genomes[max],
(self.options.nbChild > 0 ? self.options.nbChild : 1));
for (var c in childs) {
nexts.push(childs[c].network);
if (nexts.length >= self.options.population) {
// Return once number of children is equal to the
// population by generatino value.
return nexts;
}
}
}
max++;
if (max >= this.genomes.length - 1) {
max = 0;
}
}
}
/*GENERATIONS*****************************************************************/
/**
* Generations class.
*
* Hold's previous Generations and current Generation.
*
* @constructor
*/
var Generations = function () {
this.generations = [];
var currentGeneration = new Generation();
}
/**
* Create the first generation.
*
* @param {input} Input layer.
* @param {input} Hidden layer(s).
* @param {output} Output layer.
* @return First Generation.
*/
Generations.prototype.firstGeneration = function (input, hiddens, output) {
// FIXME input, hiddens, output unused.
var out = [];
for (var i = 0; i < self.options.population; i++) {
// Generate the Network and save it.
var nn = new Network();
nn.perceptronGeneration(self.options.network[0],
self.options.network[1],
self.options.network[2]);
out.push(nn.getSave());
}
this.generations.push(new Generation());
return out;
}
/**
* Create the next Generation.
*
* @return Next Generation.
*/
Generations.prototype.nextGeneration = function () {
if (this.generations.length == 0) {
// Need to create first generation.
return false;
}
var gen = this.generations[this.generations.length - 1]
.generateNextGeneration();
this.generations.push(new Generation());
return gen;
}
/**
* Add a genome to the Generations.
*
* @param {genome}
* @return False if no Generations to add to.
*/
Generations.prototype.addGenome = function (genome) {
// Can't add to a Generation if there are no Generations.
if (this.generations.length == 0) return false;
// FIXME addGenome returns void.
return this.generations[this.generations.length - 1].addGenome(genome);
}
/*SELF************************************************************************/
self.generations = new Generations();
/**
* Reset and create a new Generations object.
*
* @return void.
*/
self.restart = function () {
self.generations = new Generations();
}
/**
* Create the next generation.
*
* @return Neural Network array for next Generation.
*/
self.nextGeneration = function () {
var networks = [];
if (self.generations.generations.length == 0) {
// If no Generations, create first.
networks = self.generations.firstGeneration();
} else {
// Otherwise, create next one.
networks = self.generations.nextGeneration();
}
// Create Networks from the current Generation.
var nns = [];
for (var i in networks) {
var nn = new Network();
nn.setSave(networks[i]);
nns.push(nn);
}
if (self.options.lowHistoric) {
// Remove old Networks.
if (self.generations.generations.length >= 2) {
var genomes =
self.generations
.generations[self.generations.generations.length - 2]
.genomes;
for (var i in genomes) {
delete genomes[i].network;
}
}
}
if (self.options.historic != -1) {
// Remove older generations.
if (self.generations.generations.length > self.options.historic + 1) {
self.generations.generations.splice(0,
self.generations.generations.length - (self.options.historic + 1));
}
}
return nns;
}
/**
* Adds a new Genome with specified Neural Network and score.
*
* @param {network} Neural Network.
* @param {score} Score value.
* @return void.
*/
self.networkScore = function (network, score) {
self.generations.addGenome(new Genome(score, network.getSave()));
}
}
</script>
<script id = "game.js">
(function() {
var timeouts = [];
var messageName = "zero-timeout-message";
function setZeroTimeout(fn) {
timeouts.push(fn);
window.postMessage(messageName, "*");
}
function handleMessage(event) {
if (event.source == window && event.data == messageName) {
event.stopPropagation();
if (timeouts.length > 0) {
var fn = timeouts.shift();
fn();
}
}
}
window.addEventListener("message", handleMessage, true);
window.setZeroTimeout = setZeroTimeout;
})();
var Neuvol;
var game;
var FPS = 60;
var maxScore=0;
var images = {};
var speed = function(fps){
FPS = parseInt(fps);
}
var loadImages = function(sources, callback){
var nb = 0;
var loaded = 0;
var imgs = {};
for(var i in sources){
nb++;
imgs[i] = new Image();
imgs[i].src = sources[i];
imgs[i].onload = function(){
loaded++;
if(loaded == nb){
callback(imgs);
}
}
}
}
var Bird = function(json){
this.x = 80;
this.y = 250;
this.width = 40;
this.height = 30;
this.alive = true;
this.gravity = 0;
this.velocity = 0.3;
this.jump = -6;
this.init(json);
}
Bird.prototype.init = function(json){
for(var i in json){
this[i] = json[i];
}
}
Bird.prototype.flap = function(){
this.gravity = this.jump;
}
Bird.prototype.update = function(){
this.gravity += this.velocity;
this.y += this.gravity;
}
Bird.prototype.isDead = function(height, pipes){
if(this.y >= height || this.y + this.height <= 0){
return true;
}
for(var i in pipes){
if(!(
this.x > pipes[i].x + pipes[i].width ||
this.x + this.width < pipes[i].x ||
this.y > pipes[i].y + pipes[i].height ||
this.y + this.height < pipes[i].y
)){
return true;
}
}
}
var Pipe = function(json){
this.x = 0;
this.y = 0;
this.width = 50;
this.height = 40;
this.speed = 3;
this.init(json);
}
Pipe.prototype.init = function(json){
for(var i in json){
this[i] = json[i];
}
}
Pipe.prototype.update = function(){
this.x -= this.speed;
}
Pipe.prototype.isOut = function(){
if(this.x + this.width < 0){
return true;
}
}
var Game = function(){
this.pipes = [];
this.birds = [];
this.score = 0;
this.canvas = document.querySelector("#flappy");
this.ctx = this.canvas.getContext("2d");
this.width = this.canvas.width;
this.height = this.canvas.height;
this.spawnInterval = 90;
this.interval = 0;
this.gen = [];
this.alives = 0;
this.generation = 0;
this.backgroundSpeed = 0.5;
this.backgroundx = 0;
this.maxScore = 0;
}
Game.prototype.start = function(){
this.interval = 0;
this.score = 0;
this.pipes = [];
this.birds = [];
this.gen = Neuvol.nextGeneration();
for(var i in this.gen){
var b = new Bird();
this.birds.push(b)
}
this.generation++;
this.alives = this.birds.length;
}
Game.prototype.update = function(){
this.backgroundx += this.backgroundSpeed;
var nextHoll = 0;
if(this.birds.length > 0){
for(var i = 0; i < this.pipes.length; i+=2){
if(this.pipes[i].x + this.pipes[i].width > this.birds[0].x){
nextHoll = this.pipes[i].height/this.height;
break;
}
}
}
for(var i in this.birds){
if(this.birds[i].alive){
var inputs = [
this.birds[i].y / this.height,
nextHoll
];
var res = this.gen[i].compute(inputs);
if(res > 0.5){
this.birds[i].flap();
}
this.birds[i].update();
if(this.birds[i].isDead(this.height, this.pipes)){
this.birds[i].alive = false;
this.alives--;
//console.log(this.alives);
Neuvol.networkScore(this.gen[i], this.score);
if(this.isItEnd()){
this.start();
}
}
}
}
for(var i = 0; i < this.pipes.length; i++){
this.pipes[i].update();
if(this.pipes[i].isOut()){
this.pipes.splice(i, 1);
i--;
}
}
if(this.interval == 0){
var deltaBord = 50;
var pipeHoll = 120;
var hollPosition = Math.round(Math.random() * (this.height - deltaBord * 2 - pipeHoll)) + deltaBord;
this.pipes.push(new Pipe({x:this.width, y:0, height:hollPosition}));
this.pipes.push(new Pipe({x:this.width, y:hollPosition+pipeHoll, height:this.height}));
}
this.interval++;
if(this.interval == this.spawnInterval){
this.interval = 0;
}
this.score++;
this.maxScore = (this.score > this.maxScore) ? this.score : this.maxScore;
var self = this;
if(FPS == 0){
setZeroTimeout(function(){
self.update();
});
}else{
setTimeout(function(){
self.update();
}, 1000/FPS);
}
}
Game.prototype.isItEnd = function(){
for(var i in this.birds){
if(this.birds[i].alive){
return false;
}
}
return true;
}
Game.prototype.display = function(){
this.ctx.clearRect(0, 0, this.width, this.height);
for(var i = 0; i < Math.ceil(this.width / images.background.width) + 1; i++){
this.ctx.drawImage(images.background, i * images.background.width - Math.floor(this.backgroundx%images.background.width), 0)
}
for(var i in this.pipes){
if(i%2 == 0){
this.ctx.drawImage(images.pipetop, this.pipes[i].x, this.pipes[i].y + this.pipes[i].height - images.pipetop.height, this.pipes[i].width, images.pipetop.height);
}else{
this.ctx.drawImage(images.pipebottom, this.pipes[i].x, this.pipes[i].y, this.pipes[i].width, images.pipetop.height);
}
}
this.ctx.fillStyle = "#FFC600";
this.ctx.strokeStyle = "#CE9E00";
for(var i in this.birds){
if(this.birds[i].alive){
this.ctx.save();
this.ctx.translate(this.birds[i].x + this.birds[i].width/2, this.birds[i].y + this.birds[i].height/2);
this.ctx.rotate(Math.PI/2 * this.birds[i].gravity/20);
this.ctx.drawImage(images.bird, -this.birds[i].width/2, -this.birds[i].height/2, this.birds[i].width, this.birds[i].height);
this.ctx.restore();
}
}
this.ctx.fillStyle = "white";
this.ctx.font="20px Oswald, sans-serif";
this.ctx.fillText("Score : "+ this.score, 10, 25);
this.ctx.fillText("Max Score : "+this.maxScore, 10, 50);
this.ctx.fillText("Generation : "+this.generation, 10, 75);
this.ctx.fillText("Alive : "+this.alives+" / "+Neuvol.options.population, 10, 100);
var self = this;
requestAnimationFrame(function(){
self.display();
});
}
window.onload = function(){
var sprites = {
bird:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAYCAYAAACfpi8JAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAadEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjExR/NCNwAAAO1JREFUSEvVlrENg0AMRW8tskDalFko2zDEVanTRUJKkwGYAMnRFV9CBsvfQGIFyQXoczy+vw9KCR6X7iyRCi7PyyMQTcuv7Cj1g1/vXtYKunEaZF64vhsoHQQA+u0ftci8LCcOcyYNxAuhdgLnuO9eq7SynMB1NzPpIFYLdDagszKy25G/A4FDW51xM8I68jMQ9N7aOfWUeFPkhX8xPdrqNBB8A6zeR50YrieJlOuMNaZeSyIQTWtucFZvn7ciawU9ANhsbN5h00CsB7NAXotoR9JB9BSBnAVjM0L/sekF00A08eFvSluihN8G+QAK4pOEG0cfxAAAAABJRU5ErkJggg==",
background:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASAAAAIACAMAAAAVGUEZAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAMAUExURYXHgZbigoPCz4TDz6nX28Llw97xxuTw0O382AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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Uz+BucDe4G9zrfxZu+d+X885E70xs48RY3H30Yr2YbtEtukW36Bbdolt0i25FMqFEHake3aJbdKuNId5lxmXG4G5wN7gb3A3uBneDu8Hd4B7JhBJ1pHoGd4O7wd3g7mUpPnerp2UYbafL6MXtVKnV96igCvJBPsgH+SAf5IN8kA/ywUgmlKgj1eODfJAP8kE+yAf5oM/AXJWIxS1xC2ACTIAJMAEmwASYABNgRjKhRB2pHsAEmAATYAJMgAkwASbA9PmDErVEzagZNaNm1IyaUTNqRs2oI5lQoo5Uj1EzakbNqBk1o2bUjJpRM2qJWqJm1IyaUTNqRs2oGTWjZtSRTChRR6rHqBk1o2bUjJpRM2pGzagZtUQtUTNqRs2oGTWjZtSMmlEz6kgmlKgj1WPUjJpRM2pGzagZNaNm1IxaopaoGTWjZtSMmlEzakbNqBl1JBNK1JHqMWpGzagZNaNm1IyaUTNqRi1RS9SMmlEzakbNqBk1o2bUjDqSCSXqSPUYNaNm1IyaUTNqRs2oGTWjlqglakbNqBk1o2bUjJpRM2pGHcmEEnWkeoyaUf9PRv0bvdmWLhhK41IAAAAASUVORK5CYII="
}
var start = function(){
Neuvol = new Neuroevolution({
population:50,
network:[2, [2], 1],
});
game = new Game();
game.start();
game.update();
game.display();
}
loadImages(sprites, function(imgs){
images = imgs;
start();
})
};
//FOG // EFFECTS
var fograf;
function togglefog() {
var canvas = document.getElementById("fog");
if (!canvas) {
canvas = document.createElement("canvas");
canvas.id = "fog";
canvas.style.position = "absolute";
canvas.style.left = "50%";
canvas.style.transform = "translate(-50%)";
canvas.style.filter = "blur(100px) contrast(1.5)";
canvas.style.mixBlendMode = "lighten";
var container = document.querySelector('#flappy')
//container.insertBefore(canvas);
container.appendChild(canvas)
var context = canvas.getContext("2d");
function updatefog() {
var video =document.querySelector('#flappy')
var rect = video.getBoundingClientRect();
canvas.width = rect.width / 8;
canvas.height = rect.height / 8;
canvas.style.width = rect.width + "px";
canvas.style.height = rect.height + "px";
context.drawImage(video, 0, 0, canvas.width, canvas.height);
fograf = requestAnimationFrame(updatefog);
}
updatefog(canvas);
}
else {
canvas.remove();
cancelAnimationFrame(fograf);
}
togglefog()
}
</script>
</body>
</html>