-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
34 lines (31 loc) · 811 Bytes
/
Copy pathobjects.js
File metadata and controls
34 lines (31 loc) · 811 Bytes
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
import { ctx } from "./settings.js";
export class Cube {
constructor(x, y, width, height, color, keys = { r: 68, l: 65, u: 87, d: 83 }) {
this.pos = { x: x, y: y };
this.width = width;
this.height = height;
this.color = color;
this.keys = {
[keys.r]: "right",
[keys.l]: "left",
[keys.u]: "up",
[keys.d]: "down"
};
this.pressedKeys = {
left: false,
right: false,
up: false,
down: false
}
}
move(p) {
if (this.pressedKeys.left) { this.pos.x -= p }
if (this.pressedKeys.right) { this.pos.x += p }
if (this.pressedKeys.up) { this.pos.y -= p }
if (this.pressedKeys.down) { this.pos.y += p }
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height);
}
}