-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
328 lines (274 loc) · 7.64 KB
/
main.js
File metadata and controls
328 lines (274 loc) · 7.64 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
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
function isMobile() {
return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
}
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const gameSelect = document.getElementById("gameSelect");
function showMessage(message) {
ctx.font = "16px Arial";
ctx.textAlign = "center";
ctx.fillText(message, canvas.width / 2, canvas.height / 2);
}
// Shared state
let animationId;
let currentGame = 'dino';
// ------------------ DINO GAME ------------------
const dinoImg = new Image();
dinoImg.src = "dino.png";
const cactusImg = new Image();
cactusImg.src = "tower.png";
let dino, obstacles, score, gameSpeed, gravity, frame, dinoGameOver;
function resetDinoGame() {
dino = {
x: 50,
y: 100,
width: 40,
height: 40,
vy: 0,
jumpForce: 10,
grounded: false
};
gravity = 0.6;
obstacles = [];
score = 0;
gameSpeed = 4;
frame = 0;
dinoGameOver = false;
runDinoGame();
}
function runDinoGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw dino
ctx.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height);
// Update dino
dino.y += dino.vy;
if (!dino.grounded) dino.vy += gravity;
if (dino.y + dino.height >= canvas.height) {
dino.y = canvas.height - dino.height;
dino.vy = 0;
dino.grounded = true;
}
// Obstacles
if (frame % 90 === 0) {
obstacles.push({
x: canvas.width,
y: canvas.height - 40,
width: 25,
height: 40
});
}
for (let i = 0; i < obstacles.length; i++) {
const obs = obstacles[i];
obs.x -= gameSpeed;
ctx.drawImage(cactusImg, obs.x, obs.y, obs.width, obs.height);
if (
dino.x < obs.x + obs.width &&
dino.x + dino.width > obs.x &&
dino.y < obs.y + obs.height &&
dino.y + dino.height > obs.y
) {
dinoGameOver = true;
}
}
obstacles = obstacles.filter(obs => obs.x + obs.width > 0);
ctx.fillStyle = "#888";
ctx.font = "16px Arial";
ctx.fillText("Score: " + score, 400, 20);
if (dinoGameOver) {
showMessage("Game Over – Press Space", )
//ctx.fillText("Game Over – Press Space", 140, 80);
return;
}
score++;
frame++;
animationId = requestAnimationFrame(runDinoGame);
}
function jumpDino() {
if (dinoGameOver) {
cancelAnimationFrame(animationId);
resetDinoGame();
} else if (dino.grounded) {
dino.vy = -dino.jumpForce;
dino.grounded = false;
}
}
// ------------------ SNAKE ------------------
let snake, food, snakeDirection, snakeScore, snakeGameOver;
function resetSnakeGame() {
snake = [{
x: 5,
y: 5
},
{
x: 4,
y: 5
},
{
x: 3,
y: 5
}
];
food = generateFood();
snakeDirection = "right";
snakeScore = 0;
snakeGameOver = false;
runSnakeGame();
}
function generateFood() {
const gridSize = 20;
const maxX = Math.floor(canvas.width / gridSize);
const maxY = Math.floor(canvas.height / gridSize);
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * maxX),
y: Math.floor(Math.random() * maxY)
};
} while (snake.some(s => s.x === newFood.x && s.y === newFood.y)); // retry if overlaps
return newFood;
}
function runSnakeGame() {
if (snakeGameOver) {
// Clear frame, draw Game Over screen
showMessage("Game Over - Press Space")
return; // Stop loop
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
const gridSize = 20;
// Move snake
const head = {
...snake[0]
};
switch (snakeDirection) {
case "right":
head.x += 1;
break;
case "left":
head.x -= 1;
break;
case "up":
head.y -= 1;
break;
case "down":
head.y += 1;
break;
}
// Check collisions
if (
head.x < 0 || head.y < 0 ||
head.x >= canvas.width / gridSize ||
head.y >= canvas.height / gridSize ||
snake.some(s => s.x === head.x && s.y === head.y)
) {
snakeGameOver = true;
runSnakeGame(); // Redraw game over frame
return;
}
snake.unshift(head);
// Eat food
if (head.x === food.x && head.y === food.y) {
food = generateFood();
snakeScore++;
} else {
snake.pop(); // move forward
}
// Draw food
ctx.fillStyle = "#f55";
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
// Draw snake
ctx.fillStyle = "#333";
snake.forEach(segment => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
});
// Score
ctx.fillStyle = "#888";
ctx.font = "16px Arial";
ctx.textAlign = "left";
ctx.fillText("Score: " + snakeScore, 400, 20);
animationId = setTimeout(runSnakeGame, 150); // ⏱️ Slowed down here
}
function controlSnake(e) {
if (snakeGameOver) {
resetSnakeGame();
return;
}
switch (e.code) {
case "ArrowUp":
if (snakeDirection !== "down") snakeDirection = "up";
break;
case "ArrowDown":
if (snakeDirection !== "up") snakeDirection = "down";
break;
case "ArrowLeft":
if (snakeDirection !== "right") snakeDirection = "left";
break;
case "ArrowRight":
if (snakeDirection !== "left") snakeDirection = "right";
break;
}
}
// ------------------ HANDLERS ------------------
document.addEventListener("keydown", (e) => {
// Prevent spacebar from triggering tab click again
if (
(e.code === "Space" || e.code === "ArrowUp") &&
document.activeElement.classList.contains("tab")
) {
e.preventDefault(); // Prevent re-click
}
if (currentGame === "snake") {
controlSnake(e);
}
// Now run the intended game input
if (e.code === "Space" || e.code === "ArrowUp") {
if (currentGame === "dino") {
jumpDino();
}
}
});
const snakeTab = document.getElementById("snakeTab");
const dinoTab = document.getElementById("dinoTab");
const slider = document.querySelector(".tab-slider");
function moveSliderTo(tab) {
const parent = tab.parentElement;
const tabOffset = tab.offsetLeft;
const tabWidth = tab.offsetWidth;
slider.style.left = `${tabOffset}px`;
slider.style.width = `${tabWidth}px`;
}
function switchGame(game) {
cancelAnimationFrame(animationId);
clearTimeout(animationId); // for Snake
frame = 0;
currentGame = game;
if (game === "dino") {
dinoTab.classList.add("active");
snakeTab.classList.remove("active");
moveSliderTo(dinoTab);
if (isMobile()) {
return;
}
resetDinoGame();
} else {
snakeTab.classList.add("active");
dinoTab.classList.remove("active");
moveSliderTo(snakeTab);
if (isMobile()) {
return;
}
resetSnakeGame();
}
}
snakeTab.addEventListener("click", () => switchGame("snake"));
dinoTab.addEventListener("click", () => switchGame("dino"));
window.addEventListener('resize', () => {
const activeTab = currentGame === "dino" ? dinoTab : snakeTab;
moveSliderTo(activeTab);
});
// Start initial game
window.onload = () => {
switchGame("dino");
if (isMobile()) {
showMessage("Please use a PC to play this game");
}
};