/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var ActionButton = Container.expand(function (label) { var self = Container.call(this); var buttonGraphics = self.attachAsset('actionButton', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5 }); var buttonText = new Text2(label, { size: 60, fill: 0xFFFFFF }); buttonText.anchor.set(0.5, 0.5); self.addChild(buttonText); self.buttonType = label; self.isPressed = false; self.down = function () { self.isPressed = true; buttonGraphics.alpha = 0.8; if (self.buttonType === 'A') { player.shoot(); } else if (self.buttonType === 'B') { player.jump(); } }; self.up = function () { self.isPressed = false; buttonGraphics.alpha = 0.5; }; return self; }); var Background = Container.expand(function () { var self = Container.call(this); var backgroundGraphics = self.attachAsset('background', { anchorX: 0, anchorY: 0 }); return self; }); var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.direction = 1; // 1 for right, -1 for left self.speed = 15; self.fromPlayer = true; self.update = function () { self.x += self.speed * self.direction; // Check if bullet is off-screen if (self.x < -50 || self.x > 2100) { self.shouldRemove = true; } }; return self; }); var Character = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('soldier', { anchorX: 0.5, anchorY: 1.0 }); self.direction = 1; // 1 for right, -1 for left self.velocityY = 0; self.velocityX = 0; self.speed = 8; self.jumpForce = -20; self.gravity = 1; self.isJumping = false; self.isGrounded = false; self.jumpsRemaining = 0; self.maxJumps = 7; self.platform = null; self.health = 100; self.maxHealth = 100; self.jump = function () { if (self.isGrounded || self.jumpsRemaining > 0) { self.velocityY = self.jumpForce; self.isJumping = true; if (self.isGrounded) { self.jumpsRemaining = self.maxJumps - 1; } else { self.jumpsRemaining--; } self.isGrounded = false; LK.getSound('jump').play(); } }; self.shoot = function () { var bullet = new Bullet(); bullet.x = self.x + self.direction * 40; bullet.y = self.y - 60; bullet.direction = self.direction; bullet.fromPlayer = true; bullets.push(bullet); game.addChild(bullet); LK.getSound('shoot').play(); }; self.moveLeft = function () { self.direction = -1; self.velocityX = -self.speed; graphics.scaleX = -1; }; self.moveRight = function () { self.direction = 1; self.velocityX = self.speed; graphics.scaleX = 1; }; self.stopMoving = function () { self.velocityX = 0; }; self.update = function () { // Apply gravity self.velocityY += self.gravity; // Apply velocity self.x += self.velocityX; self.y += self.velocityY; // Check screen boundaries if (self.x < 40) { self.x = 40; } else if (self.x > 2008) { self.x = 2008; } // Check if character fell off the bottom of the screen if (self.y > 2732) { LK.getSound('playerDeath').play(); LK.effects.flashScreen(0xff0000, 500); LK.showGameOver(); } // Reset grounded state self.isGrounded = false; // Check platform collisions for (var i = 0; i < platforms.length; i++) { var platform = platforms[i]; // Simple collision detection with platform if (self.velocityY >= 0 && // Only check collision when falling self.x >= platform.x && self.x <= platform.x + platform.width && self.y >= platform.y - 10 && self.y <= platform.y + 10) { self.isGrounded = true; self.isJumping = false; self.jumpsRemaining = self.maxJumps; self.velocityY = 0; self.y = platform.y; self.platform = platform; break; } } }; return self; }); var Enemy = Character.expand(function () { var self = Character.call(this); // Replace soldier asset with enemy asset self.removeChildren(); var graphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 1.0 }); // Adjust enemy properties self.speed = 4; self.direction = -1; self.shootCooldown = 0; self.shootInterval = 180; // Three seconds (60 frames * 3) graphics.scaleX = -1; // Override update method var parentUpdate = self.update; self.update = function () { parentUpdate.call(this); // AI movement: patrol the platform if (self.platform) { // Check if reached edge of platform if (self.x <= self.platform.x + 40) { self.direction = 1; graphics.scaleX = 1; self.velocityX = self.speed; } else if (self.x >= self.platform.x + self.platform.width - 40) { self.direction = -1; graphics.scaleX = -1; self.velocityX = -self.speed; } } // Shooting logic self.shootCooldown++; if (self.shootCooldown >= self.shootInterval) { // Face the player before shooting if (player) { var directionToPlayer = player.x < self.x ? -1 : 1; graphics.scaleX = directionToPlayer; } self.shoot(); self.shootCooldown = 0; self.shootInterval = 180; // Three seconds (60 frames * 3) } }; // Override shoot method self.shoot = function () { // Determine direction to shoot based on player position var directionToPlayer = player.x < self.x ? -1 : 1; // Only 30% chance to actually fire a bullet if (Math.random() < 0.3) { var bullet = new Bullet(); bullet.x = self.x + directionToPlayer * 40; bullet.y = self.y - 60; bullet.direction = directionToPlayer; bullet.fromPlayer = false; bullets.push(bullet); game.addChild(bullet); LK.getSound('shoot').play(); } }; return self; }); var ControlButton = Container.expand(function (type) { var self = Container.call(this); var buttonGraphics = self.attachAsset('controlButton', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5 }); var buttonText = new Text2(type, { size: 60, fill: 0xFFFFFF }); buttonText.anchor.set(0.5, 0.5); self.addChild(buttonText); self.buttonType = type; self.isPressed = false; self.down = function () { self.isPressed = true; buttonGraphics.alpha = 0.8; }; self.up = function () { self.isPressed = false; buttonGraphics.alpha = 0.5; }; return self; }); var HealthBar = Container.expand(function () { var self = Container.call(this); // Background bar self.background = new Container(); var bgGraphics = self.background.attachAsset('platform', { anchorX: 0, anchorY: 0 }); bgGraphics.tint = 0x333333; self.addChild(self.background); // Health bar self.bar = new Container(); var barGraphics = self.bar.attachAsset('platform', { anchorX: 0, anchorY: 0 }); barGraphics.tint = 0x00FF00; self.addChild(self.bar); // Set size self.width = 200; self.height = 20; self.background.width = self.width; self.background.height = self.height; self.background.scaleX = self.width / 400; self.background.scaleY = self.height / 50; // Update health percentage self.updateHealth = function (current, max) { var percentage = Math.max(0, Math.min(1, current / max)); self.bar.width = self.width * percentage; self.bar.scaleX = self.width / 400 * percentage; self.bar.scaleY = self.height / 50; // Change color based on health var barGraphics = self.bar.children[0]; if (percentage > 0.6) { barGraphics.tint = 0x00FF00; // Green } else if (percentage > 0.3) { barGraphics.tint = 0xFFFF00; // Yellow } else { barGraphics.tint = 0xFF0000; // Red } }; return self; }); var Platform = Container.expand(function () { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0, anchorY: 0 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game variables var player; var platforms = []; var enemies = []; var bullets = []; var leftButton, rightButton, jumpButton, shootButton; var scoreText; var jumpIndicator; var healthBar; // Setup game UI function setupUI() { // Create score display scoreText = new Text2('Enemies: ' + enemies.length, { size: 60, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); // Create jump counter display jumpIndicator = new Text2('Jumps: 7', { size: 60, fill: 0xFFFFFF }); jumpIndicator.anchor.set(0, 0); jumpIndicator.x = 50; LK.gui.top.addChild(jumpIndicator); // Create health bar healthBar = new HealthBar(); healthBar.x = 50; healthBar.y = 100; LK.gui.top.addChild(healthBar); // Create control buttons leftButton = new ControlButton('←'); leftButton.x = 150; leftButton.y = 2500; game.addChild(leftButton); rightButton = new ControlButton('→'); rightButton.x = 350; rightButton.y = 2500; game.addChild(rightButton); jumpButton = new ActionButton('B'); jumpButton.x = 1850; jumpButton.y = 2500; game.addChild(jumpButton); shootButton = new ActionButton('A'); shootButton.x = 1650; shootButton.y = 2500; game.addChild(shootButton); } // Create game platforms function createPlatforms() { // Create ground platform var ground = new Platform(); ground.x = 0; ground.y = 2600; ground.width = 2048; ground.scaleX = 5.12; // Scale to fit screen width (2048/400) platforms.push(ground); game.addChild(ground); // Create elevated platforms (6 more for a total of 7) with random positions var platformPositions = []; // Define height sections for platform distribution var heightSections = [{ min: 2200, max: 2400 }, { min: 1900, max: 2100 }, { min: 1600, max: 1800 }, { min: 1300, max: 1500 }, { min: 1000, max: 1200 }, { min: 700, max: 900 }]; // Create one platform in each height section for (var i = 0; i < heightSections.length; i++) { var section = heightSections[i]; var randomX = Math.floor(Math.random() * (1700 - 200) + 200); // Random X between 200-1700 var randomY = Math.floor(Math.random() * (section.max - section.min) + section.min); var randomScale = Math.random() * 0.5 + 0.8; // Random scale between 0.8 and 1.3 platformPositions.push({ x: randomX, y: randomY, scale: randomScale }); } for (var i = 0; i < platformPositions.length; i++) { var pos = platformPositions[i]; var platform = new Platform(); platform.x = pos.x; platform.y = pos.y; platform.scaleX = pos.scale; platform.width = 400 * pos.scale; platforms.push(platform); game.addChild(platform); } } // Create enemies function createEnemies() { // Place enemies on different platforms with random x positions var enemyPositions = []; // Skip the ground platform (index 0) and place enemies on platforms 1-5 for (var i = 1; i <= 5; i++) { var platform = platforms[i]; var platformWidth = platform.width; // Calculate a random x position within the platform's bounds var randomX = Math.floor(Math.random() * (platformWidth - 80)) + platform.x + 40; enemyPositions.push({ x: randomX, platform: i }); } for (var i = 0; i < enemyPositions.length; i++) { var pos = enemyPositions[i]; var enemy = new Enemy(); var platform = platforms[pos.platform]; enemy.x = pos.x; enemy.y = platform.y; enemy.platform = platform; enemies.push(enemy); game.addChild(enemy); } } // Create player function createPlayer() { player = new Character(); player.x = 100; player.y = platforms[0].y; // Start on ground player.platform = platforms[0]; player.health = player.maxHealth; // Initialize full health game.addChild(player); } // Initialize game function initGame() { // Add background var background = new Background(); background.x = 0; background.y = 0; game.addChild(background); // Create game elements createPlatforms(); createEnemies(); createPlayer(); setupUI(); // Play background music LK.playMusic('bgMusic'); } // Handle input game.down = function (x, y, obj) { // Only respond to direct object clicks if (!obj) return; if (obj === leftButton) { leftButton.down(); } else if (obj === rightButton) { rightButton.down(); } else if (obj === jumpButton) { jumpButton.down(); } else if (obj === shootButton) { shootButton.down(); } }; game.up = function (x, y, obj) { // First check if any button was directly clicked if (obj) { if (obj === leftButton) { leftButton.up(); } else if (obj === rightButton) { rightButton.up(); } else if (obj === jumpButton) { jumpButton.up(); } else if (obj === shootButton) { shootButton.up(); } return; } // Check if any button is being released if (leftButton.isPressed) leftButton.up(); if (rightButton.isPressed) rightButton.up(); if (jumpButton.isPressed) jumpButton.up(); if (shootButton.isPressed) shootButton.up(); }; // Update function gets called every frame game.update = function () { if (!player) { initGame(); return; } // Handle control input if (leftButton && leftButton.isPressed) { player.moveLeft(); } else if (rightButton && rightButton.isPressed) { player.moveRight(); } else { player.stopMoving(); } // Update player player.update(); // Update jump indicator text if (jumpIndicator) { jumpIndicator.setText("Jumps: " + (player.isGrounded ? player.maxJumps : player.jumpsRemaining)); } // Update health bar if (healthBar) { healthBar.updateHealth(player.health, player.maxHealth); } // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); } // Update bullets for (var j = bullets.length - 1; j >= 0; j--) { var bullet = bullets[j]; bullet.update(); // Check if bullet should be removed if (bullet.shouldRemove) { bullet.destroy(); bullets.splice(j, 1); continue; } // Check bullet collisions with enemies if (bullet.fromPlayer) { for (var k = enemies.length - 1; k >= 0; k--) { var enemy = enemies[k]; if (bullet.intersects(enemy)) { // Enemy hit LK.getSound('enemyDeath').play(); LK.effects.flashObject(enemy, 0xFF0000, 300); // Remove enemy enemy.destroy(); enemies.splice(k, 1); // Remove bullet bullet.destroy(); bullets.splice(j, 1); // Update score scoreText.setText('Enemies: ' + enemies.length); // Check if all enemies are defeated if (enemies.length === 0) { LK.showYouWin(); } break; } } } // Check bullet collisions with player else if (!bullet.fromPlayer && bullet.intersects(player)) { // Player hit LK.getSound('shoot').play(); LK.effects.flashObject(player, 0xFF0000, 300); // Reduce player health player.health -= 20; // Each bullet deals 20 damage healthBar.updateHealth(player.health, player.maxHealth); // Game over if health is depleted if (player.health <= 0) { LK.getSound('playerDeath').play(); LK.effects.flashScreen(0xFF0000, 500); // Game over LK.showGameOver(); } // Remove bullet bullet.destroy(); bullets.splice(j, 1); } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var ActionButton = Container.expand(function (label) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('actionButton', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.5
});
var buttonText = new Text2(label, {
size: 60,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.buttonType = label;
self.isPressed = false;
self.down = function () {
self.isPressed = true;
buttonGraphics.alpha = 0.8;
if (self.buttonType === 'A') {
player.shoot();
} else if (self.buttonType === 'B') {
player.jump();
}
};
self.up = function () {
self.isPressed = false;
buttonGraphics.alpha = 0.5;
};
return self;
});
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.attachAsset('background', {
anchorX: 0,
anchorY: 0
});
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.direction = 1; // 1 for right, -1 for left
self.speed = 15;
self.fromPlayer = true;
self.update = function () {
self.x += self.speed * self.direction;
// Check if bullet is off-screen
if (self.x < -50 || self.x > 2100) {
self.shouldRemove = true;
}
};
return self;
});
var Character = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('soldier', {
anchorX: 0.5,
anchorY: 1.0
});
self.direction = 1; // 1 for right, -1 for left
self.velocityY = 0;
self.velocityX = 0;
self.speed = 8;
self.jumpForce = -20;
self.gravity = 1;
self.isJumping = false;
self.isGrounded = false;
self.jumpsRemaining = 0;
self.maxJumps = 7;
self.platform = null;
self.health = 100;
self.maxHealth = 100;
self.jump = function () {
if (self.isGrounded || self.jumpsRemaining > 0) {
self.velocityY = self.jumpForce;
self.isJumping = true;
if (self.isGrounded) {
self.jumpsRemaining = self.maxJumps - 1;
} else {
self.jumpsRemaining--;
}
self.isGrounded = false;
LK.getSound('jump').play();
}
};
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x + self.direction * 40;
bullet.y = self.y - 60;
bullet.direction = self.direction;
bullet.fromPlayer = true;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
};
self.moveLeft = function () {
self.direction = -1;
self.velocityX = -self.speed;
graphics.scaleX = -1;
};
self.moveRight = function () {
self.direction = 1;
self.velocityX = self.speed;
graphics.scaleX = 1;
};
self.stopMoving = function () {
self.velocityX = 0;
};
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
// Apply velocity
self.x += self.velocityX;
self.y += self.velocityY;
// Check screen boundaries
if (self.x < 40) {
self.x = 40;
} else if (self.x > 2008) {
self.x = 2008;
}
// Check if character fell off the bottom of the screen
if (self.y > 2732) {
LK.getSound('playerDeath').play();
LK.effects.flashScreen(0xff0000, 500);
LK.showGameOver();
}
// Reset grounded state
self.isGrounded = false;
// Check platform collisions
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
// Simple collision detection with platform
if (self.velocityY >= 0 &&
// Only check collision when falling
self.x >= platform.x && self.x <= platform.x + platform.width && self.y >= platform.y - 10 && self.y <= platform.y + 10) {
self.isGrounded = true;
self.isJumping = false;
self.jumpsRemaining = self.maxJumps;
self.velocityY = 0;
self.y = platform.y;
self.platform = platform;
break;
}
}
};
return self;
});
var Enemy = Character.expand(function () {
var self = Character.call(this);
// Replace soldier asset with enemy asset
self.removeChildren();
var graphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 1.0
});
// Adjust enemy properties
self.speed = 4;
self.direction = -1;
self.shootCooldown = 0;
self.shootInterval = 180; // Three seconds (60 frames * 3)
graphics.scaleX = -1;
// Override update method
var parentUpdate = self.update;
self.update = function () {
parentUpdate.call(this);
// AI movement: patrol the platform
if (self.platform) {
// Check if reached edge of platform
if (self.x <= self.platform.x + 40) {
self.direction = 1;
graphics.scaleX = 1;
self.velocityX = self.speed;
} else if (self.x >= self.platform.x + self.platform.width - 40) {
self.direction = -1;
graphics.scaleX = -1;
self.velocityX = -self.speed;
}
}
// Shooting logic
self.shootCooldown++;
if (self.shootCooldown >= self.shootInterval) {
// Face the player before shooting
if (player) {
var directionToPlayer = player.x < self.x ? -1 : 1;
graphics.scaleX = directionToPlayer;
}
self.shoot();
self.shootCooldown = 0;
self.shootInterval = 180; // Three seconds (60 frames * 3)
}
};
// Override shoot method
self.shoot = function () {
// Determine direction to shoot based on player position
var directionToPlayer = player.x < self.x ? -1 : 1;
// Only 30% chance to actually fire a bullet
if (Math.random() < 0.3) {
var bullet = new Bullet();
bullet.x = self.x + directionToPlayer * 40;
bullet.y = self.y - 60;
bullet.direction = directionToPlayer;
bullet.fromPlayer = false;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
}
};
return self;
});
var ControlButton = Container.expand(function (type) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('controlButton', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.5
});
var buttonText = new Text2(type, {
size: 60,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.buttonType = type;
self.isPressed = false;
self.down = function () {
self.isPressed = true;
buttonGraphics.alpha = 0.8;
};
self.up = function () {
self.isPressed = false;
buttonGraphics.alpha = 0.5;
};
return self;
});
var HealthBar = Container.expand(function () {
var self = Container.call(this);
// Background bar
self.background = new Container();
var bgGraphics = self.background.attachAsset('platform', {
anchorX: 0,
anchorY: 0
});
bgGraphics.tint = 0x333333;
self.addChild(self.background);
// Health bar
self.bar = new Container();
var barGraphics = self.bar.attachAsset('platform', {
anchorX: 0,
anchorY: 0
});
barGraphics.tint = 0x00FF00;
self.addChild(self.bar);
// Set size
self.width = 200;
self.height = 20;
self.background.width = self.width;
self.background.height = self.height;
self.background.scaleX = self.width / 400;
self.background.scaleY = self.height / 50;
// Update health percentage
self.updateHealth = function (current, max) {
var percentage = Math.max(0, Math.min(1, current / max));
self.bar.width = self.width * percentage;
self.bar.scaleX = self.width / 400 * percentage;
self.bar.scaleY = self.height / 50;
// Change color based on health
var barGraphics = self.bar.children[0];
if (percentage > 0.6) {
barGraphics.tint = 0x00FF00; // Green
} else if (percentage > 0.3) {
barGraphics.tint = 0xFFFF00; // Yellow
} else {
barGraphics.tint = 0xFF0000; // Red
}
};
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0,
anchorY: 0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var player;
var platforms = [];
var enemies = [];
var bullets = [];
var leftButton, rightButton, jumpButton, shootButton;
var scoreText;
var jumpIndicator;
var healthBar;
// Setup game UI
function setupUI() {
// Create score display
scoreText = new Text2('Enemies: ' + enemies.length, {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Create jump counter display
jumpIndicator = new Text2('Jumps: 7', {
size: 60,
fill: 0xFFFFFF
});
jumpIndicator.anchor.set(0, 0);
jumpIndicator.x = 50;
LK.gui.top.addChild(jumpIndicator);
// Create health bar
healthBar = new HealthBar();
healthBar.x = 50;
healthBar.y = 100;
LK.gui.top.addChild(healthBar);
// Create control buttons
leftButton = new ControlButton('←');
leftButton.x = 150;
leftButton.y = 2500;
game.addChild(leftButton);
rightButton = new ControlButton('→');
rightButton.x = 350;
rightButton.y = 2500;
game.addChild(rightButton);
jumpButton = new ActionButton('B');
jumpButton.x = 1850;
jumpButton.y = 2500;
game.addChild(jumpButton);
shootButton = new ActionButton('A');
shootButton.x = 1650;
shootButton.y = 2500;
game.addChild(shootButton);
}
// Create game platforms
function createPlatforms() {
// Create ground platform
var ground = new Platform();
ground.x = 0;
ground.y = 2600;
ground.width = 2048;
ground.scaleX = 5.12; // Scale to fit screen width (2048/400)
platforms.push(ground);
game.addChild(ground);
// Create elevated platforms (6 more for a total of 7) with random positions
var platformPositions = [];
// Define height sections for platform distribution
var heightSections = [{
min: 2200,
max: 2400
}, {
min: 1900,
max: 2100
}, {
min: 1600,
max: 1800
}, {
min: 1300,
max: 1500
}, {
min: 1000,
max: 1200
}, {
min: 700,
max: 900
}];
// Create one platform in each height section
for (var i = 0; i < heightSections.length; i++) {
var section = heightSections[i];
var randomX = Math.floor(Math.random() * (1700 - 200) + 200); // Random X between 200-1700
var randomY = Math.floor(Math.random() * (section.max - section.min) + section.min);
var randomScale = Math.random() * 0.5 + 0.8; // Random scale between 0.8 and 1.3
platformPositions.push({
x: randomX,
y: randomY,
scale: randomScale
});
}
for (var i = 0; i < platformPositions.length; i++) {
var pos = platformPositions[i];
var platform = new Platform();
platform.x = pos.x;
platform.y = pos.y;
platform.scaleX = pos.scale;
platform.width = 400 * pos.scale;
platforms.push(platform);
game.addChild(platform);
}
}
// Create enemies
function createEnemies() {
// Place enemies on different platforms with random x positions
var enemyPositions = [];
// Skip the ground platform (index 0) and place enemies on platforms 1-5
for (var i = 1; i <= 5; i++) {
var platform = platforms[i];
var platformWidth = platform.width;
// Calculate a random x position within the platform's bounds
var randomX = Math.floor(Math.random() * (platformWidth - 80)) + platform.x + 40;
enemyPositions.push({
x: randomX,
platform: i
});
}
for (var i = 0; i < enemyPositions.length; i++) {
var pos = enemyPositions[i];
var enemy = new Enemy();
var platform = platforms[pos.platform];
enemy.x = pos.x;
enemy.y = platform.y;
enemy.platform = platform;
enemies.push(enemy);
game.addChild(enemy);
}
}
// Create player
function createPlayer() {
player = new Character();
player.x = 100;
player.y = platforms[0].y; // Start on ground
player.platform = platforms[0];
player.health = player.maxHealth; // Initialize full health
game.addChild(player);
}
// Initialize game
function initGame() {
// Add background
var background = new Background();
background.x = 0;
background.y = 0;
game.addChild(background);
// Create game elements
createPlatforms();
createEnemies();
createPlayer();
setupUI();
// Play background music
LK.playMusic('bgMusic');
}
// Handle input
game.down = function (x, y, obj) {
// Only respond to direct object clicks
if (!obj) return;
if (obj === leftButton) {
leftButton.down();
} else if (obj === rightButton) {
rightButton.down();
} else if (obj === jumpButton) {
jumpButton.down();
} else if (obj === shootButton) {
shootButton.down();
}
};
game.up = function (x, y, obj) {
// First check if any button was directly clicked
if (obj) {
if (obj === leftButton) {
leftButton.up();
} else if (obj === rightButton) {
rightButton.up();
} else if (obj === jumpButton) {
jumpButton.up();
} else if (obj === shootButton) {
shootButton.up();
}
return;
}
// Check if any button is being released
if (leftButton.isPressed) leftButton.up();
if (rightButton.isPressed) rightButton.up();
if (jumpButton.isPressed) jumpButton.up();
if (shootButton.isPressed) shootButton.up();
};
// Update function gets called every frame
game.update = function () {
if (!player) {
initGame();
return;
}
// Handle control input
if (leftButton && leftButton.isPressed) {
player.moveLeft();
} else if (rightButton && rightButton.isPressed) {
player.moveRight();
} else {
player.stopMoving();
}
// Update player
player.update();
// Update jump indicator text
if (jumpIndicator) {
jumpIndicator.setText("Jumps: " + (player.isGrounded ? player.maxJumps : player.jumpsRemaining));
}
// Update health bar
if (healthBar) {
healthBar.updateHealth(player.health, player.maxHealth);
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
var bullet = bullets[j];
bullet.update();
// Check if bullet should be removed
if (bullet.shouldRemove) {
bullet.destroy();
bullets.splice(j, 1);
continue;
}
// Check bullet collisions with enemies
if (bullet.fromPlayer) {
for (var k = enemies.length - 1; k >= 0; k--) {
var enemy = enemies[k];
if (bullet.intersects(enemy)) {
// Enemy hit
LK.getSound('enemyDeath').play();
LK.effects.flashObject(enemy, 0xFF0000, 300);
// Remove enemy
enemy.destroy();
enemies.splice(k, 1);
// Remove bullet
bullet.destroy();
bullets.splice(j, 1);
// Update score
scoreText.setText('Enemies: ' + enemies.length);
// Check if all enemies are defeated
if (enemies.length === 0) {
LK.showYouWin();
}
break;
}
}
}
// Check bullet collisions with player
else if (!bullet.fromPlayer && bullet.intersects(player)) {
// Player hit
LK.getSound('shoot').play();
LK.effects.flashObject(player, 0xFF0000, 300);
// Reduce player health
player.health -= 20; // Each bullet deals 20 damage
healthBar.updateHealth(player.health, player.maxHealth);
// Game over if health is depleted
if (player.health <= 0) {
LK.getSound('playerDeath').play();
LK.effects.flashScreen(0xFF0000, 500);
// Game over
LK.showGameOver();
}
// Remove bullet
bullet.destroy();
bullets.splice(j, 1);
}
}
};