/**** * Classes ****/ // Boost class representing speed boosts var Boost = Container.expand(function () { var self = Container.call(this); var boostGraphics = self.attachAsset('boost', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { self.y += 5; }; }); //<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; }; }); // Traffic class representing other cars on the road var Traffic = Container.expand(function () { var self = Container.call(this); var trafficGraphics = self.attachAsset('traffic', { 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 player car var playerCar = new Car(); playerCar.x = 2048 / 2; playerCar.y = 2732 - 200; game.addChild(playerCar); // Arrays to keep track of traffic and boosts var trafficCars = []; var boosts = []; // Score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Game update function game.update = function () { // Update player car position playerCar.update(); // Update traffic cars for (var i = trafficCars.length - 1; i >= 0; i--) { trafficCars[i].update(); if (trafficCars[i].y > 2732) { trafficCars[i].destroy(); trafficCars.splice(i, 1); } if (playerCar.intersects(trafficCars[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update boosts for (var j = boosts.length - 1; j >= 0; j--) { boosts[j].update(); if (boosts[j].y > 2732) { boosts[j].destroy(); boosts.splice(j, 1); } if (playerCar.intersects(boosts[j])) { playerCar.speed += 2; boosts[j].destroy(); boosts.splice(j, 1); } } // Spawn new traffic cars if (LK.ticks % 60 == 0) { var newTraffic = new Traffic(); newTraffic.x = Math.random() * 2048; newTraffic.y = -100; trafficCars.push(newTraffic); game.addChild(newTraffic); } // Spawn new boosts if (LK.ticks % 300 == 0) { var newBoost = new Boost(); newBoost.x = Math.random() * 2048; newBoost.y = -100; boosts.push(newBoost); game.addChild(newBoost); } // Update score LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); }; // Handle player car movement game.move = function (x, y, obj) { playerCar.x = x; }; // Start the game game.down = function (x, y, obj) { playerCar.speed = 10; }; // Stop the car when touch is released game.up = function (x, y, obj) { playerCar.speed = 0; };
/****
* Classes
****/
// Boost class representing speed boosts
var Boost = Container.expand(function () {
var self = Container.call(this);
var boostGraphics = self.attachAsset('boost', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5;
};
});
//<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;
};
});
// Traffic class representing other cars on the road
var Traffic = Container.expand(function () {
var self = Container.call(this);
var trafficGraphics = self.attachAsset('traffic', {
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 player car
var playerCar = new Car();
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
game.addChild(playerCar);
// Arrays to keep track of traffic and boosts
var trafficCars = [];
var boosts = [];
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game update function
game.update = function () {
// Update player car position
playerCar.update();
// Update traffic cars
for (var i = trafficCars.length - 1; i >= 0; i--) {
trafficCars[i].update();
if (trafficCars[i].y > 2732) {
trafficCars[i].destroy();
trafficCars.splice(i, 1);
}
if (playerCar.intersects(trafficCars[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update boosts
for (var j = boosts.length - 1; j >= 0; j--) {
boosts[j].update();
if (boosts[j].y > 2732) {
boosts[j].destroy();
boosts.splice(j, 1);
}
if (playerCar.intersects(boosts[j])) {
playerCar.speed += 2;
boosts[j].destroy();
boosts.splice(j, 1);
}
}
// Spawn new traffic cars
if (LK.ticks % 60 == 0) {
var newTraffic = new Traffic();
newTraffic.x = Math.random() * 2048;
newTraffic.y = -100;
trafficCars.push(newTraffic);
game.addChild(newTraffic);
}
// Spawn new boosts
if (LK.ticks % 300 == 0) {
var newBoost = new Boost();
newBoost.x = Math.random() * 2048;
newBoost.y = -100;
boosts.push(newBoost);
game.addChild(newBoost);
}
// Update score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
};
// Handle player car movement
game.move = function (x, y, obj) {
playerCar.x = x;
};
// Start the game
game.down = function (x, y, obj) {
playerCar.speed = 10;
};
// Stop the car when touch is released
game.up = function (x, y, obj) {
playerCar.speed = 0;
};