-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbird.js
executable file
·52 lines (42 loc) · 1.06 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
// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
// This flappy bird implementation is adapted from:
// https://youtu.be/cXgA1d_E-jY&
// Mutation function to be passed into bird.brain
class Bird {
constructor() {
// position and size of bird
this.x = 64;
this.y = height / 2;
this.r = 12;
// Gravity, lift and velocity
this.gravity = 0.4;
this.lift = -6;
this.velocity = 0;
// Score is how many frames it's been alive
this.score = 0;
}
// Display the bird
show() {
fill(133);
stroke(255);
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
// 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++;
}
}