/**** * Classes ****/ // Define the Car class var Car = Container.expand(function () { var self = Container.call(this); var carGraphics = self.attachAsset('car', { anchorX: 0.5, anchorY: 0.5, orientation: 2, // Rotate 180 degrees scaleX: 3.0, scaleY: 3.0 }); self.speed = 3.125; // Increase speed by 25% self.update = function () { self.y += self.speed; }; }); // The assets will be automatically created and loaded by the LK engine // Define the Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5, orientation: 2, // Rotate 180 degrees scaleX: 3.0, scaleY: 3.0 }); self.speed = 5; self.update = function () { self.x += self.speed; if (self.x < 0) { self.x = 0; } else if (self.x > 2048 * 0.9) { self.x = 2048 * 0.9; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Attach the race track background image var raceTrack = LK.getAsset('raceTrack', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(raceTrack); // Initialize score text for car counter var carCounterTxt = new Text2('Cars: 0', { size: 100, fill: "#ffffff" }); carCounterTxt.anchor.set(0.5, 0); LK.gui.top.addChild(carCounterTxt); var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - player.height / 2; var cars = []; var carCounter = 0; // Initialize car counter // Initialize 1 to 3 cars on the screen at a time var initialCarCount = Math.floor(Math.random() * 3) + 1; for (var i = 0; i < initialCarCount; i++) { var car = game.addChild(new Car()); car.x = Math.random() * 2048; car.y = Math.random() * 2732; cars.push(car); } // Game update function game.update = function () { // Update player and cars player.update(); for (var i = 0; i < cars.length; i++) { if (carCounter >= 10 && carCounter < 20) { cars[i].speed = 3.125 * 1.2; // Increase speed by 20% } else if (carCounter >= 20) { cars[i].speed = 3.125 * 1.44; // Increase speed by another 20% (total 44%) } cars[i].update(); } // Check for collisions for (var i = 0; i < cars.length; i++) { if (player.intersects(cars[i])) { LK.showGameOver(); break; } } // Check for off-screen cars for (var i = cars.length - 1; i >= 0; i--) { if (cars[i].y > 2732) { cars[i].destroy(); cars.splice(i, 1); if (cars.length < 3) { var newCar = game.addChild(new Car()); newCar.x = Math.random() * 2048; newCar.y = 0; cars.push(newCar); carCounter++; // Increment car counter } } } carCounterTxt.setText('Cars: ' + carCounter); if (carCounter >= 30) { LK.showGameOver(); var congratsTxt = new Text2('Congratulations! You are the champion', { size: 150, fill: "#ffffff" }); congratsTxt.anchor.set(0.5, 0.5); LK.gui.center.addChild(congratsTxt); } }; // LK event listeners for controlling the player car game.down = function (x, y, obj) { if (x < player.x) { player.speed = -5; } else if (x > player.x) { player.speed = 5; } // Remove controls for the other cars }; game.up = function (x, y, obj) { player.speed = 0; // Ensure other cars stop moving when touch or mouse is released };
/****
* Classes
****/
// Define the Car class
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5,
orientation: 2,
// Rotate 180 degrees
scaleX: 3.0,
scaleY: 3.0
});
self.speed = 3.125; // Increase speed by 25%
self.update = function () {
self.y += self.speed;
};
});
// The assets will be automatically created and loaded by the LK engine
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5,
orientation: 2,
// Rotate 180 degrees
scaleX: 3.0,
scaleY: 3.0
});
self.speed = 5;
self.update = function () {
self.x += self.speed;
if (self.x < 0) {
self.x = 0;
} else if (self.x > 2048 * 0.9) {
self.x = 2048 * 0.9;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Attach the race track background image
var raceTrack = LK.getAsset('raceTrack', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(raceTrack);
// Initialize score text for car counter
var carCounterTxt = new Text2('Cars: 0', {
size: 100,
fill: "#ffffff"
});
carCounterTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(carCounterTxt);
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - player.height / 2;
var cars = [];
var carCounter = 0; // Initialize car counter
// Initialize 1 to 3 cars on the screen at a time
var initialCarCount = Math.floor(Math.random() * 3) + 1;
for (var i = 0; i < initialCarCount; i++) {
var car = game.addChild(new Car());
car.x = Math.random() * 2048;
car.y = Math.random() * 2732;
cars.push(car);
}
// Game update function
game.update = function () {
// Update player and cars
player.update();
for (var i = 0; i < cars.length; i++) {
if (carCounter >= 10 && carCounter < 20) {
cars[i].speed = 3.125 * 1.2; // Increase speed by 20%
} else if (carCounter >= 20) {
cars[i].speed = 3.125 * 1.44; // Increase speed by another 20% (total 44%)
}
cars[i].update();
}
// Check for collisions
for (var i = 0; i < cars.length; i++) {
if (player.intersects(cars[i])) {
LK.showGameOver();
break;
}
}
// Check for off-screen cars
for (var i = cars.length - 1; i >= 0; i--) {
if (cars[i].y > 2732) {
cars[i].destroy();
cars.splice(i, 1);
if (cars.length < 3) {
var newCar = game.addChild(new Car());
newCar.x = Math.random() * 2048;
newCar.y = 0;
cars.push(newCar);
carCounter++; // Increment car counter
}
}
}
carCounterTxt.setText('Cars: ' + carCounter);
if (carCounter >= 30) {
LK.showGameOver();
var congratsTxt = new Text2('Congratulations! You are the champion', {
size: 150,
fill: "#ffffff"
});
congratsTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(congratsTxt);
}
};
// LK event listeners for controlling the player car
game.down = function (x, y, obj) {
if (x < player.x) {
player.speed = -5;
} else if (x > player.x) {
player.speed = 5;
}
// Remove controls for the other cars
};
game.up = function (x, y, obj) {
player.speed = 0;
// Ensure other cars stop moving when touch or mouse is released
};