CommanderLazarus commited on
Commit
9e4d285
·
verified ·
1 Parent(s): 70b358a

🐳 13/02 - 02:43 - Reset the game.

Browse files
Files changed (1) hide show
  1. game.js +48 -4
game.js CHANGED
@@ -64,7 +64,13 @@ function createTopping(type) {
64
  }
65
 
66
  function movePlayer(e) {
67
- if (!gameRunning) return;
 
 
 
 
 
 
68
 
69
  // Store position before move
70
  lastPlayerX = player.x;
@@ -87,6 +93,44 @@ function movePlayer(e) {
87
  }
88
  }
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  function moveEnemy() {
91
  if (!gameRunning) return;
92
 
@@ -170,13 +214,13 @@ function checkCollision() {
170
  // Check win/lose conditions
171
  if (toppings.beef.length === 0) {
172
  gameRunning = false;
173
- alert('You won! Final score: ' + score + '\nEnemy stole: ' + enemy.stolenScore + ' points');
174
  } else if (enemy.stolenScore >= TOTAL_POINTS) {
175
  gameRunning = false;
176
- alert('The Red Devourer wins!\nIt stole all ' + enemy.stolenScore + ' points from you!\nYour final score: ' + score);
177
  } else if (lives <= 0) {
178
  gameRunning = false;
179
- alert('Game over! Final score: ' + score);
180
  }
181
  }
182
 
 
64
  }
65
 
66
  function movePlayer(e) {
67
+ if (!gameRunning) {
68
+ // Allow reset with R or Space when game is over
69
+ if (e.key === 'r' || e.key === 'R' || e.key === ' ') {
70
+ resetGame();
71
+ }
72
+ return;
73
+ }
74
 
75
  // Store position before move
76
  lastPlayerX = player.x;
 
93
  }
94
  }
95
 
96
+ function resetGame() {
97
+ // Reset player
98
+ player.x = canvas.width / 2;
99
+ player.y = canvas.height / 2;
100
+
101
+ // Reset enemy
102
+ enemy.x = 50;
103
+ enemy.y = 50;
104
+ enemy.stolenScore = 0;
105
+
106
+ // Reset game state
107
+ score = 0;
108
+ lives = 3;
109
+ playerMoveCount = 0;
110
+ lastPlayerX = null;
111
+ lastPlayerY = null;
112
+ gameRunning = true;
113
+
114
+ // Reset toppings
115
+ toppings.beef = [];
116
+ toppings.lettuce = [];
117
+ toppings.cheese = [];
118
+
119
+ // Recreate toppings
120
+ for (let i = 0; i < 20; i++) {
121
+ toppings.beef.push(createTopping('beef'));
122
+ toppings.lettuce.push(createTopping('lettuce'));
123
+ if (i < 5) toppings.cheese.push(createTopping('cheese'));
124
+ }
125
+
126
+ // Update display
127
+ scoreElement.textContent = score;
128
+ livesElement.textContent = lives;
129
+
130
+ // Restart loop
131
+ gameLoop();
132
+ }
133
+
134
  function moveEnemy() {
135
  if (!gameRunning) return;
136
 
 
214
  // Check win/lose conditions
215
  if (toppings.beef.length === 0) {
216
  gameRunning = false;
217
+ alert('You won! Final score: ' + score + '\nEnemy stole: ' + enemy.stolenScore + ' points\nPress R or Space to restart');
218
  } else if (enemy.stolenScore >= TOTAL_POINTS) {
219
  gameRunning = false;
220
+ alert('The Red Devourer wins!\nIt stole all ' + enemy.stolenScore + ' points from you!\nYour final score: ' + score + '\nPress R or Space to restart');
221
  } else if (lives <= 0) {
222
  gameRunning = false;
223
+ alert('Game over! Final score: ' + score + '\nPress R or Space to restart');
224
  }
225
  }
226