/****
* Classes
****/
// Cookie class for collectible items
var Cookie = Container.expand(function () {
var self = Container.call(this);
var cookieGraphics = self.attachAsset('cookie', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function () {
self.x -= 5; // Move the cookie to the left
};
});
// EnemyLlama class for obstacles
var EnemyLlama = Container.expand(function () {
var self = Container.call(this);
var enemyLlamaGraphics = self.attachAsset('enemyLlama', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function () {
self.x -= 5; // Move the enemy llama to the left
};
});
// Assets will be automatically created based on usage in the code.
// Llama class for the player's character
var Llama = Container.expand(function () {
var self = Container.call(this);
var llamaGraphics = self.attachAsset('llama', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.5;
self.jumpPower = -15;
self.isJumping = false;
self.jump = function () {
if (!self.isJumping) {
self.velocityY = self.jumpPower;
self.isJumping = true;
}
};
self.update = function () {
self.y += self.velocityY;
self.velocityY += self.gravity;
// Prevent the llama from falling off the screen
if (self.y > game.height - llamaGraphics.height / 2) {
self.y = game.height - llamaGraphics.height / 2;
self.isJumping = false;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background
});
/****
* Game Code
****/
var player = game.addChild(new Llama());
player.x = 200;
player.y = game.height / 2;
var cookies = [];
var enemyLlamas = [];
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Function to spawn cookies
function spawnCookie() {
var cookie = new Cookie();
cookie.x = game.width + 50; // Start off-screen
cookie.y = Math.random() * game.height;
cookies.push(cookie);
game.addChild(cookie);
}
// Function to spawn enemy llamas
function spawnEnemyLlama() {
var enemyLlama = new EnemyLlama();
enemyLlama.x = game.width + 50; // Start off-screen
enemyLlama.y = Math.random() * game.height;
enemyLlamas.push(enemyLlama);
game.addChild(enemyLlama);
}
// Touch event to make the llama jump
game.on('down', function () {
player.jump();
});
// Game tick event
LK.on('tick', function () {
player.update();
// Move and check for collisions with cookies
for (var i = cookies.length - 1; i >= 0; i--) {
cookies[i].move();
if (player.intersects(cookies[i])) {
score += 1;
scoreTxt.setText(score.toString());
cookies[i].destroy();
cookies.splice(i, 1);
} else if (cookies[i].x < -50) {
// Remove off-screen cookies
cookies[i].destroy();
cookies.splice(i, 1);
}
}
// Move and check for collisions with enemy llamas
for (var j = enemyLlamas.length - 1; j >= 0; j--) {
enemyLlamas[j].move();
if (player.intersects(enemyLlamas[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else if (enemyLlamas[j].x < -50) {
// Remove off-screen enemy llamas
enemyLlamas[j].destroy();
enemyLlamas.splice(j, 1);
}
}
// Spawn cookies and enemy llamas at intervals
if (LK.ticks % 120 == 0) {
spawnCookie();
}
if (LK.ticks % 300 == 0) {
spawnEnemyLlama();
}
});
a llama-shaped cookie. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a demon llama. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a rainbow llama. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.