/****
* Classes
****/
// Alien class
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
});
// 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;
};
});
// 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 = -15;
self.update = function () {
self.y += self.speed;
};
});
// NewEnemy class
var NewEnemy = Container.expand(function () {
var self = Container.call(this);
var newEnemyGraphics = self.attachAsset('new_enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
// Randomly shoot bullets less often
if (Math.random() < 0.005) {
var newEnemyBullet = new NewEnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y;
game.addChild(newEnemyBullet);
// Play the enemy_laser_shot sound
LK.getSound('enemy_laser_shot').play();
}
};
});
// NewEnemyBullet class
var NewEnemyBullet = Container.expand(function () {
var self = Container.call(this);
var newEnemyBulletGraphics = self.attachAsset('new_enemy_bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
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
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var background = game.addChild(LK.getAsset('background_image', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
scaleX: 2048 / 2000,
scaleY: 2732 / 2000
}));
// Initialize game elements
var spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
var asteroids = [];
var aliens = [];
var bullets = [];
// Game update logic
game.update = function () {
// Update spaceship
spaceship.update();
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
asteroid.update();
if (asteroid.y > 2732) {
asteroid.destroy();
asteroids.splice(i, 1);
}
}
// Update aliens
for (var j = aliens.length - 1; j >= 0; j--) {
var alien = aliens[j];
alien.update();
if (alien.y > 2732) {
alien.destroy();
aliens.splice(j, 1);
// End the game
LK.showGameOver();
}
}
// Update bullets
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
bullet.update();
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(k, 1);
}
}
// Collision detection
for (var m = bullets.length - 1; m >= 0; m--) {
var bullet = bullets[m];
for (var n = asteroids.length - 1; n >= 0; n--) {
var asteroid = asteroids[n];
if (bullet.intersects(asteroid)) {
bullet.destroy();
asteroid.destroy();
bullets.splice(m, 1);
asteroids.splice(n, 1);
// Increase the score and update the score text
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
for (var p = aliens.length - 1; p >= 0; p--) {
var alien = aliens[p];
if (bullet.intersects(alien)) {
bullet.destroy();
alien.destroy();
bullets.splice(m, 1);
aliens.splice(p, 1);
// Increase the score and update the score text
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
}
// Check if spaceship collides with an asteroid or an alien
for (var a = asteroids.length - 1; a >= 0; a--) {
var asteroid = asteroids[a];
if (spaceship.intersects(asteroid)) {
// End the game
LK.showGameOver();
break;
}
}
for (var b = aliens.length - 1; b >= 0; b--) {
var alien = aliens[b];
if (spaceship.intersects(alien)) {
// End the game
LK.showGameOver();
break;
}
}
};
// Spawn asteroids
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -50;
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn aliens
function spawnAlien() {
var enemy;
if (LK.getScore() < 10) {
enemy = new Alien();
} else {
enemy = new NewEnemy();
}
enemy.x = Math.random() * 2048;
enemy.y = -50;
aliens.push(enemy);
game.addChild(enemy);
}
// Fire bullet
function fireBullet() {
var bullet = new Bullet();
bullet.x = spaceship.x;
bullet.y = spaceship.y - 50;
bullets.push(bullet);
game.addChild(bullet);
// Play the laser_shot sound with decreased volume
LK.getSound('laser_shot').play({
volume: 0.3
});
}
// Create a score text
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
// Add the score text to the GUI overlay
LK.gui.top.addChild(scoreTxt);
// Set intervals for spawning asteroids and aliens
LK.setInterval(spawnAsteroid, 2000);
LK.setInterval(spawnAlien, 5000);
// Play the space_background_music
LK.playMusic('space_background_music', {
id: 'space_background_music_id'
});
// Handle spaceship movement
game.move = function (x, y, obj) {
spaceship.x = x;
};
// Handle firing bullets
game.down = function (x, y, obj) {
fireBullet();
}; /****
* Classes
****/
// Alien class
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
});
// 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;
};
});
// 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 = -15;
self.update = function () {
self.y += self.speed;
};
});
// NewEnemy class
var NewEnemy = Container.expand(function () {
var self = Container.call(this);
var newEnemyGraphics = self.attachAsset('new_enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
// Randomly shoot bullets less often
if (Math.random() < 0.005) {
var newEnemyBullet = new NewEnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y;
game.addChild(newEnemyBullet);
// Play the enemy_laser_shot sound
LK.getSound('enemy_laser_shot').play();
}
};
});
// NewEnemyBullet class
var NewEnemyBullet = Container.expand(function () {
var self = Container.call(this);
var newEnemyBulletGraphics = self.attachAsset('new_enemy_bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
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
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var background = game.addChild(LK.getAsset('background_image', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
scaleX: 2048 / 2000,
scaleY: 2732 / 2000
}));
// Initialize game elements
var spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 200;
var asteroids = [];
var aliens = [];
var bullets = [];
// Game update logic
game.update = function () {
// Update spaceship
spaceship.update();
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
asteroid.update();
if (asteroid.y > 2732) {
asteroid.destroy();
asteroids.splice(i, 1);
}
}
// Update aliens
for (var j = aliens.length - 1; j >= 0; j--) {
var alien = aliens[j];
alien.update();
if (alien.y > 2732) {
alien.destroy();
aliens.splice(j, 1);
// End the game
LK.showGameOver();
}
}
// Update bullets
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
bullet.update();
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(k, 1);
}
}
// Collision detection
for (var m = bullets.length - 1; m >= 0; m--) {
var bullet = bullets[m];
for (var n = asteroids.length - 1; n >= 0; n--) {
var asteroid = asteroids[n];
if (bullet.intersects(asteroid)) {
bullet.destroy();
asteroid.destroy();
bullets.splice(m, 1);
asteroids.splice(n, 1);
// Increase the score and update the score text
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
for (var p = aliens.length - 1; p >= 0; p--) {
var alien = aliens[p];
if (bullet.intersects(alien)) {
bullet.destroy();
alien.destroy();
bullets.splice(m, 1);
aliens.splice(p, 1);
// Increase the score and update the score text
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
}
// Check if spaceship collides with an asteroid or an alien
for (var a = asteroids.length - 1; a >= 0; a--) {
var asteroid = asteroids[a];
if (spaceship.intersects(asteroid)) {
// End the game
LK.showGameOver();
break;
}
}
for (var b = aliens.length - 1; b >= 0; b--) {
var alien = aliens[b];
if (spaceship.intersects(alien)) {
// End the game
LK.showGameOver();
break;
}
}
};
// Spawn asteroids
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -50;
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn aliens
function spawnAlien() {
var enemy;
if (LK.getScore() < 10) {
enemy = new Alien();
} else {
enemy = new NewEnemy();
}
enemy.x = Math.random() * 2048;
enemy.y = -50;
aliens.push(enemy);
game.addChild(enemy);
}
// Fire bullet
function fireBullet() {
var bullet = new Bullet();
bullet.x = spaceship.x;
bullet.y = spaceship.y - 50;
bullets.push(bullet);
game.addChild(bullet);
// Play the laser_shot sound with decreased volume
LK.getSound('laser_shot').play({
volume: 0.3
});
}
// Create a score text
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
// Add the score text to the GUI overlay
LK.gui.top.addChild(scoreTxt);
// Set intervals for spawning asteroids and aliens
LK.setInterval(spawnAsteroid, 2000);
LK.setInterval(spawnAlien, 5000);
// Play the space_background_music
LK.playMusic('space_background_music', {
id: 'space_background_music_id'
});
// Handle spaceship movement
game.move = function (x, y, obj) {
spaceship.x = x;
};
// Handle firing bullets
game.down = function (x, y, obj) {
fireBullet();
};
Brown rocky asteroid. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Space rocket with weapons. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
evil green alien in UFO spaceship. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
dangerous enemy spaceship with weapons. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
deep dark space with stars and very few planets. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.