User prompt
When there are two sheep or less left, it's game over.
User prompt
offset the text down by 400
User prompt
3 seconds after the farm has stopped, display the text "congratulations" on the screen below the farm for 3 seconds, then end the game
User prompt
make the moment of the farm's birth 3 seconds later than it is now.
User prompt
make the farm appear 3 seconds later than it does now.
User prompt
Please fix the bug: 'ReferenceError: farm is not defined' in or related to this line: 'farm.y += 4; // Synchronize with grass speed' Line Number: 171
User prompt
Please fix the bug: 'ReferenceError: farm is not defined' in or related to this line: 'if (farm.y < 1366 - farm.height / 2) {' Line Number: 169
User prompt
Please fix the bug: 'ReferenceError: farm is not defined' in or related to this line: 'if (sheep.intersects(farm)) {' Line Number: 500
User prompt
give the farm a collision
User prompt
give the farm a collision
User prompt
when the farm reaches the center of the screen, stop the movement of the farm, grass and stones
User prompt
the farm should appear beyond the top edge of the screen and start moving from there.
User prompt
the farm should start moving from the top edge of the screen, synchronize the speed of movement with the grass
User prompt
make the farm appear from the top border of the screen on top of all layers, after 20 fence
User prompt
Make sure that the movement behavior of the truss is defined so that it descends smoothly from the top of the screen.
User prompt
Implement a method in the Farm class that gradually changes its y-axis position from the top border of the screen to the center of the screen
User prompt
Once the 20th row of fences has been created, add the farm object to the game. You can do this by checking the fence row counter, and when it reaches 20, add the farm to the game, on top of all layers
User prompt
Keep track of the number of fence rows that have been added to the game. You can do this by increasing the counter each time a new row of fences is created.
User prompt
In the game code, after defining the Farm class, initialize the farm object, but do not add it to the game right away. Set its initial position just above the top border of the screen so that it is not visible initially.
User prompt
the farm didn't show up, fix it.
User prompt
when the fence rows are finished, add a truss that should move from top to bottom with the grass.
User prompt
how to make the object "farm" move downwards from top to bottom after the 20th row of fence is hidden behind the bottom edge of the screen
User prompt
the farm doesn't show up. Fix it.
User prompt
Add the farm to the game and make it move downwards after 20 fence rows have passed.
/**** * Classes ****/ var Arrow = Container.expand(function () { var self = Container.call(this); var arrowGraphics = self.attachAsset('arrow', { anchorX: 0.5, anchorY: 0.5, alpha: 0.3 }); self.setPosition = function (x, y) { self.x = x; self.y = y; }; self.show = function () { self.visible = true; LK.setTimeout(function () { self.visible = false; }, 3000); }; }); // Baa class var Baa = Container.expand(function () { var self = Container.call(this); var baaGraphics = self.attachAsset('baa', { anchorX: 0.5, anchorY: 0.5 }); self.visible = false; self.currentSheep = null; self.show = function (sheep) { if (!self.currentSheep) { self.currentSheep = []; } self.currentSheep.push(sheep); self.visible = true; LK.setTimeout(function () { self.currentSheep.splice(self.currentSheep.indexOf(sheep), 1); if (self.currentSheep.length === 0) { self.visible = false; } }, 1000); }; self.tick = function () { if (self.visible && self.currentSheep && self.currentSheep.length > 0) { for (var i = 0; i < self.currentSheep.length; i++) { var sheep = self.currentSheep[i]; self.x = sheep.x; self.y = sheep.y - sheep.height / 2 - self.height / 2; } } }; }); // Shepherd dog class with two states var Dog = Container.expand(function () { var self = Container.call(this); var dogGraphics = self.attachAsset('dog', { anchorX: 0.5, anchorY: 0.5 }); var dog2Graphics = self.attachAsset('dog2', { anchorX: 0.5, anchorY: 0.5 }); dog2Graphics.visible = false; // Initially hide the dog2 self.targetPosition = { x: 1024, y: 1366 }; // Default target position in the middle self.move = function (pos, lastPos) { self.x = pos.x; self.y = pos.y; if (pos.x > lastPos.x) { dogGraphics.visible = true; dog2Graphics.visible = false; } else if (pos.x < lastPos.x) { dogGraphics.visible = false; dog2Graphics.visible = true; } }; self.lastPos = { x: self.x, y: self.y }; // Store the last position self.setTargetPosition = function (pos) { self.targetPosition = pos; }; }); // Farm class var Farm = Container.expand(function () { var self = Container.call(this); var farmGraphics = self.attachAsset('farm', { anchorX: 0.5, anchorY: 0.5 }); self.resetPosition = function () { self.x = 1024; // Center of the screen self.y = -self.height; // Start from the top }; self.move = function () { self.y += 4; // Move down at a constant speed if (self.y > 2732) { self.destroy(); // Destroy the farm if it moves off the screen } }; self.resetPosition(); // Initialize position }); // Fence class var Fence = Container.expand(function () { var self = Container.call(this); var fenceGraphics = self.attachAsset('fence', { anchorX: 0.5, anchorY: 0.5 }); self.setPosition = function (x, y) { self.x = x; self.y = y; }; }); // FenceLayer class var FenceLayer = Container.expand(function () { var self = Container.call(this); self.fenceInstances = []; self.rowCounter = 0; // Initialize the row counter self.addFenceRow = function () { self.rowCounter++; // Increment the row counter each time a new row is added var fenceWidth = LK.getAsset('fence', {}).width; var totalFenceWidth = fenceWidth * Math.floor(2048 / fenceWidth); var spaceWidth = 2048 - totalFenceWidth; var spaceIndex = Math.floor(2048 / fenceWidth / 2); for (var i = 0; i < spaceIndex * 2 + 1; i++) { if (i < spaceIndex - 3 || i > spaceIndex + 3 || self.rowCounter > 14 && (i < spaceIndex - 2 || i > spaceIndex + 2) || self.rowCounter > 10 && (i < spaceIndex - 1 || i > spaceIndex + 1) || self.rowCounter > 5 && self.rowCounter <= 10 && (i === spaceIndex - 3 || i === spaceIndex + 3) || self.rowCounter >= 16 && self.rowCounter <= 20 && i == spaceIndex + 1) { var fence = new Fence(); var positionX = i * fenceWidth + spaceWidth / 2; fence.setPosition(positionX, -fence.height); self.addChild(fence); self.fenceInstances.push(fence); } } }; self.moveFences = function () { for (var i = self.fenceInstances.length - 1; i >= 0; i--) { var fence = self.fenceInstances[i]; fence.y += 4; // Move down at the same speed as grass if (fence.y > 2732) { self.removeChild(fence); self.fenceInstances.splice(i, 1); if (!self.pastFenceCount) { self.pastFenceCount = 0; } self.pastFenceCount++; if (self.pastFenceCount === 20) { game.addChild(farm); farm.y = -farm.height; // Set initial position to be just above the top border of the screen } } } }; self.scheduleNextFenceRow = function () { var minInterval = 3000; // Minimum 3 seconds before a new row can appear var maxInterval = 7000; // Maximum 7 seconds before a new row can appear var interval = Math.random() * (maxInterval - minInterval) + minInterval; LK.setTimeout(function () { if (self.rowCounter < 20) { if (self.fenceInstances.length === 0) { self.addFenceRow(); } self.scheduleNextFenceRow(); } }, interval); }; self.scheduleNextFenceRow(); }); // Grass class var Grass = Container.expand(function () { var self = Container.call(this); var grassGraphics = self.attachAsset('grass', { anchorX: 0.5, anchorY: 0.5 }); self.resetPosition = function () { self.x = Math.random() * 2048; self.y = -self.height; }; self.move = function () { self.y += 4; // Move down at a constant speed // Check for collision with any fence for (var i = 0; i < fenceLayer.fenceInstances.length; i++) { if (self.intersects(fenceLayer.fenceInstances[i])) { self.destroy(); // Destroy the grass return; // Exit the function to prevent further movement } } if (self.y > 2732) { // If grass is below the screen self.resetPosition(); // Reset to the top } }; self.resetPosition(); // Initialize position }); // GrassLayer class var GrassLayer = Container.expand(function () { var self = Container.call(this); GrassLayer.prototype.addGrassWithLimitedNumber = function () { var interval = 1000; // Interval of 1 second LK.setInterval(function () { if (self.grassInstances.length < 20) { self.addGrass(); } if (self.stoneInstances.length < 3) { self.addStone(); } }, interval); }; self.stoneInstances = []; self.addStone = function () { var stone = new Stone(); self.addChild(stone); self.stoneInstances.push(stone); self.avoidStoneGrassCollision(stone); }; self.avoidStoneGrassCollision = function (stone) { for (var i = 0; i < self.grassInstances.length; i++) { var grass = self.grassInstances[i]; if (stone.intersects(grass)) { stone.resetPosition(); i = -1; // Restart the loop to check for collisions again } } }; self.moveStones = function () { for (var j = 0; j < self.stoneInstances.length; j++) { self.stoneInstances[j].move(); } }; self.grassInstances = []; self.addGrass = function () { var grass = new Grass(); self.addChild(grass); self.grassInstances.push(grass); self.avoidGrassStoneCollision(grass); }; self.avoidGrassStoneCollision = function (grass) { for (var i = 0; i < self.stoneInstances.length; i++) { var stone = self.stoneInstances[i]; if (grass.intersects(stone)) { grass.resetPosition(); i = -1; // Restart the loop to check for collisions again } } }; self.moveGrass = function () { for (var j = 0; j < self.grassInstances.length; j++) { self.grassInstances[j].move(); } }; self.addGrassWithUniformInterval = function () { self.addGrass(); var interval = 1000; // Uniform interval of 1 second LK.setInterval(self.addGrass, interval); }; self.addGrassWithLimitedNumber(); // Initialize the process with a limited number of grass instances }); // Instructions class var Instructions = Container.expand(function () { var self = Container.call(this); var instructionsText = new Text2('Use circular mouse movements to gather the sheep into a flock', { size: 70, fill: "#ffffff", align: 'center' }); self.addChild(instructionsText); instructionsText.anchor.set(0.5, 1); instructionsText.x = 2048 / 2; instructionsText.y = 2732 - 200; self.show = function () { self.visible = true; LK.setTimeout(function () { self.visible = false; }, 4000); }; }); var Intro = Container.expand(function () { var self = Container.call(this); var introGraphics = self.attachAsset('intro', { anchorX: 0.5, anchorY: 1 }); self.x = 2048 / 2; self.y = 2732 - 450; self.show = function () { self.visible = true; LK.setTimeout(function () { self.visible = false; }, 3000); }; self.show(); }); // Mouse class var Mouse = Container.expand(function () { var self = Container.call(this); var mouseGraphics = self.attachAsset('mouse', { anchorX: 0.5, anchorY: 0.5 }); self.setPosition = function (x, y) { self.x = x; self.y = y; }; self.show = function () { self.visible = true; LK.setTimeout(function () { self.visible = false; }, 3000); }; }); // 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 = 2; self.direction = Math.random() * Math.PI * 2; self.move = function () { var deltaX = Math.cos(self.direction) * self.speed; var deltaY = Math.sin(self.direction) * self.speed; self.x += deltaX; self.y += deltaY; if (deltaX > 0) { self.rotation = 5 * (Math.PI / 180); } else if (deltaX < 0) { self.rotation = -5 * (Math.PI / 180); } }; self.avoidWalls = function () { if (self.x < 300 || self.x > 1748 || self.y < 300 || self.y > 2432) { self.direction += Math.PI; } }; self.avoidDog = function (dog, targetPosition) { if (self.intersects(dog)) { var dx = targetPosition.x - self.x; var dy = targetPosition.y - self.y; self.direction = Math.atan2(dy, dx); } }; self.avoidSheep = function (sheepArray, stoneArray, fenceArray) { var personalSpace = 150; // Define a personal space radius for each sheep for (var i = 0; i < sheepArray.length; i++) { var otherSheep = sheepArray[i]; if (self !== otherSheep) { var dx = otherSheep.x - self.x; var dy = otherSheep.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < personalSpace) { var avoidanceForce = 1; var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction self.direction = avoidanceAngle; self.x += Math.cos(self.direction) * self.speed * avoidanceForce; self.y += Math.sin(self.direction) * self.speed * avoidanceForce; } } } // Avoid stones for (var j = 0; j < stoneArray.length; j++) { var stone = stoneArray[j]; var dx = stone.x - self.x; var dy = stone.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < personalSpace) { var avoidanceForce = 1; var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction self.direction = avoidanceAngle; self.x += Math.cos(self.direction) * self.speed * avoidanceForce; self.y += Math.sin(self.direction) * self.speed * avoidanceForce; } } // Avoid fences for (var k = 0; k < fenceArray.length; k++) { var fence = fenceArray[k]; var dx = fence.x - self.x; var dy = fence.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < personalSpace) { var avoidanceForce = 1; var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction self.direction = avoidanceAngle; self.x += Math.cos(self.direction) * self.speed * avoidanceForce; self.y += Math.sin(self.direction) * self.speed * avoidanceForce; } } }; }); // Stone class var Stone = Container.expand(function () { var self = Container.call(this); var stoneGraphics = self.attachAsset('rock', { anchorX: 0.5, anchorY: 0.5 }); self.resetPosition = function () { self.x = Math.random() * 2048; self.y = -self.height; }; self.move = function () { self.y += 4; // Move down at the same speed as grass // Check for collision with any fence for (var i = 0; i < fenceLayer.fenceInstances.length; i++) { if (self.intersects(fenceLayer.fenceInstances[i])) { self.destroy(); // Destroy the stone return; // Exit the function to prevent further movement } } if (self.y > 2732) { // If stone is below the screen self.resetPosition(); // Reset to the top } }; self.resetPosition(); // Initialize position }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xA7D397 // New background color }); /**** * Game Code ****/ // Initialize the Farm object var farm = new Farm(); farm.y = -farm.height; // Set initial position to be just above the top border of the screen var instructions = game.addChild(new Instructions()); instructions.show(); // Initialize and display the Arrow // Intro instance creation moved below Sheep instances // Arrow instance creation moved below Sheep instances // Initialize and display the Mouse // Mouse instance creation moved below Sheep instances // Initialize grass layer and add it to the game var grassLayer = game.addChild(new GrassLayer()); // Initialize fence layer and add it to the game var fenceLayer = game.addChild(new FenceLayer()); // Initialize sheep array // Sheep asset // Shepherd dog asset var sheepArray = []; // Place fences in a line across the screen with a space in the center // Create 20 sheep and add them to the game and sheepArray for (var i = 0; i < 20; i++) { var sheep = new Sheep(); sheep.x = 300 + Math.random() * (2048 - 600); sheep.y = 300 + Math.random() * (2732 - 600); game.addChild(sheep); sheepArray.push(sheep); } // Create Intro, Arrow, and Mouse instances to appear above the sheep layer var intro = game.addChild(new Intro()); var arrow = new Arrow(); arrow.setPosition(2048 / 2, 2732 - arrow.height / 2 - 300); game.addChild(arrow); arrow.show(); var mouse = new Mouse(); mouse.setPosition(2048 / 2, 2732 - mouse.height / 2 - 300); game.addChild(mouse); mouse.show(); // 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 and handle direction game.on('move', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.move(pos, shepherdDog.lastPos); shepherdDog.lastPos = pos; // Update the last position }); // Main game tick event var baaInstance = game.addChild(new Baa()); LK.setInterval(function () { for (var i = 0; i < 3; i++) { var randomSheepIndex = Math.floor(Math.random() * sheepArray.length); var randomSheep = sheepArray[randomSheepIndex]; baaInstance.show(randomSheep); } }, 500); LK.on('tick', function () { // Move each sheep, handle collisions, update Baa position, move grass instances, move stones, and move fences baaInstance.tick(); grassLayer.moveGrass(); grassLayer.moveStones(); // Move the stones fenceLayer.moveFences(); // Move the fences for (var i = sheepArray.length - 1; i >= 0; i--) { var sheep = sheepArray[i]; sheep.move(); sheep.avoidWalls(); sheep.avoidDog(shepherdDog, shepherdDog.targetPosition); sheep.avoidSheep(sheepArray, grassLayer.stoneInstances, fenceLayer.fenceInstances); if (sheep.y > 2732) { game.removeChild(sheep); sheepArray.splice(i, 1); } } // Check if there are three or less sheep left and apply a pink special effect to them if (sheepArray.length <= 3) { for (var i = 0; i < sheepArray.length; i++) { LK.effects.flashObject(sheepArray[i], 0xFFB6C1, 5000); } } // End the game when there's only one sheep left if (sheepArray.length <= 1) { LK.showGameOver(); } }); // Ensure the game is touchscreen compatible game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.setTargetPosition(pos); shepherdDog.move(pos, shepherdDog.lastPos); shepherdDog.lastPos = pos; // Update the last position }); game.on('up', function (obj) { var pos = obj.event.getLocalPosition(game); shepherdDog.setTargetPosition(pos); });
/****
* Classes
****/
var Arrow = Container.expand(function () {
var self = Container.call(this);
var arrowGraphics = self.attachAsset('arrow', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3
});
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
self.show = function () {
self.visible = true;
LK.setTimeout(function () {
self.visible = false;
}, 3000);
};
});
// Baa class
var Baa = Container.expand(function () {
var self = Container.call(this);
var baaGraphics = self.attachAsset('baa', {
anchorX: 0.5,
anchorY: 0.5
});
self.visible = false;
self.currentSheep = null;
self.show = function (sheep) {
if (!self.currentSheep) {
self.currentSheep = [];
}
self.currentSheep.push(sheep);
self.visible = true;
LK.setTimeout(function () {
self.currentSheep.splice(self.currentSheep.indexOf(sheep), 1);
if (self.currentSheep.length === 0) {
self.visible = false;
}
}, 1000);
};
self.tick = function () {
if (self.visible && self.currentSheep && self.currentSheep.length > 0) {
for (var i = 0; i < self.currentSheep.length; i++) {
var sheep = self.currentSheep[i];
self.x = sheep.x;
self.y = sheep.y - sheep.height / 2 - self.height / 2;
}
}
};
});
// Shepherd dog class with two states
var Dog = Container.expand(function () {
var self = Container.call(this);
var dogGraphics = self.attachAsset('dog', {
anchorX: 0.5,
anchorY: 0.5
});
var dog2Graphics = self.attachAsset('dog2', {
anchorX: 0.5,
anchorY: 0.5
});
dog2Graphics.visible = false; // Initially hide the dog2
self.targetPosition = {
x: 1024,
y: 1366
}; // Default target position in the middle
self.move = function (pos, lastPos) {
self.x = pos.x;
self.y = pos.y;
if (pos.x > lastPos.x) {
dogGraphics.visible = true;
dog2Graphics.visible = false;
} else if (pos.x < lastPos.x) {
dogGraphics.visible = false;
dog2Graphics.visible = true;
}
};
self.lastPos = {
x: self.x,
y: self.y
}; // Store the last position
self.setTargetPosition = function (pos) {
self.targetPosition = pos;
};
});
// Farm class
var Farm = Container.expand(function () {
var self = Container.call(this);
var farmGraphics = self.attachAsset('farm', {
anchorX: 0.5,
anchorY: 0.5
});
self.resetPosition = function () {
self.x = 1024; // Center of the screen
self.y = -self.height; // Start from the top
};
self.move = function () {
self.y += 4; // Move down at a constant speed
if (self.y > 2732) {
self.destroy(); // Destroy the farm if it moves off the screen
}
};
self.resetPosition(); // Initialize position
});
// Fence class
var Fence = Container.expand(function () {
var self = Container.call(this);
var fenceGraphics = self.attachAsset('fence', {
anchorX: 0.5,
anchorY: 0.5
});
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
});
// FenceLayer class
var FenceLayer = Container.expand(function () {
var self = Container.call(this);
self.fenceInstances = [];
self.rowCounter = 0; // Initialize the row counter
self.addFenceRow = function () {
self.rowCounter++; // Increment the row counter each time a new row is added
var fenceWidth = LK.getAsset('fence', {}).width;
var totalFenceWidth = fenceWidth * Math.floor(2048 / fenceWidth);
var spaceWidth = 2048 - totalFenceWidth;
var spaceIndex = Math.floor(2048 / fenceWidth / 2);
for (var i = 0; i < spaceIndex * 2 + 1; i++) {
if (i < spaceIndex - 3 || i > spaceIndex + 3 || self.rowCounter > 14 && (i < spaceIndex - 2 || i > spaceIndex + 2) || self.rowCounter > 10 && (i < spaceIndex - 1 || i > spaceIndex + 1) || self.rowCounter > 5 && self.rowCounter <= 10 && (i === spaceIndex - 3 || i === spaceIndex + 3) || self.rowCounter >= 16 && self.rowCounter <= 20 && i == spaceIndex + 1) {
var fence = new Fence();
var positionX = i * fenceWidth + spaceWidth / 2;
fence.setPosition(positionX, -fence.height);
self.addChild(fence);
self.fenceInstances.push(fence);
}
}
};
self.moveFences = function () {
for (var i = self.fenceInstances.length - 1; i >= 0; i--) {
var fence = self.fenceInstances[i];
fence.y += 4; // Move down at the same speed as grass
if (fence.y > 2732) {
self.removeChild(fence);
self.fenceInstances.splice(i, 1);
if (!self.pastFenceCount) {
self.pastFenceCount = 0;
}
self.pastFenceCount++;
if (self.pastFenceCount === 20) {
game.addChild(farm);
farm.y = -farm.height; // Set initial position to be just above the top border of the screen
}
}
}
};
self.scheduleNextFenceRow = function () {
var minInterval = 3000; // Minimum 3 seconds before a new row can appear
var maxInterval = 7000; // Maximum 7 seconds before a new row can appear
var interval = Math.random() * (maxInterval - minInterval) + minInterval;
LK.setTimeout(function () {
if (self.rowCounter < 20) {
if (self.fenceInstances.length === 0) {
self.addFenceRow();
}
self.scheduleNextFenceRow();
}
}, interval);
};
self.scheduleNextFenceRow();
});
// Grass class
var Grass = Container.expand(function () {
var self = Container.call(this);
var grassGraphics = self.attachAsset('grass', {
anchorX: 0.5,
anchorY: 0.5
});
self.resetPosition = function () {
self.x = Math.random() * 2048;
self.y = -self.height;
};
self.move = function () {
self.y += 4; // Move down at a constant speed
// Check for collision with any fence
for (var i = 0; i < fenceLayer.fenceInstances.length; i++) {
if (self.intersects(fenceLayer.fenceInstances[i])) {
self.destroy(); // Destroy the grass
return; // Exit the function to prevent further movement
}
}
if (self.y > 2732) {
// If grass is below the screen
self.resetPosition(); // Reset to the top
}
};
self.resetPosition(); // Initialize position
});
// GrassLayer class
var GrassLayer = Container.expand(function () {
var self = Container.call(this);
GrassLayer.prototype.addGrassWithLimitedNumber = function () {
var interval = 1000; // Interval of 1 second
LK.setInterval(function () {
if (self.grassInstances.length < 20) {
self.addGrass();
}
if (self.stoneInstances.length < 3) {
self.addStone();
}
}, interval);
};
self.stoneInstances = [];
self.addStone = function () {
var stone = new Stone();
self.addChild(stone);
self.stoneInstances.push(stone);
self.avoidStoneGrassCollision(stone);
};
self.avoidStoneGrassCollision = function (stone) {
for (var i = 0; i < self.grassInstances.length; i++) {
var grass = self.grassInstances[i];
if (stone.intersects(grass)) {
stone.resetPosition();
i = -1; // Restart the loop to check for collisions again
}
}
};
self.moveStones = function () {
for (var j = 0; j < self.stoneInstances.length; j++) {
self.stoneInstances[j].move();
}
};
self.grassInstances = [];
self.addGrass = function () {
var grass = new Grass();
self.addChild(grass);
self.grassInstances.push(grass);
self.avoidGrassStoneCollision(grass);
};
self.avoidGrassStoneCollision = function (grass) {
for (var i = 0; i < self.stoneInstances.length; i++) {
var stone = self.stoneInstances[i];
if (grass.intersects(stone)) {
grass.resetPosition();
i = -1; // Restart the loop to check for collisions again
}
}
};
self.moveGrass = function () {
for (var j = 0; j < self.grassInstances.length; j++) {
self.grassInstances[j].move();
}
};
self.addGrassWithUniformInterval = function () {
self.addGrass();
var interval = 1000; // Uniform interval of 1 second
LK.setInterval(self.addGrass, interval);
};
self.addGrassWithLimitedNumber(); // Initialize the process with a limited number of grass instances
});
// Instructions class
var Instructions = Container.expand(function () {
var self = Container.call(this);
var instructionsText = new Text2('Use circular mouse movements to gather the sheep into a flock', {
size: 70,
fill: "#ffffff",
align: 'center'
});
self.addChild(instructionsText);
instructionsText.anchor.set(0.5, 1);
instructionsText.x = 2048 / 2;
instructionsText.y = 2732 - 200;
self.show = function () {
self.visible = true;
LK.setTimeout(function () {
self.visible = false;
}, 4000);
};
});
var Intro = Container.expand(function () {
var self = Container.call(this);
var introGraphics = self.attachAsset('intro', {
anchorX: 0.5,
anchorY: 1
});
self.x = 2048 / 2;
self.y = 2732 - 450;
self.show = function () {
self.visible = true;
LK.setTimeout(function () {
self.visible = false;
}, 3000);
};
self.show();
});
// Mouse class
var Mouse = Container.expand(function () {
var self = Container.call(this);
var mouseGraphics = self.attachAsset('mouse', {
anchorX: 0.5,
anchorY: 0.5
});
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
self.show = function () {
self.visible = true;
LK.setTimeout(function () {
self.visible = false;
}, 3000);
};
});
// 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 = 2;
self.direction = Math.random() * Math.PI * 2;
self.move = function () {
var deltaX = Math.cos(self.direction) * self.speed;
var deltaY = Math.sin(self.direction) * self.speed;
self.x += deltaX;
self.y += deltaY;
if (deltaX > 0) {
self.rotation = 5 * (Math.PI / 180);
} else if (deltaX < 0) {
self.rotation = -5 * (Math.PI / 180);
}
};
self.avoidWalls = function () {
if (self.x < 300 || self.x > 1748 || self.y < 300 || self.y > 2432) {
self.direction += Math.PI;
}
};
self.avoidDog = function (dog, targetPosition) {
if (self.intersects(dog)) {
var dx = targetPosition.x - self.x;
var dy = targetPosition.y - self.y;
self.direction = Math.atan2(dy, dx);
}
};
self.avoidSheep = function (sheepArray, stoneArray, fenceArray) {
var personalSpace = 150; // Define a personal space radius for each sheep
for (var i = 0; i < sheepArray.length; i++) {
var otherSheep = sheepArray[i];
if (self !== otherSheep) {
var dx = otherSheep.x - self.x;
var dy = otherSheep.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < personalSpace) {
var avoidanceForce = 1;
var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction
self.direction = avoidanceAngle;
self.x += Math.cos(self.direction) * self.speed * avoidanceForce;
self.y += Math.sin(self.direction) * self.speed * avoidanceForce;
}
}
}
// Avoid stones
for (var j = 0; j < stoneArray.length; j++) {
var stone = stoneArray[j];
var dx = stone.x - self.x;
var dy = stone.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < personalSpace) {
var avoidanceForce = 1;
var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction
self.direction = avoidanceAngle;
self.x += Math.cos(self.direction) * self.speed * avoidanceForce;
self.y += Math.sin(self.direction) * self.speed * avoidanceForce;
}
}
// Avoid fences
for (var k = 0; k < fenceArray.length; k++) {
var fence = fenceArray[k];
var dx = fence.x - self.x;
var dy = fence.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < personalSpace) {
var avoidanceForce = 1;
var avoidanceAngle = Math.atan2(dy, dx) + Math.PI; // Opposite direction
self.direction = avoidanceAngle;
self.x += Math.cos(self.direction) * self.speed * avoidanceForce;
self.y += Math.sin(self.direction) * self.speed * avoidanceForce;
}
}
};
});
// Stone class
var Stone = Container.expand(function () {
var self = Container.call(this);
var stoneGraphics = self.attachAsset('rock', {
anchorX: 0.5,
anchorY: 0.5
});
self.resetPosition = function () {
self.x = Math.random() * 2048;
self.y = -self.height;
};
self.move = function () {
self.y += 4; // Move down at the same speed as grass
// Check for collision with any fence
for (var i = 0; i < fenceLayer.fenceInstances.length; i++) {
if (self.intersects(fenceLayer.fenceInstances[i])) {
self.destroy(); // Destroy the stone
return; // Exit the function to prevent further movement
}
}
if (self.y > 2732) {
// If stone is below the screen
self.resetPosition(); // Reset to the top
}
};
self.resetPosition(); // Initialize position
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xA7D397 // New background color
});
/****
* Game Code
****/
// Initialize the Farm object
var farm = new Farm();
farm.y = -farm.height; // Set initial position to be just above the top border of the screen
var instructions = game.addChild(new Instructions());
instructions.show();
// Initialize and display the Arrow
// Intro instance creation moved below Sheep instances
// Arrow instance creation moved below Sheep instances
// Initialize and display the Mouse
// Mouse instance creation moved below Sheep instances
// Initialize grass layer and add it to the game
var grassLayer = game.addChild(new GrassLayer());
// Initialize fence layer and add it to the game
var fenceLayer = game.addChild(new FenceLayer());
// Initialize sheep array
// Sheep asset
// Shepherd dog asset
var sheepArray = []; // Place fences in a line across the screen with a space in the center
// Create 20 sheep and add them to the game and sheepArray
for (var i = 0; i < 20; i++) {
var sheep = new Sheep();
sheep.x = 300 + Math.random() * (2048 - 600);
sheep.y = 300 + Math.random() * (2732 - 600);
game.addChild(sheep);
sheepArray.push(sheep);
} // Create Intro, Arrow, and Mouse instances to appear above the sheep layer
var intro = game.addChild(new Intro());
var arrow = new Arrow();
arrow.setPosition(2048 / 2, 2732 - arrow.height / 2 - 300);
game.addChild(arrow);
arrow.show();
var mouse = new Mouse();
mouse.setPosition(2048 / 2, 2732 - mouse.height / 2 - 300);
game.addChild(mouse);
mouse.show();
// 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 and handle direction
game.on('move', function (obj) {
var pos = obj.event.getLocalPosition(game);
shepherdDog.move(pos, shepherdDog.lastPos);
shepherdDog.lastPos = pos; // Update the last position
});
// Main game tick event
var baaInstance = game.addChild(new Baa());
LK.setInterval(function () {
for (var i = 0; i < 3; i++) {
var randomSheepIndex = Math.floor(Math.random() * sheepArray.length);
var randomSheep = sheepArray[randomSheepIndex];
baaInstance.show(randomSheep);
}
}, 500);
LK.on('tick', function () {
// Move each sheep, handle collisions, update Baa position, move grass instances, move stones, and move fences
baaInstance.tick();
grassLayer.moveGrass();
grassLayer.moveStones(); // Move the stones
fenceLayer.moveFences(); // Move the fences
for (var i = sheepArray.length - 1; i >= 0; i--) {
var sheep = sheepArray[i];
sheep.move();
sheep.avoidWalls();
sheep.avoidDog(shepherdDog, shepherdDog.targetPosition);
sheep.avoidSheep(sheepArray, grassLayer.stoneInstances, fenceLayer.fenceInstances);
if (sheep.y > 2732) {
game.removeChild(sheep);
sheepArray.splice(i, 1);
}
}
// Check if there are three or less sheep left and apply a pink special effect to them
if (sheepArray.length <= 3) {
for (var i = 0; i < sheepArray.length; i++) {
LK.effects.flashObject(sheepArray[i], 0xFFB6C1, 5000);
}
}
// End the game when there's only one sheep left
if (sheepArray.length <= 1) {
LK.showGameOver();
}
});
// Ensure the game is touchscreen compatible
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
shepherdDog.setTargetPosition(pos);
shepherdDog.move(pos, shepherdDog.lastPos);
shepherdDog.lastPos = pos; // Update the last position
});
game.on('up', function (obj) {
var pos = obj.event.getLocalPosition(game);
shepherdDog.setTargetPosition(pos);
});
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