User prompt
change the mode 1 text to normal mode
User prompt
remove SELECT GAME MODE text at the gamemode selection
User prompt
remove the NIGHT OF THE MASSACRE text at the gamemode selection
User prompt
change the NIGHT OF THE MASSACRE text to NIGHT OF THE MASSACRE 2
User prompt
decrease the NIGHT OF THE MASSACRE size
User prompt
make the NIGHT OF THE MASSACRE text to the start game screen
User prompt
make the gamemode selection appear after start game clicked
User prompt
add a gamemode selection
User prompt
Please fix the bug: 'ReferenceError: npcs is not defined' in or related to this line: 'for (var i = 0; i < npcs.length; i++) {' Line Number: 62
User prompt
Please fix the bug: 'ReferenceError: npcs is not defined' in or related to this line: 'for (var i = 0; i < npcs.length; i++) {' Line Number: 62
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'x')' in or related to this line: 'bullet.x = player.x;' Line Number: 270
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of null (setting 'x')' in or related to this line: 'if (player) {' Line Number: 262
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of null (setting 'x')' in or related to this line: 'player.x = x;' Line Number: 262
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'x')' in or related to this line: 'player.x = x;' Line Number: 262
User prompt
Please fix the bug: 'ReferenceError: bullets is not defined' in or related to this line: 'for (var j = bullets.length - 1; j >= 0; j--) {' Line Number: 291
User prompt
Please fix the bug: 'ReferenceError: bullets is not defined' in or related to this line: 'npcs.splice(j, 1); // Remove NPC from array' Line Number: 287
User prompt
Please fix the bug: 'Uncaught ReferenceError: player is not defined' in or related to this line: 'player.x = x;' Line Number: 262
User prompt
Please fix the bug: 'Uncaught ReferenceError: player is not defined' in or related to this line: 'player.x = x;' Line Number: 262
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'for (var i = 0; i < enemies.length; i++) {' Line Number: 276
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'for (var i = 0; i < enemies.length; i++) {' Line Number: 276
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'for (var i = 0; i < enemies.length; i++) {' Line Number: 276
User prompt
make a menu screen
User prompt
remove bullet
User prompt
add menu screen
User prompt
increase enemy speed and NPC to 20
/**** * Classes ****/ // Define a Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; }; }); // Define an Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyBody = self.attachAsset('enemyBody', { anchorX: 0.5, anchorY: 0.5 }); var enemyArms = self.attachAsset('enemyArms', { anchorX: 0.5, anchorY: 0.5 }); var enemyLegs = self.attachAsset('enemyLegs', { anchorX: 0.5, anchorY: 0.5 }); var enemyHead = self.attachAsset('enemyHead', { anchorX: 0.5, anchorY: 0.5 }); enemyArms.y = -enemyBody.height / 4; enemyLegs.y = enemyBody.height / 2; enemyHead.y = -enemyBody.height / 2; self.speed = 6; self.update = function () { // Calculate the direction vector between enemy and NPCs var dxNpc = 0; var dyNpc = 0; var closestNpc = null; var closestDistance = Infinity; for (var i = 0; i < npcs.length; i++) { var dx = npcs[i].x - self.x; var dy = npcs[i].y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < closestDistance) { closestNpc = npcs[i]; closestDistance = distance; } } if (closestNpc) { dxNpc = closestNpc.x - self.x; dyNpc = closestNpc.y - self.y; var magnitudeNpc = Math.sqrt(dxNpc * dxNpc + dyNpc * dyNpc); // Normalize the direction vector dxNpc /= magnitudeNpc; dyNpc /= magnitudeNpc; // Move the enemy towards the closest NPC self.x += dxNpc * self.speed; self.y += dyNpc * self.speed; } else { // Calculate the direction vector between enemy and player var dxPlayer = player.x - self.x; var dyPlayer = player.y - self.y; var magnitudePlayer = Math.sqrt(dxPlayer * dxPlayer + dyPlayer * dyPlayer); // Normalize the direction vector dxPlayer /= magnitudePlayer; dyPlayer /= magnitudePlayer; // Move the enemy towards the player self.x += dxPlayer * self.speed; self.y += dyPlayer * self.speed; } }; }); // Define a MenuScreen class var MenuScreen = Container.expand(function () { var self = Container.call(this); // Create a title text var titleText = new Text2('Night of the Massacre', { size: 200, fill: "#ffffff" }); titleText.anchor.set(0.5, 0.5); titleText.x = 2048 / 2; titleText.y = 2732 / 4; self.addChild(titleText); // Create a start button var startButton = new Text2('Start Game', { size: 150, fill: "#00ff00" }); startButton.anchor.set(0.5, 0.5); startButton.x = 2048 / 2; startButton.y = 2732 / 2; self.addChild(startButton); // Add event listener to start button startButton.down = function () { self.destroy(); // Remove menu screen initializeGame(); // Start the game }; }); // Define a NPC class var NPC = Container.expand(function () { var self = Container.call(this); var npcHead = self.attachAsset('playerHead', { anchorX: 0.5, anchorY: 0.5 }); npcHead.y = -80; var npcBody = self.attachAsset('playerBody', { anchorX: 0.5, anchorY: 0.5 }); var npcArms = self.attachAsset('playerArms', { anchorX: 0.5, anchorY: 0.5 }); npcArms.y = -30; var npcLegs = self.attachAsset('playerLegs', { anchorX: 0.5, anchorY: 0.5 }); npcLegs.y = 40; self.speed = 4; self.update = function () { // Randomly change direction every 60 frames if (LK.ticks % 60 == 0) { self.direction = Math.random() * Math.PI * 2; } // Calculate dx and dy based on the current direction var dx = Math.cos(self.direction); var dy = Math.sin(self.direction); // Move the NPC self.x += dx * self.speed; self.y += dy * self.speed; // Ensure NPC stays within game boundaries if (self.x < 0) { self.x = 0; self.direction = Math.PI - self.direction; } if (self.x > 2048) { self.x = 2048; self.direction = Math.PI - self.direction; } if (self.y < 0) { self.y = 0; self.direction = -self.direction; } if (self.y > 2732) { self.y = 2732; self.direction = -self.direction; } }; }); //<Assets used in the game will automatically appear here> // Define a Player class var Player = Container.expand(function () { var self = Container.call(this); var playerHead = self.attachAsset('playerHead', { anchorX: 0.5, anchorY: 0.5 }); playerHead.y = -80; var playerBody = self.attachAsset('playerBody', { anchorX: 0.5, anchorY: 0.5 }); var playerArms = self.attachAsset('playerArms', { anchorX: 0.5, anchorY: 0.5 }); playerArms.y = -30; var playerLegs = self.attachAsset('playerLegs', { anchorX: 0.5, anchorY: 0.5 }); playerLegs.y = 40; self.speed = 5; self.update = function () { // Player update logic }; }); // Define a Tree class var Tree = Container.expand(function () { var self = Container.call(this); var treeTrunk = self.attachAsset('treeTrunk', { anchorX: 0.5, anchorY: 1 }); var treeLeaves = self.attachAsset('treeLeaves', { anchorX: 0.5, anchorY: 1 }); treeLeaves.y = -treeTrunk.height; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x0000FF //Init game with blue background }); /**** * Game Code ****/ // Add the menu screen to the game var menuScreen = game.addChild(new MenuScreen()); function initializeGame() { // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 150; // Initialize enemies var enemies = []; var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * 1000; enemies.push(enemy); game.addChild(enemy); // Initialize 30 trees and spread them across the game var trees = []; for (var i = 0; i < 30; i++) { var tree = game.addChild(new Tree()); tree.x = Math.random() * 2048; tree.y = Math.random() * 2732; trees.push(tree); } // Initialize NPCs var npcs = []; for (var i = 0; i < 20; i++) { var npc = game.addChild(new NPC()); npc.x = Math.random() * 2048; npc.y = Math.random() * 2732; npcs.push(npc); } // Initialize bullets var bullets = []; } // Handle player movement game.move = function (x, y, obj) { player.x = x; player.y = y; }; // Handle shooting game.down = function (x, y, obj) { var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y; bullets.push(bullet); game.addChild(bullet); }; // Game update loop game.update = function () { // Update enemies for (var i = 0; i < enemies.length; i++) { enemies[i].update(); if (player.intersects(enemies[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Check if any NPC intersects with enemy for (var j = npcs.length - 1; j >= 0; j--) { if (npcs[j] && npcs[j].intersects(enemies[i])) { npcs[j].destroy(); // Destroy NPC npcs.splice(j, 1); // Remove NPC from array } } } // Update bullets for (var j = bullets.length - 1; j >= 0; j--) { bullets[j].update(); for (var k = enemies.length - 1; k >= 0; k--) { if (bullets[j] && bullets[j].intersects(enemies[k])) { bullets[j].destroy(); bullets.splice(j, 1); enemies[k].destroy(); enemies.splice(k, 1); break; } } if (bullets[j] && bullets[j].y < -50) { bullets[j].destroy(); bullets.splice(j, 1); } } };
===================================================================
--- original.js
+++ change.js
@@ -1,7 +1,19 @@
/****
* Classes
****/
+// Define a Bullet class
+var Bullet = Container.expand(function () {
+ var self = Container.call(this);
+ var bulletGraphics = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -10;
+ self.update = function () {
+ self.y += self.speed;
+ };
+});
// Define an Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyBody = self.attachAsset('enemyBody', {
@@ -62,8 +74,35 @@
self.y += dyPlayer * self.speed;
}
};
});
+// Define a MenuScreen class
+var MenuScreen = Container.expand(function () {
+ var self = Container.call(this);
+ // Create a title text
+ var titleText = new Text2('Night of the Massacre', {
+ size: 200,
+ fill: "#ffffff"
+ });
+ titleText.anchor.set(0.5, 0.5);
+ titleText.x = 2048 / 2;
+ titleText.y = 2732 / 4;
+ self.addChild(titleText);
+ // Create a start button
+ var startButton = new Text2('Start Game', {
+ size: 150,
+ fill: "#00ff00"
+ });
+ startButton.anchor.set(0.5, 0.5);
+ startButton.x = 2048 / 2;
+ startButton.y = 2732 / 2;
+ self.addChild(startButton);
+ // Add event listener to start button
+ startButton.down = function () {
+ self.destroy(); // Remove menu screen
+ initializeGame(); // Start the game
+ };
+});
// Define a NPC class
var NPC = Container.expand(function () {
var self = Container.call(this);
var npcHead = self.attachAsset('playerHead', {
@@ -161,80 +200,17 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x0000FF,
- //Init game with blue background
- menu: {
- title: 'Night of the Massacre 2',
- buttons: [{
- text: 'Start Game',
- action: function action() {
- game.start();
- }
- }, {
- text: 'Instructions',
- action: function action() {
- game.showInstructions();
- }
- }]
- }
+ backgroundColor: 0x0000FF //Init game with blue background
});
/****
* Game Code
****/
-// Initialize player
-var player = game.addChild(new Player());
-player.x = 2048 / 2;
-player.y = 2732 - 150;
-// Initialize enemies
-var enemies = [];
-var enemy = new Enemy();
-enemy.x = Math.random() * 2048;
-enemy.y = Math.random() * 1000;
-enemies.push(enemy);
-game.addChild(enemy);
-// Initialize 30 trees and spread them across the game
-var trees = [];
-for (var i = 0; i < 30; i++) {
- var tree = game.addChild(new Tree());
- tree.x = Math.random() * 2048;
- tree.y = Math.random() * 2732;
- trees.push(tree);
-}
-// Initialize NPCs
-var npcs = [];
-for (var i = 0; i < 20; i++) {
- var npc = game.addChild(new NPC());
- npc.x = Math.random() * 2048;
- npc.y = Math.random() * 2732;
- npcs.push(npc);
-}
-game.move = function (x, y, obj) {
- player.x = x;
- player.y = y;
-};
-// Handle shooting
-game.update = function () {
- // Update enemies
- for (var i = 0; i < enemies.length; i++) {
- enemies[i].update();
- if (player.intersects(enemies[i])) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- // Check if any NPC intersects with enemy
- for (var j = npcs.length - 1; j >= 0; j--) {
- if (npcs[j] && npcs[j].intersects(enemies[i])) {
- npcs[j].destroy(); // Destroy NPC
- npcs.splice(j, 1); // Remove NPC from array
- }
- }
- }
- // Update bullets
-};
-game.start = function () {
+// Add the menu screen to the game
+var menuScreen = game.addChild(new MenuScreen());
+function initializeGame() {
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 150;
@@ -260,32 +236,55 @@
npc.x = Math.random() * 2048;
npc.y = Math.random() * 2732;
npcs.push(npc);
}
- game.move = function (x, y, obj) {
- player.x = x;
- player.y = y;
- };
- // Handle shooting
- game.update = function () {
- // Update enemies
- for (var i = 0; i < enemies.length; i++) {
- enemies[i].update();
- if (player.intersects(enemies[i])) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
+ // Initialize bullets
+ var bullets = [];
+}
+// Handle player movement
+game.move = function (x, y, obj) {
+ player.x = x;
+ player.y = y;
+};
+// Handle shooting
+game.down = function (x, y, obj) {
+ var bullet = new Bullet();
+ bullet.x = player.x;
+ bullet.y = player.y;
+ bullets.push(bullet);
+ game.addChild(bullet);
+};
+// Game update loop
+game.update = function () {
+ // Update enemies
+ for (var i = 0; i < enemies.length; i++) {
+ enemies[i].update();
+ if (player.intersects(enemies[i])) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ // Check if any NPC intersects with enemy
+ for (var j = npcs.length - 1; j >= 0; j--) {
+ if (npcs[j] && npcs[j].intersects(enemies[i])) {
+ npcs[j].destroy(); // Destroy NPC
+ npcs.splice(j, 1); // Remove NPC from array
}
- // Check if any NPC intersects with enemy
- for (var j = npcs.length - 1; j >= 0; j--) {
- if (npcs[j] && npcs[j].intersects(enemies[i])) {
- npcs[j].destroy(); // Destroy NPC
- npcs.splice(j, 1); // Remove NPC from array
- }
+ }
+ }
+ // Update bullets
+ for (var j = bullets.length - 1; j >= 0; j--) {
+ bullets[j].update();
+ for (var k = enemies.length - 1; k >= 0; k--) {
+ if (bullets[j] && bullets[j].intersects(enemies[k])) {
+ bullets[j].destroy();
+ bullets.splice(j, 1);
+ enemies[k].destroy();
+ enemies.splice(k, 1);
+ break;
}
}
- // Update bullets
- };
-};
-game.showInstructions = function () {
- var instructions = "Welcome to Night of the Massacre 2! \n\n" + "You are the yellow player. Use your mouse or touch to move around. \n\n" + "Click or tap to shoot bullets. \n\n" + "Your goal is to protect the NPCs (non-player characters) from the red enemies. \n\n" + "If an enemy touches you, it's game over! \n\n" + "Good luck!";
- alert(instructions);
+ if (bullets[j] && bullets[j].y < -50) {
+ bullets[j].destroy();
+ bullets.splice(j, 1);
+ }
+ }
};
\ No newline at end of file