User prompt
ilk beş seviyede fiyatlar aynı olsun
User prompt
level arttıkça çiçek fiyatları az bir şekilde artsın
User prompt
zombi öldürdükçe para vermeyi kaldır
User prompt
zombi öldürme başına para verme
User prompt
biraz daha aşağıya indir
User prompt
çiçek seçim kutularını aşagıya indir
User prompt
gidtile alnlndaki çiçek koyma yerlerini genişlet
User prompt
peashooter bitkisine kullandığım sesi ekle
User prompt
peasshooter sesini ekle mermi fırlattığı zaman
User prompt
Please fix the bug: 'Uncaught TypeError: tween.to is not a function' in or related to this line: 'tween.to(peashooterButton, 100, {' Line Number: 825 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
çiçek seçim kutularına bastığımız zaman düğme basma efekti ekle
User prompt
müzik sesini kıs biraz
User prompt
zombi sesleri sürekli zombilerden gelsin
User prompt
zombilerden seçtiğim ses çıksın
User prompt
seçtiğim müzik seviyeye başlandığı zaman çalmaya başlasın
User prompt
çiçekler bi tık daha fazla para üretsin
User prompt
level başalrında zobiler az gelsin
User prompt
wallnut bitki kalkan gibi olsun zombiler onu kırmadan geçemesin
User prompt
zombiler cevizleri geçemesin ve cevizler zombilerin 2 vuruşu ile kırılsın
User prompt
cevizi 50 sun yap
User prompt
ceviz bitkisini ceviz görseli yap
User prompt
6. seviyeye yeni bir bitki ekle
User prompt
her yeni bölümde bi önceki levelden fazla bir bitki olsun
User prompt
1. bölümü geçen 2. seviyeye 3. seviyeyi geçen 4. sonra 5 6 7 8 9 10 kadar bu şekil devam etsin
User prompt
oyunu 10 bölüm yap
/**** * Plugins ****/ var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Bullet = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('bulletCore', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.damage = 50; self.row = 0; self.update = function () { self.x += self.speed; // Check collision with zombies for (var i = zombies.length - 1; i >= 0; i--) { var zombie = zombies[i]; if (zombie.gridRow === self.row && Math.abs(self.x - zombie.x) < 30) { zombie.health -= self.damage; if (zombie.health <= 0) { sun += 2; // Give 2 money for killing zombie sunText.setText('Sun: ' + sun); zombie.destroy(); zombies.splice(i, 1); } self.destroy(); for (var j = bullets.length - 1; j >= 0; j--) { if (bullets[j] === self) { bullets.splice(j, 1); break; } } return; } } // Remove if off screen if (self.x > 2048) { self.destroy(); for (var k = bullets.length - 1; k >= 0; k--) { if (bullets[k] === self) { bullets.splice(k, 1); break; } } } }; return self; }); var CherryBomb = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('cherryImage', { anchorX: 0.5, anchorY: 0.5 }); self.health = 50; self.gridRow = 0; self.gridCol = 0; self.fuseTime = 180; // 3 seconds at 60fps self.planted = false; self.update = function () { if (!self.planted) { self.planted = true; self.plantedTick = LK.ticks; } // Explode after fuse time if (LK.ticks - self.plantedTick > self.fuseTime) { // Destroy all zombies in 3x3 area around bomb for (var i = zombies.length - 1; i >= 0; i--) { var zombie = zombies[i]; var distance = Math.sqrt(Math.pow(self.x - zombie.x, 2) + Math.pow(self.y - zombie.y, 2)); if (distance < 200) { // Explosion radius zombie.destroy(); zombies.splice(i, 1); } } // Remove self for (var j = plants.length - 1; j >= 0; j--) { if (plants[j] === self) { plants.splice(j, 1); break; } } self.destroy(); } }; return self; }); var DoublePeashooter = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('placedDoublePeaBody', { anchorX: 0.5, anchorY: 0.5 }); var cannon1 = self.attachAsset('peashooterCannon', { anchorX: 0, anchorY: 0.3 }); cannon1.x = 25; cannon1.y = -10; var cannon2 = self.attachAsset('peashooterCannon', { anchorX: 0, anchorY: 0.7 }); cannon2.x = 25; cannon2.y = 10; self.health = 100; self.lastShot = 0; self.shootDelay = 100; // Faster than regular peashooter self.gridRow = 0; self.gridCol = 0; self.shoot = function () { // Shoot two bullets var bullet1 = new Bullet(); bullet1.x = self.x + 50; bullet1.y = self.y - 10; bullet1.row = self.gridRow; bullets.push(bullet1); game.addChild(bullet1); var bullet2 = new Bullet(); bullet2.x = self.x + 50; bullet2.y = self.y + 10; bullet2.row = self.gridRow; bullets.push(bullet2); game.addChild(bullet2); }; self.update = function () { if (LK.ticks - self.lastShot > self.shootDelay) { var zombieInRow = false; for (var i = 0; i < zombies.length; i++) { if (zombies[i].gridRow === self.gridRow && zombies[i].x > self.x) { zombieInRow = true; break; } } if (zombieInRow) { self.shoot(); self.lastShot = LK.ticks; } } }; return self; }); var FastZombie = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('zombieBody', { anchorX: 0.5, anchorY: 1 }); body.y = 25; body.tint = 0xFF6666; // Reddish tint to distinguish from regular zombie var head = self.attachAsset('zombieHead', { anchorX: 0.5, anchorY: 1 }); head.y = -10; head.tint = 0xFF6666; // Reddish tint to distinguish from regular zombie self.health = 60; // Lower health than regular zombie self.speed = 1.2; // Faster than regular zombie self.gridRow = 0; self.isEating = false; self.eatDamage = 15; // More damage when eating self.hasAttackedHouse = false; self.update = function () { if (!self.isEating) { self.x -= self.speed; } // Check if reached house - zombie damages house once with 15 health if (self.x <= 150 && !self.hasAttackedHouse) { houseHealth -= 15; healthText.setText('House Health: ' + houseHealth); self.hasAttackedHouse = true; // Destroy zombie after attacking house self.destroy(); for (var z = zombies.length - 1; z >= 0; z--) { if (zombies[z] === self) { zombies.splice(z, 1); break; } } // Check game over condition if (houseHealth <= 0) { LK.showGameOver(); } } // Check collision with plants for (var i = plants.length - 1; i >= 0; i--) { var plant = plants[i]; if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) { self.isEating = true; plant.health -= 3; // Fast zombie eats faster if (plant.health <= 0) { plant.destroy(); plants.splice(i, 1); self.isEating = false; } break; } } }; return self; }); var JumperZombie = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('zombieBody', { anchorX: 0.5, anchorY: 1 }); body.y = 25; body.tint = 0x00FF00; // Green tint for jumper zombie var head = self.attachAsset('zombieHead', { anchorX: 0.5, anchorY: 1 }); head.y = -10; head.tint = 0x00FF00; self.health = 80; self.speed = 0.8; self.gridRow = 0; self.isEating = false; self.eatDamage = 12; self.hasAttackedHouse = false; self.canJump = true; self.update = function () { if (!self.isEating) { self.x -= self.speed; } // Jump over first plant encountered if (self.canJump) { for (var i = 0; i < plants.length; i++) { var plant = plants[i]; if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 80 && self.x > plant.x) { self.x = plant.x - 80; // Jump past the plant self.canJump = false; break; } } } // Check if reached house if (self.x <= 150 && !self.hasAttackedHouse) { houseHealth -= 12; healthText.setText('House Health: ' + houseHealth); self.hasAttackedHouse = true; self.destroy(); for (var z = zombies.length - 1; z >= 0; z--) { if (zombies[z] === self) { zombies.splice(z, 1); break; } } if (houseHealth <= 0) { LK.showGameOver(); } } // Check collision with plants (only if can't jump) if (!self.canJump) { for (var i = plants.length - 1; i >= 0; i--) { var plant = plants[i]; if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) { self.isEating = true; plant.health -= 3; if (plant.health <= 0) { plant.destroy(); plants.splice(i, 1); self.isEating = false; } break; } } } }; return self; }); // Event handlers var LevelButton = Container.expand(function () { var self = Container.call(this); var buttonBg = self.attachAsset('levelButton', { anchorX: 0.5, anchorY: 0.5 }); self.levelNumber = 1; self.setLevelNumber = function (number) { self.levelNumber = number; self.levelText.setText(number.toString()); }; self.isUnlocked = true; self.stars = 0; self.maxStars = 3; // Create level number text self.levelText = new Text2(self.levelNumber.toString(), { size: 50, fill: 0xFFFFFF }); self.levelText.anchor.set(0.5, 0.5); self.levelText.y = -30; self.addChild(self.levelText); // Create stars display self.starContainers = []; for (var i = 0; i < self.maxStars; i++) { var star = LK.getAsset('starShape', { anchorX: 0.5, anchorY: 0.5 }); star.x = (i - 1) * 50; star.y = 30; star.tint = 0x666666; // Dark by default self.addChild(star); self.starContainers.push(star); } self.setStars = function (starCount) { self.stars = starCount; for (var i = 0; i < self.maxStars; i++) { if (i < starCount) { self.starContainers[i].tint = 0xFFD700; // Golden } else { self.starContainers[i].tint = 0x666666; // Dark } } }; self.setUnlocked = function (unlocked) { self.isUnlocked = unlocked; if (unlocked) { buttonBg.tint = 0xFFFFFF; self.levelText.tint = 0xFFFFFF; } else { buttonBg.tint = 0x666666; self.levelText.tint = 0x666666; } }; self.down = function (x, y, obj) { if (self.isUnlocked) { LK.getSound('buttonClick').play(); // Start the selected level startLevel(self.levelNumber); } }; return self; }); // Plant classes var Peashooter = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('peashooterBody', { anchorX: 0.5, anchorY: 0.5 }); var cannon = self.attachAsset('peashooterCannon', { anchorX: 0, anchorY: 0.5 }); cannon.x = 25; cannon.y = 0; self.health = 100; self.lastShot = 0; self.shootDelay = 150; // 2.5 seconds at 60fps self.gridRow = 0; self.gridCol = 0; self.shoot = function () { var bullet = new Bullet(); bullet.x = self.x + 50; bullet.y = self.y; bullet.row = self.gridRow; bullets.push(bullet); game.addChild(bullet); }; self.update = function () { if (LK.ticks - self.lastShot > self.shootDelay) { // Check if there's a zombie in this row var zombieInRow = false; for (var i = 0; i < zombies.length; i++) { if (zombies[i].gridRow === self.gridRow && zombies[i].x > self.x) { zombieInRow = true; break; } } if (zombieInRow) { self.shoot(); self.lastShot = LK.ticks; } } }; return self; }); var Sunflower = Container.expand(function () { var self = Container.call(this); // Create petals around center for (var i = 0; i < 8; i++) { var petal = self.attachAsset('sunflowerPetal', { anchorX: 0.5, anchorY: 1 }); var angle = i / 8 * Math.PI * 2; petal.x = Math.cos(angle) * 25; petal.y = Math.sin(angle) * 25; petal.rotation = angle + Math.PI / 2; } var center = self.attachAsset('sunflowerCenter', { anchorX: 0.5, anchorY: 0.5 }); self.health = 100; self.lastSunGeneration = 0; self.sunDelay = 300; // 5 seconds at 60fps self.gridRow = 0; self.gridCol = 0; self.generateSun = function () { sun += 15; sunText.setText('Sun: ' + sun); self.lastSunGeneration = LK.ticks; }; self.update = function () { if (LK.ticks - self.lastSunGeneration > self.sunDelay) { self.generateSun(); } }; return self; }); var TankZombie = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('zombieBody', { anchorX: 0.5, anchorY: 1 }); body.y = 25; body.tint = 0x333333; // Dark gray tint for tank zombie body.scaleX = 1.5; body.scaleY = 1.5; var head = self.attachAsset('zombieHead', { anchorX: 0.5, anchorY: 1 }); head.y = -15; head.tint = 0x333333; head.scaleX = 1.5; head.scaleY = 1.5; self.health = 300; // Very high health self.speed = 0.3; // Very slow self.gridRow = 0; self.isEating = false; self.eatDamage = 20; self.hasAttackedHouse = false; self.update = function () { if (!self.isEating) { self.x -= self.speed; } // Check if reached house if (self.x <= 150 && !self.hasAttackedHouse) { houseHealth -= 25; healthText.setText('House Health: ' + houseHealth); self.hasAttackedHouse = true; self.destroy(); for (var z = zombies.length - 1; z >= 0; z--) { if (zombies[z] === self) { zombies.splice(z, 1); break; } } if (houseHealth <= 0) { LK.showGameOver(); } } // Check collision with plants for (var i = plants.length - 1; i >= 0; i--) { var plant = plants[i]; if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) { self.isEating = true; plant.health -= 5; // Tank zombie eats slowly but powerfully if (plant.health <= 0) { plant.destroy(); plants.splice(i, 1); self.isEating = false; } break; } } }; return self; }); var WallNut = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('peashooterBody', { anchorX: 0.5, anchorY: 0.5 }); body.tint = 0x8B4513; // Brown color for wall body.scaleX = 1.3; body.scaleY = 1.3; self.health = 400; // Very high health, no attacking self.gridRow = 0; self.gridCol = 0; self.update = function () { // Wall plants don't do anything, just block zombies }; return self; }); var Zombie = Container.expand(function () { var self = Container.call(this); var body = self.attachAsset('zombieBody', { anchorX: 0.5, anchorY: 1 }); body.y = 25; var head = self.attachAsset('zombieHead', { anchorX: 0.5, anchorY: 1 }); head.y = -10; self.health = 100; self.speed = 0.5; self.gridRow = 0; self.isEating = false; self.eatDamage = 10; self.hasAttackedHouse = false; self.update = function () { if (!self.isEating) { self.x -= self.speed; } // Check if reached house - zombie damages house once with 10 health if (self.x <= 150 && !self.hasAttackedHouse) { houseHealth -= 10; healthText.setText('House Health: ' + houseHealth); self.hasAttackedHouse = true; // Destroy zombie after attacking house self.destroy(); for (var z = zombies.length - 1; z >= 0; z--) { if (zombies[z] === self) { zombies.splice(z, 1); break; } } // Check game over condition if (houseHealth <= 0) { LK.showGameOver(); } } // Check collision with plants for (var i = plants.length - 1; i >= 0; i--) { var plant = plants[i]; if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) { self.isEating = true; plant.health -= 2; if (plant.health <= 0) { plant.destroy(); plants.splice(i, 1); self.isEating = false; } break; } } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x5D4E37 // Darker brown paper background color }); /**** * Game Code ****/ // Import storage for persistent level progression // Level selection screen variables var levelButtons = []; var totalLevels = 10; var unlockedLevels = storage.unlockedLevels || 1; // Load from storage or default to 1 var levelProgress = storage.levelProgress || {}; // Load from storage or default to empty // Global game variables var plants = []; var zombies = []; var bullets = []; var sun = 150; var sunText; var houseHealth; var healthText; // Create brown paper background var background = LK.getAsset('paperBackground', { anchorX: 0, anchorY: 0 }); game.addChild(background); // Create title var titleBg = LK.getAsset('titleBackground', { anchorX: 0.5, anchorY: 0.5 }); titleBg.x = 1024; titleBg.y = 300; game.addChild(titleBg); // Create level buttons in a grid layout var buttonsPerRow = 5; var buttonSpacingX = 350; var buttonSpacingY = 250; var startX = 400; var startY = 700; for (var i = 0; i < totalLevels; i++) { var levelButton = new LevelButton(); levelButton.setLevelNumber(i + 1); // Calculate position in grid var row = Math.floor(i / buttonsPerRow); var col = i % buttonsPerRow; levelButton.x = startX + col * buttonSpacingX; levelButton.y = startY + row * buttonSpacingY; // Set unlock status levelButton.setUnlocked(i < unlockedLevels); // Set stars from progress (if any) if (levelProgress[i + 1]) { levelButton.setStars(levelProgress[i + 1]); } else { levelButton.setStars(0); } levelButtons.push(levelButton); game.addChild(levelButton); } // Function to start a level function startLevel(levelNumber) { LK.getSound('levelSelect').play(); console.log('Starting level: ' + levelNumber); // Set current level for pause functionality currentLevel = levelNumber; // Clear the level selection screen game.removeChildren(); // Initialize the appropriate level if (levelNumber === 1) { initializeLevel1(); } else if (levelNumber === 2) { initializeLevel2(); } else if (levelNumber >= 3 && levelNumber <= 10) { // Initialize dynamic levels for 3-10 initializeDynamicLevel(levelNumber); } } // Initialize Level 1 Plants vs Zombies game function initializeLevel1() { // Game variables houseHealth = 100; sun = 150; // Starting money for level 1 plants = []; // Make plants global zombies = []; // Make zombies global bullets = []; // Make bullets global var sunflowers = []; var lastSunGeneration = 0; var lastZombieSpawn = 0; // Grid setup var gridRows = 10; var gridCols = 12; var gridCellWidth = 150; var gridCellHeight = 100; var gridStartX = 200; var gridStartY = 500; // UI elements sunText = new Text2('Sun: ' + sun, { size: 40, fill: 0x000000 }); sunText.x = 1500; sunText.y = 50; game.addChild(sunText); healthText = new Text2('House Health: ' + houseHealth, { size: 40, fill: 0xFF0000 }); healthText.x = 1500; healthText.y = 100; game.addChild(healthText); // Create grid for (var row = 0; row < gridRows; row++) { for (var col = 0; col < gridCols; col++) { var gridCell = LK.getAsset('gridTile', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.75 }); gridCell.x = gridStartX + col * gridCellWidth; gridCell.y = gridStartY + row * gridCellHeight; gridCell.alpha = 0.8; gridCell.tint = 0x8B7355; game.addChild(gridCell); } } // Plant selection buttons with proper spacing var peashooterButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); peashooterButton.x = 400; peashooterButton.y = 1617; peashooterButton.tint = 0x006400; game.addChild(peashooterButton); // Add peashooter image to button var peashooterImage = LK.getAsset('peashooterBody', { anchorX: 0.5, anchorY: 0.5 }); peashooterImage.x = 400; peashooterImage.y = 1597; peashooterImage.scaleX = 1.0; peashooterImage.scaleY = 1.0; game.addChild(peashooterImage); var peashooterText = new Text2('50 Sun', { size: 32, fill: 0xFFFFFF }); peashooterText.anchor.set(0.5, 0.5); peashooterText.x = 400; peashooterText.y = 1647; game.addChild(peashooterText); var sunflowerButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); sunflowerButton.x = 1000; sunflowerButton.y = 1617; sunflowerButton.tint = 0xB8860B; game.addChild(sunflowerButton); // Add sunflower image to button var sunflowerImage = LK.getAsset('sunflowerCenter', { anchorX: 0.5, anchorY: 0.5 }); sunflowerImage.x = 1000; sunflowerImage.y = 1597; sunflowerImage.scaleX = 0.75; sunflowerImage.scaleY = 0.75; game.addChild(sunflowerImage); var sunflowerText = new Text2('50 Sun', { size: 32, fill: 0xFFFFFF }); sunflowerText.anchor.set(0.5, 0.5); sunflowerText.x = 1000; sunflowerText.y = 1647; game.addChild(sunflowerText); var selectedPlant = null; // Event handlers peashooterButton.down = function () { if (sun >= 50) { selectedPlant = 'peashooter'; } }; sunflowerButton.down = function () { if (sun >= 50) { selectedPlant = 'sunflower'; } }; game.down = function (x, y, obj) { if (selectedPlant && x >= gridStartX && x <= gridStartX + gridCols * gridCellWidth && y >= gridStartY && y <= gridStartY + gridRows * gridCellHeight) { var gridCol = Math.floor((x - gridStartX) / gridCellWidth); var gridRow = Math.floor((y - gridStartY) / gridCellHeight); // Check if position is valid and empty var positionEmpty = true; for (var i = 0; i < plants.length; i++) { if (plants[i].gridRow === gridRow && plants[i].gridCol === gridCol) { positionEmpty = false; break; } } if (positionEmpty && gridCol >= 0 && gridCol < gridCols && gridRow >= 0 && gridRow < gridRows) { var plant = null; if (selectedPlant === 'peashooter' && sun >= 50) { plant = new Peashooter(); sun -= 50; } else if (selectedPlant === 'sunflower' && sun >= 50) { plant = new Sunflower(); sun -= 50; } if (plant) { plant.x = gridStartX + gridCol * gridCellWidth; plant.y = gridStartY + gridRow * gridCellHeight; plant.gridRow = gridRow; plant.gridCol = gridCol; plants.push(plant); game.addChild(plant); sunText.setText('Sun: ' + sun); selectedPlant = null; } } } }; var zombiesStarted = false; // Track if zombies have started spawning // Game update function game.update = function () { // Start zombie spawning only after first plant is placed if (!zombiesStarted && plants.length > 0) { zombiesStarted = true; lastZombieSpawn = LK.ticks; // Initialize spawn timer } // Spawn zombies only after they have been started if (zombiesStarted && LK.ticks - lastZombieSpawn > 300) { // Every 5 seconds var zombie = new Zombie(); var row = Math.floor(Math.random() * gridRows); zombie.x = 1900; zombie.y = gridStartY + row * gridCellHeight; zombie.gridRow = row; zombies.push(zombie); game.addChild(zombie); lastZombieSpawn = LK.ticks; } // Update all game objects for (var i = plants.length - 1; i >= 0; i--) { plants[i].update(); } for (var j = zombies.length - 1; j >= 0; j--) { zombies[j].update(); } for (var k = bullets.length - 1; k >= 0; k--) { bullets[k].update(); } // Generate sun from sunflowers - only if sunflowers exist var sunflowerCount = 0; for (var s = 0; s < plants.length; s++) { if (plants[s] instanceof Sunflower) { sunflowerCount++; } } if (sunflowerCount > 0 && LK.ticks - lastSunGeneration > 600) { // Every 10 seconds, only if sunflowers exist sun += 5; sunText.setText('Sun: ' + sun); lastSunGeneration = LK.ticks; } // Final wave - spawn more zombies rapidly in the last 20 seconds if (LK.ticks > 2400 && LK.ticks <= 3600) { // Between 40s and 60s if (LK.ticks - lastZombieSpawn > 120) { // Every 2 seconds instead of 5 var zombie = new Zombie(); var row = Math.floor(Math.random() * gridRows); zombie.x = 1900; zombie.y = gridStartY + row * gridCellHeight; zombie.gridRow = row; zombies.push(zombie); game.addChild(zombie); lastZombieSpawn = LK.ticks; } } // Check win condition if (LK.ticks > 3600 && zombies.length === 0) { // After 1 minute with no zombies unlockNextLevel(); // Unlock next level when current level is completed // Calculate stars based on house health: 3 stars = 80+%, 2 stars = 50+%, 1 star = any health remaining var stars = 1; if (houseHealth >= 80) { stars = 3; } else if (houseHealth >= 50) { stars = 2; } setLevelStars(1, stars); LK.showYouWin(); } }; } // Initialize Level 2 Plants vs Zombies game with more zombies and fast zombies function initializeLevel2() { // Game variables houseHealth = 100; sun = 200; // Starting money for level 2 (50 more than level 1) plants = []; // Make plants global zombies = []; // Make zombies global bullets = []; // Make bullets global // Night theme background var nightBg = LK.getAsset('nightBackground', { anchorX: 0, anchorY: 0 }); game.addChild(nightBg); var sunflowers = []; var lastSunGeneration = 0; var lastZombieSpawn = 0; var lastFastZombieSpawn = 0; // Grid setup var gridRows = 10; var gridCols = 12; var gridCellWidth = 150; var gridCellHeight = 100; var gridStartX = 200; var gridStartY = 500; // UI elements sunText = new Text2('Sun: ' + sun, { size: 40, fill: 0x000000 }); sunText.x = 1500; sunText.y = 50; game.addChild(sunText); healthText = new Text2('House Health: ' + houseHealth, { size: 40, fill: 0xFF0000 }); healthText.x = 1500; healthText.y = 100; game.addChild(healthText); // Level indicator var levelText = new Text2('Level 2', { size: 50, fill: 0xFFFFFF }); levelText.x = 1500; levelText.y = 150; game.addChild(levelText); // Create grid for (var row = 0; row < gridRows; row++) { for (var col = 0; col < gridCols; col++) { var gridCell = LK.getAsset('gridTile', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.75 }); gridCell.x = gridStartX + col * gridCellWidth; gridCell.y = gridStartY + row * gridCellHeight; gridCell.alpha = 0.9; gridCell.tint = 0x1A0D33; game.addChild(gridCell); } } // Plant selection buttons with proper spacing var peashooterButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); peashooterButton.x = 350; peashooterButton.y = 1617; peashooterButton.tint = 0x006400; game.addChild(peashooterButton); // Add peashooter image to button var peashooterImage = LK.getAsset('peashooterBody', { anchorX: 0.5, anchorY: 0.5 }); peashooterImage.x = 350; peashooterImage.y = 1597; peashooterImage.scaleX = 1.0; peashooterImage.scaleY = 1.0; game.addChild(peashooterImage); var peashooterText = new Text2('50 Sun', { size: 32, fill: 0xFFFFFF }); peashooterText.anchor.set(0.5, 0.5); peashooterText.x = 350; peashooterText.y = 1647; game.addChild(peashooterText); var sunflowerButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); sunflowerButton.x = 800; sunflowerButton.y = 1617; sunflowerButton.tint = 0xB8860B; game.addChild(sunflowerButton); // Add sunflower image to button var sunflowerImage = LK.getAsset('sunflowerCenter', { anchorX: 0.5, anchorY: 0.5 }); sunflowerImage.x = 800; sunflowerImage.y = 1597; sunflowerImage.scaleX = 0.75; sunflowerImage.scaleY = 0.75; game.addChild(sunflowerImage); var sunflowerText = new Text2('50 Sun', { size: 32, fill: 0xFFFFFF }); sunflowerText.anchor.set(0.5, 0.5); sunflowerText.x = 800; sunflowerText.y = 1647; game.addChild(sunflowerText); var selectedPlant = null; // Add CherryBomb button for level 2 var cherrybombButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); cherrybombButton.x = 1250; cherrybombButton.y = 1617; cherrybombButton.tint = 0xFF0000; game.addChild(cherrybombButton); var cherrybombImage = LK.getAsset('cherryImage', { anchorX: 0.5, anchorY: 0.5 }); cherrybombImage.x = 1250; cherrybombImage.y = 1597; cherrybombImage.scaleX = 0.75; cherrybombImage.scaleY = 0.75; game.addChild(cherrybombImage); var cherrybombText = new Text2('100 Sun', { size: 32, fill: 0xFFFFFF }); cherrybombText.anchor.set(0.5, 0.5); cherrybombText.x = 1250; cherrybombText.y = 1647; game.addChild(cherrybombText); cherrybombButton.down = function () { if (sun >= 100) { selectedPlant = 'cherrybomb'; } }; // Event handlers peashooterButton.down = function () { if (sun >= 50) { selectedPlant = 'peashooter'; } }; sunflowerButton.down = function () { if (sun >= 50) { selectedPlant = 'sunflower'; } }; game.down = function (x, y, obj) { if (selectedPlant && x >= gridStartX && x <= gridStartX + gridCols * gridCellWidth && y >= gridStartY && y <= gridStartY + gridRows * gridCellHeight) { var gridCol = Math.floor((x - gridStartX) / gridCellWidth); var gridRow = Math.floor((y - gridStartY) / gridCellHeight); // Check if position is valid and empty var positionEmpty = true; for (var i = 0; i < plants.length; i++) { if (plants[i].gridRow === gridRow && plants[i].gridCol === gridCol) { positionEmpty = false; break; } } if (positionEmpty && gridCol >= 0 && gridCol < gridCols && gridRow >= 0 && gridRow < gridRows) { var plant = null; if (selectedPlant === 'peashooter' && sun >= 50) { plant = new Peashooter(); sun -= 50; } else if (selectedPlant === 'sunflower' && sun >= 50) { plant = new Sunflower(); sun -= 50; } else if (selectedPlant === 'cherrybomb' && sun >= 100) { plant = new CherryBomb(); sun -= 100; } if (plant) { plant.x = gridStartX + gridCol * gridCellWidth; plant.y = gridStartY + gridRow * gridCellHeight; plant.gridRow = gridRow; plant.gridCol = gridCol; plants.push(plant); game.addChild(plant); sunText.setText('Sun: ' + sun); selectedPlant = null; } } } }; var zombiesStarted = false; // Track if zombies have started spawning // Game update function for Level 2 game.update = function () { // Start zombie spawning only after first plant is placed if (!zombiesStarted && plants.length > 0) { zombiesStarted = true; lastZombieSpawn = LK.ticks; // Initialize spawn timer lastFastZombieSpawn = LK.ticks; // Initialize fast zombie spawn timer } // Spawn regular zombies more frequently (every 3 seconds instead of 5) - only after zombies started if (zombiesStarted && LK.ticks - lastZombieSpawn > 180) { var zombie = new Zombie(); var row = Math.floor(Math.random() * gridRows); zombie.x = 1900; zombie.y = gridStartY + row * gridCellHeight; zombie.gridRow = row; zombies.push(zombie); game.addChild(zombie); lastZombieSpawn = LK.ticks; } // Spawn fast zombies every 8 seconds - only after zombies started if (zombiesStarted && LK.ticks - lastFastZombieSpawn > 480) { var fastZombie = new FastZombie(); var row = Math.floor(Math.random() * gridRows); fastZombie.x = 1900; fastZombie.y = gridStartY + row * gridCellHeight; fastZombie.gridRow = row; zombies.push(fastZombie); game.addChild(fastZombie); lastFastZombieSpawn = LK.ticks; } // Spawn tank zombies every 15 seconds - only after zombies started if (zombiesStarted && LK.ticks % 900 === 0) { var tankZombie = new TankZombie(); var row = Math.floor(Math.random() * gridRows); tankZombie.x = 1900; tankZombie.y = gridStartY + row * gridCellHeight; tankZombie.gridRow = row; zombies.push(tankZombie); game.addChild(tankZombie); } // Update all game objects for (var i = plants.length - 1; i >= 0; i--) { plants[i].update(); } for (var j = zombies.length - 1; j >= 0; j--) { zombies[j].update(); } for (var k = bullets.length - 1; k >= 0; k--) { bullets[k].update(); } // Generate sun from sunflowers - only if sunflowers exist var sunflowerCount = 0; for (var s = 0; s < plants.length; s++) { if (plants[s] instanceof Sunflower) { sunflowerCount++; } } if (sunflowerCount > 0 && LK.ticks - lastSunGeneration > 600) { // Every 10 seconds, only if sunflowers exist sun += 5; sunText.setText('Sun: ' + sun); lastSunGeneration = LK.ticks; } // Final wave - spawn many more zombies rapidly in the last 30 seconds if (LK.ticks > 3600 && LK.ticks <= 5400) { // Between 60s and 90s if (LK.ticks - lastZombieSpawn > 90) { // Every 1.5 seconds instead of 3 var zombie = new Zombie(); var row = Math.floor(Math.random() * gridRows); zombie.x = 1900; zombie.y = gridStartY + row * gridCellHeight; zombie.gridRow = row; zombies.push(zombie); game.addChild(zombie); lastZombieSpawn = LK.ticks; } // Also spawn fast zombies more frequently during final wave if (LK.ticks - lastFastZombieSpawn > 300) { // Every 5 seconds instead of 8 var fastZombie = new FastZombie(); var row = Math.floor(Math.random() * gridRows); fastZombie.x = 1900; fastZombie.y = gridStartY + row * gridCellHeight; fastZombie.gridRow = row; zombies.push(fastZombie); game.addChild(fastZombie); lastFastZombieSpawn = LK.ticks; } } // Check win condition (Level 2 lasts longer and requires more survival) if (LK.ticks > 5400 && zombies.length === 0) { // After 1.5 minutes with no zombies unlockNextLevel(); // Unlock next level when current level is completed // Calculate stars based on house health: 3 stars = 80+%, 2 stars = 50+%, 1 star = any health remaining var stars = 1; if (houseHealth >= 80) { stars = 3; } else if (houseHealth >= 50) { stars = 2; } setLevelStars(2, stars); LK.showYouWin(); } }; } // Initialize Dynamic Level for levels 3 and beyond function initializeDynamicLevel(levelNumber) { // Game variables houseHealth = 100; sun = 150 + (levelNumber - 1) * 50; // Progressive starting money: level 1=150, level 2=200, level 3=250, etc. plants = []; // Make plants global zombies = []; // Make zombies global bullets = []; // Make bullets global // Dynamic theme based on level var themeBackgrounds = ['desertBackground', 'iceBackground', 'jungleBackground']; var themeIndex = (levelNumber - 3) % themeBackgrounds.length; var themeBg = LK.getAsset(themeBackgrounds[themeIndex], { anchorX: 0, anchorY: 0 }); game.addChild(themeBg); var sunflowers = []; var lastSunGeneration = 0; var lastZombieSpawn = 0; var lastFastZombieSpawn = 0; // Dynamic difficulty scaling based on level var difficultyMultiplier = levelNumber - 2; // Starts at 1 for level 3 var zombieSpawnRate = Math.max(120, 180 - difficultyMultiplier * 20); // Faster spawning each level var fastZombieSpawnRate = Math.max(240, 480 - difficultyMultiplier * 30); // More fast zombies var levelDuration = 3600 + difficultyMultiplier * 600; // Longer levels each time var finalWaveStart = levelDuration - 1800; // Final wave starts 30 seconds before end // Grid setup var gridRows = 10; var gridCols = 12; var gridCellWidth = 150; var gridCellHeight = 100; var gridStartX = 200; var gridStartY = 500; // UI elements sunText = new Text2('Sun: ' + sun, { size: 40, fill: 0x000000 }); sunText.x = 1500; sunText.y = 50; game.addChild(sunText); healthText = new Text2('House Health: ' + houseHealth, { size: 40, fill: 0xFF0000 }); healthText.x = 1500; healthText.y = 100; game.addChild(healthText); // Level indicator var levelText = new Text2('Level ' + levelNumber, { size: 50, fill: 0xFFFFFF }); levelText.x = 1500; levelText.y = 150; game.addChild(levelText); // Create grid for (var row = 0; row < gridRows; row++) { for (var col = 0; col < gridCols; col++) { var gridCell = LK.getAsset('gridTile', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.75, scaleY: 0.75 }); gridCell.x = gridStartX + col * gridCellWidth; gridCell.y = gridStartY + row * gridCellHeight; gridCell.alpha = 0.9; // Apply theme-specific tinting with unique grid appearances if (themeBackgrounds[themeIndex] === 'desertBackground') { gridCell.tint = 0xD2691E; // Sandy orange for desert } else if (themeBackgrounds[themeIndex] === 'iceBackground') { gridCell.tint = 0x87CEEB; // Light blue for ice } else if (themeBackgrounds[themeIndex] === 'jungleBackground') { gridCell.tint = 0x228B22; // Forest green for jungle } game.addChild(gridCell); } } // Plant selection buttons with proper spacing var peashooterButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); peashooterButton.x = 300; peashooterButton.y = 1617; peashooterButton.tint = 0x006400; game.addChild(peashooterButton); // Add peashooter image to button var peashooterImage = LK.getAsset('peashooterBody', { anchorX: 0.5, anchorY: 0.5 }); peashooterImage.x = 300; peashooterImage.y = 1597; peashooterImage.scaleX = 1.0; peashooterImage.scaleY = 1.0; game.addChild(peashooterImage); var peashooterText = new Text2('50 Sun', { size: 32, fill: 0xFFFFFF }); peashooterText.anchor.set(0.5, 0.5); peashooterText.x = 300; peashooterText.y = 1647; game.addChild(peashooterText); var sunflowerButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); sunflowerButton.x = 700; sunflowerButton.y = 1617; sunflowerButton.tint = 0xB8860B; game.addChild(sunflowerButton); // Add sunflower image to button var sunflowerImage = LK.getAsset('sunflowerCenter', { anchorX: 0.5, anchorY: 0.5 }); sunflowerImage.x = 700; sunflowerImage.y = 1597; sunflowerImage.scaleX = 0.75; sunflowerImage.scaleY = 0.75; game.addChild(sunflowerImage); var sunflowerText = new Text2('50 Sun', { size: 32, fill: 0xFFFFFF }); sunflowerText.anchor.set(0.5, 0.5); sunflowerText.x = 700; sunflowerText.y = 1647; game.addChild(sunflowerText); var selectedPlant = null; // Add more plant options based on level if (levelNumber >= 3) { // Add DoublePeashooter button var doublePeaButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); doublePeaButton.x = 1100; doublePeaButton.y = 1617; doublePeaButton.tint = 0x00AA00; game.addChild(doublePeaButton); var doublePeaImage = LK.getAsset('placedDoublePeaBody', { anchorX: 0.5, anchorY: 0.5 }); doublePeaImage.x = 1100; doublePeaImage.y = 1597; game.addChild(doublePeaImage); var doublePeaText = new Text2('125 Sun', { size: 28, fill: 0xFFFFFF }); doublePeaText.anchor.set(0.5, 0.5); doublePeaText.x = 1100; doublePeaText.y = 1647; game.addChild(doublePeaText); doublePeaButton.down = function () { if (sun >= 125) { selectedPlant = 'doublepea'; } }; } if (levelNumber >= 4) { // Add CherryBomb button var cherrybombButton = LK.getAsset('selectionBox', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.2, scaleY: 1.2 }); cherrybombButton.x = 1500; cherrybombButton.y = 1617; cherrybombButton.tint = 0xFF0000; game.addChild(cherrybombButton); var cherrybombImage = LK.getAsset('cherryImage', { anchorX: 0.5, anchorY: 0.5 }); cherrybombImage.x = 1500; cherrybombImage.y = 1597; cherrybombImage.scaleX = 0.6; cherrybombImage.scaleY = 0.6; game.addChild(cherrybombImage); var cherrybombText = new Text2('100 Sun', { size: 28, fill: 0xFFFFFF }); cherrybombText.anchor.set(0.5, 0.5); cherrybombText.x = 1500; cherrybombText.y = 1647; game.addChild(cherrybombText); cherrybombButton.down = function () { if (sun >= 100) { selectedPlant = 'cherrybomb'; } }; } // Event handlers peashooterButton.down = function () { if (sun >= 50) { selectedPlant = 'peashooter'; } }; sunflowerButton.down = function () { if (sun >= 50) { selectedPlant = 'sunflower'; } }; game.down = function (x, y, obj) { if (selectedPlant && x >= gridStartX && x <= gridStartX + gridCols * gridCellWidth && y >= gridStartY && y <= gridStartY + gridRows * gridCellHeight) { var gridCol = Math.floor((x - gridStartX) / gridCellWidth); var gridRow = Math.floor((y - gridStartY) / gridCellHeight); // Check if position is valid and empty var positionEmpty = true; for (var i = 0; i < plants.length; i++) { if (plants[i].gridRow === gridRow && plants[i].gridCol === gridCol) { positionEmpty = false; break; } } if (positionEmpty && gridCol >= 0 && gridCol < gridCols && gridRow >= 0 && gridRow < gridRows) { var plant = null; if (selectedPlant === 'peashooter' && sun >= 50) { plant = new Peashooter(); sun -= 50; } else if (selectedPlant === 'sunflower' && sun >= 50) { plant = new Sunflower(); sun -= 50; } else if (selectedPlant === 'doublepea' && sun >= 125) { plant = new DoublePeashooter(); sun -= 125; } else if (selectedPlant === 'cherrybomb' && sun >= 100) { plant = new CherryBomb(); sun -= 100; } if (plant) { plant.x = gridStartX + gridCol * gridCellWidth; plant.y = gridStartY + gridRow * gridCellHeight; plant.gridRow = gridRow; plant.gridCol = gridCol; plants.push(plant); game.addChild(plant); sunText.setText('Sun: ' + sun); selectedPlant = null; } } } }; var zombiesStarted = false; // Track if zombies have started spawning // Game update function for Dynamic Levels game.update = function () { // Start zombie spawning only after first plant is placed if (!zombiesStarted && plants.length > 0) { zombiesStarted = true; lastZombieSpawn = LK.ticks; // Initialize spawn timer lastFastZombieSpawn = LK.ticks; // Initialize fast zombie spawn timer } // Spawn regular zombies with scaled difficulty - only after zombies started if (zombiesStarted && LK.ticks - lastZombieSpawn > zombieSpawnRate) { var zombie = new Zombie(); var row = Math.floor(Math.random() * gridRows); zombie.x = 1900; zombie.y = gridStartY + row * gridCellHeight; zombie.gridRow = row; zombies.push(zombie); game.addChild(zombie); lastZombieSpawn = LK.ticks; } // Spawn fast zombies with scaled difficulty - only after zombies started if (zombiesStarted && LK.ticks - lastFastZombieSpawn > fastZombieSpawnRate) { var fastZombie = new FastZombie(); var row = Math.floor(Math.random() * gridRows); fastZombie.x = 1900; fastZombie.y = gridStartY + row * gridCellHeight; fastZombie.gridRow = row; zombies.push(fastZombie); game.addChild(fastZombie); lastFastZombieSpawn = LK.ticks; } // Spawn tank zombies on higher levels - only after zombies started if (zombiesStarted && levelNumber >= 4 && LK.ticks % (1200 - difficultyMultiplier * 100) === 0) { var tankZombie = new TankZombie(); var row = Math.floor(Math.random() * gridRows); tankZombie.x = 1900; tankZombie.y = gridStartY + row * gridCellHeight; tankZombie.gridRow = row; zombies.push(tankZombie); game.addChild(tankZombie); } // Spawn jumper zombies on even higher levels - only after zombies started if (zombiesStarted && levelNumber >= 6 && LK.ticks % (900 - difficultyMultiplier * 50) === 0) { var jumperZombie = new JumperZombie(); var row = Math.floor(Math.random() * gridRows); jumperZombie.x = 1900; jumperZombie.y = gridStartY + row * gridCellHeight; jumperZombie.gridRow = row; zombies.push(jumperZombie); game.addChild(jumperZombie); } // Update all game objects for (var i = plants.length - 1; i >= 0; i--) { plants[i].update(); } for (var j = zombies.length - 1; j >= 0; j--) { zombies[j].update(); } for (var k = bullets.length - 1; k >= 0; k--) { bullets[k].update(); } // Generate sun from sunflowers - only if sunflowers exist var sunflowerCount = 0; for (var s = 0; s < plants.length; s++) { if (plants[s] instanceof Sunflower) { sunflowerCount++; } } if (sunflowerCount > 0 && LK.ticks - lastSunGeneration > 600) { // Every 10 seconds, only if sunflowers exist sun += 5; sunText.setText('Sun: ' + sun); lastSunGeneration = LK.ticks; } // Final wave - spawn many more zombies rapidly in the final period if (LK.ticks > finalWaveStart && LK.ticks <= levelDuration) { var finalWaveSpawnRate = Math.max(60, zombieSpawnRate / 2); // Even faster in final wave if (LK.ticks - lastZombieSpawn > finalWaveSpawnRate) { var zombie = new Zombie(); var row = Math.floor(Math.random() * gridRows); zombie.x = 1900; zombie.y = gridStartY + row * gridCellHeight; zombie.gridRow = row; zombies.push(zombie); game.addChild(zombie); lastZombieSpawn = LK.ticks; } // Also spawn fast zombies more frequently during final wave var finalFastZombieRate = Math.max(180, fastZombieSpawnRate / 2); if (LK.ticks - lastFastZombieSpawn > finalFastZombieRate) { var fastZombie = new FastZombie(); var row = Math.floor(Math.random() * gridRows); fastZombie.x = 1900; fastZombie.y = gridStartY + row * gridCellHeight; fastZombie.gridRow = row; zombies.push(fastZombie); game.addChild(fastZombie); lastFastZombieSpawn = LK.ticks; } } // Check win condition (level duration increases with each level) if (LK.ticks > levelDuration && zombies.length === 0) { unlockNextLevel(); // Unlock next level when current level is completed // Calculate stars based on house health: 3 stars = 80+%, 2 stars = 50+%, 1 star = any health remaining var stars = 1; if (houseHealth >= 80) { stars = 3; } else if (houseHealth >= 50) { stars = 2; } setLevelStars(levelNumber, stars); LK.showYouWin(); } }; } // Function to unlock next level function unlockNextLevel() { if (unlockedLevels < totalLevels) { unlockedLevels++; storage.unlockedLevels = unlockedLevels; // Save to storage if (levelButtons[unlockedLevels - 1]) { levelButtons[unlockedLevels - 1].setUnlocked(true); } } } // Function to set stars for a level function setLevelStars(levelNumber, stars) { levelProgress[levelNumber] = stars; storage.levelProgress = levelProgress; // Save to storage if (levelButtons[levelNumber - 1]) { levelButtons[levelNumber - 1].setStars(stars); } } // Pause menu variables var isPaused = false; var pauseMenu = null; var currentLevel = 0; // Function to show pause menu function showPauseMenu() { if (pauseMenu) return; // Already showing // Create pause menu background pauseMenu = new Container(); var pauseBg = LK.getAsset('paperBackground', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.6, scaleY: 0.4 }); pauseBg.x = 0; pauseBg.y = 0; pauseBg.tint = 0x000000; pauseBg.alpha = 0.8; pauseMenu.addChild(pauseBg); // Pause title var pauseTitle = new Text2('PAUSED', { size: 60, fill: 0xFFFFFF }); pauseTitle.anchor.set(0.5, 0.5); pauseTitle.x = 0; pauseTitle.y = -100; pauseMenu.addChild(pauseTitle); // Restart button var restartButton = LK.getAsset('levelButton', { anchorX: 0.5, anchorY: 0.5 }); restartButton.x = 0; restartButton.y = 0; restartButton.tint = 0x4CAF50; pauseMenu.addChild(restartButton); var restartText = new Text2('RESTART', { size: 40, fill: 0x000000 }); restartText.anchor.set(0.5, 0.5); restartText.x = 0; restartText.y = 0; pauseMenu.addChild(restartText); // Resume button var resumeButton = LK.getAsset('levelButton', { anchorX: 0.5, anchorY: 0.5 }); resumeButton.x = 0; resumeButton.y = 120; resumeButton.tint = 0x2196F3; pauseMenu.addChild(resumeButton); var resumeText = new Text2('RESUME', { size: 40, fill: 0x000000 }); resumeText.anchor.set(0.5, 0.5); resumeText.x = 0; resumeText.y = 120; pauseMenu.addChild(resumeText); // Position pause menu at center of screen pauseMenu.x = 1024; pauseMenu.y = 1366; // Add event handlers restartButton.down = function () { LK.getSound('buttonClick').play(); hidePauseMenu(); // Restart current level if (currentLevel === 1) { game.removeChildren(); initializeLevel1(); } else if (currentLevel === 2) { game.removeChildren(); initializeLevel2(); } else if (currentLevel >= 3 && currentLevel <= 10) { game.removeChildren(); initializeDynamicLevel(currentLevel); } }; resumeButton.down = function () { LK.getSound('buttonClick').play(); hidePauseMenu(); }; game.addChild(pauseMenu); } // Function to hide pause menu function hidePauseMenu() { if (pauseMenu) { game.removeChild(pauseMenu); pauseMenu = null; } isPaused = false; } // Override game pause handler LK.on('pause', function () { if (currentLevel > 0) { // Only show pause menu during gameplay isPaused = true; showPauseMenu(); } }); // Simple game update for level selection screen game.update = function () { // No specific updates needed for level selection // All interactions are handled by level button click events };
/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('bulletCore', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.damage = 50;
self.row = 0;
self.update = function () {
self.x += self.speed;
// Check collision with zombies
for (var i = zombies.length - 1; i >= 0; i--) {
var zombie = zombies[i];
if (zombie.gridRow === self.row && Math.abs(self.x - zombie.x) < 30) {
zombie.health -= self.damage;
if (zombie.health <= 0) {
sun += 2; // Give 2 money for killing zombie
sunText.setText('Sun: ' + sun);
zombie.destroy();
zombies.splice(i, 1);
}
self.destroy();
for (var j = bullets.length - 1; j >= 0; j--) {
if (bullets[j] === self) {
bullets.splice(j, 1);
break;
}
}
return;
}
}
// Remove if off screen
if (self.x > 2048) {
self.destroy();
for (var k = bullets.length - 1; k >= 0; k--) {
if (bullets[k] === self) {
bullets.splice(k, 1);
break;
}
}
}
};
return self;
});
var CherryBomb = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('cherryImage', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 50;
self.gridRow = 0;
self.gridCol = 0;
self.fuseTime = 180; // 3 seconds at 60fps
self.planted = false;
self.update = function () {
if (!self.planted) {
self.planted = true;
self.plantedTick = LK.ticks;
}
// Explode after fuse time
if (LK.ticks - self.plantedTick > self.fuseTime) {
// Destroy all zombies in 3x3 area around bomb
for (var i = zombies.length - 1; i >= 0; i--) {
var zombie = zombies[i];
var distance = Math.sqrt(Math.pow(self.x - zombie.x, 2) + Math.pow(self.y - zombie.y, 2));
if (distance < 200) {
// Explosion radius
zombie.destroy();
zombies.splice(i, 1);
}
}
// Remove self
for (var j = plants.length - 1; j >= 0; j--) {
if (plants[j] === self) {
plants.splice(j, 1);
break;
}
}
self.destroy();
}
};
return self;
});
var DoublePeashooter = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('placedDoublePeaBody', {
anchorX: 0.5,
anchorY: 0.5
});
var cannon1 = self.attachAsset('peashooterCannon', {
anchorX: 0,
anchorY: 0.3
});
cannon1.x = 25;
cannon1.y = -10;
var cannon2 = self.attachAsset('peashooterCannon', {
anchorX: 0,
anchorY: 0.7
});
cannon2.x = 25;
cannon2.y = 10;
self.health = 100;
self.lastShot = 0;
self.shootDelay = 100; // Faster than regular peashooter
self.gridRow = 0;
self.gridCol = 0;
self.shoot = function () {
// Shoot two bullets
var bullet1 = new Bullet();
bullet1.x = self.x + 50;
bullet1.y = self.y - 10;
bullet1.row = self.gridRow;
bullets.push(bullet1);
game.addChild(bullet1);
var bullet2 = new Bullet();
bullet2.x = self.x + 50;
bullet2.y = self.y + 10;
bullet2.row = self.gridRow;
bullets.push(bullet2);
game.addChild(bullet2);
};
self.update = function () {
if (LK.ticks - self.lastShot > self.shootDelay) {
var zombieInRow = false;
for (var i = 0; i < zombies.length; i++) {
if (zombies[i].gridRow === self.gridRow && zombies[i].x > self.x) {
zombieInRow = true;
break;
}
}
if (zombieInRow) {
self.shoot();
self.lastShot = LK.ticks;
}
}
};
return self;
});
var FastZombie = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('zombieBody', {
anchorX: 0.5,
anchorY: 1
});
body.y = 25;
body.tint = 0xFF6666; // Reddish tint to distinguish from regular zombie
var head = self.attachAsset('zombieHead', {
anchorX: 0.5,
anchorY: 1
});
head.y = -10;
head.tint = 0xFF6666; // Reddish tint to distinguish from regular zombie
self.health = 60; // Lower health than regular zombie
self.speed = 1.2; // Faster than regular zombie
self.gridRow = 0;
self.isEating = false;
self.eatDamage = 15; // More damage when eating
self.hasAttackedHouse = false;
self.update = function () {
if (!self.isEating) {
self.x -= self.speed;
}
// Check if reached house - zombie damages house once with 15 health
if (self.x <= 150 && !self.hasAttackedHouse) {
houseHealth -= 15;
healthText.setText('House Health: ' + houseHealth);
self.hasAttackedHouse = true;
// Destroy zombie after attacking house
self.destroy();
for (var z = zombies.length - 1; z >= 0; z--) {
if (zombies[z] === self) {
zombies.splice(z, 1);
break;
}
}
// Check game over condition
if (houseHealth <= 0) {
LK.showGameOver();
}
}
// Check collision with plants
for (var i = plants.length - 1; i >= 0; i--) {
var plant = plants[i];
if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) {
self.isEating = true;
plant.health -= 3; // Fast zombie eats faster
if (plant.health <= 0) {
plant.destroy();
plants.splice(i, 1);
self.isEating = false;
}
break;
}
}
};
return self;
});
var JumperZombie = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('zombieBody', {
anchorX: 0.5,
anchorY: 1
});
body.y = 25;
body.tint = 0x00FF00; // Green tint for jumper zombie
var head = self.attachAsset('zombieHead', {
anchorX: 0.5,
anchorY: 1
});
head.y = -10;
head.tint = 0x00FF00;
self.health = 80;
self.speed = 0.8;
self.gridRow = 0;
self.isEating = false;
self.eatDamage = 12;
self.hasAttackedHouse = false;
self.canJump = true;
self.update = function () {
if (!self.isEating) {
self.x -= self.speed;
}
// Jump over first plant encountered
if (self.canJump) {
for (var i = 0; i < plants.length; i++) {
var plant = plants[i];
if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 80 && self.x > plant.x) {
self.x = plant.x - 80; // Jump past the plant
self.canJump = false;
break;
}
}
}
// Check if reached house
if (self.x <= 150 && !self.hasAttackedHouse) {
houseHealth -= 12;
healthText.setText('House Health: ' + houseHealth);
self.hasAttackedHouse = true;
self.destroy();
for (var z = zombies.length - 1; z >= 0; z--) {
if (zombies[z] === self) {
zombies.splice(z, 1);
break;
}
}
if (houseHealth <= 0) {
LK.showGameOver();
}
}
// Check collision with plants (only if can't jump)
if (!self.canJump) {
for (var i = plants.length - 1; i >= 0; i--) {
var plant = plants[i];
if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) {
self.isEating = true;
plant.health -= 3;
if (plant.health <= 0) {
plant.destroy();
plants.splice(i, 1);
self.isEating = false;
}
break;
}
}
}
};
return self;
});
// Event handlers
var LevelButton = Container.expand(function () {
var self = Container.call(this);
var buttonBg = self.attachAsset('levelButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.levelNumber = 1;
self.setLevelNumber = function (number) {
self.levelNumber = number;
self.levelText.setText(number.toString());
};
self.isUnlocked = true;
self.stars = 0;
self.maxStars = 3;
// Create level number text
self.levelText = new Text2(self.levelNumber.toString(), {
size: 50,
fill: 0xFFFFFF
});
self.levelText.anchor.set(0.5, 0.5);
self.levelText.y = -30;
self.addChild(self.levelText);
// Create stars display
self.starContainers = [];
for (var i = 0; i < self.maxStars; i++) {
var star = LK.getAsset('starShape', {
anchorX: 0.5,
anchorY: 0.5
});
star.x = (i - 1) * 50;
star.y = 30;
star.tint = 0x666666; // Dark by default
self.addChild(star);
self.starContainers.push(star);
}
self.setStars = function (starCount) {
self.stars = starCount;
for (var i = 0; i < self.maxStars; i++) {
if (i < starCount) {
self.starContainers[i].tint = 0xFFD700; // Golden
} else {
self.starContainers[i].tint = 0x666666; // Dark
}
}
};
self.setUnlocked = function (unlocked) {
self.isUnlocked = unlocked;
if (unlocked) {
buttonBg.tint = 0xFFFFFF;
self.levelText.tint = 0xFFFFFF;
} else {
buttonBg.tint = 0x666666;
self.levelText.tint = 0x666666;
}
};
self.down = function (x, y, obj) {
if (self.isUnlocked) {
LK.getSound('buttonClick').play();
// Start the selected level
startLevel(self.levelNumber);
}
};
return self;
});
// Plant classes
var Peashooter = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('peashooterBody', {
anchorX: 0.5,
anchorY: 0.5
});
var cannon = self.attachAsset('peashooterCannon', {
anchorX: 0,
anchorY: 0.5
});
cannon.x = 25;
cannon.y = 0;
self.health = 100;
self.lastShot = 0;
self.shootDelay = 150; // 2.5 seconds at 60fps
self.gridRow = 0;
self.gridCol = 0;
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x + 50;
bullet.y = self.y;
bullet.row = self.gridRow;
bullets.push(bullet);
game.addChild(bullet);
};
self.update = function () {
if (LK.ticks - self.lastShot > self.shootDelay) {
// Check if there's a zombie in this row
var zombieInRow = false;
for (var i = 0; i < zombies.length; i++) {
if (zombies[i].gridRow === self.gridRow && zombies[i].x > self.x) {
zombieInRow = true;
break;
}
}
if (zombieInRow) {
self.shoot();
self.lastShot = LK.ticks;
}
}
};
return self;
});
var Sunflower = Container.expand(function () {
var self = Container.call(this);
// Create petals around center
for (var i = 0; i < 8; i++) {
var petal = self.attachAsset('sunflowerPetal', {
anchorX: 0.5,
anchorY: 1
});
var angle = i / 8 * Math.PI * 2;
petal.x = Math.cos(angle) * 25;
petal.y = Math.sin(angle) * 25;
petal.rotation = angle + Math.PI / 2;
}
var center = self.attachAsset('sunflowerCenter', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.lastSunGeneration = 0;
self.sunDelay = 300; // 5 seconds at 60fps
self.gridRow = 0;
self.gridCol = 0;
self.generateSun = function () {
sun += 15;
sunText.setText('Sun: ' + sun);
self.lastSunGeneration = LK.ticks;
};
self.update = function () {
if (LK.ticks - self.lastSunGeneration > self.sunDelay) {
self.generateSun();
}
};
return self;
});
var TankZombie = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('zombieBody', {
anchorX: 0.5,
anchorY: 1
});
body.y = 25;
body.tint = 0x333333; // Dark gray tint for tank zombie
body.scaleX = 1.5;
body.scaleY = 1.5;
var head = self.attachAsset('zombieHead', {
anchorX: 0.5,
anchorY: 1
});
head.y = -15;
head.tint = 0x333333;
head.scaleX = 1.5;
head.scaleY = 1.5;
self.health = 300; // Very high health
self.speed = 0.3; // Very slow
self.gridRow = 0;
self.isEating = false;
self.eatDamage = 20;
self.hasAttackedHouse = false;
self.update = function () {
if (!self.isEating) {
self.x -= self.speed;
}
// Check if reached house
if (self.x <= 150 && !self.hasAttackedHouse) {
houseHealth -= 25;
healthText.setText('House Health: ' + houseHealth);
self.hasAttackedHouse = true;
self.destroy();
for (var z = zombies.length - 1; z >= 0; z--) {
if (zombies[z] === self) {
zombies.splice(z, 1);
break;
}
}
if (houseHealth <= 0) {
LK.showGameOver();
}
}
// Check collision with plants
for (var i = plants.length - 1; i >= 0; i--) {
var plant = plants[i];
if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) {
self.isEating = true;
plant.health -= 5; // Tank zombie eats slowly but powerfully
if (plant.health <= 0) {
plant.destroy();
plants.splice(i, 1);
self.isEating = false;
}
break;
}
}
};
return self;
});
var WallNut = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('peashooterBody', {
anchorX: 0.5,
anchorY: 0.5
});
body.tint = 0x8B4513; // Brown color for wall
body.scaleX = 1.3;
body.scaleY = 1.3;
self.health = 400; // Very high health, no attacking
self.gridRow = 0;
self.gridCol = 0;
self.update = function () {
// Wall plants don't do anything, just block zombies
};
return self;
});
var Zombie = Container.expand(function () {
var self = Container.call(this);
var body = self.attachAsset('zombieBody', {
anchorX: 0.5,
anchorY: 1
});
body.y = 25;
var head = self.attachAsset('zombieHead', {
anchorX: 0.5,
anchorY: 1
});
head.y = -10;
self.health = 100;
self.speed = 0.5;
self.gridRow = 0;
self.isEating = false;
self.eatDamage = 10;
self.hasAttackedHouse = false;
self.update = function () {
if (!self.isEating) {
self.x -= self.speed;
}
// Check if reached house - zombie damages house once with 10 health
if (self.x <= 150 && !self.hasAttackedHouse) {
houseHealth -= 10;
healthText.setText('House Health: ' + houseHealth);
self.hasAttackedHouse = true;
// Destroy zombie after attacking house
self.destroy();
for (var z = zombies.length - 1; z >= 0; z--) {
if (zombies[z] === self) {
zombies.splice(z, 1);
break;
}
}
// Check game over condition
if (houseHealth <= 0) {
LK.showGameOver();
}
}
// Check collision with plants
for (var i = plants.length - 1; i >= 0; i--) {
var plant = plants[i];
if (plant.gridRow === self.gridRow && Math.abs(self.x - plant.x) < 50) {
self.isEating = true;
plant.health -= 2;
if (plant.health <= 0) {
plant.destroy();
plants.splice(i, 1);
self.isEating = false;
}
break;
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x5D4E37 // Darker brown paper background color
});
/****
* Game Code
****/
// Import storage for persistent level progression
// Level selection screen variables
var levelButtons = [];
var totalLevels = 10;
var unlockedLevels = storage.unlockedLevels || 1; // Load from storage or default to 1
var levelProgress = storage.levelProgress || {}; // Load from storage or default to empty
// Global game variables
var plants = [];
var zombies = [];
var bullets = [];
var sun = 150;
var sunText;
var houseHealth;
var healthText;
// Create brown paper background
var background = LK.getAsset('paperBackground', {
anchorX: 0,
anchorY: 0
});
game.addChild(background);
// Create title
var titleBg = LK.getAsset('titleBackground', {
anchorX: 0.5,
anchorY: 0.5
});
titleBg.x = 1024;
titleBg.y = 300;
game.addChild(titleBg);
// Create level buttons in a grid layout
var buttonsPerRow = 5;
var buttonSpacingX = 350;
var buttonSpacingY = 250;
var startX = 400;
var startY = 700;
for (var i = 0; i < totalLevels; i++) {
var levelButton = new LevelButton();
levelButton.setLevelNumber(i + 1);
// Calculate position in grid
var row = Math.floor(i / buttonsPerRow);
var col = i % buttonsPerRow;
levelButton.x = startX + col * buttonSpacingX;
levelButton.y = startY + row * buttonSpacingY;
// Set unlock status
levelButton.setUnlocked(i < unlockedLevels);
// Set stars from progress (if any)
if (levelProgress[i + 1]) {
levelButton.setStars(levelProgress[i + 1]);
} else {
levelButton.setStars(0);
}
levelButtons.push(levelButton);
game.addChild(levelButton);
}
// Function to start a level
function startLevel(levelNumber) {
LK.getSound('levelSelect').play();
console.log('Starting level: ' + levelNumber);
// Set current level for pause functionality
currentLevel = levelNumber;
// Clear the level selection screen
game.removeChildren();
// Initialize the appropriate level
if (levelNumber === 1) {
initializeLevel1();
} else if (levelNumber === 2) {
initializeLevel2();
} else if (levelNumber >= 3 && levelNumber <= 10) {
// Initialize dynamic levels for 3-10
initializeDynamicLevel(levelNumber);
}
}
// Initialize Level 1 Plants vs Zombies game
function initializeLevel1() {
// Game variables
houseHealth = 100;
sun = 150; // Starting money for level 1
plants = []; // Make plants global
zombies = []; // Make zombies global
bullets = []; // Make bullets global
var sunflowers = [];
var lastSunGeneration = 0;
var lastZombieSpawn = 0;
// Grid setup
var gridRows = 10;
var gridCols = 12;
var gridCellWidth = 150;
var gridCellHeight = 100;
var gridStartX = 200;
var gridStartY = 500;
// UI elements
sunText = new Text2('Sun: ' + sun, {
size: 40,
fill: 0x000000
});
sunText.x = 1500;
sunText.y = 50;
game.addChild(sunText);
healthText = new Text2('House Health: ' + houseHealth, {
size: 40,
fill: 0xFF0000
});
healthText.x = 1500;
healthText.y = 100;
game.addChild(healthText);
// Create grid
for (var row = 0; row < gridRows; row++) {
for (var col = 0; col < gridCols; col++) {
var gridCell = LK.getAsset('gridTile', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.75,
scaleY: 0.75
});
gridCell.x = gridStartX + col * gridCellWidth;
gridCell.y = gridStartY + row * gridCellHeight;
gridCell.alpha = 0.8;
gridCell.tint = 0x8B7355;
game.addChild(gridCell);
}
}
// Plant selection buttons with proper spacing
var peashooterButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
peashooterButton.x = 400;
peashooterButton.y = 1617;
peashooterButton.tint = 0x006400;
game.addChild(peashooterButton);
// Add peashooter image to button
var peashooterImage = LK.getAsset('peashooterBody', {
anchorX: 0.5,
anchorY: 0.5
});
peashooterImage.x = 400;
peashooterImage.y = 1597;
peashooterImage.scaleX = 1.0;
peashooterImage.scaleY = 1.0;
game.addChild(peashooterImage);
var peashooterText = new Text2('50 Sun', {
size: 32,
fill: 0xFFFFFF
});
peashooterText.anchor.set(0.5, 0.5);
peashooterText.x = 400;
peashooterText.y = 1647;
game.addChild(peashooterText);
var sunflowerButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
sunflowerButton.x = 1000;
sunflowerButton.y = 1617;
sunflowerButton.tint = 0xB8860B;
game.addChild(sunflowerButton);
// Add sunflower image to button
var sunflowerImage = LK.getAsset('sunflowerCenter', {
anchorX: 0.5,
anchorY: 0.5
});
sunflowerImage.x = 1000;
sunflowerImage.y = 1597;
sunflowerImage.scaleX = 0.75;
sunflowerImage.scaleY = 0.75;
game.addChild(sunflowerImage);
var sunflowerText = new Text2('50 Sun', {
size: 32,
fill: 0xFFFFFF
});
sunflowerText.anchor.set(0.5, 0.5);
sunflowerText.x = 1000;
sunflowerText.y = 1647;
game.addChild(sunflowerText);
var selectedPlant = null;
// Event handlers
peashooterButton.down = function () {
if (sun >= 50) {
selectedPlant = 'peashooter';
}
};
sunflowerButton.down = function () {
if (sun >= 50) {
selectedPlant = 'sunflower';
}
};
game.down = function (x, y, obj) {
if (selectedPlant && x >= gridStartX && x <= gridStartX + gridCols * gridCellWidth && y >= gridStartY && y <= gridStartY + gridRows * gridCellHeight) {
var gridCol = Math.floor((x - gridStartX) / gridCellWidth);
var gridRow = Math.floor((y - gridStartY) / gridCellHeight);
// Check if position is valid and empty
var positionEmpty = true;
for (var i = 0; i < plants.length; i++) {
if (plants[i].gridRow === gridRow && plants[i].gridCol === gridCol) {
positionEmpty = false;
break;
}
}
if (positionEmpty && gridCol >= 0 && gridCol < gridCols && gridRow >= 0 && gridRow < gridRows) {
var plant = null;
if (selectedPlant === 'peashooter' && sun >= 50) {
plant = new Peashooter();
sun -= 50;
} else if (selectedPlant === 'sunflower' && sun >= 50) {
plant = new Sunflower();
sun -= 50;
}
if (plant) {
plant.x = gridStartX + gridCol * gridCellWidth;
plant.y = gridStartY + gridRow * gridCellHeight;
plant.gridRow = gridRow;
plant.gridCol = gridCol;
plants.push(plant);
game.addChild(plant);
sunText.setText('Sun: ' + sun);
selectedPlant = null;
}
}
}
};
var zombiesStarted = false; // Track if zombies have started spawning
// Game update function
game.update = function () {
// Start zombie spawning only after first plant is placed
if (!zombiesStarted && plants.length > 0) {
zombiesStarted = true;
lastZombieSpawn = LK.ticks; // Initialize spawn timer
}
// Spawn zombies only after they have been started
if (zombiesStarted && LK.ticks - lastZombieSpawn > 300) {
// Every 5 seconds
var zombie = new Zombie();
var row = Math.floor(Math.random() * gridRows);
zombie.x = 1900;
zombie.y = gridStartY + row * gridCellHeight;
zombie.gridRow = row;
zombies.push(zombie);
game.addChild(zombie);
lastZombieSpawn = LK.ticks;
}
// Update all game objects
for (var i = plants.length - 1; i >= 0; i--) {
plants[i].update();
}
for (var j = zombies.length - 1; j >= 0; j--) {
zombies[j].update();
}
for (var k = bullets.length - 1; k >= 0; k--) {
bullets[k].update();
}
// Generate sun from sunflowers - only if sunflowers exist
var sunflowerCount = 0;
for (var s = 0; s < plants.length; s++) {
if (plants[s] instanceof Sunflower) {
sunflowerCount++;
}
}
if (sunflowerCount > 0 && LK.ticks - lastSunGeneration > 600) {
// Every 10 seconds, only if sunflowers exist
sun += 5;
sunText.setText('Sun: ' + sun);
lastSunGeneration = LK.ticks;
}
// Final wave - spawn more zombies rapidly in the last 20 seconds
if (LK.ticks > 2400 && LK.ticks <= 3600) {
// Between 40s and 60s
if (LK.ticks - lastZombieSpawn > 120) {
// Every 2 seconds instead of 5
var zombie = new Zombie();
var row = Math.floor(Math.random() * gridRows);
zombie.x = 1900;
zombie.y = gridStartY + row * gridCellHeight;
zombie.gridRow = row;
zombies.push(zombie);
game.addChild(zombie);
lastZombieSpawn = LK.ticks;
}
}
// Check win condition
if (LK.ticks > 3600 && zombies.length === 0) {
// After 1 minute with no zombies
unlockNextLevel(); // Unlock next level when current level is completed
// Calculate stars based on house health: 3 stars = 80+%, 2 stars = 50+%, 1 star = any health remaining
var stars = 1;
if (houseHealth >= 80) {
stars = 3;
} else if (houseHealth >= 50) {
stars = 2;
}
setLevelStars(1, stars);
LK.showYouWin();
}
};
}
// Initialize Level 2 Plants vs Zombies game with more zombies and fast zombies
function initializeLevel2() {
// Game variables
houseHealth = 100;
sun = 200; // Starting money for level 2 (50 more than level 1)
plants = []; // Make plants global
zombies = []; // Make zombies global
bullets = []; // Make bullets global
// Night theme background
var nightBg = LK.getAsset('nightBackground', {
anchorX: 0,
anchorY: 0
});
game.addChild(nightBg);
var sunflowers = [];
var lastSunGeneration = 0;
var lastZombieSpawn = 0;
var lastFastZombieSpawn = 0;
// Grid setup
var gridRows = 10;
var gridCols = 12;
var gridCellWidth = 150;
var gridCellHeight = 100;
var gridStartX = 200;
var gridStartY = 500;
// UI elements
sunText = new Text2('Sun: ' + sun, {
size: 40,
fill: 0x000000
});
sunText.x = 1500;
sunText.y = 50;
game.addChild(sunText);
healthText = new Text2('House Health: ' + houseHealth, {
size: 40,
fill: 0xFF0000
});
healthText.x = 1500;
healthText.y = 100;
game.addChild(healthText);
// Level indicator
var levelText = new Text2('Level 2', {
size: 50,
fill: 0xFFFFFF
});
levelText.x = 1500;
levelText.y = 150;
game.addChild(levelText);
// Create grid
for (var row = 0; row < gridRows; row++) {
for (var col = 0; col < gridCols; col++) {
var gridCell = LK.getAsset('gridTile', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.75,
scaleY: 0.75
});
gridCell.x = gridStartX + col * gridCellWidth;
gridCell.y = gridStartY + row * gridCellHeight;
gridCell.alpha = 0.9;
gridCell.tint = 0x1A0D33;
game.addChild(gridCell);
}
}
// Plant selection buttons with proper spacing
var peashooterButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
peashooterButton.x = 350;
peashooterButton.y = 1617;
peashooterButton.tint = 0x006400;
game.addChild(peashooterButton);
// Add peashooter image to button
var peashooterImage = LK.getAsset('peashooterBody', {
anchorX: 0.5,
anchorY: 0.5
});
peashooterImage.x = 350;
peashooterImage.y = 1597;
peashooterImage.scaleX = 1.0;
peashooterImage.scaleY = 1.0;
game.addChild(peashooterImage);
var peashooterText = new Text2('50 Sun', {
size: 32,
fill: 0xFFFFFF
});
peashooterText.anchor.set(0.5, 0.5);
peashooterText.x = 350;
peashooterText.y = 1647;
game.addChild(peashooterText);
var sunflowerButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
sunflowerButton.x = 800;
sunflowerButton.y = 1617;
sunflowerButton.tint = 0xB8860B;
game.addChild(sunflowerButton);
// Add sunflower image to button
var sunflowerImage = LK.getAsset('sunflowerCenter', {
anchorX: 0.5,
anchorY: 0.5
});
sunflowerImage.x = 800;
sunflowerImage.y = 1597;
sunflowerImage.scaleX = 0.75;
sunflowerImage.scaleY = 0.75;
game.addChild(sunflowerImage);
var sunflowerText = new Text2('50 Sun', {
size: 32,
fill: 0xFFFFFF
});
sunflowerText.anchor.set(0.5, 0.5);
sunflowerText.x = 800;
sunflowerText.y = 1647;
game.addChild(sunflowerText);
var selectedPlant = null;
// Add CherryBomb button for level 2
var cherrybombButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
cherrybombButton.x = 1250;
cherrybombButton.y = 1617;
cherrybombButton.tint = 0xFF0000;
game.addChild(cherrybombButton);
var cherrybombImage = LK.getAsset('cherryImage', {
anchorX: 0.5,
anchorY: 0.5
});
cherrybombImage.x = 1250;
cherrybombImage.y = 1597;
cherrybombImage.scaleX = 0.75;
cherrybombImage.scaleY = 0.75;
game.addChild(cherrybombImage);
var cherrybombText = new Text2('100 Sun', {
size: 32,
fill: 0xFFFFFF
});
cherrybombText.anchor.set(0.5, 0.5);
cherrybombText.x = 1250;
cherrybombText.y = 1647;
game.addChild(cherrybombText);
cherrybombButton.down = function () {
if (sun >= 100) {
selectedPlant = 'cherrybomb';
}
};
// Event handlers
peashooterButton.down = function () {
if (sun >= 50) {
selectedPlant = 'peashooter';
}
};
sunflowerButton.down = function () {
if (sun >= 50) {
selectedPlant = 'sunflower';
}
};
game.down = function (x, y, obj) {
if (selectedPlant && x >= gridStartX && x <= gridStartX + gridCols * gridCellWidth && y >= gridStartY && y <= gridStartY + gridRows * gridCellHeight) {
var gridCol = Math.floor((x - gridStartX) / gridCellWidth);
var gridRow = Math.floor((y - gridStartY) / gridCellHeight);
// Check if position is valid and empty
var positionEmpty = true;
for (var i = 0; i < plants.length; i++) {
if (plants[i].gridRow === gridRow && plants[i].gridCol === gridCol) {
positionEmpty = false;
break;
}
}
if (positionEmpty && gridCol >= 0 && gridCol < gridCols && gridRow >= 0 && gridRow < gridRows) {
var plant = null;
if (selectedPlant === 'peashooter' && sun >= 50) {
plant = new Peashooter();
sun -= 50;
} else if (selectedPlant === 'sunflower' && sun >= 50) {
plant = new Sunflower();
sun -= 50;
} else if (selectedPlant === 'cherrybomb' && sun >= 100) {
plant = new CherryBomb();
sun -= 100;
}
if (plant) {
plant.x = gridStartX + gridCol * gridCellWidth;
plant.y = gridStartY + gridRow * gridCellHeight;
plant.gridRow = gridRow;
plant.gridCol = gridCol;
plants.push(plant);
game.addChild(plant);
sunText.setText('Sun: ' + sun);
selectedPlant = null;
}
}
}
};
var zombiesStarted = false; // Track if zombies have started spawning
// Game update function for Level 2
game.update = function () {
// Start zombie spawning only after first plant is placed
if (!zombiesStarted && plants.length > 0) {
zombiesStarted = true;
lastZombieSpawn = LK.ticks; // Initialize spawn timer
lastFastZombieSpawn = LK.ticks; // Initialize fast zombie spawn timer
}
// Spawn regular zombies more frequently (every 3 seconds instead of 5) - only after zombies started
if (zombiesStarted && LK.ticks - lastZombieSpawn > 180) {
var zombie = new Zombie();
var row = Math.floor(Math.random() * gridRows);
zombie.x = 1900;
zombie.y = gridStartY + row * gridCellHeight;
zombie.gridRow = row;
zombies.push(zombie);
game.addChild(zombie);
lastZombieSpawn = LK.ticks;
}
// Spawn fast zombies every 8 seconds - only after zombies started
if (zombiesStarted && LK.ticks - lastFastZombieSpawn > 480) {
var fastZombie = new FastZombie();
var row = Math.floor(Math.random() * gridRows);
fastZombie.x = 1900;
fastZombie.y = gridStartY + row * gridCellHeight;
fastZombie.gridRow = row;
zombies.push(fastZombie);
game.addChild(fastZombie);
lastFastZombieSpawn = LK.ticks;
}
// Spawn tank zombies every 15 seconds - only after zombies started
if (zombiesStarted && LK.ticks % 900 === 0) {
var tankZombie = new TankZombie();
var row = Math.floor(Math.random() * gridRows);
tankZombie.x = 1900;
tankZombie.y = gridStartY + row * gridCellHeight;
tankZombie.gridRow = row;
zombies.push(tankZombie);
game.addChild(tankZombie);
}
// Update all game objects
for (var i = plants.length - 1; i >= 0; i--) {
plants[i].update();
}
for (var j = zombies.length - 1; j >= 0; j--) {
zombies[j].update();
}
for (var k = bullets.length - 1; k >= 0; k--) {
bullets[k].update();
}
// Generate sun from sunflowers - only if sunflowers exist
var sunflowerCount = 0;
for (var s = 0; s < plants.length; s++) {
if (plants[s] instanceof Sunflower) {
sunflowerCount++;
}
}
if (sunflowerCount > 0 && LK.ticks - lastSunGeneration > 600) {
// Every 10 seconds, only if sunflowers exist
sun += 5;
sunText.setText('Sun: ' + sun);
lastSunGeneration = LK.ticks;
}
// Final wave - spawn many more zombies rapidly in the last 30 seconds
if (LK.ticks > 3600 && LK.ticks <= 5400) {
// Between 60s and 90s
if (LK.ticks - lastZombieSpawn > 90) {
// Every 1.5 seconds instead of 3
var zombie = new Zombie();
var row = Math.floor(Math.random() * gridRows);
zombie.x = 1900;
zombie.y = gridStartY + row * gridCellHeight;
zombie.gridRow = row;
zombies.push(zombie);
game.addChild(zombie);
lastZombieSpawn = LK.ticks;
}
// Also spawn fast zombies more frequently during final wave
if (LK.ticks - lastFastZombieSpawn > 300) {
// Every 5 seconds instead of 8
var fastZombie = new FastZombie();
var row = Math.floor(Math.random() * gridRows);
fastZombie.x = 1900;
fastZombie.y = gridStartY + row * gridCellHeight;
fastZombie.gridRow = row;
zombies.push(fastZombie);
game.addChild(fastZombie);
lastFastZombieSpawn = LK.ticks;
}
}
// Check win condition (Level 2 lasts longer and requires more survival)
if (LK.ticks > 5400 && zombies.length === 0) {
// After 1.5 minutes with no zombies
unlockNextLevel(); // Unlock next level when current level is completed
// Calculate stars based on house health: 3 stars = 80+%, 2 stars = 50+%, 1 star = any health remaining
var stars = 1;
if (houseHealth >= 80) {
stars = 3;
} else if (houseHealth >= 50) {
stars = 2;
}
setLevelStars(2, stars);
LK.showYouWin();
}
};
}
// Initialize Dynamic Level for levels 3 and beyond
function initializeDynamicLevel(levelNumber) {
// Game variables
houseHealth = 100;
sun = 150 + (levelNumber - 1) * 50; // Progressive starting money: level 1=150, level 2=200, level 3=250, etc.
plants = []; // Make plants global
zombies = []; // Make zombies global
bullets = []; // Make bullets global
// Dynamic theme based on level
var themeBackgrounds = ['desertBackground', 'iceBackground', 'jungleBackground'];
var themeIndex = (levelNumber - 3) % themeBackgrounds.length;
var themeBg = LK.getAsset(themeBackgrounds[themeIndex], {
anchorX: 0,
anchorY: 0
});
game.addChild(themeBg);
var sunflowers = [];
var lastSunGeneration = 0;
var lastZombieSpawn = 0;
var lastFastZombieSpawn = 0;
// Dynamic difficulty scaling based on level
var difficultyMultiplier = levelNumber - 2; // Starts at 1 for level 3
var zombieSpawnRate = Math.max(120, 180 - difficultyMultiplier * 20); // Faster spawning each level
var fastZombieSpawnRate = Math.max(240, 480 - difficultyMultiplier * 30); // More fast zombies
var levelDuration = 3600 + difficultyMultiplier * 600; // Longer levels each time
var finalWaveStart = levelDuration - 1800; // Final wave starts 30 seconds before end
// Grid setup
var gridRows = 10;
var gridCols = 12;
var gridCellWidth = 150;
var gridCellHeight = 100;
var gridStartX = 200;
var gridStartY = 500;
// UI elements
sunText = new Text2('Sun: ' + sun, {
size: 40,
fill: 0x000000
});
sunText.x = 1500;
sunText.y = 50;
game.addChild(sunText);
healthText = new Text2('House Health: ' + houseHealth, {
size: 40,
fill: 0xFF0000
});
healthText.x = 1500;
healthText.y = 100;
game.addChild(healthText);
// Level indicator
var levelText = new Text2('Level ' + levelNumber, {
size: 50,
fill: 0xFFFFFF
});
levelText.x = 1500;
levelText.y = 150;
game.addChild(levelText);
// Create grid
for (var row = 0; row < gridRows; row++) {
for (var col = 0; col < gridCols; col++) {
var gridCell = LK.getAsset('gridTile', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.75,
scaleY: 0.75
});
gridCell.x = gridStartX + col * gridCellWidth;
gridCell.y = gridStartY + row * gridCellHeight;
gridCell.alpha = 0.9;
// Apply theme-specific tinting with unique grid appearances
if (themeBackgrounds[themeIndex] === 'desertBackground') {
gridCell.tint = 0xD2691E; // Sandy orange for desert
} else if (themeBackgrounds[themeIndex] === 'iceBackground') {
gridCell.tint = 0x87CEEB; // Light blue for ice
} else if (themeBackgrounds[themeIndex] === 'jungleBackground') {
gridCell.tint = 0x228B22; // Forest green for jungle
}
game.addChild(gridCell);
}
}
// Plant selection buttons with proper spacing
var peashooterButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
peashooterButton.x = 300;
peashooterButton.y = 1617;
peashooterButton.tint = 0x006400;
game.addChild(peashooterButton);
// Add peashooter image to button
var peashooterImage = LK.getAsset('peashooterBody', {
anchorX: 0.5,
anchorY: 0.5
});
peashooterImage.x = 300;
peashooterImage.y = 1597;
peashooterImage.scaleX = 1.0;
peashooterImage.scaleY = 1.0;
game.addChild(peashooterImage);
var peashooterText = new Text2('50 Sun', {
size: 32,
fill: 0xFFFFFF
});
peashooterText.anchor.set(0.5, 0.5);
peashooterText.x = 300;
peashooterText.y = 1647;
game.addChild(peashooterText);
var sunflowerButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
sunflowerButton.x = 700;
sunflowerButton.y = 1617;
sunflowerButton.tint = 0xB8860B;
game.addChild(sunflowerButton);
// Add sunflower image to button
var sunflowerImage = LK.getAsset('sunflowerCenter', {
anchorX: 0.5,
anchorY: 0.5
});
sunflowerImage.x = 700;
sunflowerImage.y = 1597;
sunflowerImage.scaleX = 0.75;
sunflowerImage.scaleY = 0.75;
game.addChild(sunflowerImage);
var sunflowerText = new Text2('50 Sun', {
size: 32,
fill: 0xFFFFFF
});
sunflowerText.anchor.set(0.5, 0.5);
sunflowerText.x = 700;
sunflowerText.y = 1647;
game.addChild(sunflowerText);
var selectedPlant = null;
// Add more plant options based on level
if (levelNumber >= 3) {
// Add DoublePeashooter button
var doublePeaButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
doublePeaButton.x = 1100;
doublePeaButton.y = 1617;
doublePeaButton.tint = 0x00AA00;
game.addChild(doublePeaButton);
var doublePeaImage = LK.getAsset('placedDoublePeaBody', {
anchorX: 0.5,
anchorY: 0.5
});
doublePeaImage.x = 1100;
doublePeaImage.y = 1597;
game.addChild(doublePeaImage);
var doublePeaText = new Text2('125 Sun', {
size: 28,
fill: 0xFFFFFF
});
doublePeaText.anchor.set(0.5, 0.5);
doublePeaText.x = 1100;
doublePeaText.y = 1647;
game.addChild(doublePeaText);
doublePeaButton.down = function () {
if (sun >= 125) {
selectedPlant = 'doublepea';
}
};
}
if (levelNumber >= 4) {
// Add CherryBomb button
var cherrybombButton = LK.getAsset('selectionBox', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2,
scaleY: 1.2
});
cherrybombButton.x = 1500;
cherrybombButton.y = 1617;
cherrybombButton.tint = 0xFF0000;
game.addChild(cherrybombButton);
var cherrybombImage = LK.getAsset('cherryImage', {
anchorX: 0.5,
anchorY: 0.5
});
cherrybombImage.x = 1500;
cherrybombImage.y = 1597;
cherrybombImage.scaleX = 0.6;
cherrybombImage.scaleY = 0.6;
game.addChild(cherrybombImage);
var cherrybombText = new Text2('100 Sun', {
size: 28,
fill: 0xFFFFFF
});
cherrybombText.anchor.set(0.5, 0.5);
cherrybombText.x = 1500;
cherrybombText.y = 1647;
game.addChild(cherrybombText);
cherrybombButton.down = function () {
if (sun >= 100) {
selectedPlant = 'cherrybomb';
}
};
}
// Event handlers
peashooterButton.down = function () {
if (sun >= 50) {
selectedPlant = 'peashooter';
}
};
sunflowerButton.down = function () {
if (sun >= 50) {
selectedPlant = 'sunflower';
}
};
game.down = function (x, y, obj) {
if (selectedPlant && x >= gridStartX && x <= gridStartX + gridCols * gridCellWidth && y >= gridStartY && y <= gridStartY + gridRows * gridCellHeight) {
var gridCol = Math.floor((x - gridStartX) / gridCellWidth);
var gridRow = Math.floor((y - gridStartY) / gridCellHeight);
// Check if position is valid and empty
var positionEmpty = true;
for (var i = 0; i < plants.length; i++) {
if (plants[i].gridRow === gridRow && plants[i].gridCol === gridCol) {
positionEmpty = false;
break;
}
}
if (positionEmpty && gridCol >= 0 && gridCol < gridCols && gridRow >= 0 && gridRow < gridRows) {
var plant = null;
if (selectedPlant === 'peashooter' && sun >= 50) {
plant = new Peashooter();
sun -= 50;
} else if (selectedPlant === 'sunflower' && sun >= 50) {
plant = new Sunflower();
sun -= 50;
} else if (selectedPlant === 'doublepea' && sun >= 125) {
plant = new DoublePeashooter();
sun -= 125;
} else if (selectedPlant === 'cherrybomb' && sun >= 100) {
plant = new CherryBomb();
sun -= 100;
}
if (plant) {
plant.x = gridStartX + gridCol * gridCellWidth;
plant.y = gridStartY + gridRow * gridCellHeight;
plant.gridRow = gridRow;
plant.gridCol = gridCol;
plants.push(plant);
game.addChild(plant);
sunText.setText('Sun: ' + sun);
selectedPlant = null;
}
}
}
};
var zombiesStarted = false; // Track if zombies have started spawning
// Game update function for Dynamic Levels
game.update = function () {
// Start zombie spawning only after first plant is placed
if (!zombiesStarted && plants.length > 0) {
zombiesStarted = true;
lastZombieSpawn = LK.ticks; // Initialize spawn timer
lastFastZombieSpawn = LK.ticks; // Initialize fast zombie spawn timer
}
// Spawn regular zombies with scaled difficulty - only after zombies started
if (zombiesStarted && LK.ticks - lastZombieSpawn > zombieSpawnRate) {
var zombie = new Zombie();
var row = Math.floor(Math.random() * gridRows);
zombie.x = 1900;
zombie.y = gridStartY + row * gridCellHeight;
zombie.gridRow = row;
zombies.push(zombie);
game.addChild(zombie);
lastZombieSpawn = LK.ticks;
}
// Spawn fast zombies with scaled difficulty - only after zombies started
if (zombiesStarted && LK.ticks - lastFastZombieSpawn > fastZombieSpawnRate) {
var fastZombie = new FastZombie();
var row = Math.floor(Math.random() * gridRows);
fastZombie.x = 1900;
fastZombie.y = gridStartY + row * gridCellHeight;
fastZombie.gridRow = row;
zombies.push(fastZombie);
game.addChild(fastZombie);
lastFastZombieSpawn = LK.ticks;
}
// Spawn tank zombies on higher levels - only after zombies started
if (zombiesStarted && levelNumber >= 4 && LK.ticks % (1200 - difficultyMultiplier * 100) === 0) {
var tankZombie = new TankZombie();
var row = Math.floor(Math.random() * gridRows);
tankZombie.x = 1900;
tankZombie.y = gridStartY + row * gridCellHeight;
tankZombie.gridRow = row;
zombies.push(tankZombie);
game.addChild(tankZombie);
}
// Spawn jumper zombies on even higher levels - only after zombies started
if (zombiesStarted && levelNumber >= 6 && LK.ticks % (900 - difficultyMultiplier * 50) === 0) {
var jumperZombie = new JumperZombie();
var row = Math.floor(Math.random() * gridRows);
jumperZombie.x = 1900;
jumperZombie.y = gridStartY + row * gridCellHeight;
jumperZombie.gridRow = row;
zombies.push(jumperZombie);
game.addChild(jumperZombie);
}
// Update all game objects
for (var i = plants.length - 1; i >= 0; i--) {
plants[i].update();
}
for (var j = zombies.length - 1; j >= 0; j--) {
zombies[j].update();
}
for (var k = bullets.length - 1; k >= 0; k--) {
bullets[k].update();
}
// Generate sun from sunflowers - only if sunflowers exist
var sunflowerCount = 0;
for (var s = 0; s < plants.length; s++) {
if (plants[s] instanceof Sunflower) {
sunflowerCount++;
}
}
if (sunflowerCount > 0 && LK.ticks - lastSunGeneration > 600) {
// Every 10 seconds, only if sunflowers exist
sun += 5;
sunText.setText('Sun: ' + sun);
lastSunGeneration = LK.ticks;
}
// Final wave - spawn many more zombies rapidly in the final period
if (LK.ticks > finalWaveStart && LK.ticks <= levelDuration) {
var finalWaveSpawnRate = Math.max(60, zombieSpawnRate / 2); // Even faster in final wave
if (LK.ticks - lastZombieSpawn > finalWaveSpawnRate) {
var zombie = new Zombie();
var row = Math.floor(Math.random() * gridRows);
zombie.x = 1900;
zombie.y = gridStartY + row * gridCellHeight;
zombie.gridRow = row;
zombies.push(zombie);
game.addChild(zombie);
lastZombieSpawn = LK.ticks;
}
// Also spawn fast zombies more frequently during final wave
var finalFastZombieRate = Math.max(180, fastZombieSpawnRate / 2);
if (LK.ticks - lastFastZombieSpawn > finalFastZombieRate) {
var fastZombie = new FastZombie();
var row = Math.floor(Math.random() * gridRows);
fastZombie.x = 1900;
fastZombie.y = gridStartY + row * gridCellHeight;
fastZombie.gridRow = row;
zombies.push(fastZombie);
game.addChild(fastZombie);
lastFastZombieSpawn = LK.ticks;
}
}
// Check win condition (level duration increases with each level)
if (LK.ticks > levelDuration && zombies.length === 0) {
unlockNextLevel(); // Unlock next level when current level is completed
// Calculate stars based on house health: 3 stars = 80+%, 2 stars = 50+%, 1 star = any health remaining
var stars = 1;
if (houseHealth >= 80) {
stars = 3;
} else if (houseHealth >= 50) {
stars = 2;
}
setLevelStars(levelNumber, stars);
LK.showYouWin();
}
};
}
// Function to unlock next level
function unlockNextLevel() {
if (unlockedLevels < totalLevels) {
unlockedLevels++;
storage.unlockedLevels = unlockedLevels; // Save to storage
if (levelButtons[unlockedLevels - 1]) {
levelButtons[unlockedLevels - 1].setUnlocked(true);
}
}
}
// Function to set stars for a level
function setLevelStars(levelNumber, stars) {
levelProgress[levelNumber] = stars;
storage.levelProgress = levelProgress; // Save to storage
if (levelButtons[levelNumber - 1]) {
levelButtons[levelNumber - 1].setStars(stars);
}
}
// Pause menu variables
var isPaused = false;
var pauseMenu = null;
var currentLevel = 0;
// Function to show pause menu
function showPauseMenu() {
if (pauseMenu) return; // Already showing
// Create pause menu background
pauseMenu = new Container();
var pauseBg = LK.getAsset('paperBackground', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.6,
scaleY: 0.4
});
pauseBg.x = 0;
pauseBg.y = 0;
pauseBg.tint = 0x000000;
pauseBg.alpha = 0.8;
pauseMenu.addChild(pauseBg);
// Pause title
var pauseTitle = new Text2('PAUSED', {
size: 60,
fill: 0xFFFFFF
});
pauseTitle.anchor.set(0.5, 0.5);
pauseTitle.x = 0;
pauseTitle.y = -100;
pauseMenu.addChild(pauseTitle);
// Restart button
var restartButton = LK.getAsset('levelButton', {
anchorX: 0.5,
anchorY: 0.5
});
restartButton.x = 0;
restartButton.y = 0;
restartButton.tint = 0x4CAF50;
pauseMenu.addChild(restartButton);
var restartText = new Text2('RESTART', {
size: 40,
fill: 0x000000
});
restartText.anchor.set(0.5, 0.5);
restartText.x = 0;
restartText.y = 0;
pauseMenu.addChild(restartText);
// Resume button
var resumeButton = LK.getAsset('levelButton', {
anchorX: 0.5,
anchorY: 0.5
});
resumeButton.x = 0;
resumeButton.y = 120;
resumeButton.tint = 0x2196F3;
pauseMenu.addChild(resumeButton);
var resumeText = new Text2('RESUME', {
size: 40,
fill: 0x000000
});
resumeText.anchor.set(0.5, 0.5);
resumeText.x = 0;
resumeText.y = 120;
pauseMenu.addChild(resumeText);
// Position pause menu at center of screen
pauseMenu.x = 1024;
pauseMenu.y = 1366;
// Add event handlers
restartButton.down = function () {
LK.getSound('buttonClick').play();
hidePauseMenu();
// Restart current level
if (currentLevel === 1) {
game.removeChildren();
initializeLevel1();
} else if (currentLevel === 2) {
game.removeChildren();
initializeLevel2();
} else if (currentLevel >= 3 && currentLevel <= 10) {
game.removeChildren();
initializeDynamicLevel(currentLevel);
}
};
resumeButton.down = function () {
LK.getSound('buttonClick').play();
hidePauseMenu();
};
game.addChild(pauseMenu);
}
// Function to hide pause menu
function hidePauseMenu() {
if (pauseMenu) {
game.removeChild(pauseMenu);
pauseMenu = null;
}
isPaused = false;
}
// Override game pause handler
LK.on('pause', function () {
if (currentLevel > 0) {
// Only show pause menu during gameplay
isPaused = true;
showPauseMenu();
}
});
// Simple game update for level selection screen
game.update = function () {
// No specific updates needed for level selection
// All interactions are handled by level button click events
};
Kahverengi sayfa yaprağı. In-Game asset. 2d. High contrast. No shadows
Kağıdın içinde plants vs zombies yazsin
Plant vs zombies ay çiçeği. In-Game asset. 2d. High contrast. No shadows
boş buton. In-Game asset. 2d. High contrast. No shadows
plans vs zombies double shoters. In-Game asset. 2d. High contrast. No shadows
çöl. In-Game asset. 2d. High contrast. No shadows
buz arkaplan. In-Game asset. 2d. High contrast. No shadows
gece gökyüzü. In-Game asset. 2d. High contrast. No shadows
orman. In-Game asset. 2d. High contrast. No shadows
toprak arka plan. In-Game asset. 2d. High contrast. No shadows
plants vs zombies ceviz. In-Game asset. 2d. High contrast. No shadows
ıcepeasshooter plants vs zombies. In-Game asset. 2d. High contrast. No shadows
kankanlı zombi plants vs zombies. In-Game asset. 2d. High contrast. No shadows
plants vs zombies olü ceviz. In-Game asset. 2d. High contrast. No shadows
bombacı zombi plants vs zombies. In-Game asset. 2d. High contrast. No shadows