/****
* Classes
****/
// LifePickup class
var LifePickup = Container.expand(function () {
var self = Container.call(this);
var lifeGraphics = self.createAsset('lifePickup', 'Extra Life Pickup', 0.5, 0.5);
self.pickup = function (hero) {
hero.lives += 1;
livesDisplay.updateLives(hero.lives);
self.destroy();
};
});
// HealthBar class
var LivesDisplay = Container.expand(function () {
var self = Container.call(this);
self.lives = 3;
self.livesText = new Text2('Lives: ' + self.lives, {
size: 100,
fill: '#ffffff'
});
self.livesText.anchor.set(0, 0);
LK.gui.topLeft.addChild(self.livesText);
self.updateLives = function (newLives) {
self.lives = newLives;
self.livesText.setText('Lives: ' + self.lives);
};
});
// ScoreDisplay class
var ScoreDisplay = Container.expand(function () {
var self = Container.call(this);
self.scoreText = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
self.scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(self.scoreText);
self.updateScore = function (score) {
self.scoreText.setText('Score: ' + score);
};
});
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
self.updateRotation = function (angle) {
self.rotation = angle;
};
var heroGraphics = self.createAsset('hero', 'Hero character', 0.5, 0.5);
self.x = 2048 / 2;
self.y = 2732 / 2;
self.lives = 3; // Hero starts with 3 lives
});
// Base Enemy class
var BaseEnemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.createAsset('enemy', 'Base Enemy character', 0.5, 0.5);
self.enemyType = 'base';
self.setGraphics = function () {
if (self.enemyType === 'shooting') {
self.createAsset('shootingEnemy', 'Shooting Enemy character', 0.5, 0.5);
}
};
self.speed = 2;
self.move = function () {
var dx = hero.x - self.x;
var dy = hero.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
};
});
// KamikazeEnemy class
var KamikazeEnemy = Container.expand(function () {
var self = Container.call(this);
var kamikazeEnemyGraphics = self.createAsset('kamikazeEnemy', 'Kamikaze Enemy character', 0.5, 0.5);
self.speed = 4;
self.move = function () {
// Movement towards the hero's position
var dx = hero.x - self.x;
var dy = hero.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
};
});
// ShootingEnemy class
var ShootingEnemy = Container.expand(function () {
var self = Container.call(this);
var shootingEnemyGraphics = self.createAsset('shootingEnemy', 'Shooting Enemy character', 0.5, 0.5);
self.speed = 3;
self.shootingRate = 120; // Shoot every 2 seconds
self.lastShotTick = 0;
self.move = function () {
// Movement towards the hero's position
var dx = hero.x - self.x;
var dy = hero.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
};
self.shoot = function () {
if (LK.ticks - self.lastShotTick >= self.shootingRate) {
var bullet = new EnemyBullet();
// Calculate the direction towards the hero at the time of shooting
var dx = hero.x - self.x;
var dy = hero.y - self.y;
var magnitude = Math.sqrt(dx * dx + dy * dy);
bullet.direction = {
x: dx / magnitude,
y: dy / magnitude
};
bullet.x = self.x;
bullet.y = self.y;
enemyBullets.push(bullet);
game.addChild(bullet);
self.lastShotTick = LK.ticks;
}
};
});
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('enemyBullet', 'Enemy Bullet Graphics', 0.5, 0.5);
self.speed = 5;
self.move = function () {
// Movement in a straight line towards the hero's last known position
self.x += self.direction.x * self.speed;
self.y += self.direction.y * self.speed;
};
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet Graphics', 0.5, 0.5);
self.speed = 10;
self.move = function () {
// Movement logic for the bullet will be defined in the fireBullet function
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x003300 // Init game with a darker green background
});
/****
* Game Code
****/
// Initialize hero, lives display, and score display
var hero = game.addChild(new Hero());
var livesDisplay = game.addChild(new LivesDisplay());
var scoreDisplay = game.addChild(new ScoreDisplay());
var score = 0; // Global score variable
// Initialize enemies and bullets arrays
var enemies = [];
var bullets = [];
var enemyBullets = []; // Array for bullets shot by ShootingEnemy
// Initialize firing rate, enemy spawn rate, and wave system
var firingRate = 30; // Increased firing rate (ticks)
var spawnRate = 180; // Increased enemy spawn rate (ticks) to decrease intensity
var waveCount = 0; // Number of waves that have passed
var enemiesPerWave = 10; // Base number of enemies per wave
var enemySpeedIncrease = 0.01; // Speed increase per wave
var waveCounterDisplay = new Text2('Wave: 1', {
size: 100,
fill: "#ffffff"
});
waveCounterDisplay.anchor.set(1, 0);
LK.gui.topRight.addChild(waveCounterDisplay);
var score = 0; // Global score variable
// Function to spawn enemies
function spawnEnemy() {
waveCount++;
waveCounterDisplay.setText('Wave: ' + waveCount);
var initialEnemies = 10;
var additionalEnemies = waveCount - 1; // Additional enemies spawned gradually
var enemiesToSpawn = initialEnemies + additionalEnemies;
var spawnEnemyAtIndex = function spawnEnemyAtIndex(index) {
if (index < enemiesToSpawn) {
var angle = Math.random() * Math.PI * 2; // Random angle
var radius = 1200; // Spawn circle radius
var enemyType = Math.random();
var enemy = enemyType < 0.33 ? new BaseEnemy() : enemyType < 0.66 ? new ShootingEnemy() : new KamikazeEnemy();
enemy.speed += enemy.speed * waveCount * enemySpeedIncrease; // Increase speed by 1% per wave
enemy.x = 1024 + radius * Math.cos(angle);
enemy.y = 1366 + radius * Math.sin(angle);
enemies.push(enemy);
game.addChild(enemy);
// Randomize the spawn interval between 0.1 and 0.5 seconds
var randomSpawnInterval = 100 + Math.random() * 400;
LK.setTimeout(function () {
spawnEnemyAtIndex(index + 1);
}, randomSpawnInterval);
}
};
spawnEnemyAtIndex(0);
}
// Function to fire a bullet
function fireBullet() {
if (enemies.length === 0) {
return;
} // No enemies to shoot at
var nearestEnemy = enemies.reduce(function (closest, enemy) {
var closestDist = Math.sqrt(Math.pow(closest.x - hero.x, 2) + Math.pow(closest.y - hero.y, 2));
var enemyDist = Math.sqrt(Math.pow(enemy.x - hero.x, 2) + Math.pow(enemy.y - hero.y, 2));
return enemyDist < closestDist ? enemy : closest;
}, enemies[0]);
var angleToNearestEnemy = Math.atan2(nearestEnemy.y - hero.y, nearestEnemy.x - hero.x);
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
hero.updateRotation(angleToNearestEnemy);
bullet.move = function () {
this.x += Math.cos(angleToNearestEnemy) * this.speed;
this.y += Math.sin(angleToNearestEnemy) * this.speed;
};
bullets.push(bullet);
game.addChild(bullet);
}
// Game tick event
LK.on('tick', function () {
// Move the hero towards the touch position with limited maximum speed
var maxSpeed = 5;
if (game.touchPosition) {
var dx = (game.touchPosition.x - hero.x) * 0.1;
var dy = (game.touchPosition.y - hero.y) * 0.1;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist > maxSpeed) {
dx = dx / dist * maxSpeed;
dy = dy / dist * maxSpeed;
}
hero.x += dx;
hero.y += dy;
}
// Move bullets and check for collisions with enemies
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].move();
// Check for bullet-enemy collisions
for (var j = enemies.length - 1; j >= 0; j--) {
if (bullets[i] && bullets[i].intersects(enemies[j])) {
// Destroy enemy and update score
score += 1; // Increment score
scoreDisplay.updateScore(score); // Update score display
spawnRate = Math.max(30, spawnRate - 5); // Decrease spawn rate by 5 ticks to a minimum of 30 ticks
// Chance to drop a LifePickup
if (Math.random() < 0.01) {
var lifePickup = new LifePickup();
lifePickup.x = enemies[j].x;
lifePickup.y = enemies[j].y;
game.addChild(lifePickup);
}
enemies[j].destroy();
enemies.splice(j, 1);
// Destroy bullet
bullets[i].destroy();
bullets.splice(i, 1);
break; // Break out of the enemies loop since the bullet is destroyed
}
}
// Remove bullets that are off-screen
if (bullets[i] && bullets[i].y < 0) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Move enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].move();
if (enemies[j].intersects(hero)) {
if (enemies[j] instanceof KamikazeEnemy) {
hero.lives -= 1; // Decrease hero's lives by one
livesDisplay.updateLives(hero.lives); // Update the lives display
LK.effects.flashScreen(0xff0000, 500); // Flash screen red for half a second
if (hero.lives <= 0) {
LK.showGameOver(); // If no lives left, show game over
return;
}
}
enemies[j].destroy(); // Destroy the enemy that collided with the hero
enemies.splice(j, 1);
}
}
// Make ShootingEnemy instances shoot at the hero
for (var i = 0; i < enemies.length; i++) {
if (enemies[i] instanceof ShootingEnemy) {
enemies[i].shoot();
}
}
// Spawn enemies only if the previous wave has been cleared
if (LK.ticks % spawnRate === 0 && enemies.length === 0) {
spawnEnemy();
}
// Move enemy bullets and check for collisions with the hero
for (var i = enemyBullets.length - 1; i >= 0; i--) {
enemyBullets[i].move();
if (enemyBullets[i].intersects(hero)) {
hero.lives -= 1; // Decrease hero's lives by one
livesDisplay.updateLives(hero.lives); // Update the lives display
if (hero.lives <= 0) {
LK.showGameOver(); // If no lives left, show game over
return;
}
LK.effects.flashScreen(0xff0000, 500); // Flash screen red for half a second
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
}
// Remove enemy bullets that are off-screen
if (enemyBullets[i] && (enemyBullets[i].y < 0 || enemyBullets[i].y > 2732)) {
enemyBullets[i].destroy();
enemyBullets.splice(i, 1);
}
}
// Check for hero collision with LifePickup
for (var i = game.children.length - 1; i >= 0; i--) {
if (game.children[i] instanceof LifePickup && game.children[i].intersects(hero)) {
game.children[i].pickup(hero);
}
}
// Fire bullets
if (LK.ticks % firingRate === 0) {
fireBullet();
}
}); // Track touch position
var touchPosition = null;
game.on('move', function (obj) {
var event = obj.event;
touchPosition = event.getLocalPosition(game);
game.touchPosition = touchPosition;
});
hero with a gun, shoots forward, top view, topdown. Single Game Texture. In-Game asset. 2d.TopDown. Blank background. High contrast. No shadows.
metal ball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A fireball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
heart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
zombie kamikaze, vertical top view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
opponent for the game, zombie kamikaze, vertical top view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.