/****
* Classes
****/
var Star = Container.expand(function () {
var self = Container.call(this);
var sizeOptions = [0.5, 0.75, 1]; // Three sizes: small, medium, large
var size = sizeOptions[Math.floor(Math.random() * sizeOptions.length)];
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
starGraphics.scale.set(size, size);
starGraphics.rotation = Math.random() * Math.PI * 2;
self.x = Math.random() * 2048;
self.y = Math.random() * 2732;
self.update = function () {
// Twinkle effect logic
if (LK.ticks % (10 * 60) === 0 && Math.random() < 0.5) {
LK.effects.flashObject(self, 0xFFFFFF, 300);
}
};
});
var LaunchTrail = Container.expand(function () {
var self = Container.call(this);
var trailGraphics = self.attachAsset('launchTrail', {
anchorX: 0.5,
anchorY: 1
});
self.y = 2732;
self.x = 2048 / 2;
self.update = function () {
self.y -= 26;
if (self.y <= self.targetY) {
self.destroy();
var firework = new Firework();
firework.explode(self.x, self.targetY);
fireworks.push(firework);
}
};
self.targetY = 0;
self.setTargetY = function (y) {
self.targetY = y;
};
});
// Firework particle class
var FireworkParticle = Container.expand(function () {
var self = Container.call(this);
var particleGraphics = self.attachAsset('fireworkParticle', {
anchorX: 0.5,
anchorY: 0.5
});
self.color = 0xFFFFFF; // Default color white
particleGraphics.tint = self.color;
self.speedX = 0;
self.speedY = 0;
self.alpha = 1;
self.scaleX = 1;
self.scaleY = 1;
self.update = function () {
var gravity = 0.05;
self.speedY += gravity;
self.x += self.speedX;
self.y += self.speedY;
self.alpha *= 0.98; // fade out more slowly
self.scaleX *= 0.99;
self.scaleY *= 0.99;
if (self.alpha < 0.05) {
self.destroy();
}
};
});
// Firework class
var Firework = Container.expand(function () {
var self = Container.call(this);
self.particles = [];
self.explode = function (x, y) {
var colors = [0xFFD700, 0x0016ff, 0xFF0000, 0xFFC0CB, 0x6a00ff, 0x00c435]; // gold, blue, red, pink, deep purple, green
var randomColor = colors[Math.floor(Math.random() * colors.length)];
// Calculate a darker version of the randomColor for the background flash
game.setBackgroundColor(randomColor);
LK.setTimeout(function () {
game.setBackgroundColor(0x000000);
}, 200);
for (var i = 0; i < 100; i++) {
var particle = new FireworkParticle();
var angle = Math.random() * Math.PI * 2;
var speed = Math.random() * 3 + 2;
particle.color = randomColor; // Assign random color to particle
particle.speedX = Math.cos(angle) * speed;
particle.speedY = Math.sin(angle) * speed;
particle.x = x;
particle.y = y;
particle.attachAsset('fireworkParticle', {
anchorX: 0.5,
anchorY: 0.5
}).tint = particle.color; // Apply color tint to particle
self.particles.push(particle);
game.addChild(particle);
}
};
self.update = function () {
for (var i = self.particles.length - 1; i >= 0; i--) {
self.particles[i].update();
if (self.particles[i].alpha < 0.05) {
self.particles.splice(i, 1);
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize fireworks array
var fireworks = [];
// Initialize stars array
var stars = [];
// Spawn 20 stars instantly at the start of the game
for (var i = 0; i < 20; i++) {
var star = new Star();
stars.push(star);
game.addChild(star);
}
// Event listener for mouse or touch down
game.on('down', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
var launchTrail = new LaunchTrail();
launchTrail.x = pos.x;
launchTrail.setTargetY(pos.y);
game.addChild(launchTrail);
});
// Update function for the game
LK.on('tick', function () {
// Spawn stars randomly, but ensure the maximum number of stars does not exceed 40
if (stars.length < 40) {
var chanceToSpawnStar = 0.1; // 10% chance to spawn a star each tick
if (Math.random() < chanceToSpawnStar) {
var star = new Star();
stars.push(star);
game.addChild(star);
}
}
for (var i = fireworks.length - 1; i >= 0; i--) {
fireworks[i].update();
if (fireworks[i].particles.length === 0) {
fireworks.splice(i, 1);
}
}
for (var i = game.children.length - 1; i >= 0; i--) {
if (game.children[i] instanceof LaunchTrail) {
game.children[i].update();
}
}
});