/****
* Classes
****/
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = Math.max(y, 2732 / 3 * 2);
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - self.height;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
var Asteroid = Container.expand(function (size) {
var self = Container.call(this);
self.size = size || 1;
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: self.size,
scaleY: self.size
});
self.speed = (Math.random() * 2 + 1) * 2;
self.move = function () {
self.y += self.speed;
};
});
var Gold = Container.expand(function () {
var self = Container.call(this);
var goldGraphics = self.attachAsset('gold', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = (Math.random() * 2 + 1) * 2;
self.move = function () {
self.y += self.speed;
};
});
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.move = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.topLeft.addChild(scoreTxt);
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
var asteroids = [];
var goldPieces = [];
var heroBullets = [];
var score = 0;
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.move(pos.x, pos.y);
hero.shoot();
});
LK.on('tick', function () {
// Move hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].move();
if (heroBullets[i].y < 0) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Move asteroids and deduct points if they reach the bottom
for (var j = asteroids.length - 1; j >= 0; j--) {
asteroids[j].move();
if (asteroids[j].y > 2732) {
score -= 20;
scoreTxt.setText(score.toString()); // Update score text immediately
asteroids[j].destroy();
asteroids.splice(j, 1);
if (score <= 0) {
LK.showGameOver();
}
}
}
// Move gold and check for collisions with hero
for (var k = goldPieces.length - 1; k >= 0; k--) {
goldPieces[k].move();
if (goldPieces[k].intersects(hero)) {
score += 50;
goldPieces[k].destroy();
goldPieces.splice(k, 1);
} else if (goldPieces[k].y > 2732) {
score -= 20;
scoreTxt.setText(score.toString()); // Update score text immediately
goldPieces[k].destroy();
goldPieces.splice(k, 1);
if (score <= 0) {
LK.showGameOver();
}
}
}
// Spawn asteroids with random sizes
if (LK.ticks % 120 === 0) {
var size = Math.random() * 1.5 + 2; // Random size between 2 and 3.5
var asteroid = new Asteroid(size);
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height * size;
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn gold
if (LK.ticks % 300 === 0) {
var gold = new Gold();
gold.x = Math.random() * 2048;
gold.y = -gold.height;
goldPieces.push(gold);
game.addChild(gold);
}
// Check for bullet-asteroid collisions
for (var b = heroBullets.length - 1; b >= 0; b--) {
for (var a = asteroids.length - 1; a >= 0; a--) {
if (heroBullets[b].intersects(asteroids[a])) {
heroBullets[b].destroy();
asteroids[a].destroy();
score += 10; // Update score for destroying an asteroid
scoreTxt.setText(score.toString()); // Update score text
heroBullets.splice(b, 1);
asteroids.splice(a, 1);
break;
}
}
}
});
Space rock. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Space rock with encrusted gold. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Top down view of Small personal space fighter with large gun at the front. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
ball of fire moving up at very high speed. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Horizon and atmosphere of a planet as seen from very low orbit.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.