/****
* Classes
****/
var Acorn = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('acorn', {
anchorX: 0.5,
anchorY: 0
});
self.blinking = false;
self.blink = function () {
self.blinking = !self.blinking;
self.alpha = self.blinking ? 0.5 : 1;
};
LK.setInterval(self.blink, 500);
self.update = function () {
self.y += platformSpeed;
if (self.y > 2732) {
self.y = -self.height;
self.x = Math.random() * 2048;
}
};
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.lastPlatform = -1;
self.speedY = 0;
self.gravity = 0.5;
self.jumpForce = -25;
self.jump = function () {
self.speedY = self.jumpForce;
self.onPlatform = false;
};
self.update = function () {
self.y += self.speedY;
self.speedY += self.gravity;
// Prevent hero from falling below the ground
if (self.y > 2732 - 25) {
self.y = 2732 - 25;
self.speedY = 0;
}
// Make the player land on platforms
for (var i = 0; i < obstacles.length; i++) {
if (self.intersects(obstacles[i])) {
if (self.y < obstacles[i].y) {
self.y = obstacles[i].y - obstacles[i].height / 2 - self.height / 2;
self.speedY = 0;
self.onPlatform = true;
} else {
self.onPlatform = false;
}
if (self.y > obstacles[i].y) {
self.speedY = 10;
}
}
}
};
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
self.update = function () {
self.y += platformSpeed;
};
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
});
var TreeTrunk = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('treeTrunk', {
anchorX: 0.5,
anchorY: 0
});
self.update = function () {
self.y += platformSpeed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Create static platforms
var scoreTimer = LK.setInterval(function () {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
}, 1000);
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.on('move', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.x = pos.x;
});
var obstacles = [];
var nbPlatform = 10;
var gap = 2732 / (nbPlatform - 2);
var platformSpeed = 1;
var treeTrunks = [];
for (var i = 0; i < 3; ++i) {
var treeTrunk = game.addChild(new TreeTrunk());
treeTrunk.x = 2048 / 2;
treeTrunk.y = i * -treeTrunk.height;
treeTrunks.push(treeTrunk);
}
for (var i = 0; i < nbPlatform; i++) {
var platform = new Obstacle();
if (i < 2) {
platform.width = 2732;
platform.x = 2048 / 2;
} else {
platform.width = (Math.random() * (0.3 - 0.1) + 0.1) * 2048;
platform.x = Math.random() * (2048 - platform.width);
}
platform.y = 2732 - i * gap;
platform.height = 40;
obstacles.push(game.addChild(platform));
}
var lastPlatformSpawned = nbPlatform - 1;
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 / 2;
var acorn = game.addChild(new Acorn());
acorn.x = Math.random() * 2048;
acorn.y = -acorn.height;
// Handle touch to make the hero jump
game.on('down', function () {
if (hero.onPlatform) {
hero.jump();
}
});
LK.on('tick', function () {
hero.update();
acorn.update();
if (hero.intersects(acorn)) {
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
acorn.y = -acorn.height;
acorn.x = Math.random() * 2048;
}
// Game tick
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
if (obstacles[i].y > 2732) {
obstacles[i].y = obstacles[lastPlatformSpawned].height - gap;
obstacles[i].x = Math.random() * (2048 - obstacles[i].width);
obstacles[i].width = (Math.random() * (0.3 - 0.1) + 0.1) * 2048;
if (platformSpeed < 20) {
platformSpeed += 0.1;
}
lastPlatformSpawned = i;
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].speedY = platformSpeed;
}
}
}
for (var i = 0; i < 3; ++i) {
treeTrunks[i].update();
if (treeTrunks[i].y > 2732) {
treeTrunks[i].y = treeTrunks[(i + 2 + treeTrunks.length) % treeTrunks.length].y - treeTrunk.height;
}
}
// Game over when hero touches bottom of the screen
if (hero.y >= 2732 - hero.height) {
LK.showGameOver();
}
});
a little squirrel with sun glasses and earring. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
2d flat leaf green horizontal platform. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
acorn. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.