Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Count and display the number of eaten boxes
User prompt
Count and display the number of eaten boxes
User prompt
Migrate to the latest version of LK
User prompt
slowly increase the number of boxes generated over time
User prompt
Increase the number of boxes generated over time
User prompt
When the pig eats the box, it will also destroy a box in the scene.
User prompt
When the pig eats the box, it will also destroy a boxes in the scene.
User prompt
When the pig eats the box, it will also destroy the a boxes in the scene.
User prompt
The number of boxes generated will increase over time
User prompt
Box generation speed will become faster over time
User prompt
When the pig eats the box, it will also destroy the two boxes in the scene.
User prompt
When the pig eats the box, all the boxes will retreat outside the scene.
User prompt
When the pig eats 10 boxes, it will become larger. The upper limit is 500*500.
User prompt
The box takes priority to the grass position
User prompt
Box movement slows down outside grass
User prompt
When a pig eats a box, a new grass is generated
User prompt
pig cannot eat boxes outside the grass
User prompt
When the box reaches the center, the game ends
User prompt
Let glider follow the mouse hover position
User prompt
Let glider follow the mouse hover position
User prompt
Let glider follow the mouse hover position
User prompt
The pig needs to eat 100 boxes to reach the size of 502*502
User prompt
When the pig exceeds the size of 500*500, the game ends
/****
* Classes
****/
// Box class
var Box = Container.expand(function () {
var self = Container.call(this);
var boxGraphics = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.move = function () {
var dx = 1024 - self.x;
var dy = 1366 - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
};
});
// Flower class
var Flower = Container.expand(function () {
var self = Container.call(this);
var flowerGraphics = self.attachAsset('flower', {
anchorX: 0.5,
anchorY: 0.5
});
self.grow = function () {
// Flower growth logic here
};
});
// Assets are automatically created based on usage in the code.
// Glider class
var Glider = Container.expand(function () {
var self = Container.call(this);
var gliderGraphics = self.attachAsset('glider', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Glider update logic here
};
});
// Grass class
var Grass = Container.expand(function () {
var self = Container.call(this);
var grassGraphics = self.attachAsset('grass', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function () {
// Obstacle movement logic here
};
});
// Pig class
var Pig = Container.expand(function () {
var self = Container.call(this);
var pigGraphics = self.attachAsset('pig', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Pig update logic here
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.collect = function () {
// Star collection logic here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to represent the sky
});
/****
* Game Code
****/
var glider = game.addChild(new Glider());
var pig = game.addChild(new Pig());
pig.x = glider.x + 200; // Start 200 pixels to the right of the glider
pig.y = glider.y; // Start at the same vertical position as the glider
var grasses = [];
for (var i = 0; i < 50; i++) {
var grass = game.addChild(new Grass());
grass.x = Math.random() * 2048; // Random position horizontally
grass.y = Math.random() * 2732; // Random position vertically
grasses.push(grass);
}
var flower = game.addChild(new Flower());
flower.x = 1024; // Start in the middle of the screen horizontally
flower.y = 1366; // Start in the middle of the screen vertically
glider.x = 1024; // Start in the middle of the screen horizontally
glider.y = 1366; // Start in the middle of the screen vertically
var stars = []; // Array to hold star objects
var obstacles = []; // Array to hold obstacle objects
// Touch and drag to move the glider
var dragNode = null;
game.on('down', function (obj) {
dragNode = glider;
});
game.on('move', function (obj) {
if (dragNode) {
var pos = obj.event.getLocalPosition(game);
dragNode.x = pos.x;
}
});
game.on('up', function (obj) {
dragNode = null;
});
// Array to hold box objects
var boxes = [];
// Game tick event
LK.on('tick', function () {
// Update glider, pig, stars, and obstacles
glider.update();
var angle = Math.atan2(pig.y - glider.y, pig.x - glider.x);
angle += 0.01; // Rotate the pig around the glider at a speed of 0.01 radians per tick
pig.x = glider.x + Math.cos(angle) * 200;
pig.y = glider.y + Math.sin(angle) * 200;
flower.grow();
stars.forEach(function (star, index) {
star.collect();
// Remove star if collected or off-screen
});
obstacles.forEach(function (obstacle, index) {
obstacle.move();
// Check for collision with glider
if (glider.intersects(obstacle)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Remove obstacle if off-screen
});
boxes.forEach(function (box, index) {
box.move();
// Check if pig intersects with box
if (pig.intersects(box)) {
box.destroy();
boxes.splice(index, 1);
// Increase the size of the pig
pig.scale.x *= 1.1;
pig.scale.y *= 1.1;
// Move the pig further from the glider
var distance = Math.sqrt(Math.pow(pig.x - glider.x, 2) + Math.pow(pig.y - glider.y, 2));
var angle = Math.atan2(pig.y - glider.y, pig.x - glider.x);
pig.x = glider.x + Math.cos(angle) * (distance + 10);
pig.y = glider.y + Math.sin(angle) * (distance + 10);
// Check if the pig size exceeds 500*500 and end the game if it does
if (pig.scale.x * pig.width > 500 && pig.scale.y * pig.height > 500) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
});
// Spawn stars, obstacles, and boxes
if (LK.ticks % 120 == 0) {// Every 2 seconds
// Code to spawn stars
}
if (LK.ticks % 180 == 0) {// Every 3 seconds
// Code to spawn obstacles
}
if (LK.ticks % 60 == 0) {
// Every second
// Spawn a box at a random position on the edge of the screen
var angle = Math.random() * Math.PI * 2;
var x = 1024 + Math.cos(angle) * 1366;
var y = 1366 + Math.sin(angle) * 1366;
var box = game.addChild(new Box());
box.x = x;
box.y = y;
boxes.push(box);
}
});