-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasPaddle.js
More file actions
47 lines (43 loc) · 1.28 KB
/
Copy pathCanvasPaddle.js
File metadata and controls
47 lines (43 loc) · 1.28 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
class CanvasPaddle extends CanvasItem {
/**
* Brick item
* @param {Number} x
* @param {Number} y
* @param {Number} width
* @param {String} color
*/
constructor(x = 0, y = 0, width = 100, height = 10, color = "#ff0000") {
super(x, y);
this.color = color;
this.width = width;
this.height = height;
this.direction = 0;
}
draw() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.rect(this.x, this.y, this.width, this.height);
ctx.fill();
ctx.closePath();
}
/**
*
* @param {CanvasBall} ball
*/
update(balls) {
this.x += this.direction * this.width * 0.05;
if (this.x <= 0)
this.x = 0;
if (this.x + this.width >= ctx.canvas.width)
this.x = ctx.canvas.width - this.width;
balls.forEach(ball => {
if (ball.y + ball.r / 2 >= this.y &&
ball.x + ball.r / 2 >= this.x && ball.x - ball.r / 2 <= this.x + this.width) {
ball.velocity[1] = -1;
if (this.direction) {
ball.velocity[0] += Math.min(1.5, Math.max(0.5, (Math.random() * 0.5 * Math.sign(this.direction * -1) / 2)))
}
}
});
}
}