User prompt
Chickens kill zombies with 1 shot, black chickens fire 3 shots
User prompt
There is a 10% chance of chickens hatching and a 2.5% chance of a black chicken hatching, and this chicken is 4 times stronger than other chickens.
Code edit (1 edits merged)
Please save this source code
User prompt
Egg Defense: Zombie Hatchery
Initial prompt
In this game, there is an egg at the bottom of the screen and when we press this egg, there is a 25% chance that a chicken will come out and this chicken will try to kill the zombies coming from the top of the screen and the zombies' task is to destroy the egg.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var BlackChicken = Container.expand(function () {
var self = Container.call(this);
var chickenGraphics = self.attachAsset('blackChicken', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1;
self.shootTimer = 0;
self.shootCooldown = 60; // 1 second at 60fps
self.target = null;
self.lastTarget = null;
self.damage = 4; // 4x stronger than regular chicken
self.update = function () {
// Find nearest zombie
var nearestZombie = null;
var nearestDistance = Infinity;
for (var i = 0; i < zombies.length; i++) {
var zombie = zombies[i];
var distance = Math.sqrt(Math.pow(zombie.x - self.x, 2) + Math.pow(zombie.y - self.y, 2));
if (distance < nearestDistance && distance < 300) {
nearestDistance = distance;
nearestZombie = zombie;
}
}
self.target = nearestZombie;
// Move towards target
if (self.target) {
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
}
// Shoot at target
self.shootTimer++;
if (self.shootTimer >= self.shootCooldown && self.target) {
self.shootTimer = 0;
self.shoot();
}
};
self.shoot = function () {
if (self.target) {
var newBullet = new Bullet();
newBullet.x = self.x;
newBullet.y = self.y;
newBullet.targetX = self.target.x;
newBullet.targetY = self.target.y;
newBullet.damage = self.damage; // Use black chicken's damage
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('shoot').play();
}
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.targetX = 0;
self.targetY = 0;
self.directionX = 0;
self.directionY = 0;
self.damage = 1;
// Calculate direction on creation
self.calculateDirection = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
self.directionX = dx / distance;
self.directionY = dy / distance;
}
};
self.update = function () {
if (self.directionX === 0 && self.directionY === 0) {
self.calculateDirection();
}
self.x += self.directionX * self.speed;
self.y += self.directionY * self.speed;
};
return self;
});
var Chicken = Container.expand(function () {
var self = Container.call(this);
var chickenGraphics = self.attachAsset('chicken', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1;
self.shootTimer = 0;
self.shootCooldown = 60; // 1 second at 60fps
self.target = null;
self.lastTarget = null;
self.update = function () {
// Find nearest zombie
var nearestZombie = null;
var nearestDistance = Infinity;
for (var i = 0; i < zombies.length; i++) {
var zombie = zombies[i];
var distance = Math.sqrt(Math.pow(zombie.x - self.x, 2) + Math.pow(zombie.y - self.y, 2));
if (distance < nearestDistance && distance < 300) {
nearestDistance = distance;
nearestZombie = zombie;
}
}
self.target = nearestZombie;
// Move towards target
if (self.target) {
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
}
// Shoot at target
self.shootTimer++;
if (self.shootTimer >= self.shootCooldown && self.target) {
self.shootTimer = 0;
self.shoot();
}
};
self.shoot = function () {
if (self.target) {
var newBullet = new Bullet();
newBullet.x = self.x;
newBullet.y = self.y;
newBullet.targetX = self.target.x;
newBullet.targetY = self.target.y;
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('shoot').play();
}
};
return self;
});
var Egg = Container.expand(function () {
var self = Container.call(this);
var eggGraphics = self.attachAsset('egg', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 10;
self.maxHealth = 10;
self.hatchChance = 0.1; // 10% chance
self.blackChickenChance = 0.025; // 2.5% chance
self.lastHealth = self.health;
self.down = function (x, y, obj) {
self.tryHatch();
};
self.tryHatch = function () {
var randomValue = Math.random();
if (randomValue < self.blackChickenChance) {
// Spawn black chicken (2.5% chance)
self.spawnBlackChicken();
LK.getSound('hatch').play();
LK.effects.flashObject(self, 0x000000, 300);
} else if (randomValue < self.hatchChance) {
// Spawn regular chicken (remaining 7.5% to reach 10% total)
self.spawnChicken();
LK.getSound('hatch').play();
LK.effects.flashObject(self, 0xFFD700, 300);
} else {
// Failed hatch visual feedback
tween(self, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 100,
onFinish: function onFinish() {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
}
});
}
};
self.spawnChicken = function () {
var newChicken = new Chicken();
newChicken.x = self.x + (Math.random() - 0.5) * 100;
newChicken.y = self.y - 50;
chickens.push(newChicken);
game.addChild(newChicken);
};
self.spawnBlackChicken = function () {
var newBlackChicken = new BlackChicken();
newBlackChicken.x = self.x + (Math.random() - 0.5) * 100;
newBlackChicken.y = self.y - 50;
chickens.push(newBlackChicken);
game.addChild(newBlackChicken);
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 500);
// Update egg color based on health
var healthRatio = self.health / self.maxHealth;
if (healthRatio > 0.6) {
eggGraphics.tint = 0xFFF8DC; // Beige
} else if (healthRatio > 0.3) {
eggGraphics.tint = 0xFFD700; // Yellow
} else {
eggGraphics.tint = 0xFF4500; // Red
}
if (self.health <= 0) {
return true; // Egg is destroyed
}
return false;
};
return self;
});
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.attachAsset('zombie', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 3;
self.maxHealth = 3;
self.lastY = 0;
self.update = function () {
self.y += self.speed;
// Update health color based on damage
var healthRatio = self.health / self.maxHealth;
if (healthRatio > 0.6) {
zombieGraphics.tint = 0x228B22; // Green
} else if (healthRatio > 0.3) {
zombieGraphics.tint = 0xFFD700; // Yellow
} else {
zombieGraphics.tint = 0xFF4500; // Red
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
return true; // Zombie is dead
}
return false;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var egg = null;
var zombies = [];
var chickens = [];
var bullets = [];
var zombieSpawnTimer = 0;
var zombieSpawnRate = 120; // 2 seconds at 60fps
var waveNumber = 1;
var zombiesKilled = 0;
var gameStartTime = Date.now();
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var healthTxt = new Text2('Egg Health: 10', {
size: 50,
fill: 0xFFFFFF
});
healthTxt.anchor.set(0, 0);
healthTxt.x = 50;
healthTxt.y = 50;
LK.gui.topLeft.addChild(healthTxt);
var waveTxt = new Text2('Wave: 1', {
size: 50,
fill: 0xFFFFFF
});
waveTxt.anchor.set(1, 0);
waveTxt.x = -50;
waveTxt.y = 50;
LK.gui.topRight.addChild(waveTxt);
var instructionTxt = new Text2('Tap the egg to hatch chickens!', {
size: 40,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 1);
instructionTxt.y = -100;
LK.gui.bottom.addChild(instructionTxt);
// Create egg at bottom center
egg = new Egg();
egg.x = 2048 / 2;
egg.y = 2732 - 200;
game.addChild(egg);
function spawnZombie() {
var newZombie = new Zombie();
newZombie.x = Math.random() * (2048 - 160) + 80;
newZombie.y = -50;
newZombie.speed = 1 + Math.random() * 2 + waveNumber * 0.5;
newZombie.lastY = newZombie.y;
zombies.push(newZombie);
game.addChild(newZombie);
}
function updateDifficulty() {
var currentTime = Date.now();
var elapsedTime = (currentTime - gameStartTime) / 1000;
// Increase wave every 30 seconds
var newWave = Math.floor(elapsedTime / 30) + 1;
if (newWave > waveNumber) {
waveNumber = newWave;
waveTxt.setText('Wave: ' + waveNumber);
// Increase spawn rate
zombieSpawnRate = Math.max(30, 120 - waveNumber * 10);
}
}
game.update = function () {
updateDifficulty();
// Spawn zombies
zombieSpawnTimer++;
if (zombieSpawnTimer >= zombieSpawnRate) {
zombieSpawnTimer = 0;
spawnZombie();
}
// Update zombies
for (var i = zombies.length - 1; i >= 0; i--) {
var zombie = zombies[i];
// Check if zombie reached egg
if (zombie.lastY < egg.y && zombie.y >= egg.y && Math.abs(zombie.x - egg.x) < 100) {
// Zombie attacks egg
if (egg.takeDamage(1)) {
// Egg is destroyed - game over
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
return;
}
healthTxt.setText('Egg Health: ' + egg.health);
// Remove zombie after attack
zombie.destroy();
zombies.splice(i, 1);
continue;
}
// Remove zombies that go off screen
if (zombie.y > 2732 + 100) {
zombie.destroy();
zombies.splice(i, 1);
continue;
}
zombie.lastY = zombie.y;
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
var bullet = bullets[j];
// Check bullet-zombie collisions
var hit = false;
for (var k = zombies.length - 1; k >= 0; k--) {
var zombie = zombies[k];
if (bullet.intersects(zombie)) {
// Bullet hits zombie
if (zombie.takeDamage(bullet.damage)) {
// Zombie is killed
LK.getSound('zombieHit').play();
zombiesKilled++;
LK.setScore(zombiesKilled * 10);
scoreTxt.setText('Score: ' + LK.getScore());
zombie.destroy();
zombies.splice(k, 1);
}
bullet.destroy();
bullets.splice(j, 1);
hit = true;
break;
}
}
// Remove bullets that go off screen
if (!hit && (bullet.x < -50 || bullet.x > 2048 + 50 || bullet.y < -50 || bullet.y > 2732 + 50)) {
bullet.destroy();
bullets.splice(j, 1);
}
}
// Update chickens
for (var l = chickens.length - 1; l >= 0; l--) {
var chicken = chickens[l];
// Check if chicken is still alive (no health system for chickens yet)
// Could add zombie attacks on chickens in future versions
}
}; ===================================================================
--- original.js
+++ change.js
@@ -5,8 +5,65 @@
/****
* Classes
****/
+var BlackChicken = Container.expand(function () {
+ var self = Container.call(this);
+ var chickenGraphics = self.attachAsset('blackChicken', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 1;
+ self.shootTimer = 0;
+ self.shootCooldown = 60; // 1 second at 60fps
+ self.target = null;
+ self.lastTarget = null;
+ self.damage = 4; // 4x stronger than regular chicken
+ self.update = function () {
+ // Find nearest zombie
+ var nearestZombie = null;
+ var nearestDistance = Infinity;
+ for (var i = 0; i < zombies.length; i++) {
+ var zombie = zombies[i];
+ var distance = Math.sqrt(Math.pow(zombie.x - self.x, 2) + Math.pow(zombie.y - self.y, 2));
+ if (distance < nearestDistance && distance < 300) {
+ nearestDistance = distance;
+ nearestZombie = zombie;
+ }
+ }
+ self.target = nearestZombie;
+ // Move towards target
+ if (self.target) {
+ var dx = self.target.x - self.x;
+ var dy = self.target.y - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance > 0) {
+ self.x += dx / distance * self.speed;
+ self.y += dy / distance * self.speed;
+ }
+ }
+ // Shoot at target
+ self.shootTimer++;
+ if (self.shootTimer >= self.shootCooldown && self.target) {
+ self.shootTimer = 0;
+ self.shoot();
+ }
+ };
+ self.shoot = function () {
+ if (self.target) {
+ var newBullet = new Bullet();
+ newBullet.x = self.x;
+ newBullet.y = self.y;
+ newBullet.targetX = self.target.x;
+ newBullet.targetY = self.target.y;
+ newBullet.damage = self.damage; // Use black chicken's damage
+ bullets.push(newBullet);
+ game.addChild(newBullet);
+ LK.getSound('shoot').play();
+ }
+ };
+ return self;
+});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
@@ -99,15 +156,23 @@
anchorY: 0.5
});
self.health = 10;
self.maxHealth = 10;
- self.hatchChance = 0.25;
+ self.hatchChance = 0.1; // 10% chance
+ self.blackChickenChance = 0.025; // 2.5% chance
self.lastHealth = self.health;
self.down = function (x, y, obj) {
self.tryHatch();
};
self.tryHatch = function () {
- if (Math.random() < self.hatchChance) {
+ var randomValue = Math.random();
+ if (randomValue < self.blackChickenChance) {
+ // Spawn black chicken (2.5% chance)
+ self.spawnBlackChicken();
+ LK.getSound('hatch').play();
+ LK.effects.flashObject(self, 0x000000, 300);
+ } else if (randomValue < self.hatchChance) {
+ // Spawn regular chicken (remaining 7.5% to reach 10% total)
self.spawnChicken();
LK.getSound('hatch').play();
LK.effects.flashObject(self, 0xFFD700, 300);
} else {
@@ -134,8 +199,15 @@
newChicken.y = self.y - 50;
chickens.push(newChicken);
game.addChild(newChicken);
};
+ self.spawnBlackChicken = function () {
+ var newBlackChicken = new BlackChicken();
+ newBlackChicken.x = self.x + (Math.random() - 0.5) * 100;
+ newBlackChicken.y = self.y - 50;
+ chickens.push(newBlackChicken);
+ game.addChild(newBlackChicken);
+ };
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 500);
// Update egg color based on health