/****
* Classes
****/
// Define a class for bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Define a class for enemies
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
self.x = Math.random() * 2048;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Add background image
var bgImage = game.attachAsset('Fondo', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
function showOptions(config) {
// Create a simple UI overlay to show options
var overlay = new Container();
var title = new Text2(config.title, {
size: 100,
fill: 0xFFFFFF
});
title.anchor.set(0.5, 0);
title.x = 1024;
title.y = 500;
overlay.addChild(title);
config.options.forEach(function (option, index) {
var button = new Text2(option.text, {
size: 80,
fill: 0xFFFFFF
});
button.anchor.set(0.5, 0);
button.x = 1024;
button.y = 700 + index * 100;
button.interactive = true;
button.on('pointerdown', option.action);
overlay.addChild(button);
});
LK.gui.center.addChild(overlay);
}
// Initialize player
var player = new Player();
player.x = 1024;
player.y = 2500;
game.addChild(player);
// Initialize score
var score = 0;
var scoreText = new Text2('Score: ' + score, {
size: 50,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Initialize enemies
var enemies = [];
for (var i = 0; i < 10; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * 1000;
enemies.push(enemy);
game.addChild(enemy);
}
// Initialize bullets
var bullets = [];
// Handle player movement
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
// Handle shooting
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
// Play the 'Disparo' sound when bullet is pressed
LK.getSound('Disparo').play();
};
// Update game state
game.update = function () {
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
// Check for collision between player and enemy
// Player is now immune to bullets, so we comment out the game over trigger
// if (player.intersects(enemies[i])) {
// Trigger game over when player collides with an enemy
// LK.showGameOver();
// return; // Exit the update loop
// }
}
// Check if all enemies are destroyed
if (enemies.length === 0) {
// Show 'You Win' when all enemies are destroyed
LK.showYouWin();
// Show options to restart or proceed to the next level
showOptions({
title: "Level Complete!",
options: [{
text: "Restart",
action: function action() {
// Restart the game
game.reset();
}
}, {
text: "Next Level",
action: function action() {
// Increase level difficulty or reset enemies
for (var i = 0; i < 10; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * 1000;
enemies.push(enemy);
game.addChild(enemy);
}
// Resume the game
LK.resumeGame();
}
}]
});
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < 0) {
bullets.splice(i, 1);
}
}
// Check for collisions
for (var i = bullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (bullets[i].intersects(enemies[j])) {
bullets[i].destroy();
enemies[j].destroy();
// Play the 'Muerte' sound when enemy is destroyed
LK.getSound('Muerte').play();
bullets.splice(i, 1);
enemies.splice(j, 1);
// Update score
score += 10;
scoreText.setText('Score: ' + score);
break;
}
}
}
};