-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
233 lines (204 loc) · 5.22 KB
/
index.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
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
"use strict";
import Board from "./modules/board.js";
import Powergirl from "./modules/Powergirl.js";
import Enemy from "./modules/Enemy.js";
import Laser from "./modules/Laser.js";
let CANVAS = document.getElementById("canvas");
let CONTEXT = CANVAS.getContext("2d");
let end = document.querySelector(".end");
let endScore = document.querySelector(".end-score"); //ADD SCORE STYLE LIKE PPG
let up = false;
let down = false;
let shooting = false;
let life = 0;
let score = 0;
let timeBetweenEnemies = 5 * 1000;
let timeBetweenCandies = 3 * 1000;
let timeoutId = null;
let girlAngry = document.querySelector("#angry");
let girlCool = document.querySelector("#cool");
let girl1 = document.querySelector("#g1");
let girl2 = document.querySelector("#g2");
let girl3 = document.querySelector("#g3");
let girl;
let startScreen = document.querySelector(".choose");
let enemies = [];
let enemyBaseSpeed = 2;
const makeEnemy = () => {
let enemyX = CANVAS.width;
let enemySize = Math.round(Math.random() * 100) + 45;
let enemyY =
Math.round(Math.random() * (CANVAS.height - enemySize * 2)) + enemySize;
let enemySpeed = Math.round(Math.random() * enemyBaseSpeed) + enemyBaseSpeed;
enemies.push(new Enemy(enemyX, enemyY, enemySize, enemySpeed, CONTEXT));
};
//Background
const board = new Board(CANVAS.width, CANVAS.height, CONTEXT);
//laser
const bullet = new Laser(0, 0, 100, 14, 10, CONTEXT);
const isWithin = (a, b, c) => {
return a > b && a < c;
};
const isColliding = (a, b) => {
let result = false;
if (isWithin(a.x, b.x, b.x + b.l) || isWithin(a.x + a.l, b.x, b.x + b.l)) {
if (isWithin(a.y, b.y, b.y + b.l) || isWithin(a.y + a.l, b.y, b.y + b.l)) {
result = true;
}
}
return result;
};
const startGame = () => {
CANVAS.focus();
timeoutId = setInterval(makeEnemy, timeBetweenEnemies, timeBetweenCandies);
board.draw();
setTimeout(makeEnemy, 1000);
draw();
};
const endGame = () => {
board.audio2.play();
board.draw();
clearInterval(timeoutId);
erase();
CONTEXT.font = "50px Sonsie One";
board.draw();
CONTEXT.fillStyle = "#213867";
CONTEXT.fillText("Game Over.", CANVAS.width / 2 - 200, CANVAS.height / 2);
scoreHearts(score);
};
const scoreHearts = (score) => {
board.audio.currentTime = 0;
board.audio.pause();
end.style.display = "flex";
endScore.innerHTML = `Your Score: ${score}`;
if (score > 5) {
girlCool.style.display = "flex";
} else {
girlAngry.style.display = "flex";
end.classList.add("fail");
}
};
CANVAS.addEventListener("keydown", (event) => {
event.preventDefault();
if (event.keyCode === 38) {
// UP
up = true;
}
if (event.keyCode === 40) {
// DOWN
down = true;
}
if (event.keyCode === 32) {
// SPACE
shoot();
}
if (event.keyCode === 13) {
board.audio.play();
return startGame();
}
});
CANVAS.addEventListener("keyup", (event) => {
event.preventDefault();
if (event.keyCode === 38) {
// UP
up = false;
}
if (event.keyCode === 40) {
// DOWN
down = false;
}
});
const erase = () => {
CONTEXT.fillRect(0, 0, 800, 400);
};
const shoot = () => {
bullet.audio.play();
if (!shooting) {
shooting = true;
bullet.x = girl.x + girl.l;
bullet.y = girl.y + girl.height / 2;
}
};
girl1.addEventListener("click", (e) => {
girl = new Powergirl(50, CANVAS.height / 2, 150, 68, 5, 0, CONTEXT);
startScreen.className += " hide";
});
girl2.addEventListener("click", (e) => {
girl = new Powergirl(50, CANVAS.height / 2, 150, 68, 5, 1, CONTEXT);
startScreen.className += " hide";
});
girl3.addEventListener("click", (e) => {
girl = new Powergirl(50, CANVAS.height / 2, 150, 68, 5, 2, CONTEXT);
startScreen.className += " hide";
});
//Draw all
const draw = () => {
erase();
board.draw();
let gameOver = false;
enemies.forEach((enemy) => {
enemy.draw();
enemy.x -= enemy.s;
if (enemy.x < 0) {
if (life < 1) {
gameOver = true;
}
}
CONTEXT.fillStyle = "#111";
});
enemies.forEach((enemy) => {
if (isColliding(enemy, girl)) {
enemy.draw();
if (life < 1) {
gameOver = true;
}
}
});
if (down) {
girl.y += girl.s;
}
if (up) {
girl.y -= girl.s;
}
if (girl.y < 0) {
girl.y = 0;
}
if (girl.y > CANVAS.height - girl.height) {
girl.y = CANVAS.height - girl.height;
}
girl.draw();
if (shooting) {
bullet.x += bullet.s;
enemies.forEach((enemy, i) => {
enemy.draw();
if (isColliding(bullet, enemy)) {
enemies.splice(i, 1);
score++;
shooting = false;
if (score > 3 && timeBetweenEnemies > 1000) {
enemyBaseSpeed = 3;
clearInterval(timeoutId);
timeBetweenEnemies -= 600;
timeoutId = setInterval(makeEnemy, timeBetweenEnemies);
} else if (score > 3 && score < 5) {
enemyBaseSpeed = 3;
} else if (score > 5 && score < 8) {
enemyBaseSpeed += 4;
}
}
});
if (bullet.x > CANVAS.width) {
shooting = false;
}
CONTEXT.fillStyle = "#111";
bullet.draw();
}
CONTEXT.font = "24px Sonsie One";
CONTEXT.textAlign = "left";
CONTEXT.fillText("Score: " + score, 1, 24);
if (gameOver) {
endGame();
} else {
window.requestAnimationFrame(draw);
}
};