-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
169 lines (143 loc) · 3.99 KB
/
game.js
File metadata and controls
169 lines (143 loc) · 3.99 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
export class Canvas {
constructor(canvas, width, height) {
this.canvas = canvas
this.width = width
this.height = height
}
getContext() {
return this.canvas.getContext('2d')
}
clearCanvas(ctx) {
ctx.clearRect(0, 0, this.width, this.height)
}
}
export class Ball {
constructor(x, y, radius, color, speedX, speedY) {
this.x = x,
this.y = y,
this.radius = radius,
this.color = color,
this.speedX = speedX,
this.speedY = speedY
}
drawBall(ctx) {
ctx.beginPath()
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
ctx.fillStyle = this.color
ctx.fill()
ctx.closePath()
}
ballCollisions(canvasW, canvasH, paddle) {
const sound = new Audio('./sounds/paddle.wav');
if (this.x + this.radius > canvasW || this.x - this.radius < 0) {
this.speedX = -this.speedX;
sound.play();
}
if (
this.y + this.radius > paddle.y &&
this.x + this.radius > paddle.x &&
this.x - this.radius < paddle.x + paddle.width
) {
const collidePoint = this.x - (paddle.x + paddle.width / 2);
const normalizedCollidePoint = collidePoint / (paddle.width / 2);
this.speedX = normalizedCollidePoint * 5;
this.speedY = -this.speedY;
sound.play();
}
if (this.y - this.radius < 0) {
this.speedY = -this.speedY;
sound.play();
}
}
moveBall() {
this.x += this.speedX
this.y += this.speedY
}
}
export class Paddle {
constructor(x, y, width, height, color) {
this.x = x
this.y = y
this.width = width
this.height = height
this.color = color
this.speed = 20
this.movePaddle()
}
drawPaddle(ctx) {
// Crear un degradado lineal para el paddle
const gradient = ctx.createLinearGradient(this.x, this.y, this.x + this.width, this.y);
gradient.addColorStop(0, '#FFD700'); // Amarillo dorado
gradient.addColorStop(0.5, '#FFA500'); // Naranja claro
gradient.addColorStop(1, '#FF4500'); // Naranja oscuro
// Dibujar el cuerpo principal del paddle con el degradado
ctx.fillStyle = gradient;
ctx.fillRect(this.x, this.y, this.width, this.height);
// Agregar un borde al paddle
ctx.strokeStyle = '#8B0000'; // Rojo oscuro para el borde
ctx.lineWidth = 2;
ctx.strokeRect(this.x, this.y, this.width, this.height);
// Agregar detalles visuales (líneas decorativas)
ctx.beginPath();
ctx.moveTo(this.x + 10, this.y + this.height / 2); // Línea izquierda
ctx.lineTo(this.x + this.width - 10, this.y + this.height / 2); // Línea derecha
ctx.strokeStyle = '#FFFFFF'; // Blanco para las líneas
ctx.lineWidth = 1;
ctx.stroke();
// Agregar un pequeño círculo en el centro del paddle para darle un toque futurista
ctx.beginPath();
ctx.arc(this.x + this.width / 2, this.y + this.height / 2, 5, 0, 2 * Math.PI);
ctx.fillStyle = '#FFFFFF'; // Blanco para el círculo
ctx.fill();
}
paddleCollisions(canvasW) {
if (this.x < 0) {
this.x = 0
} else if (this.x + this.width > canvasW) {
this.x = canvasW - this.width
}
}
moveLeft() {
this.x -= this.speed;
}
moveRight() {
this.x += this.speed;
}
movePaddle() {
document.addEventListener('keydown', (evt) => {
switch (evt.key) {
case 'ArrowLeft':
this.moveLeft()
break
case 'ArrowRight':
this.moveRight()
break
default:
break
}
})
}
}
export class Brick {
constructor(x, y, width, height, color) {
this.x = x,
this.y = y,
this.width = width,
this.height = height,
this.color = color
}
drawBrick(ctx) {
ctx.beginPath()
ctx.fillStyle = this.color
ctx.fillRect(this.x, this.y, this.width, this.height)
ctx.closePath()
}
isColliding(ball) {
return (
ball.x + ball.radius > this.x &&
ball.x - ball.radius < this.x + this.width &&
ball.y + ball.radius > this.y &&
ball.y - ball.radius < this.y + this.height
);
}
}