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);
self.move = function (x) {
self.x = x;
self.y = 2732 - 200;
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y;
LK.setTimeout(function () {
bullet.destroy();
}, 3000);
return bullet;
};
});
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero bullet', .5, .5);
self.isActive = true;
self.move = function () {
self.y -= 20;
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemy', 'Enemy spaceship', .5, .5);
self.move = function () {
self.y += 2;
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 asteroidGraphics = self.createAsset('asteroid', 'Asteroid', .5, .5);
self.direction = Math.random() * 2 - 1;
self.move = function () {
self.y += 5;
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 heroes = [];
var heroBullets = [];
var enemies = [];
var enemyBullets = [];
var asteroids = [];
var hero = self.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
heroes.push(hero);
stage.on('move', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(self);
hero.move(pos.x);
});
stage.on('down', function (obj) {
var bullet;
bullet = hero.shoot();
bullet.x = hero.x;
bullet.y = hero.y;
self.addChild(bullet);
heroBullets.push(bullet);
});
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++ % 100 == 0) {
for (var i = 0; i < 10; i++) {
var enemy;
enemy = new Enemy();
enemy.x = i * 180 + 150;
enemy.y = 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++ % 50 == 0) {
var asteroid;
asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = 0;
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 = asteroids.length - 1; i >= 0; i--) {
asteroids[i].move();
if (asteroids[i].y > 2732) {
asteroids[i].destroy();
asteroids.splice(i, 1);
} else if (hero.intersects(asteroids[i])) {
LK.showGameOver();
}
}
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.