-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
81 lines (67 loc) · 1.64 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
const TOTAL = 100;
let birds = [];
let savedBirds = [];
let pipes = [];
let counter = 0;
let slider;
function keyPressed() {
if (key === 'S') {
let bird = birds[0];
saveJSON(bird.brain, 'bird.json');
}
}
function setup() {
createCanvas(640, 480);
tf.setBackend('cpu');
slider = createSlider(1, 10, 1);
for (let i = 0; i < TOTAL; i++) {
birds[i] = new Bird();
}
}
function draw() {
for (let n = 0; n < slider.value(); n++) {
if (counter % 75 == 0) {
pipes.push(new Pipe(counter + 1));
}
counter++;
for (let i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
for (let j = birds.length - 1; j >= 0; j--) {
if (pipes[i].hits(birds[j])) {
savedBirds.push(birds.splice(j, 1)[0]);
}
}
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
for (let i = birds.length - 1; i >= 0; i--) {
if (birds[i].offScreen()) {
savedBirds.push(birds.splice(i, 1)[0]);
}
}
for (let bird of birds) {
bird.think(pipes);
bird.update();
}
if (birds.length === 0) {
counter = 0;
nextGeneration();
pipes = [];
}
}
// All the drawing stuff
background(50, 100, 230);
for (let bird of birds) {
bird.show();
}
for (let pipe of pipes) {
pipe.show();
}
}
// function keyPressed() {
// if (key == ' ') {
// bird.up();
// //console.log("SPACE");
// }
// }