···
const confetti = confettiPlugin(k);
k.addConfetti = confetti.addConfetti;
-
k.pos(0, k.height() - 48),
-
k.body({ isStatic: true }),
-
k.color(127, 200, 255),
-
// Create walls around the edge of the map
-
const leftWall = k.add([
-
k.rect(20, k.height()),
-
k.body({ isStatic: true }),
-
k.color(127, 200, 255),
-
const rightWall = k.add([
-
k.rect(20, k.height()),
-
k.body({ isStatic: true }),
-
k.color(127, 200, 255),
-
const topWall = k.add([
-
k.body({ isStatic: true }),
-
k.color(127, 200, 255),
-
// Create player object with components
-
const playerObj = k.add([
-
"player", // Add tag for collision detection
-
// Enemy spawning variables
-
let enemies: any[] = [];
-
let initialMaxEnemies = 5;
-
let maxEnemies = initialMaxEnemies;
-
let initialSpawnInterval = 3; // seconds
-
let spawnInterval = initialSpawnInterval;
-
let gameTime = 0; // Track game time in seconds
-
let difficultyLevel = 1;
-
const scoreText = k.add([k.text(`Score: ${score}`), k.pos(16, 16)]);
-
function updateDifficulty() {
-
gameTime += 1; // Increment game time by 1 second
-
// Every 30 seconds, increase difficulty
-
if (score != 0 && score % (50 + 5 * difficultyLevel) === 0) {
-
// Increase max enemies (cap at 15)
-
maxEnemies = Math.min(initialMaxEnemies + difficultyLevel * 3, 15);
-
// Decrease spawn interval (minimum 0.5 seconds)
-
spawnInterval = Math.max(initialSpawnInterval - difficultyLevel * 0.3, 0.5);
-
`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.color(255, 255, 255),
-
k.outline(2, k.rgb(0, 0, 0)),
-
// Fade out and destroy the text
-
// Start difficulty scaling
-
k.loop(1, updateDifficulty);
-
// Spawn an enemy at a random position
-
function spawnEnemy() {
-
// Don't spawn if we already have max enemies
-
if (enemies.length >= maxEnemies) return;
-
// Random position at the edges of the screen
-
const side = Math.floor(Math.random() * 4); // 0: top, 1: right, 2: bottom, 3: left
-
x = Math.random() * (k.width() - 40) + 20; // Avoid spawning behind side walls
-
y = 10; // Just inside the top wall
-
x = k.width() - 10; // Just inside the right wall
-
y = Math.random() * (k.height() - 48 - 20) + 20; // Avoid spawning behind top wall or inside ground
-
x = Math.random() * (k.width() - 40) + 20; // Avoid spawning behind side walls
-
y = k.height() - 58; // Just above the ground (ground is at height-48 with height 48)
-
x = 10; // Just inside the left wall
-
y = Math.random() * (k.height() - 48 - 20) + 20; // Avoid spawning behind top wall or inside ground
-
// Create enemy using the makeEnemy function
-
const newEnemy = makeEnemy(k, playerObj, x, y);
-
enemies.push(newEnemy);
-
// Remove from array when destroyed
-
newEnemy.on("destroy", () => {
-
enemies = enemies.filter((e) => e !== newEnemy);
-
// Increase score when enemy is destroyed
-
score += Math.round(10 + Math.pow(difficultyLevel, 0.75));
-
// Update score display
-
scoreText.text = `Score: ${score}`;
-
if (Math.random() < 0.5) spawnEnemy();
-
// Start spawning enemies
-
k.loop(spawnInterval, spawnEnemy);
-
// Update enemy list (remove destroyed enemies)
-
enemies = enemies.filter((enemy) => enemy.exists());
···
const confetti = confettiPlugin(k);
k.addConfetti = confetti.addConfetti;
+
k.scene("main", () => {
+
k.pos(0, k.height() - 48),
+
k.body({ isStatic: true }),
+
k.color(127, 200, 255),
+
// Create walls around the edge of the map
+
const leftWall = k.add([
+
k.rect(20, k.height()),
+
k.body({ isStatic: true }),
+
k.color(127, 200, 255),
+
const rightWall = k.add([
+
k.rect(20, k.height()),
+
k.body({ isStatic: true }),
+
k.color(127, 200, 255),
+
const topWall = k.add([
+
k.body({ isStatic: true }),
+
k.color(127, 200, 255),
+
// Create player object with components
+
const playerObj = k.add([
+
"player", // Add tag for collision detection
+
// Enemy spawning variables
+
let enemies: any[] = [];
+
let initialMaxEnemies = 5;
+
let maxEnemies = initialMaxEnemies;
+
let initialSpawnInterval = 3; // seconds
+
let spawnInterval = initialSpawnInterval;
+
let gameTime = 0; // Track game time in seconds
+
let difficultyLevel = 1;
+
const scoreText = k.add([k.text(`Score: ${score}`), k.pos(16, 16), "score"]);
+
function updateDifficulty() {
+
if (!gameActive) return;
+
gameTime += 1; // Increment game time by 1 second
+
// Every 30 seconds, increase difficulty
+
if (score != 0 && score % (50 + 5 * difficultyLevel) === 0) {
+
// Increase max enemies (cap at 15)
+
maxEnemies = Math.min(initialMaxEnemies + difficultyLevel * 3, 15);
+
// Decrease spawn interval (minimum 0.5 seconds)
+
spawnInterval = Math.max(
+
initialSpawnInterval - difficultyLevel * 0.3,
+
`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.color(255, 255, 255),
+
k.outline(2, k.rgb(0, 0, 0)),
+
// Fade out and destroy the text
+
// Start difficulty scaling
+
k.loop(1, updateDifficulty);
+
// Spawn an enemy at a random position
+
function spawnEnemy() {
+
if (!gameActive) return;
+
// Don't spawn if we already have max enemies
+
if (enemies.length >= maxEnemies) return;
+
// Random position at the edges of the screen
+
const side = Math.floor(Math.random() * 4); // 0: top, 1: right, 2: bottom, 3: left
+
x = Math.random() * (k.width() - 40) + 20; // Avoid spawning behind side walls
+
y = 10; // Just inside the top wall
+
x = k.width() - 10; // Just inside the right wall
+
y = Math.random() * (k.height() - 48 - 20) + 20; // Avoid spawning behind top wall or inside ground
+
x = Math.random() * (k.width() - 40) + 20; // Avoid spawning behind side walls
+
y = k.height() - 58; // Just above the ground (ground is at height-48 with height 48)
+
x = 10; // Just inside the left wall
+
y = Math.random() * (k.height() - 48 - 20) + 20; // Avoid spawning behind top wall or inside ground
+
// Create enemy using the makeEnemy function
+
const newEnemy = makeEnemy(k, playerObj, x, y);
+
enemies.push(newEnemy);
+
// Remove from array when destroyed
+
newEnemy.on("destroy", () => {
+
enemies = enemies.filter((e) => e !== newEnemy);
+
// Increase score when enemy is destroyed
+
score += Math.round(10 + Math.pow(difficultyLevel, 0.75));
+
// Update score display
+
scoreText.text = `Score: ${score}`;
+
if (Math.random() < 0.5) spawnEnemy();
+
// Start spawning enemies
+
k.loop(spawnInterval, spawnEnemy);
+
// Update enemy list (remove destroyed enemies)
+
enemies = enemies.filter((enemy) => enemy.exists());
+
// Listen for game over event
+
playerObj.on("death", () => {
+
k.cancel("spawnEnemy");
+
// Wait a moment before showing game over screen
+
k.go("gameOver", finalScore);
+
k.scene("gameOver", (score: number) => {
+
k.add([k.rect(k.width(), k.height()), k.color(0, 0, 0), k.opacity(0.7)]);
+
k.text("GAME OVER", { size: 64 }),
+
k.pos(k.width() / 2, k.height() / 3),
+
k.text(`Final Score: ${score}`, { size: 36 }),
+
k.pos(k.width() / 2, k.height() / 2),
+
k.color(255, 255, 255),
+
const restartBtn = k.add([
+
k.pos(k.width() / 2, (k.height() * 2) / 3),
+
k.text("RESTART", { size: 24 }),
+
k.pos(k.width() / 2, (k.height() * 2) / 3),
+
k.color(255, 255, 255),
+
// Restart on button click
+
restartBtn.onClick(() => {
+
// Restart on key press
+
k.onKeyPress("r", () => {
+
// Restart on enter key
+
k.onKeyPress("enter", () => {