/**** * Classes ****/ // Asteroid class var Asteroid = Container.expand(function () { var self = Container.call(this); var asteroidGraphics = self.attachAsset('asteroid', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Spaceship class var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Spaceship update logic if needed }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize spaceship var spaceship = game.addChild(new Spaceship()); spaceship.x = 2048 / 2; spaceship.y = 2732 - 200; // Array to hold asteroids var asteroids = []; // Function to handle spaceship movement function handleMove(x, y, obj) { spaceship.x = x; // spaceship.y = y; // Commented out to restrict vertical movement } // Game move event game.move = handleMove; // Game update function game.update = function () { // Create new asteroids if (LK.ticks % 60 == 0) { var newAsteroid = new Asteroid(); newAsteroid.x = Math.random() * 2048; newAsteroid.y = -50; asteroids.push(newAsteroid); game.addChild(newAsteroid); } // Initialize score display var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Update asteroids for (var i = asteroids.length - 1; i >= 0; i--) { var asteroid = asteroids[i]; asteroid.update(); // Check for collision with spaceship if (spaceship.intersects(asteroid)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } // Remove off-screen asteroids and increment score if (asteroid.y > 2732 + 50) { asteroid.destroy(); asteroids.splice(i, 1); LK.setScore(LK.getScore() + 1); // Increment score for each asteroid dodged scoreTxt.setText(LK.getScore()); // Update score display } } };
/****
* Classes
****/
// Asteroid class
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Spaceship class
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Spaceship update logic if needed
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize spaceship
var spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
// Array to hold asteroids
var asteroids = [];
// Function to handle spaceship movement
function handleMove(x, y, obj) {
spaceship.x = x;
// spaceship.y = y; // Commented out to restrict vertical movement
}
// Game move event
game.move = handleMove;
// Game update function
game.update = function () {
// Create new asteroids
if (LK.ticks % 60 == 0) {
var newAsteroid = new Asteroid();
newAsteroid.x = Math.random() * 2048;
newAsteroid.y = -50;
asteroids.push(newAsteroid);
game.addChild(newAsteroid);
}
// Initialize score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
asteroid.update();
// Check for collision with spaceship
if (spaceship.intersects(asteroid)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Remove off-screen asteroids and increment score
if (asteroid.y > 2732 + 50) {
asteroid.destroy();
asteroids.splice(i, 1);
LK.setScore(LK.getScore() + 1); // Increment score for each asteroid dodged
scoreTxt.setText(LK.getScore()); // Update score display
}
}
};