-
Notifications
You must be signed in to change notification settings - Fork 0
/
Munchers.js
185 lines (154 loc) · 4.16 KB
/
Munchers.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
const gameBoard = document.getElementById('game-board');
const scoreDisplay = document.getElementById('score');
const levelDisplay = document.getElementById('level');
const gameOverDisplay = document.getElementById('game-over');
const timerDisplay = document.getElementById('timer');
const livesDisplay = document.getElementById('lives');
let score = 0;
let level = 1;
let lives = 3;
let timer = 60; // in seconds
let gameRunning = true;
const muncher = {
row: 2,
col: 2,
};
function createSquare(number, row, col, type) {
const square = document.createElement('div');
square.classList.add('square');
square.textContent = number;
square.dataset.row = row;
square.dataset.col = col;
square.dataset.type = type;
square.dataset.multiple = number % 2 === 0; // true if the number is a multiple of 2
square.addEventListener('click', () => {
munchSquare(square);
});
return square;
}
function munchSquare(square) {
if (!gameRunning) return;
const number = parseInt(square.textContent);
const type = square.dataset.type;
const isMultiple = square.dataset.multiple === 'true';
if (type === 'enemy') {
// Game over logic
lives--;
if (lives === 0) {
endGame();
}
return;
}
// Only update score and level if the number is a multiple
if (isMultiple) {
score += number;
updateScoreAndLevel();
}
// Remove the square from the game board
square.remove();
}
function updateScoreAndLevel() {
scoreDisplay.textContent = `Score: ${score}`;
level++;
levelDisplay.textContent = `Level: ${level}`;
}
function moveMuncher(direction) {
if (!gameRunning) return;
const newMuncherPosition = { ...muncher };
switch (direction) {
case 'up':
if (newMuncherPosition.row > 0) {
newMuncherPosition.row--;
}
break;
case 'down':
if (newMuncherPosition.row < 4) {
newMuncherPosition.row++;
}
break;
case 'left':
if (newMuncherPosition.col > 0) {
newMuncherPosition.col--;
}
break;
case 'right':
if (newMuncherPosition.col < 4) {
newMuncherPosition.col++;
}
break;
default:
break;
}
muncher.row = newMuncherPosition.row;
muncher.col = newMuncherPosition.col;
updateMuncherPosition();
}
function updateMuncherPosition() {
const muncherElement = document.querySelector('.muncher');
muncherElement.style.gridRow = muncher.row + 1;
muncherElement.style.gridColumn = muncher.col + 1;
}
function getRandomSquareType() {
const types = ['number', 'power-up', 'enemy'];
const randomIndex = Math.floor(Math.random() * types.length);
return types[randomIndex];
}
function endGame() {
gameRunning = false;
gameOverDisplay.textContent = 'Game Over';
}
function generateGameBoard() {
gameBoard.innerHTML = '';
for (let i = 1; i <= 24; i++) {
const row = Math.floor((i - 1) / 5);
const col = (i - 1) % 5;
const type = getRandomSquareType();
const square = createSquare(i, row, col, type);
gameBoard.appendChild(square);
}
const muncherElement = document.createElement('div');
muncherElement.className = 'muncher';
const muncherImage = document.createElement('img');
muncherImage.src = 'green-muncher.png'; // Update this path accordingly
muncherElement.appendChild(muncherImage);
gameBoard.appendChild(muncherElement);
updateMuncherPosition();
}
function handleKeyDown(event) {
if (!gameRunning) return;
switch (event.key) {
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
moveMuncher(event.key.replace('Arrow', '').toLowerCase());
break;
default:
break;
}
}
function updateTimerDisplay() {
timerDisplay.textContent = `Time: ${timer} seconds`;
}
function updateLivesDisplay() {
livesDisplay.textContent = `Lives: ${lives}`;
}
function initGame() {
updateScoreAndLevel();
updateTimerDisplay();
updateLivesDisplay();
startTimer();
}
function startTimer() {
const interval = setInterval(() => {
timer--;
updateTimerDisplay();
if (timer === 0) {
clearInterval(interval);
endGame();
}
}, 1000);
}
document.addEventListener('keydown', handleKeyDown);
generateGameBoard();
initGame();