/****
* Classes
****/
// Class for Birds
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.x -= self.speed;
};
});
// Class for Clouds
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('Cloud', {
anchorX: 0.5,
anchorY: 0.5
});
cloudGraphics.alpha = 0.5; // Set transparency
self.lifetime = 60; // Lifetime in frames (1 second at 60 FPS)
self.update = function () {
self.lifetime--;
if (self.lifetime <= 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the Excavator
var Excavator = Container.expand(function () {
var self = Container.call(this);
var excavatorGraphics = self.attachAsset('excavator', {
anchorX: 0.5,
anchorY: 0.5
});
self.jumpHeight = 500;
self.isJumping = false;
self.jumpSpeed = 20;
self.originalY = 0;
self.jumpCount = 0; // Initialize jump counter
self.jump = function () {
if (!self.isJumping && self.y >= self.originalY) {
// Check if the excavator is not jumping and is on the ground
self.isJumping = true;
var cloud = new Cloud();
cloud.x = self.x;
cloud.y = self.y;
game.addChild(cloud);
self.originalY = self.y;
self.jumpCount++; // Increment jump counter
}
};
self.update = function () {
if (self.isJumping) {
self.y -= self.jumpSpeed;
if (self.y <= self.originalY - self.jumpHeight * 3) {
self.isJumping = false; // Reset jumping state when the excavator lands
}
} else if (self.y < self.originalY) {
self.y += self.jumpSpeed;
}
};
});
// Class for Grass
var Grass = Container.expand(function () {
var self = Container.call(this);
var grassGraphics = self.attachAsset('soil', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
// Class for Land
var Land = Container.expand(function () {
var self = Container.call(this);
var landGraphics = self.attachAsset('Land', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Land remains static, no update logic needed
};
});
// Class for Trees
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.isRed = false;
self.update = function () {
self.x -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB //Init game with sky blue background
});
/****
* Game Code
****/
var lands = [];
for (var i = 0; i < 7; i++) {
// Reduce the number of land objects per row to about 7
var land = new Land();
land.x = i * 300; // Adjust position to space out the land pieces
land.y = 2150; // Adjust the land object to be slightly lower
lands.push(land);
game.addChild(land);
var additionalLand = new Land();
additionalLand.x = i * 300; // Adjust position to space out the additional land pieces
additionalLand.y = 2250; // Position additional land objects further down
lands.push(additionalLand);
game.addChild(additionalLand);
var moreLand = new Land();
moreLand.x = i * 300; // Adjust position to space out the more land pieces
moreLand.y = 2350; // Position more land objects even further down
lands.push(moreLand);
game.addChild(moreLand);
var extraLand = new Land();
extraLand.x = i * 300; // Adjust position to space out the extra land pieces
extraLand.y = 2450; // Position extra land objects even further down
lands.push(extraLand);
game.addChild(extraLand);
}
var excavator = game.addChild(new Excavator());
excavator.x = 200;
excavator.y = 2000;
var trees = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
excavator.jump();
};
game.update = function () {
excavator.update();
// Add grass as the ground
if (LK.ticks % 30 == 0) {
var newGrass = new Grass();
newGrass.x = 2048; // Start from the right edge
newGrass.y = 2100; // Position grass as the ground
game.addChild(newGrass);
game.setChildIndex(newGrass, game.children.length - 1); // Ensure grass is rendered above other elements
for (var _i = 0, _lands = lands; _i < _lands.length; _i++) {
var land = _lands[_i];
game.setChildIndex(land, game.children.length - 3); // Ensure land is rendered below grass
}
}
for (var i = game.children.length - 1; i >= 0; i--) {
var child = game.children[i];
if (child instanceof Cloud) {
child.update();
}
}
if (LK.ticks % 60 == 0) {
var newTree = new Tree();
newTree.x = 2048;
newTree.y = 2000;
if (Math.random() < 0.2 && !(trees.length > 0 && trees[trees.length - 1].isRed)) {
newTree.isRed = true;
newTree.attachAsset('redTree', {
anchorX: 0.5,
anchorY: 0.5
});
}
trees.push(newTree);
game.addChild(newTree);
// Add birds only if there are no red trees below and with a 50% probability
if (!newTree.isRed && Math.random() < 0.5) {
var newBird = new Bird();
newBird.x = 2048;
newBird.y = 1000;
trees.push(newBird);
game.addChild(newBird);
}
}
for (var i = trees.length - 1; i >= 0; i--) {
var tree = trees[i];
tree.update();
if (tree.x < -100) {
tree.destroy();
trees.splice(i, 1);
continue;
}
if (excavator.intersects(tree)) {
if (tree instanceof Bird) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
} else if (tree.isRed && !excavator.isJumping) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
} else if (!tree.isRed) {
score++;
scoreTxt.setText("Score: " + score + ", Trees destroyed: " + (score - 1));
tree.destroy();
trees.splice(i, 1);
if (score >= 50) {
LK.showYouWin();
}
}
} else if (tree.isRed) {
for (var j = trees.length - 1; j >= 0; j--) {
var normalTree = trees[j];
if (!normalTree.isRed && tree.intersects(normalTree)) {
normalTree.destroy();
trees.splice(j, 1);
}
}
}
}
}; /****
* Classes
****/
// Class for Birds
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.x -= self.speed;
};
});
// Class for Clouds
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('Cloud', {
anchorX: 0.5,
anchorY: 0.5
});
cloudGraphics.alpha = 0.5; // Set transparency
self.lifetime = 60; // Lifetime in frames (1 second at 60 FPS)
self.update = function () {
self.lifetime--;
if (self.lifetime <= 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the Excavator
var Excavator = Container.expand(function () {
var self = Container.call(this);
var excavatorGraphics = self.attachAsset('excavator', {
anchorX: 0.5,
anchorY: 0.5
});
self.jumpHeight = 500;
self.isJumping = false;
self.jumpSpeed = 20;
self.originalY = 0;
self.jumpCount = 0; // Initialize jump counter
self.jump = function () {
if (!self.isJumping && self.y >= self.originalY) {
// Check if the excavator is not jumping and is on the ground
self.isJumping = true;
var cloud = new Cloud();
cloud.x = self.x;
cloud.y = self.y;
game.addChild(cloud);
self.originalY = self.y;
self.jumpCount++; // Increment jump counter
}
};
self.update = function () {
if (self.isJumping) {
self.y -= self.jumpSpeed;
if (self.y <= self.originalY - self.jumpHeight * 3) {
self.isJumping = false; // Reset jumping state when the excavator lands
}
} else if (self.y < self.originalY) {
self.y += self.jumpSpeed;
}
};
});
// Class for Grass
var Grass = Container.expand(function () {
var self = Container.call(this);
var grassGraphics = self.attachAsset('soil', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
// Class for Land
var Land = Container.expand(function () {
var self = Container.call(this);
var landGraphics = self.attachAsset('Land', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Land remains static, no update logic needed
};
});
// Class for Trees
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.isRed = false;
self.update = function () {
self.x -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB //Init game with sky blue background
});
/****
* Game Code
****/
var lands = [];
for (var i = 0; i < 7; i++) {
// Reduce the number of land objects per row to about 7
var land = new Land();
land.x = i * 300; // Adjust position to space out the land pieces
land.y = 2150; // Adjust the land object to be slightly lower
lands.push(land);
game.addChild(land);
var additionalLand = new Land();
additionalLand.x = i * 300; // Adjust position to space out the additional land pieces
additionalLand.y = 2250; // Position additional land objects further down
lands.push(additionalLand);
game.addChild(additionalLand);
var moreLand = new Land();
moreLand.x = i * 300; // Adjust position to space out the more land pieces
moreLand.y = 2350; // Position more land objects even further down
lands.push(moreLand);
game.addChild(moreLand);
var extraLand = new Land();
extraLand.x = i * 300; // Adjust position to space out the extra land pieces
extraLand.y = 2450; // Position extra land objects even further down
lands.push(extraLand);
game.addChild(extraLand);
}
var excavator = game.addChild(new Excavator());
excavator.x = 200;
excavator.y = 2000;
var trees = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
excavator.jump();
};
game.update = function () {
excavator.update();
// Add grass as the ground
if (LK.ticks % 30 == 0) {
var newGrass = new Grass();
newGrass.x = 2048; // Start from the right edge
newGrass.y = 2100; // Position grass as the ground
game.addChild(newGrass);
game.setChildIndex(newGrass, game.children.length - 1); // Ensure grass is rendered above other elements
for (var _i = 0, _lands = lands; _i < _lands.length; _i++) {
var land = _lands[_i];
game.setChildIndex(land, game.children.length - 3); // Ensure land is rendered below grass
}
}
for (var i = game.children.length - 1; i >= 0; i--) {
var child = game.children[i];
if (child instanceof Cloud) {
child.update();
}
}
if (LK.ticks % 60 == 0) {
var newTree = new Tree();
newTree.x = 2048;
newTree.y = 2000;
if (Math.random() < 0.2 && !(trees.length > 0 && trees[trees.length - 1].isRed)) {
newTree.isRed = true;
newTree.attachAsset('redTree', {
anchorX: 0.5,
anchorY: 0.5
});
}
trees.push(newTree);
game.addChild(newTree);
// Add birds only if there are no red trees below and with a 50% probability
if (!newTree.isRed && Math.random() < 0.5) {
var newBird = new Bird();
newBird.x = 2048;
newBird.y = 1000;
trees.push(newBird);
game.addChild(newBird);
}
}
for (var i = trees.length - 1; i >= 0; i--) {
var tree = trees[i];
tree.update();
if (tree.x < -100) {
tree.destroy();
trees.splice(i, 1);
continue;
}
if (excavator.intersects(tree)) {
if (tree instanceof Bird) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
} else if (tree.isRed && !excavator.isJumping) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
} else if (!tree.isRed) {
score++;
scoreTxt.setText("Score: " + score + ", Trees destroyed: " + (score - 1));
tree.destroy();
trees.splice(i, 1);
if (score >= 50) {
LK.showYouWin();
}
}
} else if (tree.isRed) {
for (var j = trees.length - 1; j >= 0; j--) {
var normalTree = trees[j];
if (!normalTree.isRed && tree.intersects(normalTree)) {
normalTree.destroy();
trees.splice(j, 1);
}
}
}
}
};
Excavator with 2d tender rockets. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Tree. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Red tree. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Nube. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Mud ground. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows