/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
self.directionX = 0;
self.directionY = -1; // Default direction is up
self.setDirection = function (dirX, dirY) {
var length = Math.sqrt(dirX * dirX + dirY * dirY);
if (length > 0) {
self.directionX = dirX / length;
self.directionY = dirY / length;
}
};
self.update = function () {
self.x += self.directionX * self.speed;
self.y += self.directionY * self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.speed = 8;
self.fireRate = 15; // Frames between shots
self.fireCounter = 0;
self.powerUpDuration = 300; // 5 seconds at 60fps
self.speedBoostTimer = 0;
self.fireRateBoostTimer = 0;
self.takeDamage = function (amount) {
self.health -= amount;
if (self.health <= 0) {
self.health = 0;
return true; // Player died
}
// Flash player red when taking damage
LK.effects.flashObject(self, 0xff0000, 300);
LK.getSound('playerHit').play();
return false; // Player still alive
};
self.applySpeedBoost = function () {
self.speed = 16;
self.speedBoostTimer = self.powerUpDuration;
// Tint player to indicate boost
tween(playerGraphics, {
tint: 0x00ffff
}, {
duration: 300
});
};
self.applyFireRateBoost = function () {
self.fireRate = 5;
self.fireRateBoostTimer = self.powerUpDuration;
// Tint player to indicate boost
tween(playerGraphics, {
tint: 0xff9900
}, {
duration: 300
});
};
self.update = function () {
// Handle power-up timers
if (self.speedBoostTimer > 0) {
self.speedBoostTimer--;
if (self.speedBoostTimer === 0) {
self.speed = 8;
tween(playerGraphics, {
tint: 0xffffff
}, {
duration: 300
});
}
}
if (self.fireRateBoostTimer > 0) {
self.fireRateBoostTimer--;
if (self.fireRateBoostTimer === 0) {
self.fireRate = 15;
tween(playerGraphics, {
tint: 0xffffff
}, {
duration: 300
});
}
}
// Keep player in bounds
if (self.x < 50) {
self.x = 50;
}
if (self.x > 2048 - 50) {
self.x = 2048 - 50;
}
if (self.y < 50) {
self.y = 50;
}
if (self.y > 2732 - 50) {
self.y = 2732 - 50;
}
// Update fire counter
if (self.fireCounter > 0) {
self.fireCounter--;
}
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
self.type = "health"; // Default type
self.setup = function (type) {
self.type = type;
// Remove any existing children
while (self.children.length > 0) {
self.removeChild(self.children[0]);
}
// Attach appropriate asset based on type
if (type === "health") {
self.attachAsset('healthPack', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (type === "speed") {
self.attachAsset('speedBoost', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (type === "fireRate") {
self.attachAsset('fireRateBoost', {
anchorX: 0.5,
anchorY: 0.5
});
}
// Make power-ups pulsate
self.pulsateDirection = 1;
return self;
};
self.update = function () {
// Pulsate effect
if (self.scale.x >= 1.2) {
self.pulsateDirection = -0.01;
} else if (self.scale.x <= 0.8) {
self.pulsateDirection = 0.01;
}
self.scale.x += self.pulsateDirection;
self.scale.y += self.pulsateDirection;
};
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.takeDamage = function (amount) {
self.health -= amount;
// Flash zombie when hit
LK.effects.flashObject(self, 0xff0000, 150);
LK.getSound('zombieHit').play();
return self.health <= 0;
};
self.setTarget = function (target) {
self.target = target;
};
self.update = function () {
if (!self.target) {
return;
}
// Calculate direction to player
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) {
// Normalize and apply speed
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundImage: 'hh'
});
/****
* Game Code
****/
// Game state variables
var player;
var zombies = [];
var bullets = [];
var powerUps = [];
var targetPosition = {
x: 0,
y: 0
};
var isShooting = false;
var gameActive = true;
var waveNumber = 1;
var zombiesKilled = 0;
var score = 0;
var nextWaveTimer = 0;
var powerUpSpawnTimer = 0;
// UI Elements
var scoreTxt;
var waveTxt;
var healthTxt;
// Initialize game
function initGame() {
// Create player
player = new Player();
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
// Set initial target position to player position
targetPosition.x = player.x;
targetPosition.y = player.y;
// Initialize UI
setupUI();
// Set score
LK.setScore(0);
// Start first wave
spawnWave();
// Start background music
LK.playMusic('bgMusic', {
loop: true,
fade: {
start: 0,
end: 1,
duration: 2000
}
});
}
function setupUI() {
// Score text
scoreTxt = new Text2('Score: 0', {
size: 70,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
// Wave text
waveTxt = new Text2('Wave: 1', {
size: 70,
fill: 0xFFFFFF
});
waveTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(waveTxt);
// Health text
healthTxt = new Text2('Health: 100', {
size: 70,
fill: 0xFFFFFF
});
healthTxt.anchor.set(1, 0);
LK.gui.topLeft.addChild(healthTxt);
// Position texts with some padding
scoreTxt.x = -20; // Right align with padding
scoreTxt.y = 20;
waveTxt.y = 20;
healthTxt.x = 120; // Left align with padding to avoid menu icon
healthTxt.y = 20;
}
function spawnWave() {
var zombiesToSpawn = 5 + waveNumber * 2;
for (var i = 0; i < zombiesToSpawn; i++) {
spawnZombie();
}
// Update wave text
waveTxt.setText('Wave: ' + waveNumber);
// Increase wave number for next wave
waveNumber++;
}
function spawnZombie() {
var zombie = new Zombie();
// Determine spawn position (outside the visible area)
var side = Math.floor(Math.random() * 4); // 0: top, 1: right, 2: bottom, 3: left
switch (side) {
case 0:
// top
zombie.x = Math.random() * 2048;
zombie.y = -100;
break;
case 1:
// right
zombie.x = 2048 + 100;
zombie.y = Math.random() * 2732;
break;
case 2:
// bottom
zombie.x = Math.random() * 2048;
zombie.y = 2732 + 100;
break;
case 3:
// left
zombie.x = -100;
zombie.y = Math.random() * 2732;
break;
}
// Set target to player
zombie.setTarget(player);
// Slightly randomize speed for variety
zombie.speed = 2 + Math.random() * 0.5 * (waveNumber * 0.2);
// Add to game and array
game.addChild(zombie);
zombies.push(zombie);
}
function spawnBullet() {
if (player.fireCounter > 0) {
return;
}
// Reset fire counter
player.fireCounter = player.fireRate;
// Create new bullet
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
// Set direction towards target
var dx = targetPosition.x - player.x;
var dy = targetPosition.y - player.y;
bullet.setDirection(dx, dy);
// Add to game and array
game.addChild(bullet);
bullets.push(bullet);
// Play sound
LK.getSound('shoot').play();
}
function spawnPowerUp() {
var powerUp = new PowerUp();
// Randomly determine type
var randomType = Math.floor(Math.random() * 3);
var type;
switch (randomType) {
case 0:
type = "health";
break;
case 1:
type = "speed";
break;
case 2:
type = "fireRate";
break;
}
powerUp.setup(type);
// Set random position (away from edges)
powerUp.x = 100 + Math.random() * (2048 - 200);
powerUp.y = 100 + Math.random() * (2732 - 200);
// Add to game and array
game.addChild(powerUp);
powerUps.push(powerUp);
// Reset spawn timer (10-15 seconds)
powerUpSpawnTimer = 600 + Math.floor(Math.random() * 300);
}
function updateBullets() {
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Update bullet position
bullet.update();
// Check if bullet is off-screen
if (bullet.x < -50 || bullet.x > 2048 + 50 || bullet.y < -50 || bullet.y > 2732 + 50) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
// Check for collisions with zombies
for (var j = zombies.length - 1; j >= 0; j--) {
var zombie = zombies[j];
if (bullet.intersects(zombie)) {
// Damage zombie
if (zombie.takeDamage(1)) {
// Zombie died
zombie.destroy();
zombies.splice(j, 1);
// Increase score
zombiesKilled++;
score += 10;
LK.setScore(score);
scoreTxt.setText('Score: ' + score);
}
// Remove bullet
bullet.destroy();
bullets.splice(i, 1);
break;
}
}
}
}
function updateZombies() {
for (var i = zombies.length - 1; i >= 0; i--) {
var zombie = zombies[i];
// Update zombie movement
zombie.update();
// Check for collision with player
if (zombie.intersects(player)) {
if (player.takeDamage(10)) {
// Player died
gameOver();
return;
}
// Update health text
healthTxt.setText('Health: ' + player.health);
// Remove zombie
zombie.destroy();
zombies.splice(i, 1);
}
}
// Check if all zombies are cleared
if (zombies.length === 0 && gameActive) {
if (nextWaveTimer === 0) {
nextWaveTimer = 180; // 3 seconds at 60fps
}
}
}
function updatePowerUps() {
for (var i = powerUps.length - 1; i >= 0; i--) {
var powerUp = powerUps[i];
// Update power-up animation
powerUp.update();
// Check for collision with player
if (powerUp.intersects(player)) {
// Apply power-up effect
if (powerUp.type === "health") {
player.health = Math.min(100, player.health + 25);
healthTxt.setText('Health: ' + player.health);
} else if (powerUp.type === "speed") {
player.applySpeedBoost();
} else if (powerUp.type === "fireRate") {
player.applyFireRateBoost();
}
// Play sound
LK.getSound('powerUp').play();
// Remove power-up
powerUp.destroy();
powerUps.splice(i, 1);
}
}
}
function movePlayer() {
if (!gameActive) {
return;
}
var dx = targetPosition.x - player.x;
var dy = targetPosition.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
player.x += dx / distance * player.speed;
player.y += dy / distance * player.speed;
}
}
function gameOver() {
gameActive = false;
LK.showGameOver();
}
// Event handlers
game.down = function (x, y, obj) {
if (!gameActive) {
return;
}
targetPosition.x = x;
targetPosition.y = y;
isShooting = true;
};
game.move = function (x, y, obj) {
if (!gameActive) {
return;
}
targetPosition.x = x;
targetPosition.y = y;
};
game.up = function (x, y, obj) {
isShooting = false;
};
// Game update loop
game.update = function () {
// Only update if game is active
if (gameActive) {
// Update player
player.update();
// Move player towards target
movePlayer();
// Handle shooting
if (isShooting) {
spawnBullet();
}
// Update game elements
updateBullets();
updateZombies();
updatePowerUps();
// Handle wave timer
if (nextWaveTimer > 0) {
nextWaveTimer--;
if (nextWaveTimer === 0) {
spawnWave();
}
}
// Handle power-up spawn timer
if (powerUpSpawnTimer > 0) {
powerUpSpawnTimer--;
} else {
spawnPowerUp();
}
}
};
// Initialize the game
initGame(); ===================================================================
--- original.js
+++ change.js
@@ -197,9 +197,9 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x333333
+ backgroundImage: 'hh'
});
/****
* Game Code
a two d zombies \. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
speed boast. Single Game Texture. In-Game asset. Blank background. High contrast. No shadows
first aid. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
fire boaster. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
a two way road with houses upper view two d. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows