/**** * Classes ****/ // Class for Elemental Bullets var ElementalBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.y += self.speed; }; }); // Class for Enemy characters var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for the enemy }; }); //<Assets used in the game will automatically appear here> // Class for the Hero character var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for the hero }; self.down = function (x, y, obj) { // Logic for when the hero is pressed }; self.up = function (x, y, obj) { // Logic for when the hero is released }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var enemies = []; var bullets = []; var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Function to handle game updates game.update = function () { // Update hero hero.update(); // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (enemies[i].intersects(hero)) { // Handle collision with hero LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update bullets for (var j = bullets.length - 1; j >= 0; j--) { bullets[j].update(); for (var k = enemies.length - 1; k >= 0; k--) { if (bullets[j].intersects(enemies[k])) { // Handle bullet hitting enemy bullets[j].destroy(); bullets.splice(j, 1); enemies[k].destroy(); enemies.splice(k, 1); break; } } } // Fire bullets if (LK.ticks % 30 == 0) { var newBullet = new ElementalBullet(); newBullet.x = hero.x; newBullet.y = hero.y; bullets.push(newBullet); game.addChild(newBullet); } }; // Function to spawn enemies function spawnEnemy() { var newEnemy = new Enemy(); newEnemy.x = Math.random() * 2048; newEnemy.y = 0; enemies.push(newEnemy); game.addChild(newEnemy); } // Set interval to spawn enemies LK.setInterval(spawnEnemy, 2000); // Handle touch events for hero movement game.down = function (x, y, obj) { hero.x = x; hero.y = y; }; game.move = function (x, y, obj) { hero.x = x; hero.y = y; }; game.up = function (x, y, obj) { // Logic for when touch is released };
/****
* Classes
****/
// Class for Elemental Bullets
var ElementalBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.y += self.speed;
};
});
// Class for Enemy characters
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Update logic for the enemy
};
});
//<Assets used in the game will automatically appear here>
// Class for the Hero character
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Update logic for the hero
};
self.down = function (x, y, obj) {
// Logic for when the hero is pressed
};
self.up = function (x, y, obj) {
// Logic for when the hero is released
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var enemies = [];
var bullets = [];
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Function to handle game updates
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
// Handle collision with hero
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
for (var k = enemies.length - 1; k >= 0; k--) {
if (bullets[j].intersects(enemies[k])) {
// Handle bullet hitting enemy
bullets[j].destroy();
bullets.splice(j, 1);
enemies[k].destroy();
enemies.splice(k, 1);
break;
}
}
}
// Fire bullets
if (LK.ticks % 30 == 0) {
var newBullet = new ElementalBullet();
newBullet.x = hero.x;
newBullet.y = hero.y;
bullets.push(newBullet);
game.addChild(newBullet);
}
};
// Function to spawn enemies
function spawnEnemy() {
var newEnemy = new Enemy();
newEnemy.x = Math.random() * 2048;
newEnemy.y = 0;
enemies.push(newEnemy);
game.addChild(newEnemy);
}
// Set interval to spawn enemies
LK.setInterval(spawnEnemy, 2000);
// Handle touch events for hero movement
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.up = function (x, y, obj) {
// Logic for when touch is released
};