/****
* 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 = -20 - LK.ticks / 1000;
self.update = function () {
self.y += self.speed;
if (self.y < -self.height) {
self.destroy();
}
};
return self;
});
// Define the 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 = 7;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -self.height;
}
// Increase the size of the asteroid over time
self.scaleX += 0.01;
self.scaleY += 0.01;
};
return self;
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.shield = null;
self.update = function () {
// Player update logic
self.activateShield = function () {
if (!self.shield) {
self.shield = self.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5
});
// Create a text to display 'Shield Activated' and the remaining shield duration
var shieldText = new Text2('Shield Activated for 2 seconds', {
size: 50,
fill: "#ffffff"
});
shieldText.anchor.set(1, 1);
LK.gui.bottomRight.addChild(shieldText);
LK.setTimeout(function () {
self.shield.destroy();
self.shield = null;
// Remove the 'Shield Activated' text when the shield is deactivated
shieldText.destroy();
}, 2000);
}
};
};
return self;
});
// Define the Powerup class
var Powerup = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -self.height;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
player.x = 2048 / 2;
player.y = 2732 - 200;
game.addChild(player);
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * -2732;
enemies.push(enemy);
game.addChild(enemy);
}
// Initialize bullets
var bullets = [];
// Initialize powerups
var powerups = [];
// Initialize score
var score = 0;
var highScore = 0;
// Create score text
var scoreText = new Text2('Score: 0', {
size: 50,
fill: "#ffffff"
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Handle player movement
game.move = function (x, y, obj) {
var gridSize = 100; // Define the size of the grid
player.x = Math.round(x / gridSize) * gridSize; // Snap the player's x position to the nearest grid point
};
// Keyboard inputs are not supported in this game engine
// 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);
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
};
// Update game state
game.update = function () {
// Update player
player.update();
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
if (player.intersects(enemies[i])) {
if (player.shield) {
enemies[i].destroy();
enemies.splice(i, 1);
} else {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
for (var k = enemies.length - 1; k >= 0; k--) {
if (bullets[j].intersects(enemies[k])) {
bullets[j].destroy();
bullets.splice(j, 1);
enemies[k].destroy();
enemies.splice(k, 1);
// Increase score and update score text
score++;
scoreText.setText('Score: ' + score);
break;
}
}
}
// Update powerups
for (var l = powerups.length - 1; l >= 0; l--) {
powerups[l].update();
if (player.intersects(powerups[l])) {
powerups[l].destroy();
powerups.splice(l, 1);
// Increase score and update score text
score += 5;
scoreText.setText('Score: ' + score);
// Activate shield
player.activateShield();
}
}
// Spawn new enemies
if (enemies.length < 10) {
var newEnemy = new Enemy();
newEnemy.x = Math.random() * 2048;
newEnemy.y = Math.random() * -2732;
enemies.push(newEnemy);
game.addChild(newEnemy);
}
// Spawn new powerups
if (powerups.length < 5 && LK.ticks % 600 == 0) {
var newPowerup = new Powerup();
newPowerup.x = Math.random() * 2048;
newPowerup.y = Math.random() * -2732;
powerups.push(newPowerup);
game.addChild(newPowerup);
}
};
a pixelated bullet. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a pixelated asteroid. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a pixelated plane pointed at 0 degrees from the game space shooter. Single Game Texture. In-Game asset. 1d. Blank background. High contrast. No shadows.
a pixelated shield powerup pointed at 0 degrees from the game space shooter. Single Game Texture. In-Game asset. 1d. Blank background. High contrast. No shadows.
add a shield protecting the plane