/****
* Classes
****/
// Define the 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;
};
});
// Define the Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemyShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Enemy update logic
// Move enemy
self.x += Math.sin(LK.ticks / 10) * 5;
// Fire bullet every 60 ticks with a random offset
if (LK.ticks % (60 + Math.floor(Math.random() * 30)) == 0) {
var newBullet = new EnemyBullet();
newBullet.x = self.x;
newBullet.y = self.y;
game.addChild(newBullet);
}
};
});
// Define the EnemyBullet class
var EnemyBullet = 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;
};
});
// Define the Heart class
var Heart = Container.expand(function () {
var self = Container.call(this);
var heartGraphics = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Heart update logic, if any
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('playerShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 3; // Initialize player health
self.update = function () {
// Player update logic
// Move player left if it's not at the left edge of the screen
if (self.x > 0) {
self.x -= 5;
}
// Move player right if it's not at the right edge of the screen
if (self.x < 2048) {
self.x += 5;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game over state
game.isGameOver = false;
var healthTxt = new Text2('Health: 3', {
size: 100,
fill: 0xFFFFFF
});
healthTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(healthTxt);
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = 400 + i * 300;
enemy.y = 100 + Math.random() * 200; // Randomize Y position for unsynchronized movement
enemies.push(enemy);
game.addChild(enemy);
}
// Initialize bullets
var bullets = [];
// Handle shooting
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
};
game.move = function (x, y, obj) {
// Move player to the position of the touch or mouse cursor
player.x = x;
};
// Update game state
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
bullet.update();
if (bullet.y < -50) {
bullet.destroy();
bullets.splice(i, 1);
}
}
// Update enemies
for (var j = 0; j < enemies.length; j++) {
var enemy = enemies[j];
enemy.update();
// Check for collision with bullets
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
if (bullet.intersects(enemy)) {
bullet.destroy();
bullets.splice(k, 1);
enemy.destroy();
enemies.splice(j, 1);
break;
}
}
}
// Check if all enemies are destroyed
if (enemies.length === 0) {
// Spawn more enemies
for (var i = 0; i < 5; i++) {
var newEnemy = new Enemy();
newEnemy.x = 400 + i * 300;
newEnemy.y = 100 + Math.random() * 200;
enemies.push(newEnemy);
game.addChild(newEnemy);
}
}
// Spawn hearts mid-game
if (LK.ticks % 300 == 0) {
// Every 5 seconds
for (var h = 0; h < 3; h++) {
var heart = new Heart();
heart.x = Math.random() * 2048;
heart.y = Math.random() * 1000 + 1000; // Increase the Y position to make the heart spawn lower
game.addChild(heart);
}
}
// Check for collision with enemy bullets
for (var l = game.children.length - 1; l >= 0; l--) {
var child = game.children[l];
if (child instanceof EnemyBullet && child.intersects(player)) {
child.destroy();
player.health -= 1; // Decrease health by 1
healthTxt.setText('Health: ' + player.health); // Update health display
if (player.health <= 0 && !game.isGameOver) {
game.isGameOver = true;
LK.showGameOver();
// Set a timeout to respawn the player after 3 seconds
LK.setTimeout(function () {
game.isGameOver = false;
player.x = 2048 / 2;
player.y = 2732 - 200;
player.health = 3; // Reset health
}, 3000);
}
break;
}
}
};
bullet'. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
space ship facing up. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
heart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
enemy ship facing up. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows