User prompt
slow down the spawn speed of the obstacle initially and gradually increase the speed of the spawning on each 50 score. and red flash on the screen wrong hit.
User prompt
okay so clamp the mouse movement to grid.
User prompt
Player lifes are not visible on the screen...
User prompt
player will have three lives which are displayed on the bottom left corner of the screen, and when the player hits the wrong obstacles will reduce one life of the player, on all the lives are finished game over and show the on the screen.
User prompt
Okay now remove the negative score feature..
User prompt
there is some bug in the code : hitting the correct obstacle is not dependent on the hitting the wrong obstacle and vice versa
User prompt
now add a tint check on the hit event, if the tint of the player pawn and hit object is same then increase the score on screen else add negative score screen on the right side of the scree with red color text.
Code edit (1 edits merged)
Please save this source code
User prompt
on every hit with obstacles with player pawn, player pawn will change the color ( between : Red , Green , Blue )
User prompt
Remove the Color check from the game update function.
User prompt
now as obstacle are selecting random color between Red, Green, Blue. Player pawn will also randomly select the color between these three colors on each hit with obstacles.
User prompt
there is some problem with hit event : it is sending multiple hits
User prompt
the obstacle color are showing as i want, now register for an hit event to obstacle from player pawn, on hit it will increase the score screen on the top left corner of the screen by 1.
Code edit (1 edits merged)
Please save this source code
User prompt
but the one color should not come twise per row of grid.
User prompt
fix this : in each row the color of first, second, and third obstacle is same, i want it to shuffle the colors ( Between - Red , Green , Blue ) in each row for obstacle.
User prompt
find the issue and fix the code.
User prompt
each obstacle in a row should have different color.
User prompt
spawn only 3 obstacle per row in grid and each obstacle in row should have different color ( picked from : Red, Blue, Green )
User prompt
spawn only 3 columns
User prompt
undo
User prompt
spawn on 3 obstacles per grid line
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'tint')' in or related to this line: 'if (obstacles[i].obstacleGraphics.tint !== player.playerGraphics.tint) {' Line Number: 104
User prompt
change the same in the code
/**** * Classes ****/ // Define the Life class var Life = Container.expand(function () { var self = Container.call(this); var lifeGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5, shape: 'box', width: 50, height: 50 }); lifeGraphics.tint = 0xFF0000; return self; }); // Define the Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.colors = [0xFF0000, 0x0000FF, 0x00FF00]; self.colorIndex = Math.floor(Math.random() * self.colors.length); obstacleGraphics.tint = self.colors[self.colorIndex]; self.speed = 10; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; return self; }); //<Assets used in the game will automatically appear 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, shape: 'box', width: 150, height: 150 }); self.speed = 10; self.colors = [0xFF0000, 0x0000FF, 0x00FF00]; self.colorIndex = Math.floor(Math.random() * self.colors.length); playerGraphics.tint = self.colors[self.colorIndex]; self.update = function () { if (self.x < 0) { self.x = 0; } else if (self.x > 2048) { self.x = 2048; } }; return self; }); /**** * 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 = 2048 / 2; player.y = 2732 - 200; // Initialize player's lives var lives = []; for (var i = 0; i < 3; i++) { var life = new Life(); life.x = 100 + i * 60; life.y = 2732 - 100; lives.push(life); game.addChild(life); } // Initialize obstacles array var obstacles = []; // Define the x-coordinates of the grid lines var gridLines = [512, 1024, 1536]; // Function to handle player movement function handleMove(x, y, obj) { // Find the closest grid line to the current mouse position var closestGridLine = gridLines.reduce(function (prev, curr) { return Math.abs(curr - x) < Math.abs(prev - x) ? curr : prev; }); // Clamp the player's x position to the closest grid line player.x = closestGridLine; player.y = y; } // Function to spawn obstacles function spawnObstacle() { // Spawn obstacles on the grid lines // Shuffle the colors array var colors = [0xFF0000, 0x0000FF, 0x00FF00]; colors.sort(function () { return Math.random() - 0.5; }); // Ensure that one color does not appear twice in a row if (colors[0] === colors[1]) { var temp = colors[1]; colors[1] = colors[2]; colors[2] = temp; } for (var i = 0; i < 3; i++) { var obstacle = new Obstacle(); obstacle.x = gridLines[i]; obstacle.y = -50; // Set the color of the obstacle to be different for each grid line obstacle.colorIndex = i; obstacle.children[0].tint = colors[i]; obstacles.push(obstacle); game.addChild(obstacle); } } // Set interval to spawn obstacles var obstacleInterval = LK.setInterval(spawnObstacle, 2000); // Game update function game.update = function () { for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (player.intersects(obstacles[i]) && !obstacles[i].hit) { obstacles[i].hit = true; obstacles[i].destroy(); // Check if the player and obstacle have the same color if (player.children[0].tint === obstacles[i].children[0].tint) { LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); // Update the score display // Increase the obstacle spawn speed every 50 score if (LK.getScore() % 50 == 0) { LK.clearInterval(obstacleInterval); obstacleInterval = LK.setInterval(spawnObstacle, 2000 - LK.getScore() * 10); } // Change player color on hit player.colorIndex = Math.floor(Math.random() * player.colors.length); player.children[0].tint = player.colors[player.colorIndex]; } else { // Reduce one life of the player var life = lives.pop(); life.destroy(); // Flash the screen red on wrong hit LK.effects.flashScreen(0xff0000, 1000); // End the game when all the lives are finished if (lives.length === 0) { LK.showGameOver(); } } console.log("Player intersects with obstacle"); } } }; // Handle touch/mouse move game.move = function (x, y, obj) { player.x = x; }; // Handle touch/mouse down game.down = function (x, y, obj) { player.x = x; }; // Handle touch/mouse up game.up = function (x, y, obj) { // No action needed on up event }; // Add score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(1, 0); // Sets anchor to the right of the top edge of the text. LK.gui.topRight.addChild(scoreTxt); // Add event listener for 'esc' key to exit the game
===================================================================
--- original.js
+++ change.js
@@ -120,9 +120,9 @@
game.addChild(obstacle);
}
}
// Set interval to spawn obstacles
-var obstacleInterval = LK.setInterval(spawnObstacle, 500);
+var obstacleInterval = LK.setInterval(spawnObstacle, 2000);
// Game update function
game.update = function () {
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
@@ -132,15 +132,22 @@
// Check if the player and obstacle have the same color
if (player.children[0].tint === obstacles[i].children[0].tint) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore()); // Update the score display
+ // Increase the obstacle spawn speed every 50 score
+ if (LK.getScore() % 50 == 0) {
+ LK.clearInterval(obstacleInterval);
+ obstacleInterval = LK.setInterval(spawnObstacle, 2000 - LK.getScore() * 10);
+ }
// Change player color on hit
player.colorIndex = Math.floor(Math.random() * player.colors.length);
player.children[0].tint = player.colors[player.colorIndex];
} else {
// Reduce one life of the player
var life = lives.pop();
life.destroy();
+ // Flash the screen red on wrong hit
+ LK.effects.flashScreen(0xff0000, 1000);
// End the game when all the lives are finished
if (lives.length === 0) {
LK.showGameOver();
}