/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Enemy = Container.expand(function (enemyType) {
var self = Container.call(this);
self.enemyType = enemyType;
self.lane = 0;
self.speed = 1;
self.maxHealth = 50;
self.health = 50;
if (enemyType === 'ant') {
self.graphics = self.attachAsset('ant', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1.5;
self.maxHealth = 30;
self.health = 30;
} else if (enemyType === 'beetle') {
self.graphics = self.attachAsset('beetle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0.8;
self.maxHealth = 80;
self.health = 80;
}
self.update = function () {
self.x -= self.speed;
// Check collision with plants
var plantAtPosition = getPlantAtPosition(self.x, self.y);
if (plantAtPosition && plantAtPosition.plantType === 'wallnut') {
if (plantAtPosition.takeDamage(1)) {
removePlantFromGrid(plantAtPosition);
}
self.speed = 0.2; // Slow down when eating plant
}
};
self.takeDamage = function (damage) {
self.health -= damage;
tween(self.graphics, {
tint: 0xFF0000
}, {
duration: 100
});
tween(self.graphics, {
tint: 0xFFFFFF
}, {
duration: 100
});
if (self.health <= 0) {
sunlight += 10;
updateSunlightDisplay();
LK.getSound('hit').play();
return true;
}
return false;
};
return self;
});
var Pea = Container.expand(function () {
var self = Container.call(this);
self.graphics = self.attachAsset('pea', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.damage = 20;
self.lane = 0;
self.update = function () {
self.x += self.speed;
};
return self;
});
var Plant = Container.expand(function (plantType) {
var self = Container.call(this);
self.plantType = plantType;
self.health = 100;
self.shootCooldown = 0;
self.lane = 0;
if (plantType === 'sunflower') {
self.graphics = self.attachAsset('sunflower', {
anchorX: 0.5,
anchorY: 0.5
});
self.sunTimer = 0;
} else if (plantType === 'peashooter') {
self.graphics = self.attachAsset('peashooter', {
anchorX: 0.5,
anchorY: 0.5
});
self.shootInterval = 120; // 2 seconds at 60fps
} else if (plantType === 'wallnut') {
self.graphics = self.attachAsset('wallnut', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 300;
}
self.update = function () {
if (self.plantType === 'sunflower') {
self.sunTimer++;
if (self.sunTimer >= 300) {
// 5 seconds
sunlight += 25;
updateSunlightDisplay();
self.sunTimer = 0;
tween(self.graphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 200,
easing: tween.easeOut
});
tween(self.graphics, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200,
easing: tween.easeIn
});
}
} else if (self.plantType === 'peashooter') {
self.shootCooldown--;
if (self.shootCooldown <= 0) {
var enemyInLane = getEnemyInLane(self.lane);
if (enemyInLane) {
var pea = new Pea();
pea.x = self.x + 50;
pea.y = self.y;
pea.lane = self.lane;
projectiles.push(pea);
game.addChild(pea);
LK.getSound('shoot').play();
self.shootCooldown = self.shootInterval;
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
tween(self.graphics, {
tint: 0xFF0000
}, {
duration: 100
});
tween(self.graphics, {
tint: 0xFFFFFF
}, {
duration: 100
});
if (self.health <= 0) {
self.destroy();
return true;
}
return false;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var gridWidth = 16;
var gridHeight = 9;
var cellSize = 80;
var gridStartX = 250;
var gridStartY = 300;
var grid = [];
var plants = [];
var enemies = [];
var projectiles = [];
var sunlight = 100;
var selectedPlantType = null;
var plantCosts = {
sunflower: 50,
peashooter: 100,
wallnut: 50
};
var plantCooldowns = {
sunflower: 0,
peashooter: 0,
wallnut: 0
};
var waveTimer = 0;
var enemiesLeft = 5;
var currentWave = 1;
var lives = 5;
// Initialize grid
for (var row = 0; row < gridHeight; row++) {
grid[row] = [];
for (var col = 0; col < gridWidth; col++) {
grid[row][col] = null;
var cell = LK.getAsset('gridCell', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.4,
scaleX: 0.8,
scaleY: 0.8
});
cell.x = gridStartX + col * cellSize;
cell.y = gridStartY + row * cellSize;
game.addChild(cell);
}
}
// Create plant selection panel
var plantSlots = [];
var plantTypes = ['sunflower', 'peashooter', 'wallnut'];
for (var i = 0; i < plantTypes.length; i++) {
var slot = LK.getAsset('plantSlot', {
anchorX: 0.5,
anchorY: 0.5
});
slot.x = 100 + i * 90;
slot.y = 150;
slot.plantType = plantTypes[i];
game.addChild(slot);
plantSlots.push(slot);
var plantPreview = new Plant(plantTypes[i]);
plantPreview.x = slot.x;
plantPreview.y = slot.y;
plantPreview.graphics.scaleX = 0.8;
plantPreview.graphics.scaleY = 0.8;
game.addChild(plantPreview);
}
// Create UI
var sunlightText = new Text2('Sun: ' + sunlight, {
size: 40,
fill: 0xFFD700
});
sunlightText.anchor.set(0, 0);
sunlightText.x = 50;
sunlightText.y = 50;
game.addChild(sunlightText);
var waveText = new Text2('Wave: ' + currentWave, {
size: 40,
fill: 0xFFFFFF
});
waveText.anchor.set(0, 0);
waveText.x = 50;
waveText.y = 100;
game.addChild(waveText);
var livesText = new Text2('Lives: ' + lives, {
size: 40,
fill: 0xFF6347
});
livesText.anchor.set(0, 0);
livesText.x = 50;
livesText.y = 150;
game.addChild(livesText);
function updateSunlightDisplay() {
sunlightText.setText('Sun: ' + sunlight);
}
function updateWaveDisplay() {
waveText.setText('Wave: ' + currentWave);
}
function updateLivesDisplay() {
livesText.setText('Lives: ' + lives);
}
function getGridPosition(x, y) {
var col = Math.floor((x - gridStartX + cellSize / 2) / cellSize);
var row = Math.floor((y - gridStartY + cellSize / 2) / cellSize);
if (col >= 0 && col < gridWidth && row >= 0 && row < gridHeight) {
return {
row: row,
col: col
};
}
return null;
}
function placePlant(plantType, row, col) {
if (grid[row][col] !== null) return false;
if (sunlight < plantCosts[plantType]) return false;
if (plantCooldowns[plantType] > 0) return false;
var plant = new Plant(plantType);
plant.x = gridStartX + col * cellSize;
plant.y = gridStartY + row * cellSize;
plant.lane = row;
grid[row][col] = plant;
plants.push(plant);
game.addChild(plant);
sunlight -= plantCosts[plantType];
plantCooldowns[plantType] = 180; // 3 second cooldown
updateSunlightDisplay();
LK.getSound('plant').play();
return true;
}
function spawnEnemy() {
var enemyType = Math.random() < 0.6 ? 'ant' : 'beetle';
var enemy = new Enemy(enemyType);
enemy.x = 2100;
enemy.lane = Math.floor(Math.random() * gridHeight);
enemy.y = gridStartY + enemy.lane * cellSize;
enemies.push(enemy);
game.addChild(enemy);
}
function getEnemyInLane(lane) {
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].lane === lane && enemies[i].x > gridStartX) {
return enemies[i];
}
}
return null;
}
function getPlantAtPosition(x, y) {
for (var i = 0; i < plants.length; i++) {
var plant = plants[i];
if (Math.abs(plant.x - x) < 50 && Math.abs(plant.y - y) < 50) {
return plant;
}
}
return null;
}
function removePlantFromGrid(plant) {
var gridPos = getGridPosition(plant.x, plant.y);
if (gridPos) {
grid[gridPos.row][gridPos.col] = null;
}
for (var i = plants.length - 1; i >= 0; i--) {
if (plants[i] === plant) {
plants.splice(i, 1);
break;
}
}
}
game.down = function (x, y, obj) {
// Check if clicking on plant selection
for (var i = 0; i < plantSlots.length; i++) {
var slot = plantSlots[i];
if (Math.abs(x - slot.x) < 40 && Math.abs(y - slot.y) < 40) {
if (sunlight >= plantCosts[slot.plantType] && plantCooldowns[slot.plantType] <= 0) {
selectedPlantType = slot.plantType;
tween(slot, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 100
});
tween(slot, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
}
return;
}
}
// Check if placing plant on grid
if (selectedPlantType) {
var gridPos = getGridPosition(x, y);
if (gridPos) {
if (placePlant(selectedPlantType, gridPos.row, gridPos.col)) {
selectedPlantType = null;
}
}
}
};
game.update = function () {
// Update cooldowns
for (var plantType in plantCooldowns) {
if (plantCooldowns[plantType] > 0) {
plantCooldowns[plantType]--;
}
}
// Spawn enemies
waveTimer++;
if (waveTimer >= 180 && enemiesLeft > 0) {
// Every 3 seconds
spawnEnemy();
enemiesLeft--;
waveTimer = 0;
}
// Check if wave is complete
if (enemiesLeft <= 0 && enemies.length === 0) {
currentWave++;
enemiesLeft = 5 + currentWave * 2;
updateWaveDisplay();
// Add bonus sunlight for completing wave
sunlight += 50;
updateSunlightDisplay();
}
// Update projectiles
for (var i = projectiles.length - 1; i >= 0; i--) {
var pea = projectiles[i];
if (pea.x > 2200) {
pea.destroy();
projectiles.splice(i, 1);
continue;
}
// Check collision with enemies
var hit = false;
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (enemy.lane === pea.lane && Math.abs(enemy.x - pea.x) < 30 && Math.abs(enemy.y - pea.y) < 30) {
if (enemy.takeDamage(pea.damage)) {
enemy.destroy();
enemies.splice(j, 1);
}
pea.destroy();
projectiles.splice(i, 1);
hit = true;
break;
}
}
}
// Check enemies reaching end
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
if (enemy.x < 200) {
enemy.destroy();
enemies.splice(i, 1);
lives--;
updateLivesDisplay();
LK.effects.flashScreen(0xFF0000, 500);
if (lives <= 0) {
LK.showGameOver();
return;
}
}
}
// Check win condition (survive 10 waves)
if (currentWave > 10 && enemies.length === 0 && enemiesLeft <= 0) {
LK.showYouWin();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -172,13 +172,13 @@
/****
* Game Code
****/
-var gridWidth = 12;
-var gridHeight = 7;
+var gridWidth = 16;
+var gridHeight = 9;
var cellSize = 80;
-var gridStartX = 300;
-var gridStartY = 400;
+var gridStartX = 250;
+var gridStartY = 300;
var grid = [];
var plants = [];
var enemies = [];
var projectiles = [];
@@ -299,9 +299,9 @@
}
function spawnEnemy() {
var enemyType = Math.random() < 0.6 ? 'ant' : 'beetle';
var enemy = new Enemy(enemyType);
- enemy.x = 2000;
+ enemy.x = 2100;
enemy.lane = Math.floor(Math.random() * gridHeight);
enemy.y = gridStartY + enemy.lane * cellSize;
enemies.push(enemy);
game.addChild(enemy);
@@ -394,9 +394,9 @@
}
// Update projectiles
for (var i = projectiles.length - 1; i >= 0; i--) {
var pea = projectiles[i];
- if (pea.x > 2100) {
+ if (pea.x > 2200) {
pea.destroy();
projectiles.splice(i, 1);
continue;
}
@@ -418,9 +418,9 @@
}
// Check enemies reaching end
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
- if (enemy.x < 300) {
+ if (enemy.x < 200) {
enemy.destroy();
enemies.splice(i, 1);
lives--;
updateLivesDisplay();