/**** * 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 }); }); // Class for the leprechaun var Leprechaun = Container.expand(function () { var self = Container.call(this); var leprechaunGraphics = self.attachAsset('leprechaun', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 100; self.lives = 3; self.update = function () { // Leprechaun update logic // Calculate direction towards the player var dx = player.x - self.x; var dy = player.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); // Run away from the player self.direction = Math.atan2(dy, dx) + Math.PI; // Move in the current direction self.x += Math.cos(self.direction) * self.speed; self.y += Math.sin(self.direction) * self.speed; // Keep the leprechaun within the game bounds and away from corners if (self.x < 200) { self.x = 200; } if (self.y < 200) { self.y = 200; } if (self.x > 1848) { self.x = 1848; } if (self.y > 2532) { self.y = 2532; } // Make the leprechaun run away from the player if (distance < 500) { self.speed = 10; } else { self.speed = 5; } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for the player character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { // Player update logic }; }); // Class for the wall var Wall = Container.expand(function () { var self = Container.call(this); var wallGraphics = self.attachAsset('wall', { anchorX: 0.5, anchorY: 0.5 }); }); /**** * Initialize Game ****/ // Initialize a Text2 object to display the player's coin count var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize a Text2 object to display the player's coin count // Initialize player and leprechaun var coinCountText = new Text2('0/30', { size: 150, fill: 0xFFFFFF }); coinCountText.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text. LK.gui.top.addChild(coinCountText); var player = game.addChild(new Player()); player.x = 1024; // Center horizontally player.y = 1366; // Center vertically var leprechaun = game.addChild(new Leprechaun()); leprechaun.x = Math.random() * 2048; leprechaun.y = Math.random() * 2732; // Game update logic game.update = function () { // Update player and leprechaun positions player.update(); leprechaun.update(); // Check for intersection if (player.intersects(leprechaun)) { // Player has trapped the leprechaun leprechaun.lives--; if (leprechaun.lives <= 0) { // Remove the current leprechaun game.removeChild(leprechaun); // Create a new leprechaun leprechaun = game.addChild(new Leprechaun()); leprechaun.x = Math.random() * 2048; leprechaun.y = Math.random() * 2732; // Add 5 coins when leprechaun is caught for (var i = 0; i < 5; i++) { var coin = game.addChild(new Coin()); coin.x = Math.random() * 2048; coin.y = Math.random() * 2732; } } } // End game when player collects 30 coins var coins = game.children.filter(function (child) { return child instanceof Coin; }); var collectedCoins = coins.filter(function (coin) { return player.intersects(coin); }); collectedCoins.forEach(function (coin) { game.removeChild(coin); LK.setScore(LK.getScore() + 1); // Add point to coin count coinCountText.setText(LK.getScore() + '/30'); // Update the coin count text }); if (LK.getScore() >= 30) { LK.showYouWin(); } }; // Handle player movement game.move = function (x, y, obj) { // Move the player player.x = x; player.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
});
});
// Class for the leprechaun
var Leprechaun = Container.expand(function () {
var self = Container.call(this);
var leprechaunGraphics = self.attachAsset('leprechaun', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 100;
self.lives = 3;
self.update = function () {
// Leprechaun update logic
// Calculate direction towards the player
var dx = player.x - self.x;
var dy = player.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Run away from the player
self.direction = Math.atan2(dy, dx) + Math.PI;
// Move in the current direction
self.x += Math.cos(self.direction) * self.speed;
self.y += Math.sin(self.direction) * self.speed;
// Keep the leprechaun within the game bounds and away from corners
if (self.x < 200) {
self.x = 200;
}
if (self.y < 200) {
self.y = 200;
}
if (self.x > 1848) {
self.x = 1848;
}
if (self.y > 2532) {
self.y = 2532;
}
// Make the leprechaun run away from the player
if (distance < 500) {
self.speed = 10;
} else {
self.speed = 5;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
// Player update logic
};
});
// Class for the wall
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
// Initialize a Text2 object to display the player's coin count
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize a Text2 object to display the player's coin count
// Initialize player and leprechaun
var coinCountText = new Text2('0/30', {
size: 150,
fill: 0xFFFFFF
});
coinCountText.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(coinCountText);
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 1366; // Center vertically
var leprechaun = game.addChild(new Leprechaun());
leprechaun.x = Math.random() * 2048;
leprechaun.y = Math.random() * 2732;
// Game update logic
game.update = function () {
// Update player and leprechaun positions
player.update();
leprechaun.update();
// Check for intersection
if (player.intersects(leprechaun)) {
// Player has trapped the leprechaun
leprechaun.lives--;
if (leprechaun.lives <= 0) {
// Remove the current leprechaun
game.removeChild(leprechaun);
// Create a new leprechaun
leprechaun = game.addChild(new Leprechaun());
leprechaun.x = Math.random() * 2048;
leprechaun.y = Math.random() * 2732;
// Add 5 coins when leprechaun is caught
for (var i = 0; i < 5; i++) {
var coin = game.addChild(new Coin());
coin.x = Math.random() * 2048;
coin.y = Math.random() * 2732;
}
}
}
// End game when player collects 30 coins
var coins = game.children.filter(function (child) {
return child instanceof Coin;
});
var collectedCoins = coins.filter(function (coin) {
return player.intersects(coin);
});
collectedCoins.forEach(function (coin) {
game.removeChild(coin);
LK.setScore(LK.getScore() + 1); // Add point to coin count
coinCountText.setText(LK.getScore() + '/30'); // Update the coin count text
});
if (LK.getScore() >= 30) {
LK.showYouWin();
}
};
// Handle player movement
game.move = function (x, y, obj) {
// Move the player
player.x = x;
player.y = y;
};