User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'sheepCount')' in or related to this line: 'var sheepGoalMet = sheepInGoal >= level.sheepCount;' Line Number: 1105
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'sheepCount')' in or related to this line: 'scoreText.setText('Sheep: ' + sheepInGoal + '/' + level.sheepCount);' Line Number: 1008
User prompt
Add a cool and cozy startscreen for the game
User prompt
Let's add sounds to the cow like by the sheep but cow
User prompt
Make a cow asset sepperatly for cows
User prompt
Let's add cows to the game (they only start coming at level 5) make it so they need to get to a different area then the sheeps
User prompt
Add a cool grassy ground with some visual effects โช๐ก Consider importing and using the following plugins: @upit/tween.v1
User prompt
Make it so the sheeps stay in the green area when they are in for the first time
Code edit (1 edits merged)
Please save this source code
User prompt
Shepherd's Path
Initial prompt
A herding simulator in 2D
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Dog = Container.expand(function () { var self = Container.call(this); var dogGraphics = self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 6; self.targetX = 0; self.targetY = 0; self.moving = false; self.setTarget = function (x, y) { self.targetX = x; self.targetY = y; self.moving = true; }; self.bark = function () { LK.getSound('bark').play(); LK.effects.flashObject(self, 0xA52A2A, 300); }; self.update = function () { if (self.moving) { var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > self.speed) { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; } else { self.x = self.targetX; self.y = self.targetY; self.moving = false; } } }; return self; }); var Goal = Container.expand(function () { var self = Container.call(this); var goalGraphics = self.attachAsset('goal', { anchorX: 0.5, anchorY: 0.5, alpha: 0.6 }); return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('rock', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Sheep = Container.expand(function () { var self = Container.call(this); var sheepGraphics = self.attachAsset('sheep', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.wanderSpeed = 0.5; self.flockingDistance = 150; self.dogAvoidDistance = 200; self.wanderDirection = Math.random() * Math.PI * 2; self.wanderTimer = 0; self.wanderInterval = 60 + Math.random() * 120; self.scared = false; self.goalReached = false; // Track if sheep has reached the goal area self.update = function () { // Check if sheep is in goal area if (activeGoal && !self.goalReached) { var goalDist = Math.sqrt(Math.pow(self.x - activeGoal.x, 2) + Math.pow(self.y - activeGoal.y, 2)); if (goalDist < 150) { self.goalReached = true; LK.effects.flashObject(self, 0x228b22, 500); LK.getSound('baa').play(); } } // If sheep reached the goal, make it stay there with small wandering if (self.goalReached) { // Small random movement within goal self.wanderTimer++; if (self.wanderTimer > self.wanderInterval) { self.wanderDirection = Math.random() * Math.PI * 2; self.wanderTimer = 0; self.wanderInterval = 120 + Math.random() * 180; } // Apply very small movement var slowSpeed = 0.2; self.x += Math.cos(self.wanderDirection) * slowSpeed; self.y += Math.sin(self.wanderDirection) * slowSpeed; // Ensure sheep stays in goal area if (activeGoal) { var goalDist = Math.sqrt(Math.pow(self.x - activeGoal.x, 2) + Math.pow(self.y - activeGoal.y, 2)); if (goalDist > 140) { // Pull back toward center of goal var pullX = activeGoal.x - self.x; var pullY = activeGoal.y - self.y; var pullDist = Math.sqrt(pullX * pullX + pullY * pullY); self.x += pullX / pullDist * 1.0; self.y += pullY / pullDist * 1.0; } } return; // Skip normal movement behaviors } var vx = 0; var vy = 0; // Dog avoidance behavior var dog = game.dog; var dogDx = self.x - dog.x; var dogDy = self.y - dog.y; var dogDistance = Math.sqrt(dogDx * dogDx + dogDy * dogDy); if (dogDistance < self.dogAvoidDistance) { var dogFactor = 1 - dogDistance / self.dogAvoidDistance; vx += dogDx / dogDistance * dogFactor * 3; vy += dogDy / dogDistance * dogFactor * 3; self.scared = dogDistance < self.dogAvoidDistance * 0.5; if (self.scared && Math.random() < 0.01) { LK.getSound('baa').play(); } } else { self.scared = false; } // Flocking behavior var cohesionX = 0; var cohesionY = 0; var alignmentX = 0; var alignmentY = 0; var separationX = 0; var separationY = 0; var flockCount = 0; for (var i = 0; i < game.sheep.length; i++) { var otherSheep = game.sheep[i]; if (otherSheep !== self) { var otherDx = otherSheep.x - self.x; var otherDy = otherSheep.y - self.y; var otherDistance = Math.sqrt(otherDx * otherDx + otherDy * otherDy); if (otherDistance < self.flockingDistance) { // Cohesion - move toward center of flock cohesionX += otherSheep.x; cohesionY += otherSheep.y; // Alignment - align with flock direction alignmentX += Math.cos(otherSheep.wanderDirection); alignmentY += Math.sin(otherSheep.wanderDirection); // Separation - avoid crowding if (otherDistance < 60) { separationX -= otherDx / otherDistance; separationY -= otherDy / otherDistance; } flockCount++; } } } // Apply flocking behaviors if there are nearby sheep if (flockCount > 0) { // Cohesion cohesionX = cohesionX / flockCount - self.x; cohesionY = cohesionY / flockCount - self.y; var cohesionDist = Math.sqrt(cohesionX * cohesionX + cohesionY * cohesionY); if (cohesionDist > 0) { vx += cohesionX / cohesionDist * 0.1; vy += cohesionY / cohesionDist * 0.1; } // Alignment alignmentX /= flockCount; alignmentY /= flockCount; var alignmentDist = Math.sqrt(alignmentX * alignmentX + alignmentY * alignmentY); if (alignmentDist > 0) { vx += alignmentX / alignmentDist * 0.2; vy += alignmentY / alignmentDist * 0.2; } // Separation vx += separationX * 0.3; vy += separationY * 0.3; } else { // Wandering behavior when alone self.wanderTimer++; if (self.wanderTimer > self.wanderInterval) { self.wanderDirection = Math.random() * Math.PI * 2; self.wanderTimer = 0; self.wanderInterval = 60 + Math.random() * 120; } vx += Math.cos(self.wanderDirection) * self.wanderSpeed; vy += Math.sin(self.wanderDirection) * self.wanderSpeed; } // Obstacle avoidance for (var i = 0; i < game.obstacles.length; i++) { var obstacle = game.obstacles[i]; var obsDx = self.x - obstacle.x; var obsDy = self.y - obstacle.y; var obsDistance = Math.sqrt(obsDx * obsDx + obsDy * obsDy); if (obsDistance < 120) { var obsFactor = 1 - obsDistance / 120; vx += obsDx / obsDistance * obsFactor * 2; vy += obsDy / obsDistance * obsFactor * 2; } } // Boundary avoidance if (self.x < 100) { vx += (100 - self.x) * 0.05; } if (self.x > 1948) { vx += (1948 - self.x) * 0.05; } if (self.y < 100) { vy += (100 - self.y) * 0.05; } if (self.y > 2632) { vy += (2632 - self.y) * 0.05; } // Normalize and apply movement var velocity = Math.sqrt(vx * vx + vy * vy); if (velocity > 0) { var moveSpeed = self.scared ? self.speed * 1.5 : self.speed; self.x += vx / velocity * moveSpeed; self.y += vy / velocity * moveSpeed; self.wanderDirection = Math.atan2(vy, vx); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x78AB46 }); /**** * Game Code ****/ // Game state variables var Level = function Level(sheepCount, obstacleCount, goalX, goalY) { this.sheepCount = sheepCount; this.obstacleCount = obstacleCount; this.goalX = goalX; this.goalY = goalY; this.timeLimit = 60 + sheepCount * 10; }; var currentLevel = 1; var maxLevels = 5; var levels = []; var timeLeft = 0; var timerInterval = null; var activeGoal = null; var levelCompleted = false; var sheepInGoal = 0; // Game elements game.sheep = []; game.obstacles = []; game.dog = null; // Set up levels function setupLevels() { levels = [new Level(3, 3, 1700, 500), new Level(5, 5, 1600, 1800), new Level(7, 8, 500, 2200), new Level(10, 10, 300, 1200), new Level(12, 15, 1700, 1300)]; } // Initialize UI var levelText = new Text2('Level 1', { size: 80, fill: 0xFFFFFF }); levelText.anchor.set(0.5, 0); LK.gui.top.addChild(levelText); var scoreText = new Text2('Sheep: 0/0', { size: 60, fill: 0xFFFFFF }); scoreText.anchor.set(1, 0); scoreText.x = -50; LK.gui.topRight.addChild(scoreText); var timerText = new Text2('Time: 0', { size: 60, fill: 0xFFFFFF }); timerText.anchor.set(0, 0); timerText.x = 50; LK.gui.topLeft.addChild(timerText); var instructionText = new Text2('Tap to move your dog\nHerd the sheep to the green area', { size: 60, fill: 0xFFFFFF }); instructionText.anchor.set(0.5, 0); instructionText.y = 100; LK.gui.top.addChild(instructionText); // Show the instruction text for a limited time LK.setTimeout(function () { tween(instructionText, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { LK.gui.top.removeChild(instructionText); } }); }, 5000); // Start the game function startGame() { setupLevels(); startLevel(currentLevel); LK.playMusic('countryside'); } // Setup and start a level function startLevel(levelNum) { clearLevel(); var level = levels[levelNum - 1]; levelText.setText('Level ' + levelNum); // Set timer timeLeft = level.timeLimit; timerText.setText('Time: ' + timeLeft); if (timerInterval) { LK.clearInterval(timerInterval); } timerInterval = LK.setInterval(updateTimer, 1000); // Create goal area activeGoal = new Goal(); activeGoal.x = level.goalX; activeGoal.y = level.goalY; game.addChild(activeGoal); // Create dog game.dog = new Dog(); game.dog.x = 1024; game.dog.y = 1366; game.addChild(game.dog); // Create obstacles for (var i = 0; i < level.obstacleCount; i++) { var obstacle = new Obstacle(); var validPosition = false; // Ensure obstacles don't overlap with the goal while (!validPosition) { obstacle.x = 200 + Math.random() * 1648; obstacle.y = 200 + Math.random() * 2332; var goalDist = Math.sqrt(Math.pow(obstacle.x - activeGoal.x, 2) + Math.pow(obstacle.y - activeGoal.y, 2)); if (goalDist > 300) { validPosition = true; } } game.obstacles.push(obstacle); game.addChild(obstacle); } // Create sheep for (var i = 0; i < level.sheepCount; i++) { var sheep = new Sheep(); var validPosition = false; // Place sheep away from goal and obstacles while (!validPosition) { sheep.x = 200 + Math.random() * 1648; sheep.y = 200 + Math.random() * 2332; var goalDist = Math.sqrt(Math.pow(sheep.x - activeGoal.x, 2) + Math.pow(sheep.y - activeGoal.y, 2)); if (goalDist > 500) { validPosition = true; // Check distance from obstacles for (var j = 0; j < game.obstacles.length; j++) { var obstacleDist = Math.sqrt(Math.pow(sheep.x - game.obstacles[j].x, 2) + Math.pow(sheep.y - game.obstacles[j].y, 2)); if (obstacleDist < 120) { validPosition = false; break; } } } } game.sheep.push(sheep); game.addChild(sheep); } // Update UI sheepInGoal = 0; levelCompleted = false; updateScoreText(); } // Clear all game elements from the current level function clearLevel() { // Remove sheep for (var i = 0; i < game.sheep.length; i++) { game.removeChild(game.sheep[i]); } game.sheep = []; // Remove obstacles for (var i = 0; i < game.obstacles.length; i++) { game.removeChild(game.obstacles[i]); } game.obstacles = []; // Remove dog if (game.dog) { game.removeChild(game.dog); game.dog = null; } // Remove goal if (activeGoal) { game.removeChild(activeGoal); activeGoal = null; } } // Update the timer function updateTimer() { timeLeft--; timerText.setText('Time: ' + timeLeft); if (timeLeft <= 0) { LK.clearInterval(timerInterval); endLevel(false); } } // Update the score display function updateScoreText() { var level = levels[currentLevel - 1]; scoreText.setText('Sheep: ' + sheepInGoal + '/' + level.sheepCount); } // End the current level function endLevel(success) { LK.clearInterval(timerInterval); if (success) { LK.getSound('success').play(); LK.setScore(LK.getScore() + sheepInGoal * 100 + timeLeft * 10); if (currentLevel < maxLevels) { currentLevel++; LK.setTimeout(function () { startLevel(currentLevel); }, 1500); } else { // Game completed LK.showYouWin(); } } else { LK.showGameOver(); } } // Control the dog with tap/click game.down = function (x, y) { if (game.dog && !levelCompleted) { game.dog.setTarget(x, y); game.dog.bark(); } }; // Main game update loop game.update = function () { if (levelCompleted) { return; } // Update all sheep for (var i = 0; i < game.sheep.length; i++) { game.sheep[i].update(); } // Update dog if (game.dog) { game.dog.update(); } // Check for sheep in goal area if (activeGoal) { sheepInGoal = 0; for (var i = 0; i < game.sheep.length; i++) { var sheep = game.sheep[i]; var goalDist = Math.sqrt(Math.pow(sheep.x - activeGoal.x, 2) + Math.pow(sheep.y - activeGoal.y, 2)); if (goalDist < 150) { sheepInGoal++; } } updateScoreText(); // Check for level completion var level = levels[currentLevel - 1]; if (sheepInGoal >= level.sheepCount && !levelCompleted) { levelCompleted = true; endLevel(true); } } }; // Start the game startGame();
===================================================================
--- original.js
+++ change.js
@@ -73,9 +73,46 @@
self.wanderDirection = Math.random() * Math.PI * 2;
self.wanderTimer = 0;
self.wanderInterval = 60 + Math.random() * 120;
self.scared = false;
+ self.goalReached = false; // Track if sheep has reached the goal area
self.update = function () {
+ // Check if sheep is in goal area
+ if (activeGoal && !self.goalReached) {
+ var goalDist = Math.sqrt(Math.pow(self.x - activeGoal.x, 2) + Math.pow(self.y - activeGoal.y, 2));
+ if (goalDist < 150) {
+ self.goalReached = true;
+ LK.effects.flashObject(self, 0x228b22, 500);
+ LK.getSound('baa').play();
+ }
+ }
+ // If sheep reached the goal, make it stay there with small wandering
+ if (self.goalReached) {
+ // Small random movement within goal
+ self.wanderTimer++;
+ if (self.wanderTimer > self.wanderInterval) {
+ self.wanderDirection = Math.random() * Math.PI * 2;
+ self.wanderTimer = 0;
+ self.wanderInterval = 120 + Math.random() * 180;
+ }
+ // Apply very small movement
+ var slowSpeed = 0.2;
+ self.x += Math.cos(self.wanderDirection) * slowSpeed;
+ self.y += Math.sin(self.wanderDirection) * slowSpeed;
+ // Ensure sheep stays in goal area
+ if (activeGoal) {
+ var goalDist = Math.sqrt(Math.pow(self.x - activeGoal.x, 2) + Math.pow(self.y - activeGoal.y, 2));
+ if (goalDist > 140) {
+ // Pull back toward center of goal
+ var pullX = activeGoal.x - self.x;
+ var pullY = activeGoal.y - self.y;
+ var pullDist = Math.sqrt(pullX * pullX + pullY * pullY);
+ self.x += pullX / pullDist * 1.0;
+ self.y += pullY / pullDist * 1.0;
+ }
+ }
+ return; // Skip normal movement behaviors
+ }
var vx = 0;
var vy = 0;
// Dog avoidance behavior
var dog = game.dog;
top down view of a pixel art dog. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
top down view of a pixel art sheep. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
top down view of pixel art rock. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
top down view of a pixel art cow. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows