/****
* Classes
****/
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
//<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;
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize trains array
var trains = [];
// Handle player movement
game.move = function (x, y, obj) {
player.move(x, y);
};
// Spawn trains at intervals
var trainSpawnInterval = LK.setInterval(function () {
var newTrain = new Train();
newTrain.x = Math.random() * 2048;
newTrain.y = -100;
trains.push(newTrain);
game.addChild(newTrain);
}, 1000);
// Update game state
game.update = function () {
for (var i = trains.length - 1; i >= 0; i--) {
if (trains[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (trains[i].y > 2732) {
trains[i].destroy();
trains.splice(i, 1);
}
}
};
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update score
var score = 0;
var scoreUpdateInterval = LK.setInterval(function () {
score += 1;
scoreTxt.setText(score);
}, 1000);