-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
87 lines (74 loc) · 1.33 KB
/
player.js
File metadata and controls
87 lines (74 loc) · 1.33 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
export default class Player {
#x;
#y;
#width;
#height;
#color;
#moveLeft;
#moveRight;
#speed;
constructor(x = 250, y = 660) {
this.#x = x;
this.#y = y;
this.#width = 40;
this.#height = 40;
this.#color = '#FFAEAE';
this.#speed = 2;
this.#moveLeft = false;
this.#moveRight = false;
}
move(direction) {
switch (direction) {
case 4:
this.#moveLeft = true;
break;
case 2:
this.#moveRight = true;
break;
}
}
stop(direction) {
switch (direction) {
case 4:
this.#moveLeft = false;
break;
case 2:
this.#moveRight = false;
break;
}
}
draw(ctx) {
let x = this.#x;
let y = this.#y;
let width = this.#width;
let height = this.#height;
let color = this.#color;
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
update() {
if (this.#x <= 0) {
this.#moveLeft = false;
}
if (this.#x >= 460) {
this.#moveRight = false;
}
if (this.#moveLeft) this.#x -= this.#speed;
if (this.#moveRight) this.#x += this.#speed;
}
speedUp(){
this.#speed += 0.5;
}
get x() {
return this.#x;
}
get y() {
return this.#y;
}
get width() {
return this.#width;
}
get height() {
return this.#height;
}
}