/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class representing a racing car
var Car = Container.expand(function () {
var self = Container.call(this);
// Attach car asset
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
// Set initial speed
self.speed = 0;
// Update function to move the car
self.update = function () {
self.y += self.speed;
};
// Function to accelerate the car
self.accelerate = function (amount) {
self.speed += amount;
};
// Function to decelerate the car
self.decelerate = function (amount) {
self.speed -= amount;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var playerCar, opponentCar;
var finishLineY = 100; // Y position of the finish line
var raceStarted = false;
// Initialize player and opponent cars
function initCars() {
playerCar = new Car();
playerCar.x = 1024; // Center horizontally
playerCar.y = 2400; // Near the bottom of the screen
game.addChild(playerCar);
opponentCar = new Car();
opponentCar.x = 1024; // Center horizontally
opponentCar.y = 200; // Near the top of the screen
game.addChild(opponentCar);
}
// Start the race
function startRace() {
raceStarted = true;
playerCar.accelerate(5);
opponentCar.accelerate(4);
}
// Check for race completion
function checkRaceCompletion() {
if (playerCar.y <= finishLineY || opponentCar.y <= finishLineY) {
raceStarted = false;
if (playerCar.y <= finishLineY) {
console.log("Player wins!");
} else {
console.log("Opponent wins!");
}
LK.showGameOver();
}
}
// Game update loop
game.update = function () {
if (raceStarted) {
playerCar.update();
opponentCar.update();
checkRaceCompletion();
}
};
// Start the race when the game is initialized
initCars();
startRace();