-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathARKANOID_BREAKOUT.html
442 lines (385 loc) · 17.1 KB
/
ARKANOID_BREAKOUT.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script type="text/javascript">
"use strict";
window.onload = function () {
// definimos las variables
var canvas = document.getElementById("myCanvas");
var iniciar = document.getElementById("iniciar");
var ctx = canvas.getContext("2d");
var x = canvas.width / 2;
var y = canvas.height - 50;
var velocidadInicial = 1.1;
var velocidadNivel = velocidadInicial;
var aumentoVelocidad = 1.1;
var dx = 0 * velocidadInicial;
var dy = -1 * velocidadInicial;
var ballRadius = 10;
var colorBola = "#0095DD";
var colorRGB1 = 0;
var colorRGB2 = 0;
var colorRGB3 = 0;
var playerX = 300;
var playerY = canvas.height - 20;
var playerWidth = 100;
var playerHeight = 10;
var puntos = 0;
var aumentaPuntuación = 10;
var finPartida = "";
var rightPressed = false;
var leftPressed = false;
var movimientoPlayer = 4;
var finPartida = 0;
var vidas = 3;
// tamaño canva
var margenMin = canvas.offsetLeft;
var margenMax = canvas.offsetLeft + canvas.width;
// ladrillos
var brickRowCount = 3;
var brickColumnCount = 13;
var brickWidth = 50;
var brickHeight = 20;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 15;
var brickCount = 0;
var brickColor = "";
var c;
var r;
var bricks = [];
// ┌────────────────────────────────────────────────────────────────────┐
// │ Iniciamos los ladrillos │
// └────────────────────────────────────────────────────────────────────┘
// audioMusica(); // música de fondo>
iniciar.addEventListener("click", iniciarPartida);
function iniciaLadrillos() {
brickCount = 0;
for (c = 0; c < brickColumnCount; c++) {
bricks[c] = [];
nuevoColor();
brickColor =
"rgba(" + colorRGB1 + "," + colorRGB2 + "," + colorRGB3 + ")";
for (r = 0; r < brickRowCount; r++) {
bricks[c][r] = {
x: 0,
y: 0,
status: 1,
color: brickColor
};
brickCount += 1;
}
}
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Dibujamos la bola │
// └────────────────────────────────────────────────────────────────────┘
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = colorBola;
ctx.fill();
ctx.strokeStyle = "red";
ctx.stroke();
ctx.closePath();
}
function nuevoColor() {
colorRGB1 = Math.random(1) * 255;
colorRGB2 = Math.random(1) * 255;
colorRGB3 = Math.random(1) * 255;
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Dibujamos el jugador │
// └────────────────────────────────────────────────────────────────────┘
function drawPlayer() {
//cuadrado2
ctx.beginPath();
ctx.rect(playerX, playerY, playerWidth, playerHeight);
// otra manera de guardar el color del borde (strokeStyle y stroke())
ctx.fillStyle = "rgba(100,100,100)";
ctx.fill();
ctx.closePath();
document.onmousemove = function (movimiento) {
// Movimiento con ratón ─────────────────────────────────────────
//definimos si hay que mover al player en función de la ubicación del ratón
if (
movimiento.pageX - playerWidth / 2 >= margenMin &&
movimiento.pageX + playerWidth / 2 <= margenMax
) {
playerX =
movimiento.pageX - canvas.offsetLeft - playerWidth / 2;
}
};
// Movimiento con teclado ──────────────────────────────────────────
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function keyDownHandler(e) {
if (e.keyCode == 39) {
rightPressed = true;
} else if (e.keyCode == 37) {
leftPressed = true;
}
}
function keyUpHandler(e) {
if (e.keyCode == 39) {
rightPressed = false;
} else if (e.keyCode == 37) {
leftPressed = false;
}
}
if (rightPressed && playerX + playerWidth < canvas.width) {
playerX += movimientoPlayer;
} else if (leftPressed && playerX > 0) {
playerX -= movimientoPlayer;
}
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Dibujamos los puntos │
// └────────────────────────────────────────────────────────────────────┘
function drawPuntos() {
ctx.beginPath();
ctx.fillStyle = "grey";
ctx.textAlign = "left";
ctx.font = "20px Arial";
ctx.fillText("Puntos: " + puntos, 5, 20);
ctx.closePath();
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Dibujamos las vidas │
// └────────────────────────────────────────────────────────────────────┘
function drawVidas() {
ctx.beginPath();
ctx.fillStyle = "red";
ctx.textAlign = "right";
ctx.font = "20px Arial";
var cadenaVidas = "";
var v = 0;
for (v = 1; v <= vidas; v++) {
cadenaVidas += "❤";
}
ctx.fillText(cadenaVidas, canvas.width - 5, 20);
ctx.closePath();
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Dibujamos todo │
// └────────────────────────────────────────────────────────────────────┘
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // limpia el canvas
drawBricks();
drawBall(); // dibuja la bola
drawPlayer(); // dibuja el jugador
drawPuntos(); //muestra puntuación
drawVidas(); // muestra las vidas disponibles
collisionDetection(); // detectamos colisión con los ladrillos
// ────────────────────────────────────────────────────────────────────
// detectamos las colisiones con el borde del canva
if (x + dx < ballRadius || x + dx > canvas.width - ballRadius) {
dx = -dx;
//audioPared();
}
if (y + dy < ballRadius) {
dy = -dy;
//audioPared();
}
x += dx; // desplaza la coordenada 'x'
y += dy; // desplaza la coordenada 'y'
// ────────────────────────────────────────────────────────────────────
// detectamos colisiones con player eje Y (rebote normal)
if (
y > playerY - ballRadius &&
x + ballRadius > playerX &&
x - ballRadius < playerX + playerWidth
) {
dy = -dy;
//audioPlayer();
// calcular el cambio de movimiento horizontal en función de dónde golpea la bola al jugador
dx = ((x - playerX - playerWidth / 2) / 100) * 4;
}
// Detectamos Game Over ───────────────────────────────────────────────
if (y + dy >= canvas.height + ballRadius) {
vidas -= 1;
if (vidas < 0) {
gameOver();
} else {
// pierde una vida y reiniciamos la pantalla
audioGameOver();
iniciarPartida();
}
}
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Game Over │
// └────────────────────────────────────────────────────────────────────┘
function gameOver() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
//audioGameOver();
ctx.beginPath();
// imprimimos Has perdido
ctx.font = "60px Impact";
ctx.textAlign = "center";
ctx.fillStyle = "red";
ctx.fillText("Has perdido", canvas.width / 2, canvas.height / 2);
ctx.font = "30px Impact";
// imprimimos puntuación
ctx.fillText(
"Puntuación: " + puntos,
canvas.width / 2,
canvas.height / 2 + 50
);
ctx.closePath();
clearInterval(finPartida);
dx = 1 * velocidadInicial;
dy = -1 * velocidadInicial;
velocidadNivel = velocidadInicial;
puntos = 0;
finPartida = 0;
brickRowCount = 3;
vidas = 3;
document.getElementById("iniciar").style.display = "inline";
iniciar.addEventListener("click", iniciarPartida);
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Aumento de velocidad │
// └────────────────────────────────────────────────────────────────────┘
function aumentaVelocidad() {
dx *= aumentoVelocidad;
dy *= aumentoVelocidad;
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Dibujamos los ladrillos │
// └────────────────────────────────────────────────────────────────────┘
function drawBricks() {
for (c = 0; c < brickColumnCount; c++) {
for (r = 0; r < brickRowCount; r++) {
if (bricks[c][r].status == 1) {
var brickX =
c * (brickWidth + brickPadding) + brickOffsetLeft;
var brickY =
r * (brickHeight + brickPadding) + brickOffsetTop;
bricks[c][r].x = brickX;
bricks[c][r].y = brickY;
ctx.beginPath();
ctx.rect(brickX, brickY, brickWidth, brickHeight);
ctx.fillStyle = bricks[c][r].color;
ctx.fill();
ctx.strokeStyle = "grey";
ctx.stroke();
ctx.closePath();
}
}
}
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Colisión con ladrillos │
// └────────────────────────────────────────────────────────────────────┘
function collisionDetection() {
for (c = 0; c < brickColumnCount; c++) {
for (r = 0; r < brickRowCount; r++) {
var b = bricks[c][r];
if (b.status == 1) {
if (
x + ballRadius > b.x &&
x - ballRadius < b.x + brickWidth &&
y + ballRadius > b.y &&
y - ballRadius < b.y + brickHeight
) {
dy = -dy;
b.status = 0;
puntos += aumentaPuntuación;
colorBola = b.color;
//audioLadrillo();
brickCount -= 1;
}
}
}
}
if (brickCount <= 0) {
// si eliminamos todos los ladrillos, seguimos jugando reiniciando los ladrillos con una nueva fila de ladrillos
if (brickRowCount < 7) { // si hay menos de 7 filas, añadimos fila
brickRowCount += 1;
}
x = canvas.width / 2 + parseInt(Math.random() * 300) - 150;
y = canvas.height - 80;
velocidadNivel *= aumentoVelocidad;
dx = 0 * velocidadNivel;
dy = -1 * velocidadNivel;
//audioNuevoNivel();
iniciaLadrillos();
}
}
// ┌────────────────────────────────────────────────────────────────────┐
// │ Ajuste de velocidades │
// └────────────────────────────────────────────────────────────────────┘
function iniciarPartida() {
//audioNuevoNivel();
x = canvas.width / 2;
y = canvas.height - 50;
velocidadInicial = 1.1;
aumentoVelocidad = 1.1;
dx = 0 * velocidadInicial;
dy = -1 * velocidadInicial;
iniciaLadrillos(); // definimos los bloques
document.getElementById("iniciar").style.display = "none";
if (finPartida === 0) {
finPartida = setInterval(draw, 1); // cada 10 milisegundos se llamará a la función 'draw' que: limpiará, dibujará y desplazará la bola para la siguiente llamada
}
setInterval(aumentaVelocidad, 10000); // aumento de velocidad de la bola
}
};
</script>
<style type="text/css">
/* definición de variables de colores */
* {
box-sizing: border-box;
transition: all 0.5s;
}
body {
width: 99%;
height: 100%;
display: grid;
place-items: center;
background-color: rgb(87, 87, 87);
}
canvas {
border: 2px #333 solid;
background-color: #eee;
background-image: url(../img/fondo.png);
background-size: cover;
background-position: 50%;
}
#audio {
display: none;
}
#iniciar {
position: absolute;
font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
color: #f00;
background-color: #efefef;
border: none;
border-radius: 10px;
top: 300px;
width: 200px;
height: 40px;
font-size: 1.5rem;
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.5);
}
#iniciar:hover {
color: #efefef;
background-color: #f00;
}
</style>
<title>Arkanoid</title>
</head>
<body>
<div class="container">
<!-- definimos el canvas y el tamaño que debe ocupar -->
<!-- definimos el tamaño aquí y no en css porque lo distorsiona -->
<canvas id="myCanvas" width="800px" height="600px"></canvas>
</div>
<! audio id="audio" controls></audio>
<button id="iniciar">Iniciar partida</button>
</body>
</html>