-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
97 lines (81 loc) · 1.91 KB
/
sketch.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
const POPSIZE = 300;
var highscore = 0;
var birds = [];
var savedBirds = [];
var pipes = [];
var generation = 0;
let counter = 0;
let cycles = 100;
let slider;
//--------------------------------------------------------------------------------
function keyPressed() {
if(key === 's') {
let b = birds[0];
saveJSON(bird.brain, 'bird.json');
}
}
function setup() {
createCanvas(600,400);
slider = createSlider(1, 100, 1);
for(let i = 0; i < POPSIZE; i++) {
birds[i] = new Bird();
}
}
//--------------------------------------------------------------------------------
function draw() {
for(let n = 0; n < slider.value(); n++) {
if(counter % 250 == 0) {
pipes.push(new Pipe());
}
counter++;
for (bird of birds) {
if(bird.hitWall()) {
var index = birds.indexOf(bird);
savedBirds.push(birds.splice(index, 1)[0]);
}
}
for(var i=pipes.length-1; i >= 0; i--) {
for (bird of birds) {
if(bird.hitPipe(pipes[i])) {
var index = birds.indexOf(bird);
savedBirds.push(birds.splice(index, 1)[0]);
}
}
pipes[i].update();
if(pipes[i].offscreen()) {
pipes.splice(i,1);
}
}
//--------------------------------------------------------------------------------
for (bird of birds) {
bird.think(pipes);
bird.update();
}
if(birds.length == 0) {
counter = 0;
nextGeneration();
tempMax = getGenMax();
if(tempMax > highscore) {
highscore = tempMax;
}
generation++;
console.log(generation, tempMax, highscore);
pipes = [];
}
}
//All drawing stuff - not logic
background(0);
for(let bird of birds) {
bird.show();
}
for(let pipe of pipes) {
pipe.show();
}
}
//--------------------------------------------------------------------------------
/*
function keyPressed() {
if(key == ' ') {
bird.up();
}
}*/