/**** * Classes ****/ // Class for the enemy (Teacher Todd) var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { // Enemy update logic }; }); // Class for the collectible notebooks var Notebook = Container.expand(function () { var self = Container.call(this); var notebookGraphics = self.attachAsset('notebook', { anchorX: 0.5, anchorY: 0.5 }); }); //<Assets used in the game will automatically appear 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 = 5; self.update = function () { // Player update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xFFA500 //Init game with orange background }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; // Initialize enemy var enemy = game.addChild(new Enemy()); enemy.x = 100; enemy.y = 100; // Initialize notebooks var notebooks = []; for (var i = 0; i < 12; i++) { var notebook = new Notebook(); notebook.x = Math.random() * 2048; notebook.y = Math.random() * 2732; notebooks.push(notebook); game.addChild(notebook); } // Score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Game variables var score = 0; var dragNode = null; // Update score function updateScore() { scoreTxt.setText(score); } // Handle player movement function handleMove(x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } // Check for collision with notebooks for (var i = notebooks.length - 1; i >= 0; i--) { if (player.intersects(notebooks[i])) { score++; updateScore(); notebooks[i].destroy(); notebooks.splice(i, 1); } } // Check for collision with enemy if (player.intersects(enemy)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Check for win condition if (score >= 12) { var winText = new Text2('You Win!', { size: 150, fill: "#ffffff" }); winText.anchor.set(0.5, 0); LK.gui.center.addChild(winText); } } // Mouse or touch down on the game game.down = function (x, y, obj) { dragNode = player; handleMove(x, y, obj); }; // Mouse or touch up on the game game.up = function (x, y, obj) { dragNode = null; }; // Mouse or touch move on the game game.move = handleMove; // Game update loop game.update = function () { player.update(); enemy.update(); // Enemy follows player var dx = player.x - enemy.x; var dy = player.y - enemy.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist > 0) { enemy.x += dx / dist * enemy.speed; enemy.y += dy / dist * enemy.speed; } };
/****
* Classes
****/
// Class for the enemy (Teacher Todd)
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
// Enemy update logic
};
});
// Class for the collectible notebooks
var Notebook = Container.expand(function () {
var self = Container.call(this);
var notebookGraphics = self.attachAsset('notebook', {
anchorX: 0.5,
anchorY: 0.5
});
});
//<Assets used in the game will automatically appear 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 = 5;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFA500 //Init game with orange background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
// Initialize enemy
var enemy = game.addChild(new Enemy());
enemy.x = 100;
enemy.y = 100;
// Initialize notebooks
var notebooks = [];
for (var i = 0; i < 12; i++) {
var notebook = new Notebook();
notebook.x = Math.random() * 2048;
notebook.y = Math.random() * 2732;
notebooks.push(notebook);
game.addChild(notebook);
}
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game variables
var score = 0;
var dragNode = null;
// Update score
function updateScore() {
scoreTxt.setText(score);
}
// Handle player movement
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
// Check for collision with notebooks
for (var i = notebooks.length - 1; i >= 0; i--) {
if (player.intersects(notebooks[i])) {
score++;
updateScore();
notebooks[i].destroy();
notebooks.splice(i, 1);
}
}
// Check for collision with enemy
if (player.intersects(enemy)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Check for win condition
if (score >= 12) {
var winText = new Text2('You Win!', {
size: 150,
fill: "#ffffff"
});
winText.anchor.set(0.5, 0);
LK.gui.center.addChild(winText);
}
}
// Mouse or touch down on the game
game.down = function (x, y, obj) {
dragNode = player;
handleMove(x, y, obj);
};
// Mouse or touch up on the game
game.up = function (x, y, obj) {
dragNode = null;
};
// Mouse or touch move on the game
game.move = handleMove;
// Game update loop
game.update = function () {
player.update();
enemy.update();
// Enemy follows player
var dx = player.x - enemy.x;
var dy = player.y - enemy.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 0) {
enemy.x += dx / dist * enemy.speed;
enemy.y += dy / dist * enemy.speed;
}
};