-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
60 lines (55 loc) · 1.62 KB
/
game.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
import EnemyController from "./EnemyController.js";
import Player from "./Player.js";
import BulletController from "./BulletController.js";
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 600;
canvas.height = 600;
const background = new Image();
background.src = "img/space.png";
const playerBulletController = new BulletController(canvas, 10, "red", true);
const enemyBulletController = new BulletController(canvas, 4, "white", false);
const enemyController = new EnemyController(
canvas,
enemyBulletController,
playerBulletController
);
const player = new Player(canvas, 3, playerBulletController);
let isGameOver = false;
let didWin = false;
function game() {
checkGameOver();
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
displayGameOver();
if (!isGameOver) {
enemyController.draw(ctx);
player.draw(ctx);
playerBulletController.draw(ctx);
enemyBulletController.draw(ctx);
}
}
function displayGameOver() {
if (isGameOver) {
let text = didWin ? "You Win!" : "Game Over!";
let textOffset = didWin ? 3.5 : 5;
ctx.fillStyle = "white";
ctx.font = "72px Arial";
ctx.fillText(text, canvas.width / textOffset, canvas.height / 2);
}
}
function checkGameOver() {
if (isGameOver) {
return;
}
if (enemyBulletController.collideWith(player)) {
isGameOver = true;
}
if (enemyController.collideWith(player)) {
isGameOver = true;
}
if (enemyController.enemyRows.length === 0) {
didWin = true;
isGameOver = true;
}
}
setInterval(game, 1000 / 60);