/**** * Classes ****/ // Define the Coin class var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Define the 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 = 10; self.coins = 0; // Initialize the number of coins the player has self.update = function () { // Player movement logic if (self.targetX && self.targetY) { var dx = self.targetX - self.x; var dy = self.targetY - self.y; var angle = Math.atan2(dy, dx); self.x += Math.cos(angle) * self.speed; self.y += Math.sin(angle) * self.speed; } if (self.x < 0) { self.x = 0; } if (self.x > 2048) { self.x = 2048; } }; }); // Define the Zombie class var Zombie = Container.expand(function () { var self = Container.call(this); var zombieGraphics = self.attachAsset('zombie', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -100; self.x = Math.random() * 2048; } }; }); /**** * 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 = 1024; player.y = 2400; // Initialize zombies var zombies = []; for (var i = 0; i < 5; i++) { var zombie = new Zombie(); zombie.x = Math.random() * 2048; zombie.y = Math.random() * -2732; zombies.push(zombie); game.addChild(zombie); } // Initialize the first coin var coin = new Coin(); coin.x = Math.random() * 2048; coin.y = Math.random() * 2732; game.addChild(coin); // Handle player movement game.down = function (x, y, obj) { // Set the target position for the player player.targetX = x; player.targetY = y; }; // Update game state game.update = function () { player.update(); zombies.forEach(function (zombie) { zombie.update(); if (player.intersects(zombie)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } if (player.intersects(coin)) { // Remove the old coin game.removeChild(coin); // Increase the player's coin count player.coins += 1; // Check if player has collected 23 coins if (player.coins >= 23) { // End the game LK.showYouWin(); } else { // Add a new coin at a random position coin = new Coin(); coin.x = Math.random() * 2048; coin.y = Math.random() * 2732; game.addChild(coin); } } }); };
/****
* Classes
****/
// Define the Coin class
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the 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 = 10;
self.coins = 0; // Initialize the number of coins the player has
self.update = function () {
// Player movement logic
if (self.targetX && self.targetY) {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
}
if (self.x < 0) {
self.x = 0;
}
if (self.x > 2048) {
self.x = 2048;
}
};
});
// Define the Zombie class
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.attachAsset('zombie', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -100;
self.x = Math.random() * 2048;
}
};
});
/****
* 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 = 1024;
player.y = 2400;
// Initialize zombies
var zombies = [];
for (var i = 0; i < 5; i++) {
var zombie = new Zombie();
zombie.x = Math.random() * 2048;
zombie.y = Math.random() * -2732;
zombies.push(zombie);
game.addChild(zombie);
}
// Initialize the first coin
var coin = new Coin();
coin.x = Math.random() * 2048;
coin.y = Math.random() * 2732;
game.addChild(coin);
// Handle player movement
game.down = function (x, y, obj) {
// Set the target position for the player
player.targetX = x;
player.targetY = y;
};
// Update game state
game.update = function () {
player.update();
zombies.forEach(function (zombie) {
zombie.update();
if (player.intersects(zombie)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (player.intersects(coin)) {
// Remove the old coin
game.removeChild(coin);
// Increase the player's coin count
player.coins += 1;
// Check if player has collected 23 coins
if (player.coins >= 23) {
// End the game
LK.showYouWin();
} else {
// Add a new coin at a random position
coin = new Coin();
coin.x = Math.random() * 2048;
coin.y = Math.random() * 2732;
game.addChild(coin);
}
}
});
};