-
Notifications
You must be signed in to change notification settings - Fork 17
/
bird.js
105 lines (92 loc) · 2.49 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
100
101
102
103
104
105
// Mutation function to be passed into bird.brain
function mutate(x) {
if (random(1) < 0.1) {
let offset = randomGaussian() * 0.5;
let newx = x + offset;
return newx;
} else {
return x;
}
}
class Bird {
constructor(brain) {
// position and size of bird
this.x = 64;
this.y = height / 2;
this.r = 12;
// Gravity, lift, velocity.
this.gravity = 0.8;
this.lift = -12;
this.velocity = 0;
// Is this a copy of another Bird or a new one?
// The Neural Network is the bird's "brain"
if (brain instanceof NeuralNetwork) {
this.brain = brain.copy();
this.brain.mutate(mutate);
} else {
this.brain = new NeuralNetwork(5, 8, 2);
}
// Score is how many frames it's been alive
this.score = 0;
// Fitness is normalized version of score
this.fitness = 0;
}
// Create a copy of this bird
copy() {
return new Bird(this.brain);
}
// Display the bird
show() {
image(birdSprite, this.x, this.y, this.r * 2, this.r * 2);
}
// This is the key function now that decides
// if it should jump or not jump!
think(pipes) {
// First find the closest pipe
let closest = null;
let record = Infinity;
for (let i = 0; i < pipes.length; i++) {
let diff = pipes[i].x - this.x;
if (diff > 0 && diff < record) {
record = diff;
closest = pipes[i];
}
}
if (closest != null) {
// Now create the inputs to the neural network
let inputs = [];
// x position of closest pipe
inputs[0] = map(closest.x, this.x, width, 0, 1);
// top of closest pipe opening
inputs[1] = map(closest.top, 0, height, 0, 1);
// bottom of closest pipe opening
inputs[2] = map(closest.bottom, 0, height, 0, 1);
// bird's y position
inputs[3] = map(this.y, 0, height, 0, 1);
// bird's y velocity
inputs[4] = map(this.velocity, -5, 5, 0, 1);
// Get the outputs from the network
let action = this.brain.predict(inputs);
// Decide to jump or not!
if (action[1] > action[0]) {
this.up();
}
}
}
// Jump up
up() {
this.velocity += this.lift;
}
bottomTop() {
// Bird dies when hits bottom?
return (this.y > height || this.y < 0);
}
// Update bird's position based on velocity, gravity, etc.
update() {
this.velocity += this.gravity;
// this.velocity *= 0.9;
this.y += this.velocity;
// Every frame it is alive increases the score
this.score++;
}
}