/****
* 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_migrated = function () {
var dx = 1024 - self.x;
var dy = 1366 - self.y;
var angle = Math.atan2(dy, dx);
var isInsideGrass = false;
for (var i = 0; i < grasses.length; i++) {
if (grasses[i].intersects(self)) {
isInsideGrass = true;
break;
}
}
if (isInsideGrass) {
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
} else {
self.x += Math.cos(angle) * self.speed * 0.5; // slow down the speed by half outside the grass
self.y += Math.sin(angle) * self.speed * 0.5; // slow down the speed by half outside the grass
}
};
});
// 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_migrated = 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_migrated = 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_migrated = 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 (x, y, obj) {
dragNode = glider;
});
game.on('move', function (x, y, obj) {
var pos = game.toLocal(obj.global);
glider.x = pos.x;
glider.y = pos.y;
});
game.on('up', function (x, y, obj) {
dragNode = null;
});
// Array to hold box objects
var boxes = [];
var boxesEaten = 0;
var boxesEatenTxt = new Text2('Boxes Eaten: 0', {
size: 100,
fill: "#ffffff"
});
LK.gui.top.addChild(boxesEatenTxt);
// Game tick event
LK.on('tick', function () {
// Update glider, pig, stars, and obstacles
glider._update_migrated();
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_migrated();
// 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_migrated();
// Check if pig intersects with box and the box is inside the grass
if (pig.intersects(box)) {
var isInsideGrass = false;
for (var i = 0; i < grasses.length; i++) {
if (grasses[i].intersects(box)) {
isInsideGrass = true;
break;
}
}
if (isInsideGrass) {
box.destroy();
boxes.splice(index, 1);
boxesEaten++;
boxesEatenTxt.setText(' ' + boxesEaten);
// Generate a new grass at a random position
var newGrass = game.addChild(new Grass());
newGrass.x = Math.random() * 2048; // Random position horizontally
newGrass.y = Math.random() * 2732; // Random position vertically
grasses.push(newGrass);
// Destroy a box in the scene
if (boxes.length > 0) {
var boxToDestroy = boxes[0];
boxToDestroy.destroy();
boxes.splice(0, 1);
}
}
}
// Check if box reaches the center
if (Math.abs(box.x - 1024) < 5 && Math.abs(box.y - 1366) < 5) {
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 % Math.max(60 - Math.floor(LK.ticks / 6000), 10) == 0) {
// Every second, decreasing to every 0.1 seconds over time
// 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);
}
});