Skip to content

Commit f91c7cc

Browse files
committed
minor improvements
1 parent 89bfa5d commit f91c7cc

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

examples/NeuroEvolution-flappy-bird/bird.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Bird {
55
this.brain = brain;
66
} else {
77
this.brain = ml5.neuralNetwork({
8-
inputs: 5,
8+
inputs: 4,
99
outputs: ["flap", "no flap"],
1010
task: "classification",
1111
neuroEvolution: true,
@@ -29,7 +29,7 @@ class Bird {
2929
think(pipes) {
3030
let nextPipe = null;
3131
for (let pipe of pipes) {
32-
if (pipe.x > this.x) {
32+
if (pipe.x + pipe.w > this.x) {
3333
nextPipe = pipe;
3434
break;
3535
}
@@ -39,7 +39,6 @@ class Bird {
3939
this.y / height,
4040
this.velocity / height,
4141
nextPipe.top / height,
42-
nextPipe.bottom / height,
4342
(nextPipe.x - this.x) / width,
4443
];
4544

@@ -59,12 +58,11 @@ class Bird {
5958
this.velocity += this.gravity;
6059
this.y += this.velocity;
6160
// Dampen velocity
62-
this.velocity *= 0.9;
61+
this.velocity *= 0.95;
6362

6463
// Handle the "floor"
65-
if (this.y > height) {
66-
this.y = height;
67-
this.velocity = 0;
64+
if (this.y > height || this.y < 0) {
65+
this.alive = false;
6866
}
6967

7068
this.fitness++;

examples/NeuroEvolution-flappy-bird/pipe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Pipe {
22
constructor() {
3-
this.spacing = 75;
3+
this.spacing = 100;
44
this.top = random(height - this.spacing);
55
this.bottom = this.top + this.spacing;
66
this.x = width;

examples/NeuroEvolution-flappy-bird/sketch.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function draw() {
1515
background(255);
1616

1717
for (let i = pipes.length - 1; i >= 0; i--) {
18-
pipes[i].show();
1918
pipes[i].update();
19+
pipes[i].show();
2020
if (pipes[i].offscreen()) {
2121
pipes.splice(i, 1);
2222
}
@@ -35,15 +35,13 @@ function draw() {
3535
}
3636
}
3737

38-
if (frameCount % 75 == 0) {
38+
if (frameCount % 100 == 0) {
3939
pipes.push(new Pipe());
4040
}
4141

4242
if (allBirdsDead()) {
4343
normalizeFitness();
4444
reproduction();
45-
pipes = [];
46-
pipes.push(new Pipe());
4745
}
4846
}
4947

@@ -62,7 +60,7 @@ function reproduction() {
6260
let parentA = weightedSelection();
6361
let parentB = weightedSelection();
6462
let child = parentA.brain.crossover(parentB.brain);
65-
child.mutate(0.1);
63+
child.mutate(0.01);
6664
nextBirds[i] = new Bird(child);
6765
}
6866
birds = nextBirds;

0 commit comments

Comments
 (0)