Repository navigation
Expand file tree
/
Copy pathspaceinvaders.js
More file actions
122 lines (102 loc) · 3.35 KB
/
Copy pathspaceinvaders.js
File metadata and controls
122 lines (102 loc) · 3.35 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
import { Marciano, Nave, Disparo, Juego } from './juego_marcianos.js';
var juego;
var puntuacion = 0;
var disparos = [];
var vidas = 3;
var ganada;
var cont;
function movimiento(evento) {
if (evento.keyCode == 32) {
let x = juego.nave.x + (50 / 2);
let y = juego.nave.y;
disparos.push(new Disparo(x, y, juego.marcianos));
}
if (evento.keyCode == 37) {
juego.nave.mover("-");
}
if (evento.keyCode == 39) {
juego.nave.mover("+");
}
}
window.onload = () => {
crearJuego();
}
function ganador(){
for (let marciano of juego.marcianos) {
if(marciano==null){
cont++;
}
}
if (cont==16) {
ganada=true;
}else{
ganada=false;
}
cont=0;
return ganada;
}
function crearJuego() {
juego = new Juego();
juego.dibujar();
let div = document.getElementById("puntuacion");
let h1 = document.createElement("h1");
h1.textContent = puntuacion;
h1.style.marginLeft = "250px";
let h2 = document.createElement("h2");
h2.textContent = "Vidas restantes: " + vidas;
div.appendChild(h1);
div.appendChild(h2);
document.body.addEventListener("keyup", movimiento);
document.getElementById("si").addEventListener("click", crearJuego);
document.getElementById("si2").addEventListener("click", crearJuego);
var intervalo = setInterval(() => {
juego.moverMarcianos();
if (juego.perdida) {
vidas--;
h2.textContent = "Vidas restantes: " + vidas;
if (vidas > 0) {
let div = document.getElementById("juego");
div.removeChild(document.getElementById("svg1"));
juego = new Juego();
juego.dibujar();
} else {
let div = document.getElementById("juego");
div.removeChild(document.getElementById("svg1"));
let div2 = document.getElementById("puntuacion");
div2.removeChild(h1);
div2.removeChild(h2);
vidas = 3;
puntuacion = 0;
clearInterval(intervalo);
$('#modal').modal('show');
//location.href ="perdida";
console.log("Has perdido");
}
}
for (let i = 0; i < disparos.length; i++) {
let disparo = disparos[i];
if (disparo != null) {
if (disparo.impacto()) {
puntuacion++;
h1.textContent = puntuacion;
let svg = document.getElementById("svg1");
svg.removeChild(disparo.shot);
disparos[i] = null;
if(ganador()){
let div = document.getElementById("juego");
div.removeChild(document.getElementById("svg1"));
let div2 = document.getElementById("puntuacion");
div2.removeChild(h1);
div2.removeChild(h2);
vidas = 3;
puntuacion = 0;
clearInterval(intervalo);
$('#modal2').modal('show');
}
}
disparo.dibujar();
}
}
}, 100);
}
//