-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.js
60 lines (55 loc) · 1.24 KB
/
Paddle.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
const paddleWidth = 120;
const paddleHeight = 20;
const paddleColor = "#8B322C";
var paddleX = (canvas.width - paddleWidth) / 2;
var paddleY = canvas.height - paddleHeight - 35;
var paddleSpeed = 7;
var leftArrow = false;
var rightArrow = false;
document.addEventListener("keydown", (event) => {
if (event.key == "ArrowLeft") {
leftArrow = true;
} else if (event.key == "ArrowRight") {
rightArrow = true;
}
});
document.addEventListener("keyup", (event) => {
if (event.key == "ArrowLeft") {
leftArrow = false;
} else if (event.key == "ArrowRight") {
rightArrow = false;
}
});
class Paddle {
constructor(x, y, width, height, speed) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
}
draw() {
c.beginPath();
c.rect(this.x, this.y, this.width, this.height);
c.fillStyle = paddleColor;
c.fill();
c.closePath();
}
move() {
if (leftArrow && this.x > 0) {
this.x -= this.speed;
} else if (rightArrow && this.x + this.width < canvas.width) {
this.x += this.speed;
}
}
reset() {
paddle.x = (canvas.width - paddleWidth) / 2;
}
}
const paddle = new Paddle(
paddleX,
paddleY,
paddleWidth,
paddleHeight,
paddleSpeed
);