-
Notifications
You must be signed in to change notification settings - Fork 0
/
obstacle1.js
70 lines (57 loc) · 1.33 KB
/
obstacle1.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
63
64
//first obstacle
//Every Obstacle has : a position, an acceleration, a move function,
//a draw function, a collision detection function and a reset function
class Obstacle1 {
constructor() {
this.xPos = ceil(random(15,width-15));
this.yPos = ceil(random(15,55));
this.xAcc = 1;
this.yAcc = 1;
this.size = 20;
this.xMinReached = false;
this.yMinReached = false;
}
move(){
if (this.xPos > 0 && !this.xMinReached) {
this.xPos -= this.xAcc;
if (this.xPos == 0) {
this.xMinReached = true;
}
} else {
this.xPos += this.xAcc;
if (this.xPos == width - this.size) {
this.xMinReached = false;
}
}
//horizontal movement
if (this.yPos > 0 && !this.yMinReached) {
this.yPos -= this.yAcc;
if (this.yPos == 0) {
this.yMinReached = true;
}
} else {
this.yPos += this.yAcc;
if (this.yPos == height - this.size) {
this.yMinReached = false;
}
}
}
drawObstacle() {
fill(100,150,50);
noStroke();
rect(this.xPos, this.yPos, this.size, this.size);
}
collisionDetection(ball) {
if (collideRectCircle(this.xPos, this.yPos, this.size, this.size, ball.getxPos(), ball.getyPos(), ball.getSize())) {
return true;
} else {
return false;
}
}
reset() {
this.xPos = ceil(random(15,width-15));
this.yPos = ceil(random(15,55));
this.xMinReached = false;
this.yMinReached = false;
}
}