-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
114 lines (79 loc) · 2.39 KB
/
game.js
File metadata and controls
114 lines (79 loc) · 2.39 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
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const player = {
x: canvas.width / 2,
y: canvas.height - 30,
width: 20,
height: 20,
speed: 5,
};
const objects = [
{ x: 50, y: 50, width: 10, height: 10 },
{ x: 150, y: 150, width: 10, height: 10 },
{ x: 250, y: 250, width: 10, height: 10 }
];
let gameCompleted = false;
function checkGameCompletion() {
if (objects.length === 0 && !gameCompleted) {
gameCompleted = true;
ctx.fillStyle = "green";
ctx.font = "24px Arial";
ctx.fillText("Game Completed", canvas.width / 2 - 100, canvas.height / 2 - 20);
const restartButton = document.createElement("button");
restartButton.textContent = "Click to Restart";
restartButton.addEventListener("click", restartGame);
document.body.appendChild(restartButton);
}
}
function restartGame() {
gameCompleted = false;
player.x = canvas.width / 2;
player.y = canvas.height - 30;
objects.length = 0;
objects.push(
{ x: 50, y: 50, width: 10, height: 10 },
{ x: 150, y: 150, width: 10, height: 10 },
{ x: 250, y: 250, width: 10, height: 10 }
);
ctx.clearRect(0, 0, canvas.width, canvas.height);
const restartButton = document.querySelector("button");
if (restartButton) {
restartButton.remove();
}
}
document.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft" && player.x > 0) {
player.x -= player.speed;
}
if (event.key === "ArrowRight" && player.x < canvas.width - player.width) {
player.x += player.speed;
}
if (event.key === "ArrowUp" && player.y > 0) {
player.y -= player.speed;
}
if (event.key === "ArrowDown" && player.y < canvas.height - player.height) {
player.y += player.speed;
}
});
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "blue";
ctx.fillRect(player.x, player.y, player.width, player.height);
ctx.fillStyle = "green";
objects.forEach((object) => {
ctx.fillRect(object.x, object.y, object.width, object.height);
});
objects.forEach((object, index) => {
if (
player.x < object.x + object.width &&
player.x + player.width > object.x &&
player.y < object.y + object.height &&
player.y + player.height > object.y
) {
objects.splice(index, 1);
}
});
requestAnimationFrame(draw);
checkGameCompletion();
}
draw();