···
const scoreText = k.add([k.text(`Score: ${score}`), k.pos(16, 16), "score"]);
// Add a hidden score tracker that can be accessed by other components
const scoreTracker = k.add([
101
+
difficultyLevel: difficultyLevel,
updateScore(newScore: number) {
score = newScore; // Update the main score variable
106
+
updateDifficulty(newLevel: number) {
107
+
this.difficultyLevel = newLevel;
function updateDifficulty() {
gameTime += 1; // Increment game time by 1 second
// Check if it's time to increase difficulty based on score
// Use a formula that scales with difficulty level
const scoreThreshold = 50 * difficultyLevel;
if (score >= scoreThreshold && score % scoreThreshold < 10) {
// Only trigger once when crossing the threshold
if (!k.get("level-up-text").length) {
127
+
// Update difficulty in tracker
128
+
const tracker = k.get("game-score-tracker")[0];
130
+
tracker.updateDifficulty(difficultyLevel);
// Increase max enemies (cap at 15)
maxEnemies = Math.min(initialMaxEnemies + difficultyLevel, 15);
// Decrease spawn interval (minimum 0.5 seconds)
spawnInterval = Math.max(
initialSpawnInterval - difficultyLevel * 0.2,
133
-
`Difficulty increased to level ${difficultyLevel}. Max enemies: ${maxEnemies}, Spawn interval: ${spawnInterval}s`
143
+
`Difficulty increased to level ${difficultyLevel}. Max enemies: ${maxEnemies}, Spawn interval: ${spawnInterval}s`,
// Cancel previous spawn loop and start a new one with updated interval
137
-
k.cancel("spawnEnemy");
138
-
k.loop(spawnInterval, spawnEnemy, "spawnEnemy");
147
+
k.loop(spawnInterval, spawnEnemy);
// Visual feedback for difficulty increase
const screenCenter = k.vec2(k.width() / 2, k.height() / 2);
k.addConfetti(screenCenter);
// Add difficulty level text
const levelText = k.add([
k.text(`Difficulty Level ${difficultyLevel}!`, { size: 32 }),
···
k.outline(2, k.rgb(0, 0, 0)),
// Fade out and destroy the text
···
168
-
k.easings.easeInQuad
177
+
k.easings.easeInQuad,
if (levelText.exists()) levelText.destroy();
···
// Random position at the edges of the screen
// As difficulty increases, add chance to spawn in center
// Calculate center spawn chance based on difficulty level
// 0% at level 1-2, increasing to 30% at level 10+
194
-
const centerSpawnChance = difficultyLevel <= 2 ? 0 : Math.min((difficultyLevel - 2) * 0.04, 0.3);
203
+
const centerSpawnChance =
204
+
difficultyLevel <= 2 ? 0 : Math.min((difficultyLevel - 2) * 0.04, 0.3);
// Determine spawn location
if (Math.random() < centerSpawnChance) {
···
// Side spawn (left or right)
spawnSide = Math.floor(Math.random() * 2); // 0: left, 1: right
···
scoreText.text = `Score: ${score}`;
const tracker = k.get("game-score-tracker")[0];
255
+
// Heal player when killing an enemy
256
+
const player = k.get("player")[0];
257
+
if (player && player.heal) {
258
+
// Heal amount is 5 health points
259
+
const healAmount = 5;
260
+
player.heal(healAmount);
262
+
// Show healing effect
263
+
const healText = k.add([
264
+
k.text(`+${healAmount} HP`, { size: 16 }),
265
+
k.pos(player.pos.x, player.pos.y - 60),
266
+
k.anchor("center"),
267
+
k.color(0, 255, 0),
272
+
// Float upward and fade out
278
+
if (healText.exists()) {
279
+
healText.opacity = v;
280
+
healText.pos.y -= 0.5;
283
+
k.easings.easeOutQuad,
286
+
k.wait(0.8, () => {
287
+
if (healText.exists()) healText.destroy();
if (Math.random() < 0.2 * Math.pow(difficultyLevel, 0.75)) spawnEnemy();
// Start spawning enemies
250
-
k.loop(spawnInterval, spawnEnemy, "spawnEnemy");
296
+
k.loop(spawnInterval, spawnEnemy);
···
// Listen for game over event
playerObj.on("death", () => {
// Get final score from tracker
const tracker = k.get("game-score-tracker")[0];
···
270
-
// Stop enemy spawning
271
-
k.cancel("spawnEnemy");
// Wait a moment before showing game over screen