-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.js
31 lines (27 loc) · 868 Bytes
/
square.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
class Square {
constructor(x, y, speed, squareSize) {
this.x = x;
this.y = y;
this.speed = speed;
this.squareSize = squareSize;
}
drawSquare() {
if (this.isColliding(obstacles)) {
ctx.fillStyle = "red";
} else {
ctx.fillStyle = "blue";
}
ctx.fillRect(this.x, this.y, this.squareSize, this.squareSize);
}
isColliding(obstacles) {
return obstacles.obstaclesList.some(element => element.isColliding(square));
}
moveSquare() {
if (keysHeld['KeyW'] || keysHeld['ArrowUp']) this.y -= this.speed;
if (keysHeld['KeyA'] || keysHeld['ArrowLeft']) this.x -= this.speed;
if (keysHeld['KeyS'] || keysHeld['ArrowDown']) this.y += this.speed;
if (keysHeld['KeyD'] || keysHeld['ArrowRight']) this.x += this.speed;
this.x = bound(this.x, 0, canvas.width - this.squareSize);
this.y = bound(this.y, 0, canvas.height - this.squareSize);
}
}