/****
* Classes
****/
// Gold class
var Gold = Container.expand(function () {
var self = Container.call(this);
var goldGraphics = self.attachAsset('gold', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 2;
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 3;
};
});
//<Assets used in the game will automatically appear here>
// Train class
var Train = Container.expand(function () {
var self = Container.call(this);
var trainGraphics = self.attachAsset('train', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var train;
var golds = [];
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize train
train = game.addChild(new Train());
train.x = 2048 / 2;
train.y = 2732 - 200;
// Function to handle game over
function gameOver() {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Function to handle move events
function handleMove(x, y, obj) {
train.x = x;
train.y = y;
}
// Mouse or touch move on game object
game.move = handleMove;
// Mouse or touch down on game object
game.down = function (x, y, obj) {
handleMove(x, y, obj);
};
// Mouse or touch up on game object
game.up = function (x, y, obj) {
// No action needed on up event
};
// Game update function
game.update = function () {
// Update train position
train.update();
// Update golds and check for collisions
for (var i = golds.length - 1; i >= 0; i--) {
golds[i].update();
if (train.intersects(golds[i])) {
score += 10;
scoreTxt.setText(score);
golds[i].destroy();
golds.splice(i, 1);
} else if (golds[i].y > 2732) {
golds[i].destroy();
golds.splice(i, 1);
}
}
// Update obstacles and check for collisions
for (var j = obstacles.length - 1; j >= 0; j--) {
obstacles[j].update();
if (train.intersects(obstacles[j])) {
gameOver();
return;
} else if (obstacles[j].y > 2732) {
obstacles[j].destroy();
obstacles.splice(j, 1);
}
}
// Spawn golds and obstacles
if (LK.ticks % 60 == 0) {
var newGold = new Gold();
newGold.x = Math.random() * 2048;
newGold.y = -50;
golds.push(newGold);
game.addChild(newGold);
}
if (LK.ticks % 90 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
};
A single coin. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Rocks. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Colorful Train engine looking straight with no background .. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.