/****
* Classes
****/
// Boss class
// Boss class
var Boss = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.speed = 2;
self.horizontalSpeed = 2;
self.health = 100;
// Define shootDeathLaser function here
self.shotCount = 0; // Initialize shot counter
self.shootDeathLaser = function () {
// Shake effect for 2 seconds
var shakeDuration = 120; // 2 seconds at 60 FPS
var shakeIntensity = 10;
var shakeCount = 0;
var originalX = self.x;
var originalY = self.y;
var shakeInterval = LK.setInterval(function () {
if (shakeCount < shakeDuration) {
self.x = originalX + (Math.random() - 0.5) * shakeIntensity;
self.y = originalY + (Math.random() - 0.5) * shakeIntensity;
shakeCount++;
} else {
LK.clearInterval(shakeInterval);
self.x = originalX;
self.y = originalY;
// Shoot death lasers after shaking
var deathLaser1 = new DeathLaser(self.x - bossGraphics.width * 0.2, self.y + bossGraphics.height * 0.75);
var deathLaser2 = new DeathLaser(self.x + bossGraphics.width * 0.2, self.y + bossGraphics.height * 0.75);
game.addChild(deathLaser1);
game.addChild(deathLaser2);
enemyLasers.push(deathLaser1);
enemyLasers.push(deathLaser2);
// Increment shot counter
self.shotCount++;
// Relocate boss after 6 shots
if (self.shotCount >= 6) {
self.shotCount = 0; // Reset shot counter
self.x = Math.random() * (2048 - bossGraphics.width * 1.5) + bossGraphics.width * 0.75;
self.y = Math.random() * (2732 / 3 - bossGraphics.height * 1.5) + bossGraphics.height * 0.75;
}
// Resume spawning enemies
self.resumeEnemySpawning();
}
}, 1000 / 60); // 60 FPS
};
self.update = function () {
if (self.y < 2732 / 3 - bossGraphics.height / 4) {
self.y += self.speed;
} else {
self.x += self.horizontalSpeed;
if (self.x > 2048 - bossGraphics.width * 0.75 || self.x < bossGraphics.width * 0.75) {
self.horizontalSpeed = -self.horizontalSpeed;
}
// Boss shoots death laser if health is <= 5
if (self.health <= 50 && LK.ticks % 30 == 0) {
self.shootDeathLaser();
}
}
};
self.shoot = function () {
if (self.health <= 50) {
return; // Stop shooting normal lasers if health is 5 or less
}
// Center laser
var centerLaser = new EnemyLaser(self.x, self.y + bossGraphics.height * 0.75);
game.addChild(centerLaser);
enemyLasers.push(centerLaser);
// Right side laser
var rightLaser = new EnemyLaser(self.x + bossGraphics.width * 0.3, self.y + bossGraphics.height * 0.75);
game.addChild(rightLaser);
enemyLasers.push(rightLaser);
// Left side laser
var leftLaser = new EnemyLaser(self.x - bossGraphics.width * 0.3, self.y + bossGraphics.height * 0.75);
game.addChild(leftLaser);
enemyLasers.push(leftLaser);
};
});
var DeathLaser = Container.expand(function (x, y) {
var self = Container.call(this);
var deathLaserGraphics = self.attachAsset('deathlaser', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.x = x;
self.y = y;
self.speed = 20;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// EnemyLaser class
var EnemyLaser = Container.expand(function (x, y) {
var self = Container.call(this);
var enemyLaserGraphics = self.attachAsset('enemylaser', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.x = x;
self.y = y;
self.speed = 10;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// EnemyShip class
var EnemyShip = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemyship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
self.shoot = function () {
var enemyLaser = new EnemyLaser(self.x, self.y + enemyGraphics.height / 2);
game.addChild(enemyLaser);
enemyLasers.push(enemyLaser);
};
});
// Laser class
var Laser = Container.expand(function (x, y) {
var self = Container.call(this);
var laserGraphics = self.attachAsset('laser', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = x;
self.y = y;
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// SpaceShip class
var SpaceShip = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y = 2732 - 200;
};
self.shoot = function () {
var laser = new Laser(self.x, self.y - shipGraphics.height / 2);
game.addChild(laser);
lasers.push(laser);
LK.getSound('new_laser_sound_id').play();
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -starGraphics.height;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Play background music
LK.playMusic('space');
// Removed incorrect sound play call
// Initialize arrays and variables
var spaceship;
var enemies = [];
var lasers = [];
var enemyLasers = [];
var stars = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var boss = null;
// Create spaceship
spaceship = new SpaceShip();
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
game.addChild(spaceship);
// Create and add stars
for (var i = 0; i < 50; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
// Spawn enemies
function spawnEnemy() {
var enemy = new EnemyShip();
enemy.x = Math.random() * 2048;
enemy.y = -100;
enemies.push(enemy);
game.addChild(enemy);
}
// Handle game updates
game.update = function () {
// Update spaceship
spaceship.update();
// Update stars
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].update();
}
// Update lasers
for (var i = lasers.length - 1; i >= 0; i--) {
lasers[i].update();
if (lasers[i].y < 0) {
lasers[i].destroy();
lasers.splice(i, 1);
}
}
// Update enemy lasers
for (var i = enemyLasers.length - 1; i >= 0; i--) {
enemyLasers[i].update();
if (enemyLasers[i].y > 2732) {
enemyLasers[i].destroy();
enemyLasers.splice(i, 1);
}
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].y > 2732) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Check for collisions
for (var i = lasers.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (lasers[i].intersects(enemies[j])) {
lasers[i].destroy();
var destroyEffect = LK.getAsset('destroy', {
anchorX: 0.5,
anchorY: 0.5,
x: enemies[j].x,
y: enemies[j].y
});
game.addChild(destroyEffect);
LK.setTimeout(function () {
destroyEffect.destroy();
}, 200);
LK.getSound('explosion').play();
enemies[j].destroy();
lasers.splice(i, 1);
enemies.splice(j, 1);
score++;
scoreTxt.setText(score);
if (score === 100 && !boss) {
boss = new Boss();
boss.x = 2048 / 2;
boss.y = -boss.height;
game.addChild(boss);
}
break;
}
}
if (boss && boss.intersects && lasers[i] && lasers[i].intersects(boss)) {
lasers[i].destroy();
var destroyEffect = LK.getAsset('destroy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2,
x: boss.x,
y: boss.y
});
game.addChild(destroyEffect);
LK.setTimeout(function () {
destroyEffect.destroy();
}, 200);
LK.getSound('explosion').play();
boss.health--;
if (boss.health <= 0) {
var kabomEffects = [];
for (var i = 0; i < 3; i++) {
var kabomEffect = LK.getAsset('kabom', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: boss.width / 400,
scaleY: boss.height / 400,
x: boss.x + (Math.random() - 0.5) * boss.width / 2,
y: boss.y + (Math.random() - 0.5) * boss.height / 2
});
game.addChild(kabomEffect);
kabomEffects.push(kabomEffect);
}
LK.setTimeout(function () {
for (var i = 0; i < kabomEffects.length; i++) {
kabomEffects[i].destroy();
}
}, 200);
LK.getSound('kabom').play();
boss.destroy();
boss = null;
}
lasers.splice(i, 1);
score++;
scoreTxt.setText(score);
break;
}
}
for (var i = enemyLasers.length - 1; i >= 0; i--) {
if (enemyLasers[i].intersects(spaceship)) {
enemyLasers[i].destroy();
enemyLasers.splice(i, 1);
// Flash screen red for 1 second (1000ms) to show we are dead.
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver(); // Calling this will destroy the 'Game' and reset entire game state.
break;
}
}
// Spawn enemies periodically if boss is not present
if (LK.ticks % 60 == 0) {
spawnEnemy();
}
// Enemies shoot periodically if boss is not present
if (LK.ticks % 120 == 0) {
for (var i = 0; i < enemies.length; i++) {
enemies[i].shoot();
}
}
if (boss) {
boss.update();
if (boss.health > 5 && LK.ticks % 60 == 0) {
boss.shoot();
}
}
};
// Handle touch events
game.down = function (x, y, obj) {
spaceship.shoot();
};
game.move = function (x, y, obj) {
spaceship.x = x;
};
game.up = function (x, y, obj) {
// No action needed on touch up
};
// Add method to resume enemy spawning
Boss.prototype.resumeEnemySpawning = function () {
// Spawn enemies periodically
if (LK.ticks % 60 == 0 && !boss) {
spawnEnemy();
}
// Enemies shoot periodically
if (LK.ticks % 120 == 0 && !boss) {
for (var i = 0; i < enemies.length; i++) {
enemies[i].shoot();
}
}
};
star white. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
enemy space ship boss it should be big and looking from top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
explosion. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
linear laser red horizontal. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
red pixel circle with black background and hearth on middle. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
red circle with black background and bullet on middle next to x2 symbol. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.