-
Notifications
You must be signed in to change notification settings - Fork 0
/
cactus.js
49 lines (45 loc) · 1.17 KB
/
cactus.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
class Cactus {
constructor() {
let spacing = random(60, 90);
this.cactArray = new Array();
this.cactArray[0] = new Image();
this.cactArray[1] = new Image();
this.cactArray[2] = new Image();
this.cactArray[3] = new Image();
this.cactArray[0].src = "img/firstCact.png";
this.cactArray[1].src = "img/doubleCact.png";
this.cactArray[2].src = "img/oddCact.png";
this.cactArray[3].src = "img/tripleCact.png";
let randomItem = this.cactArray[Math.floor(Math.random() * this.cactArray.length)];
this.temp = randomItem;
this.bottom = spacing;
this.x = width;
this.w = 65;
this.speed = 10;
}
// Did this cactus hit a dino?
hits(dino) {
if ((dino.y + dino.r) > (height - this.bottom)) {
if (dino.x > this.x && dino.x < this.x + this.w) {
return true;
}
}
return false;
}
// Draw the cactus
show() {
ctx.drawImage(this.temp, this.x, height - 85, this.w, this.bottom);
}
// Update the cactus
update() {
this.x -= this.speed;
}
// Has it moved offscreen?
offscreen() {
if (this.x < -this.w) {
return true;
} else {
return false;
}
}
}