···
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([
updateScore(newScore: number) {
score = newScore; // Update the main score variable
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) {
// 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,
-
`Difficulty increased to level ${difficultyLevel}. Max enemies: ${maxEnemies}, Spawn interval: ${spawnInterval}s`
// Cancel previous spawn loop and start a new one with updated interval
-
k.cancel("spawnEnemy");
-
k.loop(spawnInterval, spawnEnemy, "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
···
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+
-
const centerSpawnChance = 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];
if (Math.random() < 0.2 * Math.pow(difficultyLevel, 0.75)) spawnEnemy();
// Start spawning enemies
-
k.loop(spawnInterval, spawnEnemy, "spawnEnemy");
···
// Listen for game over event
playerObj.on("death", () => {
// Get final score from tracker
const tracker = k.get("game-score-tracker")[0];
···
-
k.cancel("spawnEnemy");
// Wait a moment before showing game over screen
···
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([
+
difficultyLevel: difficultyLevel,
updateScore(newScore: number) {
score = newScore; // Update the main score variable
+
updateDifficulty(newLevel: number) {
+
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) {
+
// Update difficulty in tracker
+
const tracker = k.get("game-score-tracker")[0];
+
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,
+
`Difficulty increased to level ${difficultyLevel}. Max enemies: ${maxEnemies}, Spawn interval: ${spawnInterval}s`,
// Cancel previous spawn loop and start a new one with updated interval
+
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
···
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+
+
const centerSpawnChance =
+
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];
+
// Heal player when killing an enemy
+
const player = k.get("player")[0];
+
if (player && player.heal) {
+
// Heal amount is 5 health points
+
player.heal(healAmount);
+
const healText = k.add([
+
k.text(`+${healAmount} HP`, { size: 16 }),
+
k.pos(player.pos.x, player.pos.y - 60),
+
// Float upward and fade out
+
if (healText.exists()) {
+
if (healText.exists()) healText.destroy();
if (Math.random() < 0.2 * Math.pow(difficultyLevel, 0.75)) spawnEnemy();
// Start spawning enemies
+
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];
···
// Wait a moment before showing game over screen