/**** * Classes ****/ var Bullet = Container.expand(function (startX, startY) { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.x = startX; self.y = startY; self.speed = -10; self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); } }; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = Math.random() * 3 + 1; // Random speed between 1 and 4 self.direction = Math.random() < 0.5 ? -1 : 1; // Random direction self.health = 3; // Add health property self.update = function () { self.y += self.speed; // Move vertically towards the bottom of the screen self.x += self.direction * self.speed; // Move horizontally if (self.x < 0 || self.x > 2048) { self.direction *= -1; // Change direction if hitting screen edge } if (self.y > 2732) { self.y = 0; self.x = Math.random() * 2048; } }; self.takeDamage = function () { self.health -= 1; if (self.health <= 0) { createExplosion(self.x, self.y); // Add explosion effect self.destroy(); LK.getSound('shoot').play(); // Play sound when enemy is destroyed } }; }); var EnemyBullet = Container.expand(function (startX, startY) { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.x = startX; self.y = startY; self.speed = 10; // Increase bullet speed self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.health = 3; // Add health property self.shoot = function () { var bullet = new Bullet(self.x, self.y - 50); game.addChild(bullet); bullets.push(bullet); LK.getSound('shoot').play(); }; self.update = function () { // Player movement logic if (isTouching) { var deltaX = touchStartX - self.x; self.x += deltaX; touchStartX = self.x; } }; self.takeDamage = function () { if (!self.invincible) { self.health -= 1; updateHealthDisplay(); if (self.health <= 0) { createExplosion(self.x, self.y); // Add explosion effect LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver('Final Score: ' + score); return; // Exit function to prevent further execution } else { self.invincible = true; LK.setTimeout(function () { self.invincible = false; }, 2000); // 2 seconds of invincibility } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Initialize game with black background }); /**** * Game Code ****/ // Play background music LK.playMusic('1battlefieldepic'); function createExplosion(x, y) { var explosion = LK.getAsset('explosion', { anchorX: 0.5, anchorY: 0.5, x: x, y: y }); game.addChild(explosion); LK.effects.flashObject(explosion, 0xffa500, 500); // Flash orange for 500ms LK.setTimeout(function () { explosion.destroy(); }, 500); // Destroy explosion after 500ms } var healthTxt = new Text2('Health: 3', { size: 100, fill: 0xFFFFFF }); healthTxt.anchor.set(1, 0); // Set anchor to the top-right corner LK.gui.topRight.addChild(healthTxt); // Move health display to the top-right corner var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function updateHealthDisplay() { healthTxt.setText('Health: ' + player.health); } function updateScoreDisplay() { scoreTxt.setText('Score: ' + score); } // Add shooting mechanism on touch game.down = function (x, y, obj) { player.shoot(); player.x = x; // Move player to touch position }; game.move = function (x, y, obj) { player.x = x; // Move player to touch position }; var isTouching = false; // Initialize isTouching variable var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1366 }); game.addChild(background); var player = new Player(); game.addChild(player); player.x = 1024; player.y = 2500; updateHealthDisplay(); // Update health display on game start var enemies = []; for (var i = 0; i < 3; i++) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * 1000; game.addChild(enemy); enemies.push(enemy); } var score = 0; var bullets = []; var enemyBullets = []; game.update = function () { player.update(); // Regenerate player health over time if (LK.ticks % 600 === 0 && player.health < 3) { // Every 10 seconds player.health += 1; updateHealthDisplay(); } enemies.forEach(function (enemy) { enemy.update(); if (player.intersects(enemy)) { player.takeDamage(); if (player.health <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Enemy shooting mechanism if (LK.ticks % 180 === 0) { // Shoot every 3 seconds var enemyBullet = new EnemyBullet(enemy.x, enemy.y + 50); game.addChild(enemyBullet); enemyBullets.push(enemyBullet); } }); bullets.forEach(function (bullet, index) { bullet.update(); if (bullet.y < 0) { bullet.destroy(); bullets.splice(index, 1); return; // Exit loop to prevent further execution } else { enemies.forEach(function (enemy, enemyIndex) { if (bullet.intersects(enemy)) { bullet.destroy(); bullets.splice(index, 1); enemy.takeDamage(); // Reduce enemy health if (enemy.health <= 0) { score += 10; // Increase score by 10 updateScoreDisplay(); // Update score display enemies.splice(enemyIndex, 1); if (enemies.length === 0) { LK.showYouWin(); // Show winning screen when all enemies are destroyed } else { var newEnemy = new Enemy(); // Respawn a new enemy newEnemy.x = Math.random() * 2048; newEnemy.y = Math.random() * 1000; game.addChild(newEnemy); enemies.push(newEnemy); } } } }); } }); enemyBullets.forEach(function (bullet, index) { bullet.update(); if (bullet.y > 2732) { bullet.destroy(); enemyBullets.splice(index, 1); return; // Exit loop to prevent further execution } else { if (bullet.intersects(player)) { bullet.destroy(); enemyBullets.splice(index, 1); player.takeDamage(); } } }); };
/****
* Classes
****/
var Bullet = Container.expand(function (startX, startY) {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = startX;
self.y = startY;
self.speed = -10;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 3 + 1; // Random speed between 1 and 4
self.direction = Math.random() < 0.5 ? -1 : 1; // Random direction
self.health = 3; // Add health property
self.update = function () {
self.y += self.speed; // Move vertically towards the bottom of the screen
self.x += self.direction * self.speed; // Move horizontally
if (self.x < 0 || self.x > 2048) {
self.direction *= -1; // Change direction if hitting screen edge
}
if (self.y > 2732) {
self.y = 0;
self.x = Math.random() * 2048;
}
};
self.takeDamage = function () {
self.health -= 1;
if (self.health <= 0) {
createExplosion(self.x, self.y); // Add explosion effect
self.destroy();
LK.getSound('shoot').play(); // Play sound when enemy is destroyed
}
};
});
var EnemyBullet = Container.expand(function (startX, startY) {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = startX;
self.y = startY;
self.speed = 10; // Increase bullet speed
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.health = 3; // Add health property
self.shoot = function () {
var bullet = new Bullet(self.x, self.y - 50);
game.addChild(bullet);
bullets.push(bullet);
LK.getSound('shoot').play();
};
self.update = function () {
// Player movement logic
if (isTouching) {
var deltaX = touchStartX - self.x;
self.x += deltaX;
touchStartX = self.x;
}
};
self.takeDamage = function () {
if (!self.invincible) {
self.health -= 1;
updateHealthDisplay();
if (self.health <= 0) {
createExplosion(self.x, self.y); // Add explosion effect
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver('Final Score: ' + score);
return; // Exit function to prevent further execution
} else {
self.invincible = true;
LK.setTimeout(function () {
self.invincible = false;
}, 2000); // 2 seconds of invincibility
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Initialize game with black background
});
/****
* Game Code
****/
// Play background music
LK.playMusic('1battlefieldepic');
function createExplosion(x, y) {
var explosion = LK.getAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5,
x: x,
y: y
});
game.addChild(explosion);
LK.effects.flashObject(explosion, 0xffa500, 500); // Flash orange for 500ms
LK.setTimeout(function () {
explosion.destroy();
}, 500); // Destroy explosion after 500ms
}
var healthTxt = new Text2('Health: 3', {
size: 100,
fill: 0xFFFFFF
});
healthTxt.anchor.set(1, 0); // Set anchor to the top-right corner
LK.gui.topRight.addChild(healthTxt); // Move health display to the top-right corner
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function updateHealthDisplay() {
healthTxt.setText('Health: ' + player.health);
}
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
}
// Add shooting mechanism on touch
game.down = function (x, y, obj) {
player.shoot();
player.x = x; // Move player to touch position
};
game.move = function (x, y, obj) {
player.x = x; // Move player to touch position
};
var isTouching = false; // Initialize isTouching variable
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
game.addChild(background);
var player = new Player();
game.addChild(player);
player.x = 1024;
player.y = 2500;
updateHealthDisplay(); // Update health display on game start
var enemies = [];
for (var i = 0; i < 3; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * 1000;
game.addChild(enemy);
enemies.push(enemy);
}
var score = 0;
var bullets = [];
var enemyBullets = [];
game.update = function () {
player.update();
// Regenerate player health over time
if (LK.ticks % 600 === 0 && player.health < 3) {
// Every 10 seconds
player.health += 1;
updateHealthDisplay();
}
enemies.forEach(function (enemy) {
enemy.update();
if (player.intersects(enemy)) {
player.takeDamage();
if (player.health <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Enemy shooting mechanism
if (LK.ticks % 180 === 0) {
// Shoot every 3 seconds
var enemyBullet = new EnemyBullet(enemy.x, enemy.y + 50);
game.addChild(enemyBullet);
enemyBullets.push(enemyBullet);
}
});
bullets.forEach(function (bullet, index) {
bullet.update();
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(index, 1);
return; // Exit loop to prevent further execution
} else {
enemies.forEach(function (enemy, enemyIndex) {
if (bullet.intersects(enemy)) {
bullet.destroy();
bullets.splice(index, 1);
enemy.takeDamage(); // Reduce enemy health
if (enemy.health <= 0) {
score += 10; // Increase score by 10
updateScoreDisplay(); // Update score display
enemies.splice(enemyIndex, 1);
if (enemies.length === 0) {
LK.showYouWin(); // Show winning screen when all enemies are destroyed
} else {
var newEnemy = new Enemy(); // Respawn a new enemy
newEnemy.x = Math.random() * 2048;
newEnemy.y = Math.random() * 1000;
game.addChild(newEnemy);
enemies.push(newEnemy);
}
}
}
});
}
});
enemyBullets.forEach(function (bullet, index) {
bullet.update();
if (bullet.y > 2732) {
bullet.destroy();
enemyBullets.splice(index, 1);
return; // Exit loop to prevent further execution
} else {
if (bullet.intersects(player)) {
bullet.destroy();
enemyBullets.splice(index, 1);
player.takeDamage();
}
}
});
};