/**** * Classes ****/ // Class for the Coin var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for coin }; }); // Class for the Diamond var Diamond = Container.expand(function () { var self = Container.call(this); var diamondGraphics = self.attachAsset('diamond', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for diamond }; }); // Class for the Emerald var Emerald = Container.expand(function () { var self = Container.call(this); var emeraldGraphics = self.attachAsset('emerald', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Update logic for emerald }; }); // Assets will be automatically created and loaded by the LK engine based on their usage in the code. // Class for the Hero var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Update logic for hero }; }); // Class for the Zombie var Zombie = Container.expand(function () { var self = Container.call(this); var zombieGraphics = self.attachAsset('zombie', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { // Update logic for zombie }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var zombies = []; var coins = []; var diamonds = []; var emeralds = []; var hero = game.addChild(new Hero()); hero.x = 1024; // Center horizontally hero.y = 1366; // Center vertically // Function to spawn a zombie function spawnZombie() { var zombie = new Zombie(); zombie.x = Math.random() * 2048; zombie.y = Math.random() * 2732; zombies.push(zombie); game.addChild(zombie); } // Function to spawn a coin function spawnCoin() { var coin = new Coin(); coin.x = Math.random() * 2048; coin.y = Math.random() * 2732; coins.push(coin); game.addChild(coin); } // Function to spawn a diamond function spawnDiamond() { var diamond = new Diamond(); diamond.x = Math.random() * 2048; diamond.y = Math.random() * 2732; diamonds.push(diamond); game.addChild(diamond); } // Function to spawn an emerald function spawnEmerald() { var emerald = new Emerald(); emerald.x = Math.random() * 2048; emerald.y = Math.random() * 2732; emeralds.push(emerald); game.addChild(emerald); } // Game update function game.update = function () { // Update hero hero.update(); // Update zombies for (var i = zombies.length - 1; i >= 0; i--) { zombies[i].update(); if (hero.intersects(zombies[i])) { // Handle collision with zombie zombies[i].destroy(); zombies.splice(i, 1); } } // Update coins for (var i = coins.length - 1; i >= 0; i--) { coins[i].update(); if (hero.intersects(coins[i])) { // Handle collision with coin coins[i].destroy(); coins.splice(i, 1); } } // Update diamonds for (var i = diamonds.length - 1; i >= 0; i--) { diamonds[i].update(); if (hero.intersects(diamonds[i])) { // Handle collision with diamond diamonds[i].destroy(); diamonds.splice(i, 1); } } // Update emeralds for (var i = emeralds.length - 1; i >= 0; i--) { emeralds[i].update(); if (hero.intersects(emeralds[i])) { // Handle collision with emerald emeralds[i].destroy(); emeralds.splice(i, 1); } } }; // Spawn initial entities spawnZombie(); spawnCoin(); spawnDiamond(); spawnEmerald(); // Set up game controls game.down = function (x, y, obj) { // Move hero to the touch position hero.x = x; hero.y = y; };
/****
* Classes
****/
// Class for the Coin
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Update logic for coin
};
});
// Class for the Diamond
var Diamond = Container.expand(function () {
var self = Container.call(this);
var diamondGraphics = self.attachAsset('diamond', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Update logic for diamond
};
});
// Class for the Emerald
var Emerald = Container.expand(function () {
var self = Container.call(this);
var emeraldGraphics = self.attachAsset('emerald', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Update logic for emerald
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Class for the Hero
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Update logic for hero
};
});
// Class for the Zombie
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.attachAsset('zombie', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
// Update logic for zombie
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var zombies = [];
var coins = [];
var diamonds = [];
var emeralds = [];
var hero = game.addChild(new Hero());
hero.x = 1024; // Center horizontally
hero.y = 1366; // Center vertically
// Function to spawn a zombie
function spawnZombie() {
var zombie = new Zombie();
zombie.x = Math.random() * 2048;
zombie.y = Math.random() * 2732;
zombies.push(zombie);
game.addChild(zombie);
}
// Function to spawn a coin
function spawnCoin() {
var coin = new Coin();
coin.x = Math.random() * 2048;
coin.y = Math.random() * 2732;
coins.push(coin);
game.addChild(coin);
}
// Function to spawn a diamond
function spawnDiamond() {
var diamond = new Diamond();
diamond.x = Math.random() * 2048;
diamond.y = Math.random() * 2732;
diamonds.push(diamond);
game.addChild(diamond);
}
// Function to spawn an emerald
function spawnEmerald() {
var emerald = new Emerald();
emerald.x = Math.random() * 2048;
emerald.y = Math.random() * 2732;
emeralds.push(emerald);
game.addChild(emerald);
}
// Game update function
game.update = function () {
// Update hero
hero.update();
// Update zombies
for (var i = zombies.length - 1; i >= 0; i--) {
zombies[i].update();
if (hero.intersects(zombies[i])) {
// Handle collision with zombie
zombies[i].destroy();
zombies.splice(i, 1);
}
}
// Update coins
for (var i = coins.length - 1; i >= 0; i--) {
coins[i].update();
if (hero.intersects(coins[i])) {
// Handle collision with coin
coins[i].destroy();
coins.splice(i, 1);
}
}
// Update diamonds
for (var i = diamonds.length - 1; i >= 0; i--) {
diamonds[i].update();
if (hero.intersects(diamonds[i])) {
// Handle collision with diamond
diamonds[i].destroy();
diamonds.splice(i, 1);
}
}
// Update emeralds
for (var i = emeralds.length - 1; i >= 0; i--) {
emeralds[i].update();
if (hero.intersects(emeralds[i])) {
// Handle collision with emerald
emeralds[i].destroy();
emeralds.splice(i, 1);
}
}
};
// Spawn initial entities
spawnZombie();
spawnCoin();
spawnDiamond();
spawnEmerald();
// Set up game controls
game.down = function (x, y, obj) {
// Move hero to the touch position
hero.x = x;
hero.y = y;
};