/****
* Classes
****/
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 200;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.destroy();
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for hero
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
var ShootButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('shootButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
hero.shoot();
};
self.containsPoint = function (point) {
var localPoint = self.toLocal(point);
return localPoint.x >= -self.width / 2 && localPoint.x <= self.width / 2 && localPoint.y >= -self.height / 2 && localPoint.y <= self.height / 2;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Update hero bullets
// Initialize arrays and variables
var heroBullets = [];
if (heroBullets && heroBullets.length) {
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (bossSpawned && heroBullets[i].intersects(boss)) {
boss.takeDamage(10); // Reduce boss health by 10
if (boss.health <= 0) {
score += 100;
scoreTxt.setText('Score: ' + score);
bossSpawned = false;
}
heroBullets[i].destroy(); // Destroy the bullet after hitting the boss
heroBullets.splice(i, 1);
continue;
}
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].destroy();
heroBullets[i].destroy();
enemies.splice(j, 1);
heroBullets.splice(i, 1);
score += 10;
kills += 1;
scoreTxt.setText('Score: ' + score);
if (kills >= 30) {
LK.showGameOver();
}
break;
}
}
}
}
var boss;
var bossSpawned = false;
var hero;
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
var score = 0;
var kills = 0;
var gameDuration = 60000; // 1 minute in milliseconds
var startTime = Date.now();
var scoreTxt = new Text2('Score: 0', {
size: 50,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var timerTxt = new Text2('Time: 60', {
size: 50,
fill: "#ffffff"
});
timerTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(timerTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 400;
game.addChild(hero);
// Initialize shoot button
var shootButton = new ShootButton();
shootButton.x = 2048 - 200;
shootButton.y = 2732 - 200;
game.addChild(shootButton);
// Spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
game.addChild(enemy);
enemies.push(enemy);
}
// Update game state
game.update = function () {
// Update timer text
var elapsedTime = Date.now() - startTime;
var remainingTime = Math.max(0, Math.ceil((gameDuration - elapsedTime) / 1000));
timerTxt.setText('Time: ' + remainingTime);
// Spawn boss at 30 seconds
if (!bossSpawned && elapsedTime >= 30000) {
boss = new Boss();
boss.x = 2048 / 2;
boss.y = -300;
game.addChild(boss);
bossSpawned = true;
}
if (elapsedTime >= gameDuration) {
LK.showGameOver();
alert("You Win! Your score: " + score);
alert("Game Over! Your score: " + score);
return;
}
// Update hero
hero.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
LK.showGameOver();
}
}
// Update boss
if (bossSpawned) {
boss.update();
if (boss.intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].destroy();
heroBullets[i].destroy();
enemies.splice(j, 1);
heroBullets.splice(i, 1);
score += 10;
kills += 1;
scoreTxt.setText('Score: ' + score);
if (kills >= 30) {
LK.showGameOver();
}
break;
}
}
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].update();
if (enemyBullets[i].intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn enemies periodically
if (LK.ticks % 60 == 0) {
spawnEnemy();
}
};
// Handle touch events
game.move = function (x, y, obj) {
hero.x = x;
};
game.up = function (x, y, obj) {
// No action needed on touch up
}; /****
* Classes
****/
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.health = 200;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.destroy();
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for hero
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
var ShootButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('shootButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
hero.shoot();
};
self.containsPoint = function (point) {
var localPoint = self.toLocal(point);
return localPoint.x >= -self.width / 2 && localPoint.x <= self.width / 2 && localPoint.y >= -self.height / 2 && localPoint.y <= self.height / 2;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Update hero bullets
// Initialize arrays and variables
var heroBullets = [];
if (heroBullets && heroBullets.length) {
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (bossSpawned && heroBullets[i].intersects(boss)) {
boss.takeDamage(10); // Reduce boss health by 10
if (boss.health <= 0) {
score += 100;
scoreTxt.setText('Score: ' + score);
bossSpawned = false;
}
heroBullets[i].destroy(); // Destroy the bullet after hitting the boss
heroBullets.splice(i, 1);
continue;
}
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].destroy();
heroBullets[i].destroy();
enemies.splice(j, 1);
heroBullets.splice(i, 1);
score += 10;
kills += 1;
scoreTxt.setText('Score: ' + score);
if (kills >= 30) {
LK.showGameOver();
}
break;
}
}
}
}
var boss;
var bossSpawned = false;
var hero;
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
var score = 0;
var kills = 0;
var gameDuration = 60000; // 1 minute in milliseconds
var startTime = Date.now();
var scoreTxt = new Text2('Score: 0', {
size: 50,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var timerTxt = new Text2('Time: 60', {
size: 50,
fill: "#ffffff"
});
timerTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(timerTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 400;
game.addChild(hero);
// Initialize shoot button
var shootButton = new ShootButton();
shootButton.x = 2048 - 200;
shootButton.y = 2732 - 200;
game.addChild(shootButton);
// Spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
game.addChild(enemy);
enemies.push(enemy);
}
// Update game state
game.update = function () {
// Update timer text
var elapsedTime = Date.now() - startTime;
var remainingTime = Math.max(0, Math.ceil((gameDuration - elapsedTime) / 1000));
timerTxt.setText('Time: ' + remainingTime);
// Spawn boss at 30 seconds
if (!bossSpawned && elapsedTime >= 30000) {
boss = new Boss();
boss.x = 2048 / 2;
boss.y = -300;
game.addChild(boss);
bossSpawned = true;
}
if (elapsedTime >= gameDuration) {
LK.showGameOver();
alert("You Win! Your score: " + score);
alert("Game Over! Your score: " + score);
return;
}
// Update hero
hero.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
LK.showGameOver();
}
}
// Update boss
if (bossSpawned) {
boss.update();
if (boss.intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
enemies[j].destroy();
heroBullets[i].destroy();
enemies.splice(j, 1);
heroBullets.splice(i, 1);
score += 10;
kills += 1;
scoreTxt.setText('Score: ' + score);
if (kills >= 30) {
LK.showGameOver();
}
break;
}
}
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].update();
if (enemyBullets[i].intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn enemies periodically
if (LK.ticks % 60 == 0) {
spawnEnemy();
}
};
// Handle touch events
game.move = function (x, y, obj) {
hero.x = x;
};
game.up = function (x, y, obj) {
// No action needed on touch up
};