/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var BossEnemy = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 3,
scaleY: 3
});
self.health = 30;
self.speedY = 0.5;
self.movePattern = 2; // Always use sine wave pattern
self.patternOffset = Math.random() * 100;
self.shootTimer = 100; // Shoot three times every 5 seconds (60 FPS * 5 / 3 = 100 frames between shots)
self.update = function () {
// Move down slowly until reaching position
if (self.y < 300) {
self.y += self.speedY;
}
// Side-to-side movement
self.x = self.startX + Math.sin(LK.ticks * 0.02 + self.patternOffset) * 200;
// Shoot less frequently
self.shootTimer--;
if (self.shootTimer <= 0) {
// Create enemy bullets (multiple)
var bullet1 = new Bullet();
bullet1.speed = 6;
bullet1.x = self.x - 30;
bullet1.y = self.y + 50;
game.addChild(bullet1);
enemyBullets.push(bullet1);
var bullet2 = new Bullet();
bullet2.speed = 6;
bullet2.x = self.x + 30;
bullet2.y = self.y + 50;
game.addChild(bullet2);
enemyBullets.push(bullet2);
var bullet3 = new Bullet();
bullet3.speed = 5;
bullet3.x = self.x;
bullet3.y = self.y + 50;
game.addChild(bullet3);
enemyBullets.push(bullet3);
self.shootTimer = 100; // Reset to fire three times every 5 seconds
}
return false;
};
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 = -10;
self.update = function () {
self.y += self.speed;
// Remove bullets that go off screen
if (self.y < -50) {
return true; // Return true to mark for removal
}
return false;
};
return self;
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 1.5 + Math.random() * 1.5;
self.movePattern = Math.floor(Math.random() * 3); // 0: straight, 1: zigzag, 2: sine
self.patternOffset = Math.random() * 100;
self.shootTimer = 100; // Fire three times every 5 seconds (60 FPS * 5 / 3 = 100 frames between shots)
self.update = function () {
self.y += self.speedY;
// Different movement patterns
switch (self.movePattern) {
case 1:
// Zigzag
self.x += Math.sin(LK.ticks * 0.05 + self.patternOffset) * 3;
break;
case 2:
// Sine wave
self.x = self.startX + Math.sin(LK.ticks * 0.03 + self.patternOffset) * 150;
break;
}
// Shoot occasionally
self.shootTimer--;
if (self.shootTimer <= 0) {
// Create enemy bullet
var bullet = new Bullet();
bullet.speed = 5; // Bullets move down
bullet.x = self.x;
bullet.y = self.y + 30;
game.addChild(bullet);
enemyBullets.push(bullet);
self.shootTimer = 100; // Reset to fire three times every 5 seconds
}
// Remove if off bottom of screen
if (self.y > 2732 + 100) {
return true; // Mark for removal
}
return false;
};
return self;
});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.shootCooldown = 0;
self.shootDelay = 15;
self.speed = 5;
self.lives = 3;
self.shoot = function () {
if (self.shootCooldown <= 0) {
self.shootCooldown = self.shootDelay;
// Create a single bullet
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
bullets.push(bullet);
// Play shoot sound
LK.getSound('shoot').play();
return true;
}
return false;
};
self.update = function () {
// Update cooldown
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game state
var gameRunning = true;
var level = 1;
var score = 0;
var lives = 3;
var enemySpawnRate = 150; // Reduced from 300 to spawn enemies more frequently
var enemySpawnCountdown = 150;
var bossActive = false;
var bossHealth = 30;
var boss = null;
// Game elements
var spaceship;
var bullets = [];
var enemyBullets = [];
var enemies = [];
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
size: 50,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -250; // Offset from right edge
var levelTxt = new Text2('Level: 1', {
size: 50,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(levelTxt);
levelTxt.y = 10;
var livesTxt = new Text2('Lives: 3', {
size: 50,
fill: 0xFFFFFF
});
livesTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(livesTxt);
livesTxt.x = 120; // Offset to avoid menu button
livesTxt.y = 10;
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
size: 40,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.y = 60;
// Drag handling
var isDragging = false;
// Initialize game elements
function initializeGame() {
// Create player spaceship
spaceship = new Spaceship();
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
game.addChild(spaceship);
// Reset variables
bullets = [];
enemyBullets = [];
enemies = [];
level = 1;
score = 0;
lives = 3;
// Update UI
updateUI();
// Start game music
LK.playMusic('gameMusic');
}
// Update UI elements
function updateUI() {
scoreTxt.setText('Score: ' + score);
levelTxt.setText('Level: ' + level);
livesTxt.setText('Lives: ' + lives);
highScoreTxt.setText('High Score: ' + storage.highScore);
// Update score in LK system
LK.setScore(score);
}
// Create explosion effect
function createExplosion(x, y, color) {
// Flash object
var explosionObj = new Container();
explosionObj.x = x;
explosionObj.y = y;
var explosionGraphic = explosionObj.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.7
});
game.addChild(explosionObj);
// Animate explosion
tween(explosionGraphic, {
scaleX: 2,
scaleY: 2,
alpha: 0
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
explosionObj.destroy();
}
});
// Play explosion sound
LK.getSound('explosion').play();
}
// Handle collisions
function checkCollisions() {
// Player bullets with enemies
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Check collision with boss if active
if (bossActive && boss && bullet.intersects(boss)) {
// Create small explosion effect
createExplosion(bullet.x, bullet.y, 0xe74c3c);
// Remove bullet
bullet.destroy();
bullets.splice(i, 1);
// Reduce boss health
bossHealth--;
updateBossHealthBar();
// Check if boss is defeated
if (bossHealth <= 0) {
// Create large explosion
createExplosion(boss.x, boss.y, 0xe74c3c);
createExplosion(boss.x - 50, boss.y + 30, 0xe74c3c);
createExplosion(boss.x + 50, boss.y - 30, 0xe74c3c);
// Remove boss
boss.destroy();
boss = null;
bossActive = false;
// Award big score
score += 200;
updateUI();
// End the game with a win
if (score > storage.highScore) {
storage.highScore = score;
updateUI();
}
LK.showYouWin();
}
continue; // Skip checking other enemies
}
// Check collision with regular enemies
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (bullet.intersects(enemy)) {
// Create explosion
createExplosion(enemy.x, enemy.y, 0xe74c3c);
// Remove enemy and bullet
enemy.destroy();
enemies.splice(j, 1);
bullet.destroy();
bullets.splice(i, 1);
// Increase score
score += 15;
updateUI();
break; // Bullet can only hit one enemy
}
}
}
// Check player collisions with enemy ships
for (var i = enemies.length - 1; i >= 0; i--) {
if (spaceship.intersects(enemies[i])) {
// Create explosion
createExplosion(enemies[i].x, enemies[i].y, 0xe74c3c);
// Remove enemy
enemies[i].destroy();
enemies.splice(i, 1);
// Player loses a life
lives--;
updateUI();
if (lives <= 0) {
gameOver();
}
}
}
// Check player collisions with enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
if (spaceship.intersects(enemyBullets[i])) {
// Create small explosion
createExplosion(enemyBullets[i].x, enemyBullets[i].y, 0xffffff);
// Remove bullet
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
// Player loses a life
lives--;
updateUI();
if (lives <= 0) {
gameOver();
}
}
}
}
// Check if game level should increase
function checkLevelProgression() {
if (score >= level * 100) {
level++;
updateUI();
// Increase difficulty - make spawn rate lower but not as aggressively
enemySpawnRate = Math.max(90, enemySpawnRate - 10);
// Spawn boss at level 2 and above
if (level >= 2 && !bossActive) {
spawnBoss();
}
// Show level up text
var levelUpTxt = new Text2('Level ' + level + '!', {
size: 100,
fill: 0xFFFF00
});
levelUpTxt.anchor.set(0.5, 0.5);
levelUpTxt.x = 2048 / 2;
levelUpTxt.y = 2732 / 2;
game.addChild(levelUpTxt);
// Animate and remove
tween(levelUpTxt, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 1500,
onFinish: function onFinish() {
levelUpTxt.destroy();
}
});
}
}
// Spawn boss function
function spawnBoss() {
bossActive = true;
bossHealth = 30;
// Create boss warning text
var warningText = new Text2('BOSS INCOMING!', {
size: 120,
fill: 0xFF0000
});
warningText.anchor.set(0.5, 0.5);
warningText.x = 2048 / 2;
warningText.y = 2732 / 2;
game.addChild(warningText);
// Animate and remove warning
tween(warningText, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 1500,
onFinish: function onFinish() {
warningText.destroy();
// Create and add boss
boss = new BossEnemy();
boss.x = 2048 / 2;
boss.y = -150;
boss.startX = boss.x;
game.addChild(boss);
// Create boss health bar
updateBossHealthBar();
}
});
}
// Update boss health bar
function updateBossHealthBar() {
// Remove old health bar if exists
if (game.bossHealthBar) {
game.bossHealthBar.destroy();
}
// Create new health bar
var healthBar = new Container();
healthBar.x = 2048 / 2 - 300;
healthBar.y = 100;
// Background
var barBg = healthBar.attachAsset('asteroid', {
anchorX: 0,
anchorY: 0.5,
scaleX: 6,
scaleY: 0.5
});
barBg.tint = 0x333333;
// Health fill
var barFill = healthBar.attachAsset('asteroid', {
anchorX: 0,
anchorY: 0.5,
scaleX: bossHealth / 30 * 6,
scaleY: 0.4
});
barFill.tint = 0xFF0000;
// Add health text
var healthText = new Text2('Boss: ' + bossHealth + '/30', {
size: 40,
fill: 0xFFFFFF
});
healthText.anchor.set(0.5, 0.5);
healthText.x = 300;
healthText.y = 0;
healthBar.addChild(healthText);
game.addChild(healthBar);
game.bossHealthBar = healthBar;
}
// Game over function
function gameOver() {
gameRunning = false;
// Update high score
if (score > storage.highScore) {
storage.highScore = score;
updateUI();
}
// Show game over screen
LK.showGameOver();
}
// Initialize the game
initializeGame();
// Touch and mouse controls
game.down = function (x, y, obj) {
isDragging = true;
// Fire a bullet when tapping
if (gameRunning) {
spaceship.shoot();
}
// Only move spaceship horizontally
spaceship.x = x;
// Keep Y position fixed
spaceship.y = 2732 - 200; // Fixed Y position at the bottom
};
game.up = function (x, y, obj) {
isDragging = false;
};
game.move = function (x, y, obj) {
if (isDragging && gameRunning) {
// Only move spaceship horizontally (left-right)
spaceship.x = x;
// Keep Y position fixed at the bottom area
spaceship.y = 2732 - 200; // Fixed Y position at the bottom
}
};
// Game update function
game.update = function () {
if (!gameRunning) return;
// Update spaceship
spaceship.update();
// Auto-fire when holding
if (isDragging && LK.ticks % 15 === 0) {
spaceship.shoot();
}
// Update boss if active
if (bossActive && boss) {
boss.update();
}
// Only spawn regular enemies if boss is not active
else {
// Spawn enemies
enemySpawnCountdown--;
if (enemySpawnCountdown <= 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
enemy.startX = enemy.x; // Save starting X for sine movement
game.addChild(enemy);
enemies.push(enemy);
enemySpawnCountdown = enemySpawnRate;
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
if (bullets[i].update()) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
if (enemyBullets[i].update()) {
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
}
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].update()) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Check collisions
checkCollisions();
// Check level progression
checkLevelProgression();
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var BossEnemy = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 3,
scaleY: 3
});
self.health = 30;
self.speedY = 0.5;
self.movePattern = 2; // Always use sine wave pattern
self.patternOffset = Math.random() * 100;
self.shootTimer = 100; // Shoot three times every 5 seconds (60 FPS * 5 / 3 = 100 frames between shots)
self.update = function () {
// Move down slowly until reaching position
if (self.y < 300) {
self.y += self.speedY;
}
// Side-to-side movement
self.x = self.startX + Math.sin(LK.ticks * 0.02 + self.patternOffset) * 200;
// Shoot less frequently
self.shootTimer--;
if (self.shootTimer <= 0) {
// Create enemy bullets (multiple)
var bullet1 = new Bullet();
bullet1.speed = 6;
bullet1.x = self.x - 30;
bullet1.y = self.y + 50;
game.addChild(bullet1);
enemyBullets.push(bullet1);
var bullet2 = new Bullet();
bullet2.speed = 6;
bullet2.x = self.x + 30;
bullet2.y = self.y + 50;
game.addChild(bullet2);
enemyBullets.push(bullet2);
var bullet3 = new Bullet();
bullet3.speed = 5;
bullet3.x = self.x;
bullet3.y = self.y + 50;
game.addChild(bullet3);
enemyBullets.push(bullet3);
self.shootTimer = 100; // Reset to fire three times every 5 seconds
}
return false;
};
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 = -10;
self.update = function () {
self.y += self.speed;
// Remove bullets that go off screen
if (self.y < -50) {
return true; // Return true to mark for removal
}
return false;
};
return self;
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 1.5 + Math.random() * 1.5;
self.movePattern = Math.floor(Math.random() * 3); // 0: straight, 1: zigzag, 2: sine
self.patternOffset = Math.random() * 100;
self.shootTimer = 100; // Fire three times every 5 seconds (60 FPS * 5 / 3 = 100 frames between shots)
self.update = function () {
self.y += self.speedY;
// Different movement patterns
switch (self.movePattern) {
case 1:
// Zigzag
self.x += Math.sin(LK.ticks * 0.05 + self.patternOffset) * 3;
break;
case 2:
// Sine wave
self.x = self.startX + Math.sin(LK.ticks * 0.03 + self.patternOffset) * 150;
break;
}
// Shoot occasionally
self.shootTimer--;
if (self.shootTimer <= 0) {
// Create enemy bullet
var bullet = new Bullet();
bullet.speed = 5; // Bullets move down
bullet.x = self.x;
bullet.y = self.y + 30;
game.addChild(bullet);
enemyBullets.push(bullet);
self.shootTimer = 100; // Reset to fire three times every 5 seconds
}
// Remove if off bottom of screen
if (self.y > 2732 + 100) {
return true; // Mark for removal
}
return false;
};
return self;
});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.shootCooldown = 0;
self.shootDelay = 15;
self.speed = 5;
self.lives = 3;
self.shoot = function () {
if (self.shootCooldown <= 0) {
self.shootCooldown = self.shootDelay;
// Create a single bullet
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
bullets.push(bullet);
// Play shoot sound
LK.getSound('shoot').play();
return true;
}
return false;
};
self.update = function () {
// Update cooldown
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game state
var gameRunning = true;
var level = 1;
var score = 0;
var lives = 3;
var enemySpawnRate = 150; // Reduced from 300 to spawn enemies more frequently
var enemySpawnCountdown = 150;
var bossActive = false;
var bossHealth = 30;
var boss = null;
// Game elements
var spaceship;
var bullets = [];
var enemyBullets = [];
var enemies = [];
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
size: 50,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -250; // Offset from right edge
var levelTxt = new Text2('Level: 1', {
size: 50,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(levelTxt);
levelTxt.y = 10;
var livesTxt = new Text2('Lives: 3', {
size: 50,
fill: 0xFFFFFF
});
livesTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(livesTxt);
livesTxt.x = 120; // Offset to avoid menu button
livesTxt.y = 10;
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
size: 40,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.y = 60;
// Drag handling
var isDragging = false;
// Initialize game elements
function initializeGame() {
// Create player spaceship
spaceship = new Spaceship();
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
game.addChild(spaceship);
// Reset variables
bullets = [];
enemyBullets = [];
enemies = [];
level = 1;
score = 0;
lives = 3;
// Update UI
updateUI();
// Start game music
LK.playMusic('gameMusic');
}
// Update UI elements
function updateUI() {
scoreTxt.setText('Score: ' + score);
levelTxt.setText('Level: ' + level);
livesTxt.setText('Lives: ' + lives);
highScoreTxt.setText('High Score: ' + storage.highScore);
// Update score in LK system
LK.setScore(score);
}
// Create explosion effect
function createExplosion(x, y, color) {
// Flash object
var explosionObj = new Container();
explosionObj.x = x;
explosionObj.y = y;
var explosionGraphic = explosionObj.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.7
});
game.addChild(explosionObj);
// Animate explosion
tween(explosionGraphic, {
scaleX: 2,
scaleY: 2,
alpha: 0
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
explosionObj.destroy();
}
});
// Play explosion sound
LK.getSound('explosion').play();
}
// Handle collisions
function checkCollisions() {
// Player bullets with enemies
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Check collision with boss if active
if (bossActive && boss && bullet.intersects(boss)) {
// Create small explosion effect
createExplosion(bullet.x, bullet.y, 0xe74c3c);
// Remove bullet
bullet.destroy();
bullets.splice(i, 1);
// Reduce boss health
bossHealth--;
updateBossHealthBar();
// Check if boss is defeated
if (bossHealth <= 0) {
// Create large explosion
createExplosion(boss.x, boss.y, 0xe74c3c);
createExplosion(boss.x - 50, boss.y + 30, 0xe74c3c);
createExplosion(boss.x + 50, boss.y - 30, 0xe74c3c);
// Remove boss
boss.destroy();
boss = null;
bossActive = false;
// Award big score
score += 200;
updateUI();
// End the game with a win
if (score > storage.highScore) {
storage.highScore = score;
updateUI();
}
LK.showYouWin();
}
continue; // Skip checking other enemies
}
// Check collision with regular enemies
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (bullet.intersects(enemy)) {
// Create explosion
createExplosion(enemy.x, enemy.y, 0xe74c3c);
// Remove enemy and bullet
enemy.destroy();
enemies.splice(j, 1);
bullet.destroy();
bullets.splice(i, 1);
// Increase score
score += 15;
updateUI();
break; // Bullet can only hit one enemy
}
}
}
// Check player collisions with enemy ships
for (var i = enemies.length - 1; i >= 0; i--) {
if (spaceship.intersects(enemies[i])) {
// Create explosion
createExplosion(enemies[i].x, enemies[i].y, 0xe74c3c);
// Remove enemy
enemies[i].destroy();
enemies.splice(i, 1);
// Player loses a life
lives--;
updateUI();
if (lives <= 0) {
gameOver();
}
}
}
// Check player collisions with enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
if (spaceship.intersects(enemyBullets[i])) {
// Create small explosion
createExplosion(enemyBullets[i].x, enemyBullets[i].y, 0xffffff);
// Remove bullet
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
// Player loses a life
lives--;
updateUI();
if (lives <= 0) {
gameOver();
}
}
}
}
// Check if game level should increase
function checkLevelProgression() {
if (score >= level * 100) {
level++;
updateUI();
// Increase difficulty - make spawn rate lower but not as aggressively
enemySpawnRate = Math.max(90, enemySpawnRate - 10);
// Spawn boss at level 2 and above
if (level >= 2 && !bossActive) {
spawnBoss();
}
// Show level up text
var levelUpTxt = new Text2('Level ' + level + '!', {
size: 100,
fill: 0xFFFF00
});
levelUpTxt.anchor.set(0.5, 0.5);
levelUpTxt.x = 2048 / 2;
levelUpTxt.y = 2732 / 2;
game.addChild(levelUpTxt);
// Animate and remove
tween(levelUpTxt, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 1500,
onFinish: function onFinish() {
levelUpTxt.destroy();
}
});
}
}
// Spawn boss function
function spawnBoss() {
bossActive = true;
bossHealth = 30;
// Create boss warning text
var warningText = new Text2('BOSS INCOMING!', {
size: 120,
fill: 0xFF0000
});
warningText.anchor.set(0.5, 0.5);
warningText.x = 2048 / 2;
warningText.y = 2732 / 2;
game.addChild(warningText);
// Animate and remove warning
tween(warningText, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 1500,
onFinish: function onFinish() {
warningText.destroy();
// Create and add boss
boss = new BossEnemy();
boss.x = 2048 / 2;
boss.y = -150;
boss.startX = boss.x;
game.addChild(boss);
// Create boss health bar
updateBossHealthBar();
}
});
}
// Update boss health bar
function updateBossHealthBar() {
// Remove old health bar if exists
if (game.bossHealthBar) {
game.bossHealthBar.destroy();
}
// Create new health bar
var healthBar = new Container();
healthBar.x = 2048 / 2 - 300;
healthBar.y = 100;
// Background
var barBg = healthBar.attachAsset('asteroid', {
anchorX: 0,
anchorY: 0.5,
scaleX: 6,
scaleY: 0.5
});
barBg.tint = 0x333333;
// Health fill
var barFill = healthBar.attachAsset('asteroid', {
anchorX: 0,
anchorY: 0.5,
scaleX: bossHealth / 30 * 6,
scaleY: 0.4
});
barFill.tint = 0xFF0000;
// Add health text
var healthText = new Text2('Boss: ' + bossHealth + '/30', {
size: 40,
fill: 0xFFFFFF
});
healthText.anchor.set(0.5, 0.5);
healthText.x = 300;
healthText.y = 0;
healthBar.addChild(healthText);
game.addChild(healthBar);
game.bossHealthBar = healthBar;
}
// Game over function
function gameOver() {
gameRunning = false;
// Update high score
if (score > storage.highScore) {
storage.highScore = score;
updateUI();
}
// Show game over screen
LK.showGameOver();
}
// Initialize the game
initializeGame();
// Touch and mouse controls
game.down = function (x, y, obj) {
isDragging = true;
// Fire a bullet when tapping
if (gameRunning) {
spaceship.shoot();
}
// Only move spaceship horizontally
spaceship.x = x;
// Keep Y position fixed
spaceship.y = 2732 - 200; // Fixed Y position at the bottom
};
game.up = function (x, y, obj) {
isDragging = false;
};
game.move = function (x, y, obj) {
if (isDragging && gameRunning) {
// Only move spaceship horizontally (left-right)
spaceship.x = x;
// Keep Y position fixed at the bottom area
spaceship.y = 2732 - 200; // Fixed Y position at the bottom
}
};
// Game update function
game.update = function () {
if (!gameRunning) return;
// Update spaceship
spaceship.update();
// Auto-fire when holding
if (isDragging && LK.ticks % 15 === 0) {
spaceship.shoot();
}
// Update boss if active
if (bossActive && boss) {
boss.update();
}
// Only spawn regular enemies if boss is not active
else {
// Spawn enemies
enemySpawnCountdown--;
if (enemySpawnCountdown <= 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
enemy.startX = enemy.x; // Save starting X for sine movement
game.addChild(enemy);
enemies.push(enemy);
enemySpawnCountdown = enemySpawnRate;
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
if (bullets[i].update()) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Update enemy bullets
for (var i = enemyBullets.length - 1; i >= 0; i--) {
if (enemyBullets[i].update()) {
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
}
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].update()) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Check collisions
checkCollisions();
// Check level progression
checkLevelProgression();
};