This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbird.js
70 lines (56 loc) · 1.46 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
function Bird(decisionMaker) {
var max_height = height - 116;
this.y = (max_height - 100) / 2;
this.x = 40;
this.velocity = 0;
this.lift = -9;
this.gravity = 0.5;
this.w = 32;
this.velocity = 0;
this.score = 0;
this.fitness = 0;
if (decisionMaker instanceof NeuralNetwork) {
this.decisionMaker = decisionMaker.copy();
} else {
this.decisionMaker = new NeuralNetwork(5, 2, 2);
}
this.show = function() {
image(imgBird, this.x, this.y, this.w * 1.4, this.w);
}
this.up = function() {
this.velocity += this.lift;
}
this.update = function() {
this.velocity += this.gravity;
this.y += this.velocity;
this.score += 1;
if (this.y < 0) {
this.y = 0;
this.velocity = 0;
}
}
this.decide = function(pipes) {
let inputs = []
if (pipes.length != 0) {
inputs[0] = map(this.x, this.x, width, -1, 1);
inputs[1] = map(pipes[0].top, 0, height, -1, 1);
inputs[2] = map(pipes[0].bottom, 0, height, -1, 1);
inputs[3] = map(this.y, 0, height, -1, 1);
inputs[4] = map(this.velocity, -5, 5, 0, 1);
let decision = this.decisionMaker.predict(inputs);
if (decision[0] > decision[1] && this.velocity >= 0) {
this.up();
}
} else {
if (counter % 15 == 0 && counter != 0) {
this.up();
}
}
}
this.copy = function() {
return new Bird(this.decisionMaker);
}
this.mutate = function() {
this.brain.mutate(0.1);
}
}