var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.createAsset('star', 'Background star', .5, .5);
self.move = function () {
self.y += 1;
if (self.y > 2732) self.y = 0;
};
});
var Explosion = Container.expand(function () {
var self = Container.call(this);
self.isActive = false;
self.start = function () {
self.isActive = true;
var explosionAnimation = LK.getAsset('explosionAnimation', 'Explosion Animation', .5, .5);
self.addChild(explosionAnimation);
LK.setTimeout(function () {
self.isActive = false;
self.destroy();
}, 500);
};
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('hero', 'Hero spaceship', .5, .5);
var moveDelay = 0;
self.move = function (x, y) {
self.x += (x - self.x) * 0.3;
if (y >= 2732 * 0.6 && y <= 2732) {
self.y += (y - self.y) * 0.3;
}
};
self.shoot = function () {
var bulletSize = Math.floor(Math.random() * 3);
switch (bulletSize) {
case 0:
return new SmallHeroBullet(self.x, self.y);
case 1:
return new MediumHeroBullet(self.x, self.y);
default:
return new LargeHeroBullet(self.x, self.y);
}
};
});
var SmallHeroBullet = Container.expand(function (x, y) {
var self = Container.call(this);
var bulletGraphics = self.createAsset('smallHeroBullet', 'Small Hero bullet', .5, .5);
self.x = x;
self.y = y;
self.isActive = true;
self.move = function () {
self.y -= 20;
};
LK.setTimeout(function () {
self.destroy();
}, 3000);
});
var MediumHeroBullet = Container.expand(function (x, y) {
var self = Container.call(this);
var bulletGraphics = self.createAsset('mediumHeroBullet', 'Medium Hero bullet', .5, .5);
self.x = x;
self.y = y;
self.isActive = true;
self.move = function () {
self.y -= 20;
};
LK.setTimeout(function () {
self.destroy();
}, 3000);
});
var LargeHeroBullet = Container.expand(function (x, y) {
var self = Container.call(this);
var bulletGraphics = self.createAsset('largeHeroBullet', 'Large Hero bullet', .5, .5);
self.x = x;
self.y = y;
self.isActive = true;
self.move = function () {
self.y -= 20;
};
LK.setTimeout(function () {
self.destroy();
}, 3000);
});
var Enemy = Container.expand(function (x, y) {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemy', 'Enemy spaceship', .5, .5);
self.x = x;
self.y = y;
self.speed = 2 + Math.random();
self.move = function () {
self.y += self.speed;
if (self.y > 2732) self.destroy();
self.x += Math.sin(self.y / 50) * 5;
};
self.shoot = function () {};
});
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('enemyBullet', 'Enemy bullet', .5, .5);
self.move = function () {};
});
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var size = (Math.random() * 2 + 0.5) * 1.3 * 0.6;
var asteroidGraphics = self.createAsset('asteroid', 'Asteroid', size, size);
asteroidGraphics.scale.set(size);
self.speed = Math.random() * 5 + 1;
self.direction = Math.random() * 2 - 1;
self.move = function () {
self.y += self.speed;
self.x += self.direction;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
var fpsCounter = new Text2('FPS: 0', {
size: 50,
fill: '#ffffff',
font: 'Arial Bold'
});
LK.gui.topLeft.addChild(fpsCounter);
var scoreText = new Text2('Score: 0', {
size: 50,
fill: '#ffffff',
font: 'Arial Bold'
});
LK.gui.topLeft.addChild(scoreText);
scoreText.y = fpsCounter.height;
var frameCount = 0;
var lastTime = Date.now();
LK.on('tick', function () {
frameCount++;
var currentTime = Date.now();
var deltaTime = currentTime - lastTime;
if (deltaTime >= 1000) {
var fps = Math.round(frameCount / deltaTime * 1000);
fpsCounter.setText('FPS: ' + fps);
scoreText.setText('Score: ' + score);
frameCount = 0;
lastTime = currentTime;
}
});
var score = 0;
var lives = 3;
var heroes = [];
var heroBullets = [];
var enemies = [];
var enemyBullets = [];
var asteroids = [];
var stars = [];
for (var i = 0; i < 100; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
self.addChild(star);
stars.push(star);
}
var hero = self.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
heroes.push(hero);
var heartIcon = LK.getAsset('heart', 'Heart icon', .5, .5);
var heartIcons = [];
for (var i = 0; i < lives; i++) {
var heart = LK.getAsset('heart', 'Heart icon', .5, .5);
heart.x = 2048 / 3 - lives * heart.width / 2 + i * heart.width + 100;
heart.y = 50;
LK.gui.topLeft.addChild(heart);
heartIcons.push(heart);
}
stage.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
hero.move(pos.x, pos.y);
});
var canShoot = true;
var shooting = false;
var shootInterval;
stage.on('down', function (obj) {
shooting = true;
if (canShoot) {
var bullet;
bullet = hero.shoot();
bullet.x = hero.x;
bullet.y = hero.y;
self.addChild(bullet);
heroBullets.push(bullet);
canShoot = false;
}
shootInterval = LK.setInterval(function () {
if (shooting) {
var bullet;
bullet = hero.shoot();
bullet.x = hero.x;
bullet.y = hero.y;
self.addChild(bullet);
heroBullets.push(bullet);
}
}, 200);
});
stage.on('up', function (obj) {
shooting = false;
LK.clearInterval(shootInterval);
});
for (var i = heroBullets.length - 1; i >= 0; i--) {
if (heroBullets[i].y < 0) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
var enemySpawnTimer = 0;
var asteroidSpawnTimer = 0;
LK.on('tick', function () {
if (enemySpawnTimer++ % Math.floor(Math.random() * 400) == 0 && enemies.length < 45) {
var enemyCount = Math.floor(Math.random() * 5);
var enemySpacing = 2048 / (enemyCount + 1);
for (var i = 0; i < enemyCount; i++) {
var enemy = new Enemy((i + 1) * enemySpacing, 0);
self.addChild(enemy);
enemies.push(enemy);
}
}
for (var i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].y > 2732) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
if (asteroidSpawnTimer++ % (Math.floor(Math.random() * 70) + 28) == 0) {
var asteroid;
asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = 0;
asteroid.size = Math.random() * 1.5 + 0.5;
self.addChild(asteroid);
asteroids.push(asteroid);
}
for (var i = 0; i < enemies.length; i++) {
enemies[i].move();
if (enemies[i].y >= hero.y) {
LK.showGameOver();
}
}
for (var i = 0; i < stars.length; i++) {
stars[i].move();
}
for (var i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].move();
if (asteroids[i].y > 2732 + asteroids[i].height) {
asteroids[i].destroy();
asteroids.splice(i, 1);
} else if (hero.intersects(asteroids[i])) {
lives--;
if (heartIcons.length > 0) {
var lastHeart = heartIcons.pop();
lastHeart.destroy();
}
var explosion = new Explosion();
explosion.x = hero.x;
explosion.y = hero.y;
self.addChild(explosion);
explosion.start();
if (lives <= 0) {
LK.showGameOver();
} else {
asteroids[i].destroy();
asteroids.splice(i, 1);
}
}
}
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].move();
if (heroBullets[i].y < 0 || heroBullets[i].y > 2732) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
continue;
}
for (var n = 0; n < enemies.length; n++) {
if (heroBullets[i] && heroBullets[i].intersects(enemies[n])) {
var explosion = new Explosion();
explosion.x = enemies[n].x;
explosion.y = enemies[n].y;
self.addChild(explosion);
explosion.start();
if (enemies[n]) {
enemies[n].destroy();
enemies.splice(n, 1);
}
heroBullets[i].destroy();
heroBullets.splice(i, 1);
score += 10;
scoreText.setText('Score: ' + score);
break;
}
}
}
for (var i = 0; i < enemyBullets.length; i++) {
enemyBullets[i].move();
}
});
});
enemy space ship Single Game Texture. In-Game asset. 2d. Transparent background. High contrast. No shadows.
asteroid Single Game Texture. In-Game asset. 2d. Transparent background. High contrast. No shadows.
plasma ball Single Game Texture. In-Game asset. 2d. Transparent background. High contrast. No shadows.
explosion particle Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
space ship facing upwards Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
1 heart for lives in a video game Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
alien 1 bullet Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.