/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class representing each car in the race
var Car = Container.expand(function () {
var self = Container.call(this);
// Randomly select one of the 25 car types
var carType = Math.floor(Math.random() * 25) + 1;
var carGraphics = self.attachAsset('car' + carType, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 2 + 1; // Random speed for each car
self.update = function () {
self.y -= self.speed;
if (self.y < -100) {
self.y = 2732 + 100;
self.speed = Math.random() * 2 + 1; // Reset speed
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize cars array
var cars = [];
for (var i = 0; i < 10; i++) {
var car = new Car();
car.x = 2048 / 2 + (i - 5) * 150; // Spread cars horizontally
car.y = 2732 - 200; // Start position at the bottom
cars.push(car);
game.addChild(car);
}
// Initialize score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Update function to check for the winning car
game.update = function () {
for (var i = 0; i < cars.length; i++) {
if (cars[i].y < 100) {
// Car has reached the finish line
LK.setScore(LK.getScore() + 1000);
scoreTxt.setText(LK.getScore());
// Reset car position
cars[i].y = 2732 + 100;
cars[i].speed = Math.random() * 2 + 1; // Reset speed
}
}
};