-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBird.js
70 lines (61 loc) · 1.47 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
class Bird {
constructor(brain) {
this.x = 40;
this.y = height / 2;
this.velocity = 0;
this.score = 0;
this.fitness = 0;
if (brain) {
this.brain = brain.copy();
} else {
this.brain = new NeuralNetwork(5, 8, 2, brain);
}
}
dispose() {
this.brain.dispose();
}
draw() {
fill(243, 145, 145);
// rect(this.x, this.y, 40, 40);
push();
imageMode(CENTER);
translate(this.x, this.y);
rotate(map(this.velocity, -10, 10, -45, 45));
image(birdImg, 0, 0, 40, 40);
pop();
}
update() {
this.score++;
this.velocity += GRAVITY;
this.y += this.velocity;
}
jump() {
this.velocity -= 12;
}
isOffScreen() {
return this.y > height || this.y < 0;
}
think(pipes) {
let closestPipe = null;
let shortestDist = Infinity;
for (let pipe of pipes) {
let dist = pipe.x + Pipe.w - this.x;
if (dist < shortestDist && dist > 0) {
shortestDist = dist;
closestPipe = pipe;
}
}
let inputs = [
this.y / height, //current height
closestPipe.x / width, //closest pipe's x
closestPipe.top / height, //closest pipe's top
closestPipe.bottom / height, //closest pipe's bottom
this.velocity / 10, //current velocity
];
let outputs = this.brain.predict(inputs);
if (outputs[0] > outputs[1]) this.jump();
}
mutate() {
this.brain.mutate(0.1);
}
}