/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Car movement logic
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Road class
var Road = Container.expand(function () {
var self = Container.call(this);
var roadGraphics = self.attachAsset('road', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Tree class
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x00000000 //Init game with transparent background
});
/****
* Game Code
****/
// Initialize variables
var car;
var obstacles = [];
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
// Create road
var road = new Road();
road.x = 2048 / 2;
road.y = 2732 / 2;
game.addChild(road);
// Create car
car = new Car();
car.x = 2048 / 2;
car.y = 2732 - 200;
game.addChild(car);
// Create tree
var tree = new Tree();
tree.x = 2048 / 2;
tree.y = 2732 - 400;
game.addChild(tree);
// Create score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Set up game update loop
game.update = function () {
// Update car
car.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (car.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new obstacles
if (LK.ticks % 60 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Update score
score++;
scoreTxt.setText(score);
};
// Set up touch controls
game.down = function (x, y, obj) {
car.x = x;
};
game.move = function (x, y, obj) {
car.x = x;
};
}
// Start the game
initGame();