/****
* Classes
****/
var AsteroidBelt = Container.expand(function () {
var self = Container.call(this);
self.asteroids = [];
self.spawnAsteroids = function () {
for (var i = 0; i < 4 + Math.random() * 6; i++) {
var asteroid = new Enemy1();
asteroid.x = Math.random() * 2048;
asteroid.y = -50 - i * 100; // Stagger the asteroids vertically
self.asteroids.push(asteroid);
game.addChild(asteroid);
enemies.push(asteroid);
}
};
self.update = function () {
for (var i = self.asteroids.length - 1; i >= 0; i--) {
self.asteroids[i].update();
if (self.asteroids[i].y > 2732 + 50) {
self.asteroids[i].destroy();
self.asteroids.splice(i, 1);
}
}
};
});
// Blink effect class
var BlinkEffect = Container.expand(function () {
var self = Container.call(this);
self.blink = function (target, times, duration) {
var blinkInterval = duration / (times * 2);
var blinkCount = 0;
var blinkTimer = LK.setInterval(function () {
target.alpha = target.alpha === 1 ? 0 : 1;
blinkCount++;
if (blinkCount >= times * 2) {
LK.clearInterval(blinkTimer);
target.alpha = 1; // Ensure the target is visible at the end
}
}, blinkInterval);
};
});
var BossBullet = Container.expand(function (owner) {
var self = Container.call(this);
self.owner = owner;
var bulletGraphics = self.attachAsset('bossBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5; // Speed of the boss bullet
self.update = function () {
self.y += self.speed;
self.rotation += 0.1; // Add rotation effect
// Destroy bullet if it goes off screen
if (self.y > 2732 + 50) {
self.destroy();
bossBullets.splice(bossBullets.indexOf(self), 1);
}
};
});
var BossEnemy = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss1', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.health = 100; // Boss health
self.barWidth = 600;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0,
anchorY: 1.0,
x: -self.barWidth / 2,
y: -bossGraphics.height / 2 - 15 // Position the health bar above the boss
});
self.shootingPeriod = true; // Initialize shooting period
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer > 300) {
// Switch period every 5 seconds (300 ticks)
self.shootingPeriod = !self.shootingPeriod;
self.shootingTimer = 0;
}
self.healthBar.width = self.health / 100 * self.barWidth; // Update health bar width based on health
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
// Boss shooting logic
if (self.shootingPeriod && LK.ticks % 60 == 0) {
// Shoot every second
var bossBullet = new BossBullet(self);
bossBullet.x = self.x;
bossBullet.y = self.y + bossGraphics.height / 2;
game.addChild(bossBullet);
bossBullets.push(bossBullet);
}
};
self.takeDamage = function (damage) {
self.health -= damage;
var redBoss = self.attachAsset('boss1', {
anchorX: 0.5,
anchorY: 0.5
});
redBoss.tint = 0xff0000;
redBoss.alpha = 0;
var blinkEffect = new BlinkEffect();
blinkEffect.blink(redBoss, 3, 500);
LK.setTimeout(function () {
redBoss.destroy();
}, 500);
if (self.health <= 0) {
enemies.splice(enemies.indexOf(self), 1);
// Remove all boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].destroy();
bossBullets.splice(i, 1);
}
self.destroy();
// Stop the boss background music
bossBackgroundMusic.stop();
// Resume the regular background music
backgroundMusic.play();
}
};
});
var BossEnemy2 = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss2', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.health = 150; // Boss health
self.barWidth = 600;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0,
anchorY: 1.0,
x: -self.barWidth / 2,
y: -bossGraphics.height / 2 - 15 // Position the health bar above the boss
});
self.shootingPeriod = true; // Initialize shooting period
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer > 300) {
// Switch period every 5 seconds (300 ticks)
self.shootingPeriod = !self.shootingPeriod;
self.shootingTimer = 0;
}
self.healthBar.width = self.health / 150 * self.barWidth; // Update health bar width based on health
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
// Boss shooting logic
if (self.shootingPeriod && LK.ticks % 60 == 0) {
// Shoot every second
var bossBullet = new BossBullet();
bossBullet.x = self.x;
bossBullet.y = self.y + bossGraphics.height / 2;
game.addChild(bossBullet);
bossBullets.push(bossBullet);
}
};
self.takeDamage = function (damage) {
self.health -= damage;
var redBoss = self.attachAsset('boss2', {
anchorX: 0.5,
anchorY: 0.5
});
redBoss.tint = 0xff0000;
redBoss.alpha = 0;
var blinkEffect = new BlinkEffect();
blinkEffect.blink(redBoss, 3, 500);
LK.setTimeout(function () {
redBoss.destroy();
}, 500);
if (self.health <= 0) {
enemies.splice(enemies.indexOf(self), 1);
// Remove all boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].destroy();
bossBullets.splice(i, 1);
}
self.destroy();
// Stop the boss background music
bossBackgroundMusic.stop();
// Resume the regular background music
backgroundMusic.play();
}
};
});
var BossEnemy3 = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss3', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.health = 200; // Boss health
self.barWidth = 600;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0,
anchorY: 1.0,
x: -self.barWidth / 2,
y: -bossGraphics.height / 2 - 15 // Position the health bar above the boss
});
self.shootingPeriod = true; // Initialize shooting period
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer > 300) {
// Switch period every 5 seconds (300 ticks)
self.shootingPeriod = !self.shootingPeriod;
self.shootingTimer = 0;
}
self.healthBar.width = self.health / 200 * self.barWidth; // Update health bar width based on health
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
// Boss shooting logic
if (self.shootingPeriod && LK.ticks % 60 == 0) {
// Shoot every second
var bossBullet = new BossBullet();
bossBullet.x = self.x;
bossBullet.y = self.y + bossGraphics.height / 2;
game.addChild(bossBullet);
bossBullets.push(bossBullet);
}
};
self.takeDamage = function (damage) {
self.health -= damage;
var redBoss = self.attachAsset('boss3', {
anchorX: 0.5,
anchorY: 0.5
});
redBoss.tint = 0xff0000;
redBoss.alpha = 0;
var blinkEffect = new BlinkEffect();
blinkEffect.blink(redBoss, 3, 500);
LK.setTimeout(function () {
redBoss.destroy();
}, 500);
if (self.health <= 0) {
enemies.splice(enemies.indexOf(self), 1);
// Remove all boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].destroy();
bossBullets.splice(i, 1);
}
self.destroy();
// Stop the boss background music
bossBackgroundMusic.stop();
// Resume the regular background music
backgroundMusic.play();
}
};
});
//<Assets used in the game will automatically appear here>
// Bullet class
var Bullet = Container.expand(function (owner) {
var self = Container.call(this);
self.owner = owner;
var bulletGraphics = self.attachAsset(owner instanceof Player ? 'bullet' : 'enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10 - Math.random() * 5; // Random speed between -10 and -15
self.update = function () {
self.y += self.speed;
if (LK.ticks % 8 != 0) {
return;
}
// Create particle trail effect with varying colors and sizes
var colors = [0xff0000, 0x00ff00, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 0.8 + Math.random() * 1.5;
for (var i = 0; i < 1; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + (Math.random() - 0.5) * 15,
y: self.y + 50 + (Math.random() - 0.5) * 15,
alpha: 1,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.02; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChildAt(trailParticle, game.getChildIndex(self));
}
};
});
// Enemy class
var Enemy1 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy1', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3 + Math.random() * 3; // Random speed between 5 and 10
self.update = function () {
self.y += self.speed + LK.ticks / 10000; // Increase speed over time
};
});
var Enemy2 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy2', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5 + Math.random() * 5; // Random speed between 5 and 10
self.update = function () {
self.y += self.speed + LK.ticks / 10000; // Increase speed over time
self.x += Math.sin(LK.ticks / 100) * 5; // Add a sine wave movement pattern
};
});
var Enemy3 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy3', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5 + Math.random() * 5; // Random speed between 5 and 10
self.update = function () {
self.y += self.speed + LK.ticks / 10000; // Increase speed over time
self.x += Math.cos(LK.ticks / 100) * 5; // Add a cosine wave movement pattern
};
});
// Explosion class
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
// Initialize particles array
self.particles = [];
// Create particles
for (var i = 0; i < 50; i++) {
var particle = self.attachAsset('explosionParticle', {
anchorX: 0.5,
anchorY: 0.5,
x: Math.random() * explosionGraphics.width - explosionGraphics.width / 2,
y: Math.random() * explosionGraphics.height - explosionGraphics.height / 2
});
particle.speedX = Math.random() * 2 - 1;
particle.speedY = Math.random() * 2 - 1;
self.particles.push(particle);
}
self.update = function () {
self.alpha -= 0.02; // Fade out
explosionGraphics.scale.x += 0.05; // Scale up faster
explosionGraphics.scale.y += 0.05; // Scale up faster
// Update particles
for (var i = self.particles.length - 1; i >= 0; i--) {
self.particles[i].x += self.particles[i].speedX * 5; // Increase speed
self.particles[i].y += self.particles[i].speedY * 5; // Increase speed
self.particles[i].rotation += 0.2; // Add rotation
self.particles[i].alpha -= 0.04;
if (self.particles[i].alpha <= 0) {
self.particles[i].destroy();
self.particles.splice(i, 1);
}
}
if (self.alpha <= 0) {
self.destroy();
}
};
});
// FloatingEnemy class
var FloatingEnemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('floatingEnemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer >= 180) {
// Shoot every 3 seconds (180 ticks)
var enemyBullet = new Bullet(self);
enemyBullet.x = self.x;
enemyBullet.y = self.y + enemyGraphics.height / 2;
enemyBullet.speed = 5; // Set bullet speed
game.addChild(enemyBullet);
bullets.push(enemyBullet);
self.shootingTimer = 0; // Reset shooting timer
}
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
console.log("enemy out of top");
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
self.baseSpeed = 10;
self.speed = self.baseSpeed;
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.shoot = function () {
if (game.isLevelingUp) {
return;
}
var bullet1 = new Bullet();
bullet1.x = self.x - 20; // Adjust position for dual cannon
bullet1.y = self.y - playerGraphics.height / 2;
bullets.push(bullet1);
game.addChild(bullet1);
if (powerUpActive) {
var bullet2 = new Bullet();
bullet2.x = self.x + 20; // Adjust position for dual cannon
bullet2.y = self.y - playerGraphics.height / 2;
bullets.push(bullet2);
game.addChild(bullet2);
}
// Play shooting sound
LK.getSound('shootingSound').play();
// Add shooting effect
var shootingEffect = self.attachAsset('shootingEffect', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: -playerGraphics.height / 2
});
var fadeOutInterval = LK.setInterval(function () {
shootingEffect.alpha -= 0.1;
if (shootingEffect.alpha <= 0) {
LK.clearInterval(fadeOutInterval);
shootingEffect.destroy();
}
}, 20);
// Add bounceback effect
self.y += 20; // Move player down by 20 pixels
LK.setTimeout(function () {
self.y = 2732 - 200; // Reset player y position to main position
}, 100);
// Add particle trail effect with varying colors and sizes
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 4 + Math.random() * 4;
for (var i = 0; i < 3; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + (Math.random() - 0.5) * 15,
y: self.y + 50 + (Math.random() - 0.5) * 15,
alpha: 0.8,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.05; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChildAt(trailParticle, 0);
}
};
});
// PowerUpCannons class
var PowerUpCannons = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp_cannons', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5;
};
});
// PowerUpSpeed class
var PowerUpSpeed = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp_speed', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5;
};
});
// Progress Bar class
var ProgressBar = Container.expand(function () {
var self = Container.call(this);
var progressBarGraphics = self.attachAsset('progressBar', {
anchorX: 0.0,
anchorY: 0.0
});
self.lastLevelUp = 0;
self.reset = function () {
progressBarGraphics.width = 0;
self.lastLevelUp = LK.ticks;
};
self.update = function () {
progressBarGraphics.width = (LK.ticks - self.lastLevelUp) / 1000 * 2048; // Fill the progress bar over time
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
starGraphics.scale.set(1 + .2 * Math.random());
self.speed = 0.5 + Math.random() * 1.5; // Random speed between 0.5 and 2
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + 50) {
self.y = -50;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var bossBackgroundMusic = LK.getSound('bossBackgroundMusic');
var asteroidBelt; // Initialize asteroidBelt variable
// Play background music when the game starts
var backgroundMusic = LK.getSound('backgroundMusic');
backgroundMusic.loop = true;
backgroundMusic.play();
// Add initial text 'Tap to shoot and turn!' when the game starts
var initialText = new Text2('Tap to shoot and turn!', {
size: 150,
fill: "#ffffff"
});
initialText.anchor.set(0.5, 0.5);
initialText.x = 2048 / 2;
initialText.y = 2732 / 2 + 500;
game.addChild(initialText);
// Remove the initial text after 3 seconds
LK.setTimeout(function () {
initialText.destroy();
}, 3000);
// Initialize stars array
var stars = [];
// Add stars to the game
for (var i = 0; i < 100; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
// Initialize arrays and variables
var bullets = [];
var enemies = [];
var bossBullets = []; // Initialize bossBullets array
var powerUpActive = 0; // Initialize powerUpActive variable
var speedPowerUpActive = 0;
var enemySpawnCounter = 0; // Initialize enemy spawn counter
var powerUps = []; // Initialize powerUps array
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
var playerDirection = 1;
// Score display
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var level = 1;
// Initialize progress bar
var progressBar = game.addChild(new ProgressBar());
progressBar.y = 0;
var isMouseDown = false;
// Event listener for mouse down
game.down = function (x, y, obj) {
player.shoot();
playerDirection *= -1;
isMouseDown = true;
};
// Event listener for mouse up
game.up = function (x, y, obj) {
isMouseDown = false;
};
var speedPowerUpMultiplier = .2;
// Update game state
game.update = function () {
progressBar.update();
// Update player's position with speed boost if power-up is active
//if (isMouseDown) {
if (!game.isLevelingUp) {
player.x += (player.speed + speedPowerUpActive * speedPowerUpMultiplier * player.speed) * playerDirection;
}
// }
// Wrap player's position around the screen
if (player.x > 2048 + player.width / 2) {
player.x = -player.width / 2;
} else if (player.x < -player.width / 2) {
player.x = 2048 + player.width / 2;
// Change direction if player hits the edge
}
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 0.8 + Math.random() * 1.5;
for (var i = 0; i < 3; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: player.x + (Math.random() - 0.5) * 15,
y: player.y + 50 + (Math.random() - 0.5) * 15,
alpha: 0.8,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.05; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChildAt(trailParticle, game.getChildIndex(player));
}
if (speedPowerUpActive) {
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 0.8 + Math.random() * 1.5;
for (var i = 0; i < 3; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: player.x + (Math.random() - 0.5) * 15,
y: player.y + 50 + (Math.random() - 0.5) * 15,
alpha: 0.8,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.05; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChild(trailParticle);
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < -50 || bullets[i].y > 2732 + 50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Update boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].update();
if (bossBullets[i] && bossBullets[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (enemies[j].y > 2732 + 50) {
enemies[j].destroy();
enemies.splice(j, 1);
}
}
// Check for collisions
for (var k = bullets.length - 1; k >= 0; k--) {
for (var l = enemies.length - 1; l >= 0; l--) {
if (bullets[k].intersects(enemies[l]) && bullets[k].owner !== enemies[l] && !(bullets[k].owner instanceof Enemy1 || bullets[k].owner instanceof Enemy2 || bullets[k].owner instanceof Enemy3 || bullets[k].owner instanceof FloatingEnemy || bullets[k].owner instanceof BossEnemy || bullets[k].owner instanceof BossEnemy2 || bullets[k].owner instanceof BossEnemy3)) {
if (enemies[l] instanceof BossEnemy || enemies[l] instanceof BossEnemy2 || enemies[l] instanceof BossEnemy3) {
enemies[l].takeDamage(10); // Boss takes 10 damage per bullet
bullets[k].destroy();
bullets.splice(k, 1);
break;
}
var explosion = new Explosion();
explosion.x = enemies[l].x;
explosion.y = enemies[l].y;
game.addChild(explosion);
bullets[k].destroy();
// Play explosion sound
LK.getSound('explosionSound').play();
if (enemies[l] instanceof Enemy1) {
enemies[l].destroy();
score += 1;
} else if (enemies[l] instanceof Enemy2) {
enemies[l].destroy();
score += 2;
} else if (enemies[l] instanceof Enemy3) {
enemies[l].destroy();
score += 3;
} else if (enemies[l] instanceof FloatingEnemy) {
enemies[l].destroy();
score += 4;
}
bullets.splice(k, 1);
enemies.splice(l, 1);
scoreTxt.setText(score);
break;
}
}
}
// Update powerUps
for (var i = powerUps.length - 1; i >= 0; i--) {
powerUps[i].update();
if (powerUps[i].y > 2732 + 50) {
powerUps[i].destroy();
powerUps.splice(i, 1);
}
}
// Spawn asteroid belt occasionally
if (LK.ticks % 600 == 500 && progressBar.width < 2040) {
// Every 10 seconds
asteroidBelt = new AsteroidBelt();
asteroidBelt.spawnAsteroids();
}
// Spawn enemies and occasionally power-ups
if (LK.ticks % 60 == 0 && progressBar.width < 2040) {
var enemyType = Math.floor(Math.random() * 4) + 1; // Randomly select an enemy type
var enemy;
switch (enemyType) {
case 1:
enemy = new Enemy1();
break;
case 2:
enemy = new Enemy2();
break;
case 3:
enemy = new Enemy3();
break;
case 4:
enemy = new FloatingEnemy();
break;
}
enemy.x = Math.random() * 2048;
enemy.y = -50;
enemies.push(enemy);
game.addChild(enemy);
enemySpawnCounter++;
}
// Spawn a power-up every 10th enemy with cooldown mechanism
if (enemySpawnCounter % 10 == 0 && progressBar.width < 2048 && !game.powerUpCooldown) {
var powerUpType = Math.random() < 0.5 ? PowerUpSpeed : PowerUpCannons; // Randomly select power-up type
var powerUp = new powerUpType();
powerUp.x = Math.random() * 2048;
powerUp.y = -50;
powerUps.push(powerUp);
game.addChild(powerUp);
game.powerUpCooldown = true;
LK.setTimeout(function () {
game.powerUpCooldown = false;
}, 10000); // 10 seconds cooldown
}
// Check for powerUp collision with player
for (var n = powerUps.length - 1; n >= 0; n--) {
if (powerUps[n].intersects(player)) {
if (powerUps[n] instanceof PowerUpSpeed) {
player.speed = player.baseSpeed * 1.25; // Double the player's speed
speedPowerUpActive = 1;
LK.setTimeout(function () {
player.speed = player.baseSpeed; // Reset speed after 10 seconds
speedPowerUpActive = 0;
}, 10000);
} else if (powerUps[n] instanceof PowerUpCannons) {
powerUpActive = 1; // Activate dual cannons
LK.setTimeout(function () {
powerUpActive = 0; // Deactivate dual cannons after 10 seconds
}, 10000);
}
powerUps[n].destroy();
powerUps.splice(n, 1);
}
}
// Update asteroid belt
if (asteroidBelt) {
asteroidBelt.update();
}
// Update stars
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].update();
}
// Check if the progress bar is full
if (game.isLevelingUp) {
// Speed boost effect for stars
game.starSpeedIncrease++;
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].speed++; // Increase speed by 5 times
}
}
if (progressBar.width >= 2048 && enemies.length == 0 && !game.isLevelingUp) {
if (!game.hasBossThisLevel) {
// Stop the regular background music
backgroundMusic.stop();
// Play the boss background music
bossBackgroundMusic.loop = true;
bossBackgroundMusic.play();
var bossType = level % 3; // Cycle through 3 different bosses
var bossEnemy;
switch (bossType) {
case 0:
bossEnemy = new BossEnemy();
break;
case 1:
bossEnemy = new BossEnemy2();
break;
case 2:
bossEnemy = new BossEnemy3();
break;
}
bossEnemy.x = 2048 / 2;
bossEnemy.y = -50;
enemies.push(bossEnemy);
game.addChild(bossEnemy);
game.hasBossThisLevel = true;
return;
}
game.isLevelingUp = true;
game.starSpeedIncrease = 0;
var initialPlayerY = player.y;
var targetPlayerY = -player.height; // Move player out of the top of the screen
var levelUpDuration = 3000; // 3 seconds
var overlay = new Container();
var overlayGraphics = overlay.attachAsset('progressBar', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
alpha: 0
});
overlayGraphics.width = overlayGraphics.height = 3000;
game.addChild(overlay);
// Move player upwards and fade in overlay
var moveUpInterval = LK.setInterval(function () {
player.y -= 300 / (levelUpDuration / 60); // Move up in 60 frames per second
player.x += (2048 / 2 - player.x) / (levelUpDuration / 60); // Move X position to center
}, 1000 / 60);
// Reset player position after moving up
LK.setTimeout(function () {
LK.clearInterval(moveUpInterval);
var secondMoveUpInterval = LK.setInterval(function () {
overlayGraphics.alpha += 1 / (levelUpDuration / 60); // Fade in overlay
}, 500 / 60);
LK.setTimeout(function () {
LK.clearInterval(secondMoveUpInterval);
player.y = initialPlayerY;
player.x = 2048 / 2; // Reset X position to center
overlayGraphics.destroy(); // Remove overlay
// Blink player 3 times when a new level starts
var blinkEffect = new BlinkEffect();
blinkEffect.blink(player, 3, 1000); // Blink 3 times over 1 second
}, levelUpDuration);
}, levelUpDuration);
game.isLevelingUp = true;
game.starSpeedIncrease = 0;
// Reset star speed after 3 seconds
LK.setTimeout(function () {
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].speed -= game.starSpeedIncrease; // Reset speed to original
}
// Increase the player's speed
player.speed += 1;
// List of nice colors
var colors = [0x000080, 0x008000, 0x800000, 0x808000, 0x800080, 0x008080];
// Select a random color from the list
var randomColor = colors[Math.floor(Math.random() * colors.length)];
// Change the background color
game.setBackgroundColor(randomColor);
// Show a popup with fade-in and fade-out effects
var levelTextBackground = new Container();
var levelText = new Text2('Level ' + (level + 1), {
size: 300,
fill: "#ffffff"
});
levelText.anchor.set(0.5, 0.5);
levelTextBackground.addChild(levelText);
levelTextBackground.x = 2048 / 2;
levelTextBackground.y = 2732 / 2;
levelTextBackground.alpha = 0; // Start with invisible
game.addChild(levelTextBackground);
// Fade-in effect
var fadeInInterval = LK.setInterval(function () {
levelTextBackground.alpha += 0.05;
if (levelTextBackground.alpha >= 1) {
LK.clearInterval(fadeInInterval);
}
}, 50);
// Remove the text after 2 seconds with fade-out effect
LK.setTimeout(function () {
var fadeOutInterval = LK.setInterval(function () {
levelTextBackground.alpha -= 0.05;
if (levelTextBackground.alpha <= 0) {
LK.clearInterval(fadeOutInterval);
levelTextBackground.destroy();
}
}, 50);
}, 2000);
level++;
// Reset the progress bar
progressBar.reset();
game.hasBossThisLevel = false;
game.isLevelingUp = false;
player.y = initialPlayerY;
}, 7000);
// Play a level up sound
LK.getSound('levelUp').play();
}
// Check for game over
for (var m = enemies.length - 1; m >= 0; m--) {
if (enemies[m].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}; /****
* Classes
****/
var AsteroidBelt = Container.expand(function () {
var self = Container.call(this);
self.asteroids = [];
self.spawnAsteroids = function () {
for (var i = 0; i < 4 + Math.random() * 6; i++) {
var asteroid = new Enemy1();
asteroid.x = Math.random() * 2048;
asteroid.y = -50 - i * 100; // Stagger the asteroids vertically
self.asteroids.push(asteroid);
game.addChild(asteroid);
enemies.push(asteroid);
}
};
self.update = function () {
for (var i = self.asteroids.length - 1; i >= 0; i--) {
self.asteroids[i].update();
if (self.asteroids[i].y > 2732 + 50) {
self.asteroids[i].destroy();
self.asteroids.splice(i, 1);
}
}
};
});
// Blink effect class
var BlinkEffect = Container.expand(function () {
var self = Container.call(this);
self.blink = function (target, times, duration) {
var blinkInterval = duration / (times * 2);
var blinkCount = 0;
var blinkTimer = LK.setInterval(function () {
target.alpha = target.alpha === 1 ? 0 : 1;
blinkCount++;
if (blinkCount >= times * 2) {
LK.clearInterval(blinkTimer);
target.alpha = 1; // Ensure the target is visible at the end
}
}, blinkInterval);
};
});
var BossBullet = Container.expand(function (owner) {
var self = Container.call(this);
self.owner = owner;
var bulletGraphics = self.attachAsset('bossBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5; // Speed of the boss bullet
self.update = function () {
self.y += self.speed;
self.rotation += 0.1; // Add rotation effect
// Destroy bullet if it goes off screen
if (self.y > 2732 + 50) {
self.destroy();
bossBullets.splice(bossBullets.indexOf(self), 1);
}
};
});
var BossEnemy = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss1', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.health = 100; // Boss health
self.barWidth = 600;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0,
anchorY: 1.0,
x: -self.barWidth / 2,
y: -bossGraphics.height / 2 - 15 // Position the health bar above the boss
});
self.shootingPeriod = true; // Initialize shooting period
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer > 300) {
// Switch period every 5 seconds (300 ticks)
self.shootingPeriod = !self.shootingPeriod;
self.shootingTimer = 0;
}
self.healthBar.width = self.health / 100 * self.barWidth; // Update health bar width based on health
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
// Boss shooting logic
if (self.shootingPeriod && LK.ticks % 60 == 0) {
// Shoot every second
var bossBullet = new BossBullet(self);
bossBullet.x = self.x;
bossBullet.y = self.y + bossGraphics.height / 2;
game.addChild(bossBullet);
bossBullets.push(bossBullet);
}
};
self.takeDamage = function (damage) {
self.health -= damage;
var redBoss = self.attachAsset('boss1', {
anchorX: 0.5,
anchorY: 0.5
});
redBoss.tint = 0xff0000;
redBoss.alpha = 0;
var blinkEffect = new BlinkEffect();
blinkEffect.blink(redBoss, 3, 500);
LK.setTimeout(function () {
redBoss.destroy();
}, 500);
if (self.health <= 0) {
enemies.splice(enemies.indexOf(self), 1);
// Remove all boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].destroy();
bossBullets.splice(i, 1);
}
self.destroy();
// Stop the boss background music
bossBackgroundMusic.stop();
// Resume the regular background music
backgroundMusic.play();
}
};
});
var BossEnemy2 = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss2', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.health = 150; // Boss health
self.barWidth = 600;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0,
anchorY: 1.0,
x: -self.barWidth / 2,
y: -bossGraphics.height / 2 - 15 // Position the health bar above the boss
});
self.shootingPeriod = true; // Initialize shooting period
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer > 300) {
// Switch period every 5 seconds (300 ticks)
self.shootingPeriod = !self.shootingPeriod;
self.shootingTimer = 0;
}
self.healthBar.width = self.health / 150 * self.barWidth; // Update health bar width based on health
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
// Boss shooting logic
if (self.shootingPeriod && LK.ticks % 60 == 0) {
// Shoot every second
var bossBullet = new BossBullet();
bossBullet.x = self.x;
bossBullet.y = self.y + bossGraphics.height / 2;
game.addChild(bossBullet);
bossBullets.push(bossBullet);
}
};
self.takeDamage = function (damage) {
self.health -= damage;
var redBoss = self.attachAsset('boss2', {
anchorX: 0.5,
anchorY: 0.5
});
redBoss.tint = 0xff0000;
redBoss.alpha = 0;
var blinkEffect = new BlinkEffect();
blinkEffect.blink(redBoss, 3, 500);
LK.setTimeout(function () {
redBoss.destroy();
}, 500);
if (self.health <= 0) {
enemies.splice(enemies.indexOf(self), 1);
// Remove all boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].destroy();
bossBullets.splice(i, 1);
}
self.destroy();
// Stop the boss background music
bossBackgroundMusic.stop();
// Resume the regular background music
backgroundMusic.play();
}
};
});
var BossEnemy3 = Container.expand(function () {
var self = Container.call(this);
var bossGraphics = self.attachAsset('boss3', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.health = 200; // Boss health
self.barWidth = 600;
self.healthBar = self.attachAsset('bossHealthBar', {
anchorX: 0,
anchorY: 1.0,
x: -self.barWidth / 2,
y: -bossGraphics.height / 2 - 15 // Position the health bar above the boss
});
self.shootingPeriod = true; // Initialize shooting period
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer > 300) {
// Switch period every 5 seconds (300 ticks)
self.shootingPeriod = !self.shootingPeriod;
self.shootingTimer = 0;
}
self.healthBar.width = self.health / 200 * self.barWidth; // Update health bar width based on health
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
// Boss shooting logic
if (self.shootingPeriod && LK.ticks % 60 == 0) {
// Shoot every second
var bossBullet = new BossBullet();
bossBullet.x = self.x;
bossBullet.y = self.y + bossGraphics.height / 2;
game.addChild(bossBullet);
bossBullets.push(bossBullet);
}
};
self.takeDamage = function (damage) {
self.health -= damage;
var redBoss = self.attachAsset('boss3', {
anchorX: 0.5,
anchorY: 0.5
});
redBoss.tint = 0xff0000;
redBoss.alpha = 0;
var blinkEffect = new BlinkEffect();
blinkEffect.blink(redBoss, 3, 500);
LK.setTimeout(function () {
redBoss.destroy();
}, 500);
if (self.health <= 0) {
enemies.splice(enemies.indexOf(self), 1);
// Remove all boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].destroy();
bossBullets.splice(i, 1);
}
self.destroy();
// Stop the boss background music
bossBackgroundMusic.stop();
// Resume the regular background music
backgroundMusic.play();
}
};
});
//<Assets used in the game will automatically appear here>
// Bullet class
var Bullet = Container.expand(function (owner) {
var self = Container.call(this);
self.owner = owner;
var bulletGraphics = self.attachAsset(owner instanceof Player ? 'bullet' : 'enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10 - Math.random() * 5; // Random speed between -10 and -15
self.update = function () {
self.y += self.speed;
if (LK.ticks % 8 != 0) {
return;
}
// Create particle trail effect with varying colors and sizes
var colors = [0xff0000, 0x00ff00, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 0.8 + Math.random() * 1.5;
for (var i = 0; i < 1; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + (Math.random() - 0.5) * 15,
y: self.y + 50 + (Math.random() - 0.5) * 15,
alpha: 1,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.02; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChildAt(trailParticle, game.getChildIndex(self));
}
};
});
// Enemy class
var Enemy1 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy1', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3 + Math.random() * 3; // Random speed between 5 and 10
self.update = function () {
self.y += self.speed + LK.ticks / 10000; // Increase speed over time
};
});
var Enemy2 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy2', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5 + Math.random() * 5; // Random speed between 5 and 10
self.update = function () {
self.y += self.speed + LK.ticks / 10000; // Increase speed over time
self.x += Math.sin(LK.ticks / 100) * 5; // Add a sine wave movement pattern
};
});
var Enemy3 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy3', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5 + Math.random() * 5; // Random speed between 5 and 10
self.update = function () {
self.y += self.speed + LK.ticks / 10000; // Increase speed over time
self.x += Math.cos(LK.ticks / 100) * 5; // Add a cosine wave movement pattern
};
});
// Explosion class
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
// Initialize particles array
self.particles = [];
// Create particles
for (var i = 0; i < 50; i++) {
var particle = self.attachAsset('explosionParticle', {
anchorX: 0.5,
anchorY: 0.5,
x: Math.random() * explosionGraphics.width - explosionGraphics.width / 2,
y: Math.random() * explosionGraphics.height - explosionGraphics.height / 2
});
particle.speedX = Math.random() * 2 - 1;
particle.speedY = Math.random() * 2 - 1;
self.particles.push(particle);
}
self.update = function () {
self.alpha -= 0.02; // Fade out
explosionGraphics.scale.x += 0.05; // Scale up faster
explosionGraphics.scale.y += 0.05; // Scale up faster
// Update particles
for (var i = self.particles.length - 1; i >= 0; i--) {
self.particles[i].x += self.particles[i].speedX * 5; // Increase speed
self.particles[i].y += self.particles[i].speedY * 5; // Increase speed
self.particles[i].rotation += 0.2; // Add rotation
self.particles[i].alpha -= 0.04;
if (self.particles[i].alpha <= 0) {
self.particles[i].destroy();
self.particles.splice(i, 1);
}
}
if (self.alpha <= 0) {
self.destroy();
}
};
});
// FloatingEnemy class
var FloatingEnemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('floatingEnemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.origSpeedX = 2 + Math.random() * 3; // Random horizontal speed between 2 and 5
self.origSpeedY = 1 + Math.random() * 2; // Random vertical speed between 1 and 3
self.speedX = self.origSpeedX;
self.speedY = self.origSpeedY;
self.shootingTimer = 0; // Initialize shooting timer
self.update = function () {
self.shootingTimer++;
if (self.shootingTimer >= 180) {
// Shoot every 3 seconds (180 ticks)
var enemyBullet = new Bullet(self);
enemyBullet.x = self.x;
enemyBullet.y = self.y + enemyGraphics.height / 2;
enemyBullet.speed = 5; // Set bullet speed
game.addChild(enemyBullet);
bullets.push(enemyBullet);
self.shootingTimer = 0; // Reset shooting timer
}
self.x += self.speedX * Math.sin(LK.ticks / 50);
self.y += self.speedY + self.speedY * Math.cos(LK.ticks / 50);
// Bounce off the edges of the screen
if (self.x < 0 || self.x > 2048) {
self.speedX *= -1;
}
if (self.y < 600 && self.speedY < 0) {
console.log("enemy out of top");
self.speedY = self.origSpeedY;
} else if (self.y > 1200 && self.speedY > 0) {
self.speedY = -self.origSpeedY;
}
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
self.baseSpeed = 10;
self.speed = self.baseSpeed;
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.shoot = function () {
if (game.isLevelingUp) {
return;
}
var bullet1 = new Bullet();
bullet1.x = self.x - 20; // Adjust position for dual cannon
bullet1.y = self.y - playerGraphics.height / 2;
bullets.push(bullet1);
game.addChild(bullet1);
if (powerUpActive) {
var bullet2 = new Bullet();
bullet2.x = self.x + 20; // Adjust position for dual cannon
bullet2.y = self.y - playerGraphics.height / 2;
bullets.push(bullet2);
game.addChild(bullet2);
}
// Play shooting sound
LK.getSound('shootingSound').play();
// Add shooting effect
var shootingEffect = self.attachAsset('shootingEffect', {
anchorX: 0.5,
anchorY: 0.5,
x: 0,
y: -playerGraphics.height / 2
});
var fadeOutInterval = LK.setInterval(function () {
shootingEffect.alpha -= 0.1;
if (shootingEffect.alpha <= 0) {
LK.clearInterval(fadeOutInterval);
shootingEffect.destroy();
}
}, 20);
// Add bounceback effect
self.y += 20; // Move player down by 20 pixels
LK.setTimeout(function () {
self.y = 2732 - 200; // Reset player y position to main position
}, 100);
// Add particle trail effect with varying colors and sizes
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 4 + Math.random() * 4;
for (var i = 0; i < 3; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + (Math.random() - 0.5) * 15,
y: self.y + 50 + (Math.random() - 0.5) * 15,
alpha: 0.8,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.05; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChildAt(trailParticle, 0);
}
};
});
// PowerUpCannons class
var PowerUpCannons = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp_cannons', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5;
};
});
// PowerUpSpeed class
var PowerUpSpeed = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp_speed', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5;
};
});
// Progress Bar class
var ProgressBar = Container.expand(function () {
var self = Container.call(this);
var progressBarGraphics = self.attachAsset('progressBar', {
anchorX: 0.0,
anchorY: 0.0
});
self.lastLevelUp = 0;
self.reset = function () {
progressBarGraphics.width = 0;
self.lastLevelUp = LK.ticks;
};
self.update = function () {
progressBarGraphics.width = (LK.ticks - self.lastLevelUp) / 1000 * 2048; // Fill the progress bar over time
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
starGraphics.scale.set(1 + .2 * Math.random());
self.speed = 0.5 + Math.random() * 1.5; // Random speed between 0.5 and 2
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + 50) {
self.y = -50;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var bossBackgroundMusic = LK.getSound('bossBackgroundMusic');
var asteroidBelt; // Initialize asteroidBelt variable
// Play background music when the game starts
var backgroundMusic = LK.getSound('backgroundMusic');
backgroundMusic.loop = true;
backgroundMusic.play();
// Add initial text 'Tap to shoot and turn!' when the game starts
var initialText = new Text2('Tap to shoot and turn!', {
size: 150,
fill: "#ffffff"
});
initialText.anchor.set(0.5, 0.5);
initialText.x = 2048 / 2;
initialText.y = 2732 / 2 + 500;
game.addChild(initialText);
// Remove the initial text after 3 seconds
LK.setTimeout(function () {
initialText.destroy();
}, 3000);
// Initialize stars array
var stars = [];
// Add stars to the game
for (var i = 0; i < 100; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
// Initialize arrays and variables
var bullets = [];
var enemies = [];
var bossBullets = []; // Initialize bossBullets array
var powerUpActive = 0; // Initialize powerUpActive variable
var speedPowerUpActive = 0;
var enemySpawnCounter = 0; // Initialize enemy spawn counter
var powerUps = []; // Initialize powerUps array
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
var playerDirection = 1;
// Score display
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var level = 1;
// Initialize progress bar
var progressBar = game.addChild(new ProgressBar());
progressBar.y = 0;
var isMouseDown = false;
// Event listener for mouse down
game.down = function (x, y, obj) {
player.shoot();
playerDirection *= -1;
isMouseDown = true;
};
// Event listener for mouse up
game.up = function (x, y, obj) {
isMouseDown = false;
};
var speedPowerUpMultiplier = .2;
// Update game state
game.update = function () {
progressBar.update();
// Update player's position with speed boost if power-up is active
//if (isMouseDown) {
if (!game.isLevelingUp) {
player.x += (player.speed + speedPowerUpActive * speedPowerUpMultiplier * player.speed) * playerDirection;
}
// }
// Wrap player's position around the screen
if (player.x > 2048 + player.width / 2) {
player.x = -player.width / 2;
} else if (player.x < -player.width / 2) {
player.x = 2048 + player.width / 2;
// Change direction if player hits the edge
}
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 0.8 + Math.random() * 1.5;
for (var i = 0; i < 3; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: player.x + (Math.random() - 0.5) * 15,
y: player.y + 50 + (Math.random() - 0.5) * 15,
alpha: 0.8,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.05; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChildAt(trailParticle, game.getChildIndex(player));
}
if (speedPowerUpActive) {
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
var randomScale = 0.8 + Math.random() * 1.5;
for (var i = 0; i < 3; i++) {
var trailParticle = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
x: player.x + (Math.random() - 0.5) * 15,
y: player.y + 50 + (Math.random() - 0.5) * 15,
alpha: 0.8,
scaleX: randomScale * 1.2,
scaleY: randomScale * 1.2,
tint: randomColor
});
trailParticle.update = function () {
this.alpha -= 0.05; // Fade out slower
this.rotation += 0.1; // Add rotation effect
if (this.alpha <= 0) {
this.destroy();
}
};
game.addChild(trailParticle);
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < -50 || bullets[i].y > 2732 + 50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Update boss bullets
for (var i = bossBullets.length - 1; i >= 0; i--) {
bossBullets[i].update();
if (bossBullets[i] && bossBullets[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (enemies[j].y > 2732 + 50) {
enemies[j].destroy();
enemies.splice(j, 1);
}
}
// Check for collisions
for (var k = bullets.length - 1; k >= 0; k--) {
for (var l = enemies.length - 1; l >= 0; l--) {
if (bullets[k].intersects(enemies[l]) && bullets[k].owner !== enemies[l] && !(bullets[k].owner instanceof Enemy1 || bullets[k].owner instanceof Enemy2 || bullets[k].owner instanceof Enemy3 || bullets[k].owner instanceof FloatingEnemy || bullets[k].owner instanceof BossEnemy || bullets[k].owner instanceof BossEnemy2 || bullets[k].owner instanceof BossEnemy3)) {
if (enemies[l] instanceof BossEnemy || enemies[l] instanceof BossEnemy2 || enemies[l] instanceof BossEnemy3) {
enemies[l].takeDamage(10); // Boss takes 10 damage per bullet
bullets[k].destroy();
bullets.splice(k, 1);
break;
}
var explosion = new Explosion();
explosion.x = enemies[l].x;
explosion.y = enemies[l].y;
game.addChild(explosion);
bullets[k].destroy();
// Play explosion sound
LK.getSound('explosionSound').play();
if (enemies[l] instanceof Enemy1) {
enemies[l].destroy();
score += 1;
} else if (enemies[l] instanceof Enemy2) {
enemies[l].destroy();
score += 2;
} else if (enemies[l] instanceof Enemy3) {
enemies[l].destroy();
score += 3;
} else if (enemies[l] instanceof FloatingEnemy) {
enemies[l].destroy();
score += 4;
}
bullets.splice(k, 1);
enemies.splice(l, 1);
scoreTxt.setText(score);
break;
}
}
}
// Update powerUps
for (var i = powerUps.length - 1; i >= 0; i--) {
powerUps[i].update();
if (powerUps[i].y > 2732 + 50) {
powerUps[i].destroy();
powerUps.splice(i, 1);
}
}
// Spawn asteroid belt occasionally
if (LK.ticks % 600 == 500 && progressBar.width < 2040) {
// Every 10 seconds
asteroidBelt = new AsteroidBelt();
asteroidBelt.spawnAsteroids();
}
// Spawn enemies and occasionally power-ups
if (LK.ticks % 60 == 0 && progressBar.width < 2040) {
var enemyType = Math.floor(Math.random() * 4) + 1; // Randomly select an enemy type
var enemy;
switch (enemyType) {
case 1:
enemy = new Enemy1();
break;
case 2:
enemy = new Enemy2();
break;
case 3:
enemy = new Enemy3();
break;
case 4:
enemy = new FloatingEnemy();
break;
}
enemy.x = Math.random() * 2048;
enemy.y = -50;
enemies.push(enemy);
game.addChild(enemy);
enemySpawnCounter++;
}
// Spawn a power-up every 10th enemy with cooldown mechanism
if (enemySpawnCounter % 10 == 0 && progressBar.width < 2048 && !game.powerUpCooldown) {
var powerUpType = Math.random() < 0.5 ? PowerUpSpeed : PowerUpCannons; // Randomly select power-up type
var powerUp = new powerUpType();
powerUp.x = Math.random() * 2048;
powerUp.y = -50;
powerUps.push(powerUp);
game.addChild(powerUp);
game.powerUpCooldown = true;
LK.setTimeout(function () {
game.powerUpCooldown = false;
}, 10000); // 10 seconds cooldown
}
// Check for powerUp collision with player
for (var n = powerUps.length - 1; n >= 0; n--) {
if (powerUps[n].intersects(player)) {
if (powerUps[n] instanceof PowerUpSpeed) {
player.speed = player.baseSpeed * 1.25; // Double the player's speed
speedPowerUpActive = 1;
LK.setTimeout(function () {
player.speed = player.baseSpeed; // Reset speed after 10 seconds
speedPowerUpActive = 0;
}, 10000);
} else if (powerUps[n] instanceof PowerUpCannons) {
powerUpActive = 1; // Activate dual cannons
LK.setTimeout(function () {
powerUpActive = 0; // Deactivate dual cannons after 10 seconds
}, 10000);
}
powerUps[n].destroy();
powerUps.splice(n, 1);
}
}
// Update asteroid belt
if (asteroidBelt) {
asteroidBelt.update();
}
// Update stars
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].update();
}
// Check if the progress bar is full
if (game.isLevelingUp) {
// Speed boost effect for stars
game.starSpeedIncrease++;
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].speed++; // Increase speed by 5 times
}
}
if (progressBar.width >= 2048 && enemies.length == 0 && !game.isLevelingUp) {
if (!game.hasBossThisLevel) {
// Stop the regular background music
backgroundMusic.stop();
// Play the boss background music
bossBackgroundMusic.loop = true;
bossBackgroundMusic.play();
var bossType = level % 3; // Cycle through 3 different bosses
var bossEnemy;
switch (bossType) {
case 0:
bossEnemy = new BossEnemy();
break;
case 1:
bossEnemy = new BossEnemy2();
break;
case 2:
bossEnemy = new BossEnemy3();
break;
}
bossEnemy.x = 2048 / 2;
bossEnemy.y = -50;
enemies.push(bossEnemy);
game.addChild(bossEnemy);
game.hasBossThisLevel = true;
return;
}
game.isLevelingUp = true;
game.starSpeedIncrease = 0;
var initialPlayerY = player.y;
var targetPlayerY = -player.height; // Move player out of the top of the screen
var levelUpDuration = 3000; // 3 seconds
var overlay = new Container();
var overlayGraphics = overlay.attachAsset('progressBar', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
alpha: 0
});
overlayGraphics.width = overlayGraphics.height = 3000;
game.addChild(overlay);
// Move player upwards and fade in overlay
var moveUpInterval = LK.setInterval(function () {
player.y -= 300 / (levelUpDuration / 60); // Move up in 60 frames per second
player.x += (2048 / 2 - player.x) / (levelUpDuration / 60); // Move X position to center
}, 1000 / 60);
// Reset player position after moving up
LK.setTimeout(function () {
LK.clearInterval(moveUpInterval);
var secondMoveUpInterval = LK.setInterval(function () {
overlayGraphics.alpha += 1 / (levelUpDuration / 60); // Fade in overlay
}, 500 / 60);
LK.setTimeout(function () {
LK.clearInterval(secondMoveUpInterval);
player.y = initialPlayerY;
player.x = 2048 / 2; // Reset X position to center
overlayGraphics.destroy(); // Remove overlay
// Blink player 3 times when a new level starts
var blinkEffect = new BlinkEffect();
blinkEffect.blink(player, 3, 1000); // Blink 3 times over 1 second
}, levelUpDuration);
}, levelUpDuration);
game.isLevelingUp = true;
game.starSpeedIncrease = 0;
// Reset star speed after 3 seconds
LK.setTimeout(function () {
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].speed -= game.starSpeedIncrease; // Reset speed to original
}
// Increase the player's speed
player.speed += 1;
// List of nice colors
var colors = [0x000080, 0x008000, 0x800000, 0x808000, 0x800080, 0x008080];
// Select a random color from the list
var randomColor = colors[Math.floor(Math.random() * colors.length)];
// Change the background color
game.setBackgroundColor(randomColor);
// Show a popup with fade-in and fade-out effects
var levelTextBackground = new Container();
var levelText = new Text2('Level ' + (level + 1), {
size: 300,
fill: "#ffffff"
});
levelText.anchor.set(0.5, 0.5);
levelTextBackground.addChild(levelText);
levelTextBackground.x = 2048 / 2;
levelTextBackground.y = 2732 / 2;
levelTextBackground.alpha = 0; // Start with invisible
game.addChild(levelTextBackground);
// Fade-in effect
var fadeInInterval = LK.setInterval(function () {
levelTextBackground.alpha += 0.05;
if (levelTextBackground.alpha >= 1) {
LK.clearInterval(fadeInInterval);
}
}, 50);
// Remove the text after 2 seconds with fade-out effect
LK.setTimeout(function () {
var fadeOutInterval = LK.setInterval(function () {
levelTextBackground.alpha -= 0.05;
if (levelTextBackground.alpha <= 0) {
LK.clearInterval(fadeOutInterval);
levelTextBackground.destroy();
}
}, 50);
}, 2000);
level++;
// Reset the progress bar
progressBar.reset();
game.hasBossThisLevel = false;
game.isLevelingUp = false;
player.y = initialPlayerY;
}, 7000);
// Play a level up sound
LK.getSound('levelUp').play();
}
// Check for game over
for (var m = enemies.length - 1; m >= 0; m--) {
if (enemies[m].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
explosion toony. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
SKULL BALL. A ball with a skull on, billard ball with skull. Studio Ghibli. Ghibli style. Mobile game. Colorful. hand drawn. cute. fun. In-Game asset. 2d. Blank background. High contrast. No shadows.