-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird.js
62 lines (56 loc) · 1.6 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
class Bird {
constructor(sprites) {
this.position = createVector(40, height/8)
this.velocity = createVector()
this.gravity = createVector(0,1)
this.lift = -23
this.w = 34
this.h = 24
this.xpos = this.position.x - (this.w / 2)
this.ypos = this.position.y - (this.h / 2)
this.animation = []
if (sprites) {
this.animation = sprites
}
this.animate = false
}
show() {
noStroke()
fill(255,100,100, 0)
ellipse(this.position.x, this.position.y, this.w, this.h)
fill(255,100,100)
if (this.animation.length > 0 && !this.animate) {
image(this.animation[0], this.xpos, this.ypos);
}
if (this.animate) {
let sp = 0.3
for (let i = 0; i < 500; i+=sp) {
let index = floor(i) % 3
image(this.animation[index], this.xpos, this.ypos)
}
this.animate = false
}
}
fly() {
let jump = createVector(0,this.lift)
this.velocity.add(jump)
this.animate = true
}
updateImagePos() {
this.xpos = this.position.x - this.w / 2;
this.ypos = this.position.y - this.h / 2;
}
update() {
this.velocity.add(this.gravity)
this.velocity.mult(0.8)
if (this.position.y > ground) {
this.position.y = ground
this.velocity.mult(0)
}
this.position.add(this.velocity)
if (this.position.y < 0) {
this.position.y = 0
}
this.updateImagePos()
}
}