/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Car class representing the player's car var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0; self.update = function () { self.y += self.speed; }; }); // Obstacle class representing obstacles on the track 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(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var car; var obstacles = []; var score = 0; var scoreTxt; var gameSpeed = 5; // Initialize the game elements function initGame() { car = game.addChild(new Car()); car.x = 2048 / 2; car.y = 2732 - 200; car.speed = 0; scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); } // Handle touch/mouse move events game.move = function (x, y, obj) { car.x = x; }; // Update the game state game.update = function () { // Update car position 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 = -100; obstacles.push(newObstacle); game.addChild(newObstacle); } // Update score score += gameSpeed; scoreTxt.setText(score); }; // Initialize the game initGame();
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class representing the player's car
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.update = function () {
self.y += self.speed;
};
});
// Obstacle class representing obstacles on the track
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();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var car;
var obstacles = [];
var score = 0;
var scoreTxt;
var gameSpeed = 5;
// Initialize the game elements
function initGame() {
car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2732 - 200;
car.speed = 0;
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
}
// Handle touch/mouse move events
game.move = function (x, y, obj) {
car.x = x;
};
// Update the game state
game.update = function () {
// Update car position
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 = -100;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Update score
score += gameSpeed;
scoreTxt.setText(score);
};
// Initialize the game
initGame();