-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obstacle.pde
67 lines (64 loc) · 2.09 KB
/
Obstacle.pde
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
63
64
65
66
67
class Obstacle {
float posX;
int w ;
int h ;
int type;
//------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
Obstacle(int t) {
posX = width;
type = t;
switch(type) {
case 0://small cactus
w = 40;
h = 80;
break;
case 1://big cactus
w = 60;
h = 120;
break;
case 2://small cacti
w = 120;
h = 80;
break;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------
//show the cactus
void show() {
fill(0);
rectMode(CENTER);
switch(type) {
case 0:
image(smallCactus, posX - smallCactus.width/2, height - groundHeight - smallCactus.height);
break;
case 1:
image(bigCactus, posX - bigCactus.width/2, height - groundHeight - bigCactus.height);
break;
case 2:
image(manySmallCactus, posX - manySmallCactus.width/2, height - groundHeight - manySmallCactus.height);
break;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------
// move the obstacle
void move(float speed) {
posX -= speed;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------
//returns whether or not the player collides with this obstacle
boolean collided(float playerX, float playerY, float playerWidth, float playerHeight) {
float playerLeft = playerX - playerWidth/2;
float playerRight = playerX + playerWidth/2;
float thisLeft = posX - w/2 ;
float thisRight = posX + w/2;
if ((playerLeft<= thisRight && playerRight >= thisLeft ) || (thisLeft <= playerRight && thisRight >= playerLeft)) {
float playerDown = playerY - playerHeight/2;
float thisUp = h;
if (playerDown <= thisUp) {
return true;
}
}
return false;
}
}