/**** * Classes ****/ // AIPlayer class representing the AI controlled players var AIPlayer = Container.expand(function () { var self = Container.call(this); var aiPlayerGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // AIPlayer update logic // AIPlayer moves randomly self.x += (Math.random() - 0.5) * self.speed; self.y += (Math.random() - 0.5) * self.speed; // Ensure AIPlayer stays within game boundaries if (self.x < 0) { self.x = 0; } if (self.x > 2048) { self.x = 2048; } if (self.y < 0) { self.y = 0; } if (self.y > 2732) { self.y = 2732; } // Add chat messages from AI players if (LK.ticks % 120 == 0) { var message = "AI Player at (" + self.x + ", " + self.y + ")"; chat.addMessage(message); } }; }); // Chat class representing the chat system var Chat = Container.expand(function () { var self = Container.call(this); self.messages = []; self.addMessage = function (message) { self.messages.push(message); // Display the last 10 messages var displayMessages = self.messages.slice(-10); var displayText = displayMessages.join('\n'); self.text = displayText; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Hero class representing the player character var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Hero update logic }; }); // Menu class representing the game menu var Menu = Container.expand(function () { var self = Container.call(this); var menuGraphics = self.attachAsset('menu', { anchorX: 0.5, anchorY: 0.5 }); self.visible = false; self.show = function () { self.visible = true; }; self.hide = function () { self.visible = false; }; }); // Multiplayer class to handle multiplayer mode var Multiplayer = Container.expand(function () { var self = Container.call(this); self.players = []; self.addPlayer = function (player) { self.players.push(player); }; self.removePlayer = function (player) { var index = self.players.indexOf(player); if (index > -1) { self.players.splice(index, 1); } }; self.update = function () { for (var i = 0; i < self.players.length; i++) { self.players[i].update(); } }; }); // Nextbot class representing the chasing enemies var Nextbot = Container.expand(function () { var self = Container.call(this); var nextbotGraphics = self.attachAsset('nextbot', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Calculate the direction vector from the nextbot to the hero var dx = hero.x - self.x; var dy = hero.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); // Normalize the direction vector dx /= distance; dy /= distance; // Move the nextbot towards the hero self.x += dx * self.speed; self.y += dy * self.speed; }; }); // OST class representing the game music var OST = Container.expand(function () { var self = Container.call(this); self.play = function () { // Play the game music LK.playMusic('Brat'); }; self.stop = function () { // Stop the game music LK.stopMusic(); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize game variables var hero; var nextbots = []; var score = 0; var scoreTxt; // Function to initialize game elements function initGame() { // Create and position the hero hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Create score text scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Initialize the game music ost = new OST(); ost.play(); // Initialize the game menu menu = new Menu(); game.addChild(menu); // Initialize the chat system chat = new Chat(); game.addChild(chat); // Initialize multiplayer mode multiplayer = new Multiplayer(); // Create and position multiple AIPlayers aiPlayers = []; for (var i = 0; i < 5; i++) { var aiPlayer = game.addChild(new AIPlayer()); aiPlayer.x = Math.random() * 2048; aiPlayer.y = Math.random() * 2732; multiplayer.addPlayer(aiPlayer); } // Spawn initial nextbots spawnNextbot(); } // Function to spawn a nextbot function spawnNextbot() { var nextbot = new Nextbot(); nextbot.x = Math.random() * 2048; nextbot.y = -100; // Start above the screen nextbots.push(nextbot); game.addChild(nextbot); } // Handle game updates game.update = function () { // Update hero and AIPlayer position based on touch input if (dragNode) { hero.x = dragNode.x; hero.y = dragNode.y; } // Update multiplayer mode multiplayer.update(); // Update nextbots for (var i = nextbots.length - 1; i >= 0; i--) { var nextbot = nextbots[i]; nextbot.y += nextbot.speed; // Check for collision with hero if (nextbot.intersects(hero)) { LK.effects.flashScreen(0xff0000, 1000); ost.stop(); LK.showGameOver(); } // Remove nextbots that are off-screen if (nextbot.y > 2732 + 100) { nextbot.destroy(); nextbots.splice(i, 1); } } // Spawn new nextbots periodically if (LK.ticks % 120 == 0) { spawnNextbot(); } }; // Handle touch input var dragNode = null; game.down = function (x, y, obj) { dragNode = { x: x, y: y }; // Hide the game menu menu.hide(); }; game.move = function (x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } }; game.up = function (x, y, obj) { dragNode = null; }; // Initialize the game initGame(); // Show the game menu menu.show();
/****
* Classes
****/
// AIPlayer class representing the AI controlled players
var AIPlayer = Container.expand(function () {
var self = Container.call(this);
var aiPlayerGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// AIPlayer update logic
// AIPlayer moves randomly
self.x += (Math.random() - 0.5) * self.speed;
self.y += (Math.random() - 0.5) * self.speed;
// Ensure AIPlayer stays within game boundaries
if (self.x < 0) {
self.x = 0;
}
if (self.x > 2048) {
self.x = 2048;
}
if (self.y < 0) {
self.y = 0;
}
if (self.y > 2732) {
self.y = 2732;
}
// Add chat messages from AI players
if (LK.ticks % 120 == 0) {
var message = "AI Player at (" + self.x + ", " + self.y + ")";
chat.addMessage(message);
}
};
});
// Chat class representing the chat system
var Chat = Container.expand(function () {
var self = Container.call(this);
self.messages = [];
self.addMessage = function (message) {
self.messages.push(message);
// Display the last 10 messages
var displayMessages = self.messages.slice(-10);
var displayText = displayMessages.join('\n');
self.text = displayText;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Hero class representing the player character
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Hero update logic
};
});
// Menu class representing the game menu
var Menu = Container.expand(function () {
var self = Container.call(this);
var menuGraphics = self.attachAsset('menu', {
anchorX: 0.5,
anchorY: 0.5
});
self.visible = false;
self.show = function () {
self.visible = true;
};
self.hide = function () {
self.visible = false;
};
});
// Multiplayer class to handle multiplayer mode
var Multiplayer = Container.expand(function () {
var self = Container.call(this);
self.players = [];
self.addPlayer = function (player) {
self.players.push(player);
};
self.removePlayer = function (player) {
var index = self.players.indexOf(player);
if (index > -1) {
self.players.splice(index, 1);
}
};
self.update = function () {
for (var i = 0; i < self.players.length; i++) {
self.players[i].update();
}
};
});
// Nextbot class representing the chasing enemies
var Nextbot = Container.expand(function () {
var self = Container.call(this);
var nextbotGraphics = self.attachAsset('nextbot', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
// Calculate the direction vector from the nextbot to the hero
var dx = hero.x - self.x;
var dy = hero.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize the direction vector
dx /= distance;
dy /= distance;
// Move the nextbot towards the hero
self.x += dx * self.speed;
self.y += dy * self.speed;
};
});
// OST class representing the game music
var OST = Container.expand(function () {
var self = Container.call(this);
self.play = function () {
// Play the game music
LK.playMusic('Brat');
};
self.stop = function () {
// Stop the game music
LK.stopMusic();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var hero;
var nextbots = [];
var score = 0;
var scoreTxt;
// Function to initialize game elements
function initGame() {
// Create and position the hero
hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Create score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize the game music
ost = new OST();
ost.play();
// Initialize the game menu
menu = new Menu();
game.addChild(menu);
// Initialize the chat system
chat = new Chat();
game.addChild(chat);
// Initialize multiplayer mode
multiplayer = new Multiplayer();
// Create and position multiple AIPlayers
aiPlayers = [];
for (var i = 0; i < 5; i++) {
var aiPlayer = game.addChild(new AIPlayer());
aiPlayer.x = Math.random() * 2048;
aiPlayer.y = Math.random() * 2732;
multiplayer.addPlayer(aiPlayer);
}
// Spawn initial nextbots
spawnNextbot();
}
// Function to spawn a nextbot
function spawnNextbot() {
var nextbot = new Nextbot();
nextbot.x = Math.random() * 2048;
nextbot.y = -100; // Start above the screen
nextbots.push(nextbot);
game.addChild(nextbot);
}
// Handle game updates
game.update = function () {
// Update hero and AIPlayer position based on touch input
if (dragNode) {
hero.x = dragNode.x;
hero.y = dragNode.y;
}
// Update multiplayer mode
multiplayer.update();
// Update nextbots
for (var i = nextbots.length - 1; i >= 0; i--) {
var nextbot = nextbots[i];
nextbot.y += nextbot.speed;
// Check for collision with hero
if (nextbot.intersects(hero)) {
LK.effects.flashScreen(0xff0000, 1000);
ost.stop();
LK.showGameOver();
}
// Remove nextbots that are off-screen
if (nextbot.y > 2732 + 100) {
nextbot.destroy();
nextbots.splice(i, 1);
}
}
// Spawn new nextbots periodically
if (LK.ticks % 120 == 0) {
spawnNextbot();
}
};
// Handle touch input
var dragNode = null;
game.down = function (x, y, obj) {
dragNode = {
x: x,
y: y
};
// Hide the game menu
menu.hide();
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Initialize the game
initGame();
// Show the game menu
menu.show();