/****
* Classes
****/
// Bullet class
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 < -bulletGraphics.height / 2) {
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.direction = 1; // 1 for right, -1 for left
self.update = function () {
self.x += self.speed * self.direction;
if (self.x > 2048 - enemyGraphics.width / 2 || self.x < enemyGraphics.width / 2) {
self.direction *= -1;
}
};
});
// 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 + bulletGraphics.height / 2) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Spacecraft class for player
var Spacecraft = Container.expand(function () {
var self = Container.call(this);
var spacecraftGraphics = self.attachAsset('spacecraft', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.moveLeft = function () {
self.x -= self.speed;
if (self.x < spacecraftGraphics.width / 2) {
self.x = spacecraftGraphics.width / 2;
}
};
self.moveRight = function () {
self.x += self.speed;
if (self.x > 2048 - spacecraftGraphics.width / 2) {
self.x = 2048 - spacecraftGraphics.width / 2;
}
};
self.shoot = function () {
if (!self.lastShotTime || LK.ticks - self.lastShotTime >= 30) {
// 30 ticks = 0.5 seconds at 60 FPS
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y - spacecraftGraphics.height / 2;
game.addChild(bullet);
bullets.push(bullet);
self.lastShotTime = LK.ticks;
// Play shooting sound effect
LK.getSound('shoot').play();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundImage: LK.getAsset('spaceBackgroundImage', {
anchorX: 0.5,
anchorY: 0.5
}) // Set game background to a space theme with planets and nebulae
});
/****
* Game Code
****/
function showVictory() {
// Flash screen green for 1 second (1000ms) to show victory.
LK.effects.flashScreen(0x00ff00, 1000);
// Display 'You Win' message
var winText = new Text2('You Win', {
size: 200,
fill: "#ffffff"
});
winText.anchor.set(0.5, 0.5);
winText.x = 2048 / 2;
winText.y = 2732 / 2;
LK.gui.center.addChild(winText);
// 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.
}
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var bullets = [];
var enemyBullets = [];
var backgroundObject = LK.getAsset('spaceBackgroundImage', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundObject.x = 2048 / 2;
backgroundObject.y = 2732 / 2;
game.addChild(backgroundObject);
var player = new Spacecraft();
player.x = 2048 / 2;
player.y = 2732 - 150;
game.addChild(player);
var enemy = new Enemy();
enemy.x = 2048 / 2;
enemy.y = 100;
game.addChild(enemy);
// Handle touch events for player movement
game.down = function (x, y, obj) {
player.x = x;
player.shoot();
};
// Play background music
LK.playMusic('space');
game.update = function () {
enemy.update();
// Enemy shooting logic
var shootInterval = 60;
if (LK.getScore() >= 10) {
shootInterval = 30; // Double the shooting speed
}
if (LK.ticks % shootInterval === 0) {
// Enemy shoots every second
var enemyBullet = new EnemyBullet();
enemyBullet.x = enemy.x;
enemyBullet.y = enemy.y + enemy.height / 2;
game.addChild(enemyBullet);
enemyBullets.push(enemyBullet);
}
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].intersects(enemy)) {
// Increase score
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText(LK.getScore());
// Add flash effect on enemy hit
LK.effects.flashObject(enemy, 0xff0000, 500); // Red flash for 500ms
// Play explosion sound effect
LK.getSound('explosion').play();
bullets[i].destroy();
bullets.splice(i, 1);
}
if (bullets[i] && bullets[i].y < -50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Check for collision between enemy bullets and player
for (var j = enemyBullets.length - 1; j >= 0; j--) {
enemyBullets[j].update();
if (enemyBullets[j].intersects(player)) {
// Flash screen red for 1 second (1000ms) to show defeat.
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.
return; // Exit the update loop
}
if (enemyBullets[j].y > 2732 + 50) {
enemyBullets[j].destroy();
enemyBullets.splice(j, 1);
}
}
};
space ship. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Laser bullets. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A space full of stars. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.