-
Notifications
You must be signed in to change notification settings - Fork 83
/
bird.js
46 lines (37 loc) · 930 Bytes
/
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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/cXgA1d_E-jY
// Class is exported (eslint flag)
/* exported Bird */
class Bird {
constructor() {
this.y = height / 2;
this.x = 64;
this.gravity = 0.6;
this.lift = -10;
this.velocity = 0;
this.icon = birdSprite;
this.width = 64;
this.height = 64;
}
show() {
// draw the icon CENTERED around the X and Y coords of the bird object
image(this.icon, this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
}
up() {
this.velocity = this.lift;
}
update() {
this.velocity += this.gravity;
this.y += this.velocity;
if (this.y >= height - this.height / 2) {
this.y = height - this.height / 2;
this.velocity = 0;
}
if (this.y <= this.height / 2) {
this.y = this.height / 2;
this.velocity = 0;
}
}
}