/****
* Classes
****/
// Define a Collectible class
var Collectible = Container.expand(function () {
var self = Container.call(this);
var collectibleGraphics = self.attachAsset('collectible', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Collectible update logic
};
});
// Define an Enemy class
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 () {
// Enemy update logic
};
});
//<Assets used in the game will automatically appear here>
// Define a Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * 2732;
enemies.push(enemy);
game.addChild(enemy);
}
// Initialize collectibles
var collectibles = [];
for (var i = 0; i < 10; i++) {
var collectible = new Collectible();
collectible.x = Math.random() * 2048;
collectible.y = Math.random() * 2732;
collectibles.push(collectible);
game.addChild(collectible);
}
// Handle player movement
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
// Update game logic
game.update = function () {
// Update player
player.update();
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
if (player.intersects(enemies[i])) {
// Handle player-enemy collision
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update collectibles
for (var j = 0; j < collectibles.length; j++) {
collectibles[j].update();
if (player.intersects(collectibles[j])) {
// Handle player-collectible collision
LK.setScore(LK.getScore() + 1);
collectibles[j].destroy();
collectibles.splice(j, 1);
}
}
};