/**** * Classes ****/ var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Hero class representing the player var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - heroGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); // Play the bullet sound LK.getSound('BulletSoundoflaser1').play(); }; // Add joystick and keyboard control for player movement if (game.keyboard) { if (game.keyboard.isDown('A')) { self.x -= self.speed / 2; } else if (game.keyboard.isDown('D')) { self.x += self.speed / 2; } } else if (joystick.active) { self.x += joystick.deltaX * (self.speed / 4); } // Restrict player movement to only sideways self.y = 2732 - 150; }; }); // HeroBullet class representing bullets shot by the hero var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('heroBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -15; self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); } }; }); var Joystick = Container.expand(function () { var self = Container.call(this); var joystickGraphics = self.attachAsset('joystick', { anchorX: 0.5, anchorY: 0.5 }); self.active = false; self.startX = 0; self.startY = 0; self.down = function (x, y, obj) { self.active = true; self.startX = x; self.startY = y; }; self.move = function (x, y, obj) { if (self.active) { self.deltaX = x - self.startX; self.startX = x; } }; self.up = function (x, y, obj) { self.active = false; }; }); // MainVillain class representing the main villain var MainVillain = Container.expand(function () { var self = Container.call(this); var mainVillainGraphics = self.attachAsset('mainVillain', { anchorX: 0.5, anchorY: 0.5 }); self.health = 5; // Takes 5 bullets to destroy self.update = function () { // Fire missile continuously if (LK.ticks % 60 == 0) { var missile = new VillainMissile(); missile.x = self.x; missile.y = self.y + mainVillainGraphics.height / 2; game.addChild(missile); villainMissiles.push(missile); // Play the 'enemymissilesound1' sound LK.getSound('enemymissilesound1').play(); } }; }); // VillainMissile class representing missiles fired by the main villain var VillainMissile = Container.expand(function () { var self = Container.call(this); var missileGraphics = self.attachAsset('villainMissile', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var backgroundImage = game.addChild(LK.getAsset('backgroundImage', {})); var hero = new Hero(); hero.x = 2048 / 2; hero.y = 2732 - 150; game.addChild(hero); var joystick = new Joystick(); joystick.x = 200; // Position joystick on the screen joystick.y = 2732 - 200; game.addChild(joystick); if (enemies && enemies.length > 0 && heroBullets && heroBullets.length > 0) { LK.getSound('Backgroundsound1').play({ loop: true }); } // Initialize the score text var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var enemies = []; var heroBullets = []; var villainMissiles = []; var mainVillain = null; // Function to spawn enemies function spawnEnemy() { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -50; game.addChild(enemy); enemies.push(enemy); } // Handle game updates game.update = function () { // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); if (heroBullets[i].y < 0) { heroBullets[i].destroy(); heroBullets.splice(i, 1); } } // Update enemies if (!mainVillain) { for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (enemies[j].y > 2732) { enemies[j].destroy(); enemies.splice(j, 1); // Display a game over message and end the game LK.showGameOver("Game Over! Enemy reached the boundary."); } } } // Update villain missiles for (var n = villainMissiles.length - 1; n >= 0; n--) { villainMissiles[n].update(); if (villainMissiles[n].intersects(hero)) { // Display a losing message and end the game LK.showGameOver("You died!"); break; } } // Update main villain if (mainVillain) { mainVillain.update(); } // Check for collisions between hero bullets and enemies for (var k = heroBullets.length - 1; k >= 0; k--) { if (mainVillain && heroBullets[k].intersects(mainVillain)) { heroBullets[k].destroy(); heroBullets.splice(k, 1); mainVillain.health -= 1; if (mainVillain.health <= 0) { mainVillain.destroy(); mainVillain = null; // Update the score LK.setScore(LK.getScore() + 10); scoreTxt.setText(LK.getScore()); // Display a winning message and end the game LK.showGameOver("Congratulations! You won the game!"); } break; } for (var l = enemies.length - 1; l >= 0; l--) { if (heroBullets[k].intersects(enemies[l])) { heroBullets[k].destroy(); enemies[l].destroy(); heroBullets.splice(k, 1); enemies.splice(l, 1); // Update the score LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); // Check if the score reaches 490 if (LK.getScore() == 490) { // Spawn the main villain mainVillain = new MainVillain(); mainVillain.x = 2048 / 2; mainVillain.y = -50; game.addChild(mainVillain); } break; } } } // Check for collisions between hero and enemies for (var m = enemies.length - 1; m >= 0; m--) { if (hero.intersects(enemies[m])) { // Display a losing message and end the game LK.showGameOver("You died!"); break; } } }; // Handle touch events for shooting game.down = function (x, y, obj) { if (obj.target === joystick) { joystick.down(x, y, obj); } // Shoot bullets on mouse down hero.shoot(); // Move hero to mouse down position hero.x = x; // Restrict player movement to only sideways hero.y = 2732 - 150; }; // Spawn enemies at intervals var enemySpawnInterval = LK.setInterval(spawnEnemy, 1000); game.move = function (x, y, obj) { if (obj.target === joystick) { joystick.move(x, y, obj); } // Move hero to mouse move position hero.x = x; // Restrict player movement to only sideways hero.y = 2732 - 150; }; game.up = function (x, y, obj) { if (obj.target === joystick) { joystick.up(x, y, obj); } };
/****
* Classes
****/
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Hero class representing the player
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
// Play the bullet sound
LK.getSound('BulletSoundoflaser1').play();
};
// Add joystick and keyboard control for player movement
if (game.keyboard) {
if (game.keyboard.isDown('A')) {
self.x -= self.speed / 2;
} else if (game.keyboard.isDown('D')) {
self.x += self.speed / 2;
}
} else if (joystick.active) {
self.x += joystick.deltaX * (self.speed / 4);
}
// Restrict player movement to only sideways
self.y = 2732 - 150;
};
});
// HeroBullet class representing bullets shot by the hero
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
var Joystick = Container.expand(function () {
var self = Container.call(this);
var joystickGraphics = self.attachAsset('joystick', {
anchorX: 0.5,
anchorY: 0.5
});
self.active = false;
self.startX = 0;
self.startY = 0;
self.down = function (x, y, obj) {
self.active = true;
self.startX = x;
self.startY = y;
};
self.move = function (x, y, obj) {
if (self.active) {
self.deltaX = x - self.startX;
self.startX = x;
}
};
self.up = function (x, y, obj) {
self.active = false;
};
});
// MainVillain class representing the main villain
var MainVillain = Container.expand(function () {
var self = Container.call(this);
var mainVillainGraphics = self.attachAsset('mainVillain', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 5; // Takes 5 bullets to destroy
self.update = function () {
// Fire missile continuously
if (LK.ticks % 60 == 0) {
var missile = new VillainMissile();
missile.x = self.x;
missile.y = self.y + mainVillainGraphics.height / 2;
game.addChild(missile);
villainMissiles.push(missile);
// Play the 'enemymissilesound1' sound
LK.getSound('enemymissilesound1').play();
}
};
});
// VillainMissile class representing missiles fired by the main villain
var VillainMissile = Container.expand(function () {
var self = Container.call(this);
var missileGraphics = self.attachAsset('villainMissile', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var backgroundImage = game.addChild(LK.getAsset('backgroundImage', {}));
var hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 150;
game.addChild(hero);
var joystick = new Joystick();
joystick.x = 200; // Position joystick on the screen
joystick.y = 2732 - 200;
game.addChild(joystick);
if (enemies && enemies.length > 0 && heroBullets && heroBullets.length > 0) {
LK.getSound('Backgroundsound1').play({
loop: true
});
}
// Initialize the score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var enemies = [];
var heroBullets = [];
var villainMissiles = [];
var mainVillain = null;
// Function to spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -50;
game.addChild(enemy);
enemies.push(enemy);
}
// Handle game updates
game.update = function () {
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (heroBullets[i].y < 0) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Update enemies
if (!mainVillain) {
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (enemies[j].y > 2732) {
enemies[j].destroy();
enemies.splice(j, 1);
// Display a game over message and end the game
LK.showGameOver("Game Over! Enemy reached the boundary.");
}
}
}
// Update villain missiles
for (var n = villainMissiles.length - 1; n >= 0; n--) {
villainMissiles[n].update();
if (villainMissiles[n].intersects(hero)) {
// Display a losing message and end the game
LK.showGameOver("You died!");
break;
}
}
// Update main villain
if (mainVillain) {
mainVillain.update();
}
// Check for collisions between hero bullets and enemies
for (var k = heroBullets.length - 1; k >= 0; k--) {
if (mainVillain && heroBullets[k].intersects(mainVillain)) {
heroBullets[k].destroy();
heroBullets.splice(k, 1);
mainVillain.health -= 1;
if (mainVillain.health <= 0) {
mainVillain.destroy();
mainVillain = null;
// Update the score
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
// Display a winning message and end the game
LK.showGameOver("Congratulations! You won the game!");
}
break;
}
for (var l = enemies.length - 1; l >= 0; l--) {
if (heroBullets[k].intersects(enemies[l])) {
heroBullets[k].destroy();
enemies[l].destroy();
heroBullets.splice(k, 1);
enemies.splice(l, 1);
// Update the score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
// Check if the score reaches 490
if (LK.getScore() == 490) {
// Spawn the main villain
mainVillain = new MainVillain();
mainVillain.x = 2048 / 2;
mainVillain.y = -50;
game.addChild(mainVillain);
}
break;
}
}
}
// Check for collisions between hero and enemies
for (var m = enemies.length - 1; m >= 0; m--) {
if (hero.intersects(enemies[m])) {
// Display a losing message and end the game
LK.showGameOver("You died!");
break;
}
}
};
// Handle touch events for shooting
game.down = function (x, y, obj) {
if (obj.target === joystick) {
joystick.down(x, y, obj);
}
// Shoot bullets on mouse down
hero.shoot();
// Move hero to mouse down position
hero.x = x;
// Restrict player movement to only sideways
hero.y = 2732 - 150;
};
// Spawn enemies at intervals
var enemySpawnInterval = LK.setInterval(spawnEnemy, 1000);
game.move = function (x, y, obj) {
if (obj.target === joystick) {
joystick.move(x, y, obj);
}
// Move hero to mouse move position
hero.x = x;
// Restrict player movement to only sideways
hero.y = 2732 - 150;
};
game.up = function (x, y, obj) {
if (obj.target === joystick) {
joystick.up(x, y, obj);
}
};