-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird.js
99 lines (85 loc) · 2.31 KB
/
bird.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
// Daniel Shiffman
// Neuro-Evolution Flappy Bird with TensorFlow.js
// http://thecodingtrain.com
// https://youtu.be/cdUNkwXx-I4
function sigmoid(x) {
return 1 / (1 + exp(-x));
}
class Bird {
constructor(brain) {
this.y = height / 2;
this.x = 64;
this.gravity = 0.8;
this.lift = -12;
this.velocity = 0;
this.score = 0;
this.fitness = 0;
if (brain) {
this.brain = brain.copy();
} else {
this.brain = new NeuralNetwork(5, 8, 2);
}
}
dispose() {
this.brain.dispose();
}
show() {
var heading = new p5.Vector(1, 2 * sigmoid(1e-1 * this.velocity) - 1);
heading.setMag(40);
//stroke(255, 0, 0);
//line(this.x, this.y, this.x + heading.x, this.y + heading.y);
push();
strokeWeight(1);
translate(this.x, this.y);
rotate(heading.heading());
stroke(0, 255);
fill(255, 255, 0, 255);
ellipse(0, 0, 48, 32);
ellipse(-16, -8, 24, 16);
fill(255, 0, 0, 255);
ellipse(16, 8, 24, 8);
fill(255, 255, 255, 255);
ellipse(14, -6, 16, 12);
fill(0, 0, 0, 255);
ellipse(18, -6, 4, 4);
pop();
}
up() {
this.velocity += this.lift;
}
mutate() {
this.brain.mutate(0.05);
}
think(pipes) {
// Find the closest pipe
let closest = null;
let closestD = Infinity;
for (let i = 0; i < pipes.length; i++) {
let d = pipes[i].x + pipes[i].w - this.x;
if (d < closestD && d > 0) {
closest = pipes[i];
closestD = d;
}
}
let inputs = [];
inputs[0] = this.y / height;
inputs[1] = closest.top / height;
inputs[2] = closest.bottom / height;
inputs[3] = closest.x / width;
inputs[4] = this.velocity / 10;
let output = this.brain.predict(inputs);
//if (output[0] > output[1] && this.velocity >= 0) {
if (output[0] > output[1]) {
this.up();
}
}
offScreen() {
return this.y > height || this.y < 0;
}
update() {
this.score++;
this.velocity += this.gravity;
//this.velocity *= 0.9;
this.y += this.velocity;
}
}