User prompt
hide the mouse cursor
User prompt
add a light green background.
User prompt
add a green background.
User prompt
make the sheep's repulsion force from the dog greater.
User prompt
make the repulsor force three times greater.
User prompt
make the repulsor force three times greater
User prompt
the dog must push the sheep away without touching them
User prompt
optimise sheep movements using Particle Swarm Optimisation so that there are no collisions between sheep
User prompt
introduce collisions for sheep.
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < sheepArray.length; i++) {' Line Number: 37
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < sheepArray.length; i++) {' Line Number: 37
User prompt
implement Particle Swarm Optimisation to move the sheep without colliding with each other
User prompt
implement Particle Swarm Optimisation to move the sheep without colliding with each other
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 41
User prompt
implement Particle Swarm Optimisation to move the sheep without colliding with each other
User prompt
Fix Bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'avoidDog')' in or related to this line: 'self.avoidDog = function (dog) {' Line Number: 119
User prompt
Fix Bug: 'TypeError: sheep.avoidDog is not a function' in or related to this line: 'sheep.avoidDog(shepherdDog);' Line Number: 146
User prompt
implement ant colony optimisation to move the sheep without colliding with each other
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 68
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'if (sheepArray.length > 1) {' Line Number: 52
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 44
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 68
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 57
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'forEach')' in or related to this line: 'sheepArray.forEach(function (otherSheep) {' Line Number: 44
/**** * Classes ****/ // Sheep class var Sheep = Container.expand(function () { var self = Container.call(this); var sheepGraphics = self.attachAsset('sheep', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 1; self.direction = Math.random() * Math.PI * 2; self.move = function (sheepArray) { var personalSpace = 150; var alignmentWeight = 0.5; var cohesionWeight = 0.05; var separationWeight = 0.5; var velocity = { x: Math.cos(self.direction), y: Math.sin(self.direction) }; var alignment = { x: 0, y: 0 }; var cohesion = { x: 0, y: 0 }; var separation = { x: 0, y: 0 }; var count = 0; sheepArray.forEach(function (otherSheep) { if (otherSheep !== self) { var distance = Math.sqrt(Math.pow(otherSheep.x - self.x, 2) + Math.pow(otherSheep.y - self.y, 2)); if (distance < personalSpace) { count++; alignment.x += Math.cos(otherSheep.direction); alignment.y += Math.sin(otherSheep.direction); cohesion.x += otherSheep.x - self.x; cohesion.y += otherSheep.y - self.y; separation.x += (self.x - otherSheep.x) / distance; separation.y += (self.y - otherSheep.y) / distance; } } }); if (count > 0) { alignment.x = alignment.x / count; alignment.y = alignment.y / count; cohesion.x = cohesion.x / count; cohesion.y = cohesion.y / count; velocity.x += alignment.x * alignmentWeight + cohesion.x * cohesionWeight + separation.x * separationWeight; velocity.y += alignment.y * alignmentWeight + cohesion.y * cohesionWeight + separation.y * separationWeight; var newDirection = Math.atan2(velocity.y, velocity.x); self.direction = newDirection; } self.x += Math.cos(self.direction) * self.speed; self.y += Math.sin(self.direction) * self.speed; }; self.avoidWalls = function () { if (self.x < 50 || self.x > 1998 || self.y < 50 || self.y > 2682) { self.direction += Math.PI; } }; self.avoidDog = function (dog) { if (self.intersects(dog)) { self.direction += Math.PI / 2; } }; }); // Shepherd dog class var Dog = Container.expand(function () { var self = Container.call(this); var dogGraphics = self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); self.move = function (pos) { self.x = pos.x; self.y = pos.y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to represent the sky }); /**** * Game Code ****/ // Shepherd dog asset // Sheep asset // Initialize sheep array var sheepArray = []; // Create 20 sheep and add them to the game and sheepArray for (var i = 0; i < 20; i++) { var sheep = new Sheep(); sheep.x = Math.random() * 2048; sheep.y = Math.random() * 2732; game.addChild(sheep); sheepArray.push(sheep); } // Create shepherd dog and add it to the game var shepherdDog = new Dog(); shepherdDog.x = 1024; // Start in the middle of the game area shepherdDog.y = 1366; game.addChild(shepherdDog); // Event listener for touch move to move the shepherd dog game.on('move', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos); }); // Main game tick event LK.on('tick', function () { // Move each sheep for (var i = 0; i < sheepArray.length; i++) { var sheep = sheepArray[i]; sheep.move(); sheep.avoidWalls(); sheep.avoidDog(shepherdDog); } }); // Ensure the game is touchscreen compatible game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos); }); game.on('up', function (obj) { // This can be used for future features where the dog stops moving on touch up });
===================================================================
--- original.js
+++ change.js
@@ -3,93 +3,72 @@
****/
// Sheep class
var Sheep = Container.expand(function () {
var self = Container.call(this);
- self.avoidDog = function (dog) {
- var dogAvoidanceDistance = 150;
- var distanceToDog = Math.sqrt(Math.pow(dog.x - self.x, 2) + Math.pow(dog.y - self.y, 2));
- if (distanceToDog < dogAvoidanceDistance) {
- self.direction += Math.PI;
- }
- };
var sheepGraphics = self.attachAsset('sheep', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1;
self.direction = Math.random() * Math.PI * 2;
- self.move = function () {
- self.x += Math.cos(self.direction) * self.speed;
- self.y += Math.sin(self.direction) * self.speed;
- };
- self.avoidWalls = function () {
- if (self.x < 50 || self.x > 1998 || self.y < 50 || self.y > 2682) {
- self.direction += Math.PI;
- }
- };
- self.applySwarmBehaviour = function (sheepArray) {
- var separationDistance = 120;
- var alignmentDistance = 200;
- var cohesionDistance = 200;
- var separationAmount = 0.05;
- var alignmentAmount = 0.05;
- var cohesionAmount = 0.05;
- var steerVector = {
- x: 0,
- y: 0
+ self.move = function (sheepArray) {
+ var personalSpace = 150;
+ var alignmentWeight = 0.5;
+ var cohesionWeight = 0.05;
+ var separationWeight = 0.5;
+ var velocity = {
+ x: Math.cos(self.direction),
+ y: Math.sin(self.direction)
};
- var totalSeparation = {
+ var alignment = {
x: 0,
y: 0
};
- var totalAlignment = {
+ var cohesion = {
x: 0,
y: 0
};
- var totalCohesion = {
+ var separation = {
x: 0,
y: 0
};
- var countSeparation = 0;
- var countAlignment = 0;
- var countCohesion = 0;
+ var count = 0;
sheepArray.forEach(function (otherSheep) {
- var distance = Math.sqrt(Math.pow(otherSheep.x - self.x, 2) + Math.pow(otherSheep.y - self.y, 2));
- if (otherSheep !== self && distance < separationDistance) {
- totalSeparation.x += self.x - otherSheep.x;
- totalSeparation.y += self.y - otherSheep.y;
- countSeparation++;
+ if (otherSheep !== self) {
+ var distance = Math.sqrt(Math.pow(otherSheep.x - self.x, 2) + Math.pow(otherSheep.y - self.y, 2));
+ if (distance < personalSpace) {
+ count++;
+ alignment.x += Math.cos(otherSheep.direction);
+ alignment.y += Math.sin(otherSheep.direction);
+ cohesion.x += otherSheep.x - self.x;
+ cohesion.y += otherSheep.y - self.y;
+ separation.x += (self.x - otherSheep.x) / distance;
+ separation.y += (self.y - otherSheep.y) / distance;
+ }
}
- if (otherSheep !== self && distance < alignmentDistance) {
- totalAlignment.x += Math.cos(otherSheep.direction);
- totalAlignment.y += Math.sin(otherSheep.direction);
- countAlignment++;
- }
- if (otherSheep !== self && distance < cohesionDistance) {
- totalCohesion.x += otherSheep.x;
- totalCohesion.y += otherSheep.y;
- countCohesion++;
- }
});
- if (countSeparation > 0) {
- totalSeparation.x /= countSeparation;
- totalSeparation.y /= countSeparation;
- steerVector.x += totalSeparation.x * separationAmount;
- steerVector.y += totalSeparation.y * separationAmount;
+ if (count > 0) {
+ alignment.x = alignment.x / count;
+ alignment.y = alignment.y / count;
+ cohesion.x = cohesion.x / count;
+ cohesion.y = cohesion.y / count;
+ velocity.x += alignment.x * alignmentWeight + cohesion.x * cohesionWeight + separation.x * separationWeight;
+ velocity.y += alignment.y * alignmentWeight + cohesion.y * cohesionWeight + separation.y * separationWeight;
+ var newDirection = Math.atan2(velocity.y, velocity.x);
+ self.direction = newDirection;
}
- if (countAlignment > 0) {
- totalAlignment.x /= countAlignment;
- totalAlignment.y /= countAlignment;
- steerVector.x += totalAlignment.x * alignmentAmount;
- steerVector.y += totalAlignment.y * alignmentAmount;
+ self.x += Math.cos(self.direction) * self.speed;
+ self.y += Math.sin(self.direction) * self.speed;
+ };
+ self.avoidWalls = function () {
+ if (self.x < 50 || self.x > 1998 || self.y < 50 || self.y > 2682) {
+ self.direction += Math.PI;
}
- if (countCohesion > 0) {
- totalCohesion.x /= countCohesion;
- totalCohesion.y /= countCohesion;
- steerVector.x += (totalCohesion.x - self.x) * cohesionAmount;
- steerVector.y += (totalCohesion.y - self.y) * cohesionAmount;
+ };
+ self.avoidDog = function (dog) {
+ if (self.intersects(dog)) {
+ self.direction += Math.PI / 2;
}
- self.direction = Math.atan2(steerVector.y, steerVector.x);
};
});
// Shepherd dog class
var Dog = Container.expand(function () {
@@ -116,15 +95,8 @@
****/
// Shepherd dog asset
// Sheep asset
// Initialize sheep array
-self.avoidDog = function (dog) {
- var dogAvoidanceDistance = 150;
- var distanceToDog = Math.sqrt(Math.pow(dog.x - self.x, 2) + Math.pow(dog.y - self.y, 2));
- if (distanceToDog < dogAvoidanceDistance) {
- self.direction += Math.PI;
- }
-};
var sheepArray = [];
// Create 20 sheep and add them to the game and sheepArray
for (var i = 0; i < 20; i++) {
var sheep = new Sheep();
@@ -144,12 +116,11 @@
shepherdDog.move(pos);
});
// Main game tick event
LK.on('tick', function () {
- // Move each sheep and apply swarm behavior
+ // Move each sheep
for (var i = 0; i < sheepArray.length; i++) {
var sheep = sheepArray[i];
- sheep.applySwarmBehaviour(sheepArray);
sheep.move();
sheep.avoidWalls();
sheep.avoidDog(shepherdDog);
}
sheep, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
sheep, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Collie shepherd dog, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a white text bubble that says "baa," comic book style.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
double-sided blue arrow in the form of a semicircle. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
computer mouse top view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
angry big bear, lying down and sleeping, top view, cartoon style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
erase