-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas_game.html
361 lines (306 loc) · 10.6 KB
/
canvas_game.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
<!DOCTYPE html>
<html>
<!-- Thanks to my web app development teacher at school for creating this assignment! -->
<head>
<style>
.canvasContainer {
border: 1px solid black;
width: 500px;
margin: 60px auto;
}
canvas {
width: 100%;
height: 100%;
display: block;
}
p,button {
display: inline;
font-size: 24pt;
}
input {
display: inline;
height: 18pt;
width: 18pt;
}
</style>
</head>
<body>
<p id="versionText">V1.2</p>
<button onclick="alert('If you are within 100 pixels of an exploding enemy, it cannot explode. Your hitbox has radius 3 less than your displayed radius.')">Tips</button>
<br>
<p>Include bonuses?</p>
<input type="checkbox" id="bonusCheckbox" onchange="changeBonus()" tabindex="-1">
<p style="display: none;" id="bonusTip">Press space to use a bonus.</p>
<div class="canvasContainer">
<canvas id="myCanvas" width="500" height="300"></canvas>
</div>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
let playerX = 50;
let playerY = 50;
let playerR = 10;
let moveOffset = 2;
let canvasIt = 0;
class Circle {
constructor(x, y, r, vx=0, vy=0) {
this.x = x;
this.y = y;
this.r = r;
this.vx = vx;
this.vy = vy;
}
overlapsPlayer() {
return (this.x-playerX)**2 + (this.y-playerY)**2 < (this.r+playerR-3)**2; // -3 for leniency
}
move(dt) {
this.x += this.vx*dt;
this.y += this.vy*dt;
}
}
class Enemy extends Circle {
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, 2*Math.PI);
ctx.fillStyle = "red";
ctx.fill();
}
}
class ExplodingEnemy extends Circle {
constructor(x, y, r, vx=0, vy=0) {
super(x, y, r, vx, vy);
this.explodeTicks = 50+Math.floor(Math.random()*100);
this.originalTicks = this.explodeTicks;
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, 2*Math.PI);
let c1 = "#FF0000";
let c2 = "#800080";
let c = "#";
for(let i = 0; i < 3; i++) {
let sub1 = c1.substring(1+2*i, 3+2*i);
let sub2 = c2.substring(1+2*i, 3+2*i);
let v1 = parseInt(sub1, 16);
let v2 = parseInt(sub2, 16);
let v = Math.floor((v1*this.explodeTicks + v2*(this.originalTicks-this.explodeTicks)) / this.originalTicks);
let sub = v.toString(16).toUpperCase();
let padsub = ('0'+sub).slice(-2);
c += padsub;
}
ctx.fillStyle = c;
ctx.fill();
}
move(dt) {
this.x += this.vx*dt;
this.y += this.vy*dt;
this.explodeTicks--;
if (this.explodeTicks % (this.originalTicks / 4.5 | 0) == 0) this.r++;
let sqdist = (this.x - playerX)**2 + (this.y - playerY)**2;
if (this.explodeTicks <= 0 && (100*difficultyFactor)**2 <= sqdist) {
for (let theta = 0; theta < 2*Math.PI-0.001; theta += 2*Math.PI/(difficultyFactor*10))
enemies.push(new Enemy(this.x, this.y, 3,
2*difficultyFactor*Math.cos(theta), 2*difficultyFactor*Math.sin(theta)));
this.x = -1000;
this.y = -1000;
}
}
}
class Bonus extends Circle {
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, 2*Math.PI);
ctx.fillStyle = "yellow";
ctx.fill();
}
}
function changeBonus() {
includeBonuses = document.getElementById("bonusCheckbox").checked;
if (includeBonuses) document.getElementById("bonusTip").style.display = "";
else document.getElementById("bonusTip").style.display = "none";
restart();
}
let enemies = [];
let bonuses = [];
let keysHeld = [0,0,0,0];
let bonusesHeld = 0;
let usingBonus = 0;
let difficultyFactor = 0.8;
let includeBonuses = document.getElementById("bonusCheckbox").checked;
if (includeBonuses) document.getElementById("bonusTip").style.display = "";
else document.getElementById("bonusTip").style.display = "none";
let gameover = false;
function gameOver() {
gameover = true;
}
let randomEnemyIt = 0;
function addRandomEnemy() {
randomEnemyIt++;
if (randomEnemyIt % Math.floor(10/difficultyFactor) != 0) enemies.push(new Enemy(Math.random()*500,300,10*(difficultyFactor**0.5),Math.random()*5-2.5,Math.random()-3));
else enemies.push(new ExplodingEnemy(Math.random()*500,300,10*(difficultyFactor**0.5),Math.random()*5-2.5,Math.random()-3));
}
function addRandomBonus() {
bonuses.push(new Bonus(Math.random()*500,300,10,Math.random()*5-2.5,Math.random()-3));
}
function addRandomWaveEnemy() {
enemies.push(new Enemy(Math.random()*500,0,3*difficultyFactor,Math.random()-0.5,2-Math.random()));
}
function addScriptedEnemy() {
let theta = 15+40*Math.random();
theta = theta / 180 * Math.PI;
let drp = 0.9 + Math.random() * 0.2;
drp *= 1/2;
if (Math.random() < 0.333)
enemies.push(new Enemy(0,300,5,5*drp*Math.cos(theta),-3*drp*Math.sin(theta)));
else if (Math.random() < 0.5)
enemies.push(new Enemy(500,300,5,-5*drp*Math.cos(theta),-3*drp*Math.sin(theta)));
else
enemies.push(new Enemy(250,0,5,-0.3+0.6*Math.random(),2));
}
function useBonus() {
if (bonusesHeld <= 0) return;
bonusesHeld -= 1;
for (let i = enemies.length-1; i >= 0; i--) {
let enemy = enemies[i];
if ((enemy.x-playerX)**2 + (enemy.y-playerY)**2 < 10000) enemies.splice(i,1);
}
usingBonus = 15;
document.activeElement.blur();
}
function restart() {
addAnimate = gameover;
playerX = 50;
playerY = 50;
gameover = false;
usingBonus = 0;
enemies = [];
bonuses = [];
bonusesHeld = 0;
difficultyFactor = 0.8;
canvasIt = 0;
keysHeld = [0,0,0,0];
if (addAnimate) doAnimate();
}
let lastTick = Date.now();
let desiredFramerate = 1000/60;
const doAnimate = () => {
if (gameover) return;
let currTime = Date.now();
let dt = currTime - lastTick;
dt /= desiredFramerate; // Number of ticks
lastTick = currTime;
ctx.clearRect(0, 0, canvas.width,canvas.height);
for (let enemy of enemies) {
if (enemy.overlapsPlayer()) gameOver();
}
ctx.fillStyle = "#FF9999";
if (canvasIt % 1000 >= 900 && canvasIt % 50 < 25 && !gameover) ctx.fillRect(0, 0, canvas.width, 50);
if (canvasIt % 1000 >= 400 && canvasIt % 50 < 25 && canvasIt % 1000 < 500 && !gameover) {
ctx.fillRect(225, 0, 50, 50);
ctx.fillRect(0, 250, 50, 50);
ctx.fillRect(450, 250, 50, 50);
}
ctx.fillStyle = "#FF9999";
if (keysHeld[0]) playerY -= moveOffset*dt;
if (keysHeld[1]) playerY += moveOffset*dt;
if (keysHeld[2]) playerX -= moveOffset*dt;
if (keysHeld[3]) playerX += moveOffset*dt;
playerX = Math.max(playerR,Math.min(500-playerR, playerX));
playerY = Math.max(playerR,Math.min(300-playerR, playerY));
ctx.lineWidth = 5;
usingBonus--;
if (usingBonus > 0) {
ctx.beginPath();
ctx.arc(playerX, playerY, 100, 0, 2*Math.PI);
ctx.fillStyle = "lightgreen";
ctx.fill();
}
if (canvasIt >= 300) difficultyFactor = 1.0;
if (canvasIt >= 1300) difficultyFactor = 1.2;
if (canvasIt >= 2300) difficultyFactor = 1.5;
if (canvasIt >= 3300) difficultyFactor = 1.7;
if (canvasIt >= 4300) difficultyFactor = 2.0;
ctx.beginPath();
ctx.arc(playerX, playerY, playerR, 0, 2*Math.PI);
ctx.fillStyle = "green";
ctx.fill();
for (let i = enemies.length-1; i >= 0; i--) {
let enemy = enemies[i];
if (-100 > enemy.x || -100 > enemy.y || 600 < enemy.x || 400 < enemy.y) enemies.splice(i,1);
}
for (let enemy of enemies) {
enemy.move(dt);
enemy.draw(ctx);
}
for (let i = bonuses.length-1; i >= 0; i--) {
let bonus = bonuses[i];
bonus.move(dt);
bonus.draw(ctx);
if (bonus.overlapsPlayer()) {
bonusesHeld++;
bonuses.splice(i,1);
}
}
ctx.textBaseline = "alphabetic";
ctx.textAlign = "left";
ctx.fillStyle = "black";
ctx.textAlign = "right";
ctx.font = "36px serif";
ctx.fillText(canvasIt, 490, 40, 160);
ctx.textAlign = "left";
ctx.fillStyle = "#BBBB00";
if (includeBonuses) ctx.fillText(bonusesHeld, 20, 40, 160);
if (gameover) {
ctx.fillStyle = "red";
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.font = "48px serif";
ctx.fillStyle = "#772222";
ctx.fillText("Game over!", 250, 100, 480);
ctx.fillText("Press R to restart.", 250, 150, 480);
ctx.font = "36px serif";
ctx.fillStyle = "#772222";
ctx.fillText(document.getElementById("versionText").innerHTML, 250, 200, 480);
}
if (canvasIt % 150 == 149 && includeBonuses) addRandomBonus();
if (canvasIt % 1000 == 999) {
for (let eit = 0; eit < 30*difficultyFactor; eit++) addRandomWaveEnemy();
} else if (canvasIt % 1000 == 49 && canvasIt > 1000) {
for (let eit = 0; eit < 30*difficultyFactor; eit++) addRandomWaveEnemy();
} else if (canvasIt % 10 == 0 && 500 <= canvasIt % 1000 && canvasIt % 1000 < 700) {
for (let eit = 0; eit < 2+Math.floor(Math.random()*difficultyFactor); eit++) addScriptedEnemy();
} else if (canvasIt % (19-6*Math.floor(difficultyFactor)) == 0
&& canvasIt % 1000 < 950
&& (canvasIt % 1000 > 200 || canvasIt < 1000)
&& !(canvasIt % 1000 > 500 && canvasIt % 1000 < 600)) {
addRandomEnemy();
}
canvasIt++;
}
setInterval(doAnimate, desiredFramerate);
// document.onkeydown = (e) => {
// if (e.key == "ArrowUp" || e.key == "w") playerY -= moveOffset;
// else if (e.key == "ArrowDown" || e.key == "s") playerY += moveOffset;
// else if (e.key == "ArrowLeft" || e.key == "a") playerX -= moveOffset;
// else if (e.key == "ArrowRight" || e.key == "d") playerX += moveOffset;
// }
document.onkeydown = (e) => {
if (e.key == "ArrowUp" ) keysHeld[0] = 1;
else if (e.key == "ArrowDown" ) keysHeld[1] = 1;
else if (e.key == "ArrowLeft" ) keysHeld[2] = 1;
else if (e.key == "ArrowRight") keysHeld[3] = 1;
else if (e.key == " ") {
e.preventDefault();
useBonus();
}
else if (e.key == "r") restart();
}
document.onkeyup = (e) => {
if (e.key == "ArrowUp" ) keysHeld[0] = 0;
else if (e.key == "ArrowDown" ) keysHeld[1] = 0;
else if (e.key == "ArrowLeft" ) keysHeld[2] = 0;
else if (e.key == "ArrowRight") keysHeld[3] = 0;
}
</script>
</body>
</html>