/**** * Classes ****/ // Boss var Boss = Container.expand(function () { var self = Container.call(this); var bossGraphics = self.attachAsset('boss', { anchorX: 0.5, anchorY: 0.5 }); self.hitPoints = 10; // Boss takes 10 hits to be destroyed self.update = function () { if (LK.ticks % 60 == 0) { self.y += 40; // Boss moves slower than regular targets } }; }); // Assets will be automatically created and loaded by the LK engine based on usage in the game code. // Player's Bullet var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y -= self.speed; if (self.y < 0) { self.destroy(); } }; }); // Double Bullet var DoubleBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics1 = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics2 = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); bulletGraphics2.x = 50; // Offset the second bullet self.speed = 10; self.update = function () { self.y -= self.speed; if (self.y < 0) { self.destroy(); } }; }); // Target var Target = Container.expand(function () { var self = Container.call(this); var targetGraphics = self.attachAsset('target', { anchorX: 0.5, anchorY: 0.5 }); self.hitPoints = 2; // Add hitPoints property self.update = function () { if (LK.ticks % 60 == 0) { self.y += 80; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Initialize game with a black background }); /**** * Game Code ****/ var bullets = []; var targets = []; var score = 0; // Create a target at a random position function spawnTarget() { var target = new Target(); target.x = Math.random() * 2048; target.y = 0; game.addChild(target); targets.push(target); } // Check for bullet collisions with targets function checkCollisions() { bullets.forEach(function (bullet, bulletIndex) { targets.forEach(function (target, targetIndex) { if (bullet.children[0].intersects(target) || bullet.children[1].intersects(target)) { bullet.destroy(); bullets.splice(bulletIndex, 1); target.hitPoints -= 1; // Decrease target's hitPoints if (target.hitPoints <= 0) { target.destroy(); targets.splice(targetIndex, 1); score += 1; if (target instanceof Boss) { score += 49; // Destroying a boss counts as 50 targets } } LK.effects.flashObject(target, 0xff0000, 500); // Flash target red on hit } }); }); } // Shoot a bullet from the bottom center of the screen function shoot(x, y) { var bullet = new DoubleBullet(); bullet.x = x; bullet.y = 2732; // Start from the bottom of the screen game.addChild(bullet); bullets.push(bullet); } // Game update function game.update = function () { if (LK.ticks % 30 == 0) { // Spawn a target every second spawnTarget(); // Spawn a boss every 50 targets destroyed if (score % 50 == 0 && score > 0) { var boss = new Boss(); boss.x = Math.random() * 2048; boss.y = 0; game.addChild(boss); targets.push(boss); } } checkCollisions(); targets.forEach(function (target, targetIndex) { if (target.y >= 2732) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }); }; // Touch event to shoot game.down = function (x, y, obj) { shoot(x, y); }; // Initialize a few targets for (var i = 10; i < 10; i++) { spawnTarget(); }
/****
* Classes
****/
// Boss
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5
});
self.hitPoints = 10; // Boss takes 10 hits to be destroyed
self.update = function () {
if (LK.ticks % 60 == 0) {
self.y += 40; // Boss moves slower than regular targets
}
};
});
// Assets will be automatically created and loaded by the LK engine based on usage in the game code.
// Player's Bullet
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y -= self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Double Bullet
var DoubleBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics1 = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
var bulletGraphics2 = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
bulletGraphics2.x = 50; // Offset the second bullet
self.speed = 10;
self.update = function () {
self.y -= self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Target
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.hitPoints = 2; // Add hitPoints property
self.update = function () {
if (LK.ticks % 60 == 0) {
self.y += 80;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Initialize game with a black background
});
/****
* Game Code
****/
var bullets = [];
var targets = [];
var score = 0;
// Create a target at a random position
function spawnTarget() {
var target = new Target();
target.x = Math.random() * 2048;
target.y = 0;
game.addChild(target);
targets.push(target);
}
// Check for bullet collisions with targets
function checkCollisions() {
bullets.forEach(function (bullet, bulletIndex) {
targets.forEach(function (target, targetIndex) {
if (bullet.children[0].intersects(target) || bullet.children[1].intersects(target)) {
bullet.destroy();
bullets.splice(bulletIndex, 1);
target.hitPoints -= 1; // Decrease target's hitPoints
if (target.hitPoints <= 0) {
target.destroy();
targets.splice(targetIndex, 1);
score += 1;
if (target instanceof Boss) {
score += 49; // Destroying a boss counts as 50 targets
}
}
LK.effects.flashObject(target, 0xff0000, 500); // Flash target red on hit
}
});
});
}
// Shoot a bullet from the bottom center of the screen
function shoot(x, y) {
var bullet = new DoubleBullet();
bullet.x = x;
bullet.y = 2732; // Start from the bottom of the screen
game.addChild(bullet);
bullets.push(bullet);
}
// Game update function
game.update = function () {
if (LK.ticks % 30 == 0) {
// Spawn a target every second
spawnTarget();
// Spawn a boss every 50 targets destroyed
if (score % 50 == 0 && score > 0) {
var boss = new Boss();
boss.x = Math.random() * 2048;
boss.y = 0;
game.addChild(boss);
targets.push(boss);
}
}
checkCollisions();
targets.forEach(function (target, targetIndex) {
if (target.y >= 2732) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
};
// Touch event to shoot
game.down = function (x, y, obj) {
shoot(x, y);
};
// Initialize a few targets
for (var i = 10; i < 10; i++) {
spawnTarget();
}