/**** * Classes ****/ // Explosion class var Explosion = Container.expand(function () { var self = Container.call(this); var explosionGraphics = self.attachAsset('explosion', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { explosionGraphics.alpha -= 0.1; if (explosionGraphics.alpha <= 0) { self.destroy(); } }; }); // Laser class var Laser = Container.expand(function () { var self = Container.call(this); var laserGraphics = self.attachAsset('laser', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; 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 }; self.move = function (x, y) { self.x = x; self.y = y; }; self.lasers = []; self.fire = function () { var laser = new Laser(); laser.x = self.x; laser.y = self.y; self.lasers.push(laser); game.addChild(laser); }; }); // 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 = 0; self.x = Math.random() * 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize score var score = 0; var scoreTxt = new Text2('0', { size: 50, fill: 0xFFFFFF, align: 'right' }); scoreTxt.x = -100; // Move the score display 100 pixels to the left LK.gui.topRight.addChild(scoreTxt); // Initialize spaceship var spaceship = new Spaceship(); spaceship.x = 2048 / 2; spaceship.y = 2732 - 200; game.addChild(spaceship); // Initialize stars var 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); } // Handle spaceship movement game.down = function (x, y, obj) { spaceship.move(x, y); spaceship.fire(); }; // Update game elements game.update = function () { for (var i = 0; i < stars.length; i++) { stars[i].update(); // Check for collision between each star and the spaceship's lasers for (var j = 0; j < spaceship.lasers.length; j++) { if (stars[i].intersects(spaceship.lasers[j])) { // Create an explosion at the star's position var explosion = new Explosion(); explosion.x = stars[i].x; explosion.y = stars[i].y; game.addChild(explosion); // Destroy the star and the laser stars[i].destroy(); spaceship.lasers[j].destroy(); // Remove the star and the laser from their respective arrays stars.splice(i, 1); spaceship.lasers.splice(j, 1); // Increase score by 10 score += 10; // Update score display scoreTxt.setText(score); break; } } } spaceship.update(); };
/****
* Classes
****/
// Explosion class
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
explosionGraphics.alpha -= 0.1;
if (explosionGraphics.alpha <= 0) {
self.destroy();
}
};
});
// Laser class
var Laser = Container.expand(function () {
var self = Container.call(this);
var laserGraphics = self.attachAsset('laser', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
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
};
self.move = function (x, y) {
self.x = x;
self.y = y;
};
self.lasers = [];
self.fire = function () {
var laser = new Laser();
laser.x = self.x;
laser.y = self.y;
self.lasers.push(laser);
game.addChild(laser);
};
});
// 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 = 0;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize score
var score = 0;
var scoreTxt = new Text2('0', {
size: 50,
fill: 0xFFFFFF,
align: 'right'
});
scoreTxt.x = -100; // Move the score display 100 pixels to the left
LK.gui.topRight.addChild(scoreTxt);
// Initialize spaceship
var spaceship = new Spaceship();
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
game.addChild(spaceship);
// Initialize stars
var 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);
}
// Handle spaceship movement
game.down = function (x, y, obj) {
spaceship.move(x, y);
spaceship.fire();
};
// Update game elements
game.update = function () {
for (var i = 0; i < stars.length; i++) {
stars[i].update();
// Check for collision between each star and the spaceship's lasers
for (var j = 0; j < spaceship.lasers.length; j++) {
if (stars[i].intersects(spaceship.lasers[j])) {
// Create an explosion at the star's position
var explosion = new Explosion();
explosion.x = stars[i].x;
explosion.y = stars[i].y;
game.addChild(explosion);
// Destroy the star and the laser
stars[i].destroy();
spaceship.lasers[j].destroy();
// Remove the star and the laser from their respective arrays
stars.splice(i, 1);
spaceship.lasers.splice(j, 1);
// Increase score by 10
score += 10;
// Update score display
scoreTxt.setText(score);
break;
}
}
}
spaceship.update();
};
asteroid. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Spaceship like in galaga. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. Straight up and down. Seen from above.
Explosion. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.