/**** * Classes ****/ var LifePowerUp = Container.expand(function () { var self = Container.call(this); var powerUpGraphics = self.createAsset('lifePowerUp', 'Life Power Up', 0.5, 0.5); self.speed = 2; self.move = function () { self.y += self.speed; if (self.y > 2732 + self.height / 2) { self.destroy(); } }; }); // Define the EnemyBullet class var EnemyBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.createAsset('enemyBullet', 'Enemy Bullet', 0.5, 0.5); self.speed = 5; self.move = function () { self.y += self.speed * 2; }; }); // Define the DimStar class for stars with lower brightness var DimStar = Container.expand(function () { var self = Container.call(this); var starGraphics = self.createAsset('dimStar', 'Dim Star', 0.5, 0.5); starGraphics.alpha = Math.random() * 0.5 + 0.1; // Random alpha between 0.1 and 0.6 self.speed = Math.random() * 4 + 2; // Random speed between 2 and 6 self.move = function () { self.y += self.speed; if (self.y > 2732 + self.height / 2) { self.y = -self.height / 2; } }; }); // Define the Space Ranger class var SpaceRanger = Container.expand(function () { var self = Container.call(this); var rangerGraphics = self.createAsset('spaceRanger', 'Space Ranger', 0.5, 0.5); self.velocity = { x: 0, y: 0 }; self.acceleration = { x: 0, y: 0 }; self.maxSpeed = 2.5; self.lives = 6; // Space Ranger starts with 6 lives self.shootingBonusActive = false; self.shootingBonusEndTime = 0; self.applyPhysics = function () { self.velocity.x += self.acceleration.x; self.velocity.y += self.acceleration.y; self.velocity.x = Math.max(-self.maxSpeed, Math.min(self.maxSpeed, self.velocity.x)); self.velocity.y = Math.max(-self.maxSpeed, Math.min(self.maxSpeed, self.velocity.y)); self.x += self.velocity.x; self.y += self.velocity.y; self.x = Math.max(self.width / 2, Math.min(2048 - self.width / 2, self.x)); self.y = Math.max(self.height / 2, Math.min(2732 - self.height / 2, self.y)); self.acceleration.x = self.acceleration.y = 0; // Reset acceleration after applying }; }); // Define the Meteor class var Meteor = Container.expand(function () { var self = Container.call(this); var meteorGraphics = self.createAsset('meteor', 'Meteor', 0.5, 0.5); self.baseSpeed = 3; self.speedMultiplier = 1; self.move = function () { self.y += self.baseSpeed * (1 + waveCount * 0.3); }; }); // Define the Alien class var Alien = Container.expand(function () { var self = Container.call(this); var alienGraphics = self.createAsset('alien', 'Alien', 0.5, 0.5); self.speed = 2; self.health = 5; // Aliens can take 5 hits before being destroyed self.move = function () { self.y += self.speed * 2; }; }); // Define the HeroBullet class var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', 0.5, 0.5); self.speed = -10; self.move = function () { self.y += self.speed * 1.5; }; }); // Define the EnemyType1 class var EnemyType1 = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.createAsset('enemyType1', 'Enemy Type 1', 0.5, 0.5); self.speed = 2; self.health = 3; self.shootInterval = 120; // Enemy shoots every 2 seconds self.lastShotTick = 0; // Last tick when the enemy shot self.move = function () { self.y += self.speed * 1.5; }; self.shoot = function () { if (LK.ticks - self.lastShotTick >= self.shootInterval) { var newEnemyBullet = new EnemyBullet(); newEnemyBullet.x = self.x; newEnemyBullet.y = self.y + self.height / 2; // Calculate the direction to the hero var direction = { x: spaceRanger.x - self.x, y: spaceRanger.y - self.y }; var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y); direction.x /= magnitude; direction.y /= magnitude; // Set bullet speed to aim at the hero newEnemyBullet.speedX = direction.x * 5; newEnemyBullet.speedY = direction.y * 5; newEnemyBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; game.addChild(newEnemyBullet); enemyBullets.push(newEnemyBullet); self.lastShotTick = LK.ticks; } }; }); // Define the EnemyType2 class var EnemyType2 = Container.expand(function () { var self = Container.call(this); self.shootInterval = 120; // Enemy shoots every 2 seconds self.lastShotTick = 0; // Last tick when the enemy shot self.shoot = function () { if (LK.ticks - self.lastShotTick >= self.shootInterval) { var newEnemyBullet = new EnemyBullet(); newEnemyBullet.x = self.x; newEnemyBullet.y = self.y + self.height / 2; // Calculate the direction to the hero var direction = { x: spaceRanger.x - self.x, y: spaceRanger.y - self.y }; var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y); direction.x /= magnitude; direction.y /= magnitude; // Set bullet speed to aim at the hero newEnemyBullet.speedX = direction.x * 5; newEnemyBullet.speedY = direction.y * 5; newEnemyBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; game.addChild(newEnemyBullet); enemyBullets.push(newEnemyBullet); self.lastShotTick = LK.ticks; } }; var enemyGraphics = self.createAsset('enemyType2', 'Enemy Type 2', 0.5, 0.5); self.speed = 4; self.health = 2; self.move = function () { self.y += self.speed * 1.5; }; }); // Define the EnemyType3 class var EnemyType3 = Container.expand(function () { var self = Container.call(this); self.shootInterval = 180; // Enemy shoots every 3 seconds self.lastShotTick = 0; // Last tick when the enemy shot self.shoot = function () { if (LK.ticks - self.lastShotTick >= self.shootInterval) { var newEnemyBullet = new EnemyBullet(); newEnemyBullet.x = self.x; newEnemyBullet.y = self.y + self.height / 2; // Calculate the direction to the hero var direction = { x: spaceRanger.x - self.x, y: spaceRanger.y - self.y }; var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y); direction.x /= magnitude; direction.y /= magnitude; // Set bullet speed to aim at the hero newEnemyBullet.speedX = direction.x * 5; newEnemyBullet.speedY = direction.y * 5; newEnemyBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; game.addChild(newEnemyBullet); enemyBullets.push(newEnemyBullet); self.lastShotTick = LK.ticks; } }; var enemyGraphics = self.createAsset('enemyType3', 'Enemy Type 3', 0.5, 0.5); self.speed = 1; self.health = 7; self.move = function () { self.y += self.speed * 1.5; }; }); // Define the EnemyType4 class var EnemyType4 = Container.expand(function () { var self = Container.call(this); self.shootInterval = 150; // Enemy shoots every 2.5 seconds self.lastShotTick = 0; // Last tick when the enemy shot self.shoot = function () { if (LK.ticks - self.lastShotTick >= self.shootInterval) { var newEnemyBullet = new EnemyBullet(); newEnemyBullet.x = self.x; newEnemyBullet.y = self.y + self.height / 2; // Calculate the direction to the hero var direction = { x: spaceRanger.x - self.x, y: spaceRanger.y - self.y }; var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y); direction.x /= magnitude; direction.y /= magnitude; // Set bullet speed to aim at the hero newEnemyBullet.speedX = direction.x * 5; newEnemyBullet.speedY = direction.y * 5; newEnemyBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; game.addChild(newEnemyBullet); enemyBullets.push(newEnemyBullet); self.lastShotTick = LK.ticks; } }; var enemyGraphics = self.createAsset('enemyType4', 'Enemy Type 4', 0.5, 0.5); self.speed = 3; self.health = 4; self.move = function () { self.x += Math.sin(self.y / 100) * 10; // Sine wave movement self.y += self.speed * 1.5; }; }); // Define the EnemyType5 class var EnemyType5 = Container.expand(function () { var self = Container.call(this); self.shootInterval = 90; // Enemy shoots every 1.5 seconds self.lastShotTick = 0; // Last tick when the enemy shot self.shoot = function () { if (LK.ticks - self.lastShotTick >= self.shootInterval) { var newEnemyBullet = new EnemyBullet(); newEnemyBullet.x = self.x; newEnemyBullet.y = self.y + self.height / 2; // Calculate the direction to the hero var direction = { x: spaceRanger.x - self.x, y: spaceRanger.y - self.y }; var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y); direction.x /= magnitude; direction.y /= magnitude; // Set bullet speed to aim at the hero newEnemyBullet.speedX = direction.x * 5; newEnemyBullet.speedY = direction.y * 5; newEnemyBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; game.addChild(newEnemyBullet); enemyBullets.push(newEnemyBullet); self.lastShotTick = LK.ticks; } }; var enemyGraphics = self.createAsset('enemyType5', 'Enemy Type 5', 0.5, 0.5); self.speed = 5; self.health = 1; self.move = function () { if (self.x < 1024) { self.x += self.speed; } else { self.x -= self.speed; } self.y += self.speed / 2 * 1.5; }; }); var ShootingPowerUp = Container.expand(function () { var self = Container.call(this); var powerUpGraphics = self.createAsset('shootingPowerUp', 'Shooting Power Up', 0.5, 0.5); self.speed = 2; self.move = function () { self.y += self.speed; if (self.y > 2732 + self.height / 2) { self.destroy(); } }; }); // Define the WaveDisplay class var WaveDisplay = Container.expand(function () { var self = Container.call(this); self.waveCount = 1; var waveLabel = new Text2('WAVE', { size: 50, fill: "#ffffff" }); waveLabel.anchor.set(0.5, 0); waveLabel.y = -60; self.addChild(waveLabel); var waveText = new Text2('Wave: 1', { size: 100, fill: "#ffffff" }); waveText.anchor.set(0.5, 0); self.addChild(waveText); self.updateWave = function (wave) { waveText.setText('Wave: ' + wave); }; self.positionAtTopRight = function () { self.x = 2048 - self.width / 2 - 20; self.y = 50; }; self.positionAtTopRight(); }); // Define the ScoreDisplay class var ScoreDisplay = Container.expand(function () { var self = Container.call(this); var scoreLabel = new Text2('SCORE', { size: 50, fill: "#ffffff" }); scoreLabel.anchor.set(0.5, 0); scoreLabel.y = -60; self.addChild(scoreLabel); var scoreText = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreText.anchor.set(0.5, 0); self.addChild(scoreText); self.updateScore = function (score) { scoreText.setText('Score: ' + score); }; self.positionAtTopLeft = function () { self.x = self.width / 2 + 20; self.y = 50; }; self.positionAtTopLeft(); }); // Define the LivesDisplay class var LivesDisplay = Container.expand(function () { var self = Container.call(this); self.lives = 6; var livesLabel = new Text2('LIVES', { size: 50, fill: "#ffffff" }); livesLabel.anchor.set(0.5, 0); livesLabel.y = -60; self.addChild(livesLabel); var livesText = new Text2('Lives: 6', { size: 100, fill: "#ffffff" }); // Include the initial number of lives in the text self.livesIcons = []; for (var i = 0; i < self.lives; i++) { var heartIcon = self.createAsset('heartIcon', 'Heart Icon', 0, 0); heartIcon.x = livesText.width + 10 + i * (heartIcon.width + 10); heartIcon.y = livesText.height / 2; self.livesIcons.push(heartIcon); self.addChild(heartIcon); } livesText.anchor.set(0.5, 0); self.addChild(livesText); self.updateLives = function (lives) { livesText.setText('Lives: ' + lives); // Update the lives text when lives change for (var i = 0; i < self.livesIcons.length; i++) { self.livesIcons[i].visible = i < lives; } }; self.positionAtTopCenter = function () { self.x = 2048 / 2 - self.width / 2; self.y = 50; }; self.positionAtTopCenter(); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize background stars with a mix of Star and DimStar var stars = []; for (var i = 0; i < 100; i++) { var starType = DimStar; // Use only DimStar class for star creation var star = new starType(); star.x = Math.random() * 2048; star.y = Math.random() * 2732; stars.push(star); game.addChild(star); } // Instantiate ScoreDisplay var scoreDisplay = new ScoreDisplay(); LK.gui.topLeft.addChild(scoreDisplay); // Instantiate LivesDisplay var livesDisplay = new LivesDisplay(); // Instantiate WaveDisplay var waveDisplay = new WaveDisplay(); // Initialize wave properties var waveCount = 1; var waveInterval = 200; // Further decreased interval for spawning waves to increase game pace var enemiesPerWave = 10; // Start with 10 enemies in the first wave, increase by 1 for each subsequent wave LK.gui.topRight.addChild(waveDisplay); // Add lives display to the GUI overlay at the top center of the screen LK.gui.top.addChild(livesDisplay); // Initialize important asset arrays and wave-related variables var meteors = []; var aliens = []; var heroBullets = []; var enemyBullets = []; var enemiesSpawnedThisWave = 0; // Initialize the counter for enemies spawned in the current wave var enemiesSpawnedThisWave = 0; // Initialize the counter for enemies spawned in the current wave // Create the Space Ranger var spaceRanger = game.addChild(new SpaceRanger()); spaceRanger.x = 2048 / 2; spaceRanger.y = 2732 - 200; // Start position near the bottom of the screen // Touch event handling function handleTouchMove(obj) { var touchPos = obj.event.getLocalPosition(game); spaceRanger.x = touchPos.x; spaceRanger.y = touchPos.y; } game.on('move', handleTouchMove); // Game tick event LK.on('tick', function () { // Move all stars for (var s = stars.length - 1; s >= 0; s--) { stars[s].move(); } // Move all meteors for (var i = meteors.length - 1; i >= 0; i--) { meteors[i].move(); if (meteors[i].y > 2732 + meteors[i].height / 2) { meteors[i].destroy(); meteors.splice(i, 1); } } // Move all aliens and make them shoot if they are of EnemyType1 for (var j = aliens.length - 1; j >= 0; j--) { aliens[j].move(); if (aliens[j].shoot) { aliens[j].shoot(); } if (aliens[j].y > 2732 + aliens[j].height / 2) { aliens[j].destroy(); aliens.splice(j, 1); } } // Move all enemy bullets for (var p = enemyBullets.length - 1; p >= 0; p--) { enemyBullets[p].move(); if (enemyBullets[p].y > 2732 + enemyBullets[p].height / 2) { enemyBullets[p].destroy(); enemyBullets.splice(p, 1); } else if (spaceRanger.intersects(enemyBullets[p])) { spaceRanger.lives--; livesDisplay.updateLives(spaceRanger.lives); LK.effects.flashScreen(0xff0000, 500); if (spaceRanger.lives <= 0) { LK.showGameOver(); return; } enemyBullets[p].destroy(); enemyBullets.splice(p, 1); } } // Move all hero bullets for (var k = heroBullets.length - 1; k >= 0; k--) { heroBullets[k].move(); if (heroBullets[k].y < -heroBullets[k].height / 2) { heroBullets[k].destroy(); heroBullets.splice(k, 1); } } // Collision detection between hero bullets and aliens for (var l = heroBullets.length - 1; l >= 0; l--) { for (var m = aliens.length - 1; m >= 0; m--) { if (heroBullets[l].intersects(aliens[m])) { // Decrease alien health and check if it should be destroyed aliens[m].health--; if (aliens[m].health <= 0) { // Update score for destroying an alien LK.setScore(LK.getScore() + 100); scoreDisplay.updateScore(LK.getScore()); // Chance to drop a life power-up if (Math.random() < 0.0667) { // 1 in 15 chance var shootingPowerUp = new ShootingPowerUp(); shootingPowerUp.x = aliens[m].x; shootingPowerUp.y = aliens[m].y; game.addChild(shootingPowerUp); } aliens[m].destroy(); aliens.splice(m, 1); } // Destroy the hero bullet heroBullets[l].destroy(); heroBullets.splice(l, 1); break; } } } // Collision detection between space ranger and meteors/aliens for (var n = 0; n < meteors.length; n++) { if (spaceRanger.intersects(meteors[n])) { spaceRanger.lives--; livesDisplay.updateLives(spaceRanger.lives); LK.effects.flashScreen(0xff0000, 500); if (spaceRanger.lives <= 0) { LK.showGameOver(); return; } meteors[n].destroy(); meteors.splice(n, 1); } } for (var o = 0; o < aliens.length; o++) { if (spaceRanger.intersects(aliens[o])) { spaceRanger.lives--; LK.effects.flashScreen(0xff0000, 500); if (spaceRanger.lives <= 0) { LK.showGameOver(); return; } aliens[o].destroy(); aliens.splice(o, 1); } } // Pick up ShootingPowerUp for (var p = 0; p < game.children.length; p++) { var obj = game.children[p]; if (obj instanceof ShootingPowerUp && spaceRanger.intersects(obj)) { spaceRanger.shootingBonusActive = true; spaceRanger.shootingBonusEndTime = LK.ticks + 600; // 10 seconds at 60FPS obj.destroy(); } } // Pick up LifePowerUp for (var p = 0; p < game.children.length; p++) { var obj = game.children[p]; if (obj instanceof LifePowerUp && spaceRanger.intersects(obj)) { spaceRanger.lives += 1; // Add 1 life to the Space Ranger livesDisplay.updateLives(spaceRanger.lives); obj.destroy(); } } // Spawn enemies in waves with progressive difficulty if (LK.ticks % waveInterval === 0 && meteors.length + aliens.length <= 2) { for (var i = 0; i < meteors.length; i++) { meteors[i].speedMultiplier = 1 + waveCount * 0.1; } var enemyTypes = [Meteor, Alien, EnemyType1, EnemyType2, EnemyType3, EnemyType4, EnemyType5]; var waveEnemyCount = Math.min(enemiesPerWave + waveCount, 60); // Cap the number of enemies per wave // Spawn enemies gradually var enemiesToSpawn = Math.ceil(waveEnemyCount / (waveInterval / 10)); for (var i = 0; i < enemiesToSpawn; i++) { if (enemiesSpawnedThisWave < waveEnemyCount) { var randomTypeIndex = Math.floor(Math.random() * enemyTypes.length); var EnemyClass = enemyTypes[randomTypeIndex]; var newEnemy = new EnemyClass(); newEnemy.x = Math.random() * 2048; newEnemy.y = -newEnemy.height / 2; game.addChild(newEnemy); if (newEnemy instanceof Meteor) { meteors.push(newEnemy); } else { aliens.push(newEnemy); } enemiesSpawnedThisWave++; } } if (enemiesSpawnedThisWave >= waveEnemyCount) { // Reset for the next wave enemiesSpawnedThisWave = 0; // Adjust the number of enemies for the next wave enemiesPerWave += 1; // Increase by 1 each wave // Adjust the interval between waves waveInterval = Math.max(200, waveInterval - 10); // Decrease interval by a fixed amount // Increment wave count waveCount++; // Update the wave counter display waveDisplay.updateWave(waveCount); } } // Fire hero bullets if (LK.ticks % 30 == 0) { var newHeroBullet1 = new HeroBullet(); newHeroBullet1.x = spaceRanger.x - spaceRanger.width / 4; newHeroBullet1.y = spaceRanger.y - spaceRanger.height / 2; heroBullets.push(newHeroBullet1); game.addChild(newHeroBullet1); var newHeroBullet2 = new HeroBullet(); newHeroBullet2.x = spaceRanger.x + spaceRanger.width / 4; newHeroBullet2.y = spaceRanger.y - spaceRanger.height / 2; heroBullets.push(newHeroBullet2); game.addChild(newHeroBullet2); if (spaceRanger.shootingBonusActive) { var leftDiagonalBullet = new HeroBullet(); leftDiagonalBullet.x = spaceRanger.x; leftDiagonalBullet.y = spaceRanger.y - spaceRanger.height / 2; leftDiagonalBullet.speedX = -Math.cos(Math.PI / 4) * leftDiagonalBullet.speed; leftDiagonalBullet.speedY = -Math.sin(Math.PI / 4) * leftDiagonalBullet.speed; leftDiagonalBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; var rightDiagonalBullet = new HeroBullet(); rightDiagonalBullet.x = spaceRanger.x; rightDiagonalBullet.y = spaceRanger.y - spaceRanger.height / 2; rightDiagonalBullet.speedX = Math.cos(Math.PI / 4) * rightDiagonalBullet.speed; rightDiagonalBullet.speedY = -Math.sin(Math.PI / 4) * rightDiagonalBullet.speed; rightDiagonalBullet.move = function () { this.x += this.speedX; this.y += this.speedY; }; heroBullets.push(leftDiagonalBullet); heroBullets.push(rightDiagonalBullet); game.addChild(leftDiagonalBullet); game.addChild(rightDiagonalBullet); // Activate the move function for diagonal bullets leftDiagonalBullet.move(); rightDiagonalBullet.move(); } // Deactivate shooting bonus if time has expired if (spaceRanger.shootingBonusEndTime && LK.ticks > spaceRanger.shootingBonusEndTime) { spaceRanger.shootingBonusActive = false; } } });
/****
* Classes
****/
var LifePowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.createAsset('lifePowerUp', 'Life Power Up', 0.5, 0.5);
self.speed = 2;
self.move = function () {
self.y += self.speed;
if (self.y > 2732 + self.height / 2) {
self.destroy();
}
};
});
// Define the EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('enemyBullet', 'Enemy Bullet', 0.5, 0.5);
self.speed = 5;
self.move = function () {
self.y += self.speed * 2;
};
});
// Define the DimStar class for stars with lower brightness
var DimStar = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.createAsset('dimStar', 'Dim Star', 0.5, 0.5);
starGraphics.alpha = Math.random() * 0.5 + 0.1; // Random alpha between 0.1 and 0.6
self.speed = Math.random() * 4 + 2; // Random speed between 2 and 6
self.move = function () {
self.y += self.speed;
if (self.y > 2732 + self.height / 2) {
self.y = -self.height / 2;
}
};
});
// Define the Space Ranger class
var SpaceRanger = Container.expand(function () {
var self = Container.call(this);
var rangerGraphics = self.createAsset('spaceRanger', 'Space Ranger', 0.5, 0.5);
self.velocity = {
x: 0,
y: 0
};
self.acceleration = {
x: 0,
y: 0
};
self.maxSpeed = 2.5;
self.lives = 6; // Space Ranger starts with 6 lives
self.shootingBonusActive = false;
self.shootingBonusEndTime = 0;
self.applyPhysics = function () {
self.velocity.x += self.acceleration.x;
self.velocity.y += self.acceleration.y;
self.velocity.x = Math.max(-self.maxSpeed, Math.min(self.maxSpeed, self.velocity.x));
self.velocity.y = Math.max(-self.maxSpeed, Math.min(self.maxSpeed, self.velocity.y));
self.x += self.velocity.x;
self.y += self.velocity.y;
self.x = Math.max(self.width / 2, Math.min(2048 - self.width / 2, self.x));
self.y = Math.max(self.height / 2, Math.min(2732 - self.height / 2, self.y));
self.acceleration.x = self.acceleration.y = 0; // Reset acceleration after applying
};
});
// Define the Meteor class
var Meteor = Container.expand(function () {
var self = Container.call(this);
var meteorGraphics = self.createAsset('meteor', 'Meteor', 0.5, 0.5);
self.baseSpeed = 3;
self.speedMultiplier = 1;
self.move = function () {
self.y += self.baseSpeed * (1 + waveCount * 0.3);
};
});
// Define the Alien class
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.createAsset('alien', 'Alien', 0.5, 0.5);
self.speed = 2;
self.health = 5; // Aliens can take 5 hits before being destroyed
self.move = function () {
self.y += self.speed * 2;
};
});
// Define the HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', 0.5, 0.5);
self.speed = -10;
self.move = function () {
self.y += self.speed * 1.5;
};
});
// Define the EnemyType1 class
var EnemyType1 = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemyType1', 'Enemy Type 1', 0.5, 0.5);
self.speed = 2;
self.health = 3;
self.shootInterval = 120; // Enemy shoots every 2 seconds
self.lastShotTick = 0; // Last tick when the enemy shot
self.move = function () {
self.y += self.speed * 1.5;
};
self.shoot = function () {
if (LK.ticks - self.lastShotTick >= self.shootInterval) {
var newEnemyBullet = new EnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y + self.height / 2;
// Calculate the direction to the hero
var direction = {
x: spaceRanger.x - self.x,
y: spaceRanger.y - self.y
};
var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
direction.x /= magnitude;
direction.y /= magnitude;
// Set bullet speed to aim at the hero
newEnemyBullet.speedX = direction.x * 5;
newEnemyBullet.speedY = direction.y * 5;
newEnemyBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
game.addChild(newEnemyBullet);
enemyBullets.push(newEnemyBullet);
self.lastShotTick = LK.ticks;
}
};
});
// Define the EnemyType2 class
var EnemyType2 = Container.expand(function () {
var self = Container.call(this);
self.shootInterval = 120; // Enemy shoots every 2 seconds
self.lastShotTick = 0; // Last tick when the enemy shot
self.shoot = function () {
if (LK.ticks - self.lastShotTick >= self.shootInterval) {
var newEnemyBullet = new EnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y + self.height / 2;
// Calculate the direction to the hero
var direction = {
x: spaceRanger.x - self.x,
y: spaceRanger.y - self.y
};
var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
direction.x /= magnitude;
direction.y /= magnitude;
// Set bullet speed to aim at the hero
newEnemyBullet.speedX = direction.x * 5;
newEnemyBullet.speedY = direction.y * 5;
newEnemyBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
game.addChild(newEnemyBullet);
enemyBullets.push(newEnemyBullet);
self.lastShotTick = LK.ticks;
}
};
var enemyGraphics = self.createAsset('enemyType2', 'Enemy Type 2', 0.5, 0.5);
self.speed = 4;
self.health = 2;
self.move = function () {
self.y += self.speed * 1.5;
};
});
// Define the EnemyType3 class
var EnemyType3 = Container.expand(function () {
var self = Container.call(this);
self.shootInterval = 180; // Enemy shoots every 3 seconds
self.lastShotTick = 0; // Last tick when the enemy shot
self.shoot = function () {
if (LK.ticks - self.lastShotTick >= self.shootInterval) {
var newEnemyBullet = new EnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y + self.height / 2;
// Calculate the direction to the hero
var direction = {
x: spaceRanger.x - self.x,
y: spaceRanger.y - self.y
};
var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
direction.x /= magnitude;
direction.y /= magnitude;
// Set bullet speed to aim at the hero
newEnemyBullet.speedX = direction.x * 5;
newEnemyBullet.speedY = direction.y * 5;
newEnemyBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
game.addChild(newEnemyBullet);
enemyBullets.push(newEnemyBullet);
self.lastShotTick = LK.ticks;
}
};
var enemyGraphics = self.createAsset('enemyType3', 'Enemy Type 3', 0.5, 0.5);
self.speed = 1;
self.health = 7;
self.move = function () {
self.y += self.speed * 1.5;
};
});
// Define the EnemyType4 class
var EnemyType4 = Container.expand(function () {
var self = Container.call(this);
self.shootInterval = 150; // Enemy shoots every 2.5 seconds
self.lastShotTick = 0; // Last tick when the enemy shot
self.shoot = function () {
if (LK.ticks - self.lastShotTick >= self.shootInterval) {
var newEnemyBullet = new EnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y + self.height / 2;
// Calculate the direction to the hero
var direction = {
x: spaceRanger.x - self.x,
y: spaceRanger.y - self.y
};
var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
direction.x /= magnitude;
direction.y /= magnitude;
// Set bullet speed to aim at the hero
newEnemyBullet.speedX = direction.x * 5;
newEnemyBullet.speedY = direction.y * 5;
newEnemyBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
game.addChild(newEnemyBullet);
enemyBullets.push(newEnemyBullet);
self.lastShotTick = LK.ticks;
}
};
var enemyGraphics = self.createAsset('enemyType4', 'Enemy Type 4', 0.5, 0.5);
self.speed = 3;
self.health = 4;
self.move = function () {
self.x += Math.sin(self.y / 100) * 10; // Sine wave movement
self.y += self.speed * 1.5;
};
});
// Define the EnemyType5 class
var EnemyType5 = Container.expand(function () {
var self = Container.call(this);
self.shootInterval = 90; // Enemy shoots every 1.5 seconds
self.lastShotTick = 0; // Last tick when the enemy shot
self.shoot = function () {
if (LK.ticks - self.lastShotTick >= self.shootInterval) {
var newEnemyBullet = new EnemyBullet();
newEnemyBullet.x = self.x;
newEnemyBullet.y = self.y + self.height / 2;
// Calculate the direction to the hero
var direction = {
x: spaceRanger.x - self.x,
y: spaceRanger.y - self.y
};
var magnitude = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
direction.x /= magnitude;
direction.y /= magnitude;
// Set bullet speed to aim at the hero
newEnemyBullet.speedX = direction.x * 5;
newEnemyBullet.speedY = direction.y * 5;
newEnemyBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
game.addChild(newEnemyBullet);
enemyBullets.push(newEnemyBullet);
self.lastShotTick = LK.ticks;
}
};
var enemyGraphics = self.createAsset('enemyType5', 'Enemy Type 5', 0.5, 0.5);
self.speed = 5;
self.health = 1;
self.move = function () {
if (self.x < 1024) {
self.x += self.speed;
} else {
self.x -= self.speed;
}
self.y += self.speed / 2 * 1.5;
};
});
var ShootingPowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.createAsset('shootingPowerUp', 'Shooting Power Up', 0.5, 0.5);
self.speed = 2;
self.move = function () {
self.y += self.speed;
if (self.y > 2732 + self.height / 2) {
self.destroy();
}
};
});
// Define the WaveDisplay class
var WaveDisplay = Container.expand(function () {
var self = Container.call(this);
self.waveCount = 1;
var waveLabel = new Text2('WAVE', {
size: 50,
fill: "#ffffff"
});
waveLabel.anchor.set(0.5, 0);
waveLabel.y = -60;
self.addChild(waveLabel);
var waveText = new Text2('Wave: 1', {
size: 100,
fill: "#ffffff"
});
waveText.anchor.set(0.5, 0);
self.addChild(waveText);
self.updateWave = function (wave) {
waveText.setText('Wave: ' + wave);
};
self.positionAtTopRight = function () {
self.x = 2048 - self.width / 2 - 20;
self.y = 50;
};
self.positionAtTopRight();
});
// Define the ScoreDisplay class
var ScoreDisplay = Container.expand(function () {
var self = Container.call(this);
var scoreLabel = new Text2('SCORE', {
size: 50,
fill: "#ffffff"
});
scoreLabel.anchor.set(0.5, 0);
scoreLabel.y = -60;
self.addChild(scoreLabel);
var scoreText = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreText.anchor.set(0.5, 0);
self.addChild(scoreText);
self.updateScore = function (score) {
scoreText.setText('Score: ' + score);
};
self.positionAtTopLeft = function () {
self.x = self.width / 2 + 20;
self.y = 50;
};
self.positionAtTopLeft();
});
// Define the LivesDisplay class
var LivesDisplay = Container.expand(function () {
var self = Container.call(this);
self.lives = 6;
var livesLabel = new Text2('LIVES', {
size: 50,
fill: "#ffffff"
});
livesLabel.anchor.set(0.5, 0);
livesLabel.y = -60;
self.addChild(livesLabel);
var livesText = new Text2('Lives: 6', {
size: 100,
fill: "#ffffff"
}); // Include the initial number of lives in the text
self.livesIcons = [];
for (var i = 0; i < self.lives; i++) {
var heartIcon = self.createAsset('heartIcon', 'Heart Icon', 0, 0);
heartIcon.x = livesText.width + 10 + i * (heartIcon.width + 10);
heartIcon.y = livesText.height / 2;
self.livesIcons.push(heartIcon);
self.addChild(heartIcon);
}
livesText.anchor.set(0.5, 0);
self.addChild(livesText);
self.updateLives = function (lives) {
livesText.setText('Lives: ' + lives); // Update the lives text when lives change
for (var i = 0; i < self.livesIcons.length; i++) {
self.livesIcons[i].visible = i < lives;
}
};
self.positionAtTopCenter = function () {
self.x = 2048 / 2 - self.width / 2;
self.y = 50;
};
self.positionAtTopCenter();
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize background stars with a mix of Star and DimStar
var stars = [];
for (var i = 0; i < 100; i++) {
var starType = DimStar; // Use only DimStar class for star creation
var star = new starType();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
// Instantiate ScoreDisplay
var scoreDisplay = new ScoreDisplay();
LK.gui.topLeft.addChild(scoreDisplay);
// Instantiate LivesDisplay
var livesDisplay = new LivesDisplay();
// Instantiate WaveDisplay
var waveDisplay = new WaveDisplay();
// Initialize wave properties
var waveCount = 1;
var waveInterval = 200; // Further decreased interval for spawning waves to increase game pace
var enemiesPerWave = 10; // Start with 10 enemies in the first wave, increase by 1 for each subsequent wave
LK.gui.topRight.addChild(waveDisplay);
// Add lives display to the GUI overlay at the top center of the screen
LK.gui.top.addChild(livesDisplay);
// Initialize important asset arrays and wave-related variables
var meteors = [];
var aliens = [];
var heroBullets = [];
var enemyBullets = [];
var enemiesSpawnedThisWave = 0; // Initialize the counter for enemies spawned in the current wave
var enemiesSpawnedThisWave = 0; // Initialize the counter for enemies spawned in the current wave
// Create the Space Ranger
var spaceRanger = game.addChild(new SpaceRanger());
spaceRanger.x = 2048 / 2;
spaceRanger.y = 2732 - 200; // Start position near the bottom of the screen
// Touch event handling
function handleTouchMove(obj) {
var touchPos = obj.event.getLocalPosition(game);
spaceRanger.x = touchPos.x;
spaceRanger.y = touchPos.y;
}
game.on('move', handleTouchMove);
// Game tick event
LK.on('tick', function () {
// Move all stars
for (var s = stars.length - 1; s >= 0; s--) {
stars[s].move();
}
// Move all meteors
for (var i = meteors.length - 1; i >= 0; i--) {
meteors[i].move();
if (meteors[i].y > 2732 + meteors[i].height / 2) {
meteors[i].destroy();
meteors.splice(i, 1);
}
}
// Move all aliens and make them shoot if they are of EnemyType1
for (var j = aliens.length - 1; j >= 0; j--) {
aliens[j].move();
if (aliens[j].shoot) {
aliens[j].shoot();
}
if (aliens[j].y > 2732 + aliens[j].height / 2) {
aliens[j].destroy();
aliens.splice(j, 1);
}
}
// Move all enemy bullets
for (var p = enemyBullets.length - 1; p >= 0; p--) {
enemyBullets[p].move();
if (enemyBullets[p].y > 2732 + enemyBullets[p].height / 2) {
enemyBullets[p].destroy();
enemyBullets.splice(p, 1);
} else if (spaceRanger.intersects(enemyBullets[p])) {
spaceRanger.lives--;
livesDisplay.updateLives(spaceRanger.lives);
LK.effects.flashScreen(0xff0000, 500);
if (spaceRanger.lives <= 0) {
LK.showGameOver();
return;
}
enemyBullets[p].destroy();
enemyBullets.splice(p, 1);
}
}
// Move all hero bullets
for (var k = heroBullets.length - 1; k >= 0; k--) {
heroBullets[k].move();
if (heroBullets[k].y < -heroBullets[k].height / 2) {
heroBullets[k].destroy();
heroBullets.splice(k, 1);
}
}
// Collision detection between hero bullets and aliens
for (var l = heroBullets.length - 1; l >= 0; l--) {
for (var m = aliens.length - 1; m >= 0; m--) {
if (heroBullets[l].intersects(aliens[m])) {
// Decrease alien health and check if it should be destroyed
aliens[m].health--;
if (aliens[m].health <= 0) {
// Update score for destroying an alien
LK.setScore(LK.getScore() + 100);
scoreDisplay.updateScore(LK.getScore());
// Chance to drop a life power-up
if (Math.random() < 0.0667) {
// 1 in 15 chance
var shootingPowerUp = new ShootingPowerUp();
shootingPowerUp.x = aliens[m].x;
shootingPowerUp.y = aliens[m].y;
game.addChild(shootingPowerUp);
}
aliens[m].destroy();
aliens.splice(m, 1);
}
// Destroy the hero bullet
heroBullets[l].destroy();
heroBullets.splice(l, 1);
break;
}
}
}
// Collision detection between space ranger and meteors/aliens
for (var n = 0; n < meteors.length; n++) {
if (spaceRanger.intersects(meteors[n])) {
spaceRanger.lives--;
livesDisplay.updateLives(spaceRanger.lives);
LK.effects.flashScreen(0xff0000, 500);
if (spaceRanger.lives <= 0) {
LK.showGameOver();
return;
}
meteors[n].destroy();
meteors.splice(n, 1);
}
}
for (var o = 0; o < aliens.length; o++) {
if (spaceRanger.intersects(aliens[o])) {
spaceRanger.lives--;
LK.effects.flashScreen(0xff0000, 500);
if (spaceRanger.lives <= 0) {
LK.showGameOver();
return;
}
aliens[o].destroy();
aliens.splice(o, 1);
}
}
// Pick up ShootingPowerUp
for (var p = 0; p < game.children.length; p++) {
var obj = game.children[p];
if (obj instanceof ShootingPowerUp && spaceRanger.intersects(obj)) {
spaceRanger.shootingBonusActive = true;
spaceRanger.shootingBonusEndTime = LK.ticks + 600; // 10 seconds at 60FPS
obj.destroy();
}
}
// Pick up LifePowerUp
for (var p = 0; p < game.children.length; p++) {
var obj = game.children[p];
if (obj instanceof LifePowerUp && spaceRanger.intersects(obj)) {
spaceRanger.lives += 1; // Add 1 life to the Space Ranger
livesDisplay.updateLives(spaceRanger.lives);
obj.destroy();
}
}
// Spawn enemies in waves with progressive difficulty
if (LK.ticks % waveInterval === 0 && meteors.length + aliens.length <= 2) {
for (var i = 0; i < meteors.length; i++) {
meteors[i].speedMultiplier = 1 + waveCount * 0.1;
}
var enemyTypes = [Meteor, Alien, EnemyType1, EnemyType2, EnemyType3, EnemyType4, EnemyType5];
var waveEnemyCount = Math.min(enemiesPerWave + waveCount, 60); // Cap the number of enemies per wave
// Spawn enemies gradually
var enemiesToSpawn = Math.ceil(waveEnemyCount / (waveInterval / 10));
for (var i = 0; i < enemiesToSpawn; i++) {
if (enemiesSpawnedThisWave < waveEnemyCount) {
var randomTypeIndex = Math.floor(Math.random() * enemyTypes.length);
var EnemyClass = enemyTypes[randomTypeIndex];
var newEnemy = new EnemyClass();
newEnemy.x = Math.random() * 2048;
newEnemy.y = -newEnemy.height / 2;
game.addChild(newEnemy);
if (newEnemy instanceof Meteor) {
meteors.push(newEnemy);
} else {
aliens.push(newEnemy);
}
enemiesSpawnedThisWave++;
}
}
if (enemiesSpawnedThisWave >= waveEnemyCount) {
// Reset for the next wave
enemiesSpawnedThisWave = 0;
// Adjust the number of enemies for the next wave
enemiesPerWave += 1; // Increase by 1 each wave
// Adjust the interval between waves
waveInterval = Math.max(200, waveInterval - 10); // Decrease interval by a fixed amount
// Increment wave count
waveCount++;
// Update the wave counter display
waveDisplay.updateWave(waveCount);
}
}
// Fire hero bullets
if (LK.ticks % 30 == 0) {
var newHeroBullet1 = new HeroBullet();
newHeroBullet1.x = spaceRanger.x - spaceRanger.width / 4;
newHeroBullet1.y = spaceRanger.y - spaceRanger.height / 2;
heroBullets.push(newHeroBullet1);
game.addChild(newHeroBullet1);
var newHeroBullet2 = new HeroBullet();
newHeroBullet2.x = spaceRanger.x + spaceRanger.width / 4;
newHeroBullet2.y = spaceRanger.y - spaceRanger.height / 2;
heroBullets.push(newHeroBullet2);
game.addChild(newHeroBullet2);
if (spaceRanger.shootingBonusActive) {
var leftDiagonalBullet = new HeroBullet();
leftDiagonalBullet.x = spaceRanger.x;
leftDiagonalBullet.y = spaceRanger.y - spaceRanger.height / 2;
leftDiagonalBullet.speedX = -Math.cos(Math.PI / 4) * leftDiagonalBullet.speed;
leftDiagonalBullet.speedY = -Math.sin(Math.PI / 4) * leftDiagonalBullet.speed;
leftDiagonalBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
var rightDiagonalBullet = new HeroBullet();
rightDiagonalBullet.x = spaceRanger.x;
rightDiagonalBullet.y = spaceRanger.y - spaceRanger.height / 2;
rightDiagonalBullet.speedX = Math.cos(Math.PI / 4) * rightDiagonalBullet.speed;
rightDiagonalBullet.speedY = -Math.sin(Math.PI / 4) * rightDiagonalBullet.speed;
rightDiagonalBullet.move = function () {
this.x += this.speedX;
this.y += this.speedY;
};
heroBullets.push(leftDiagonalBullet);
heroBullets.push(rightDiagonalBullet);
game.addChild(leftDiagonalBullet);
game.addChild(rightDiagonalBullet);
// Activate the move function for diagonal bullets
leftDiagonalBullet.move();
rightDiagonalBullet.move();
}
// Deactivate shooting bonus if time has expired
if (spaceRanger.shootingBonusEndTime && LK.ticks > spaceRanger.shootingBonusEndTime) {
spaceRanger.shootingBonusActive = false;
}
}
});
military spaceship flies upward. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
meteorite. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
ракета, снаряд летит вверх. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
energy ball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
alien ship, flying saucer. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
alien spaceship heading down. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
alien spaceship heading down. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
alien spaceship heading down. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
alien spaceship heading down. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
сердце. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
lightning. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.