/**** * Classes ****/ // The assets will be automatically created and loaded by the LK engine based on their usage in the code. // For example, car, obstacle, and road assets will be initialized when they are used in the game logic. // Car class to represent 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.lane = 1; // Start in the middle lane self.moveLeft = function () { if (self.lane > 0) { self.lane--; var targetX = self.x - 2048 / 3; var moveInterval = LK.setInterval(function () { if (self.x > targetX) { self.x -= 10; // Adjust speed as needed } else { self.x = targetX; LK.clearInterval(moveInterval); LK.getSound('carMove').play(); } }, 16); // Approximately 60 FPS } }; self.moveRight = function () { if (self.lane < 2) { self.lane++; var targetX = self.x + 2048 / 3; var moveInterval = LK.setInterval(function () { if (self.x < targetX) { self.x += 10; // Adjust speed as needed } else { self.x = targetX; LK.clearInterval(moveInterval); LK.getSound('carMove').play(); } }, 16); // Approximately 60 FPS } }; }); // Obstacle class to represent traffic obstacles 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 a black background to represent a road }); /**** * Game Code ****/ // Create lane indicators var laneWidth = 2048 / 3; for (var i = 1; i < 3; i++) { var laneLine = LK.getAsset('road', { width: 10, height: 2732, color: 0xFFFFFF }); laneLine.x = laneWidth * i; laneLine.y = 0; game.addChild(laneLine); } var car = game.addChild(new Car()); car.x = 2048 / 2; car.y = 2732 - 200; var obstacles = []; var spawnObstacle = function spawnObstacle() { var lane = Math.floor(Math.random() * 3); var obstacle = new Obstacle(); obstacle.x = 2048 / 3 * lane + 2048 / 6; obstacle.y = -100; obstacles.push(obstacle); game.addChild(obstacle); }; var checkCollision = function checkCollision() { for (var i = obstacles.length - 1; i >= 0; i--) { if (car.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.getSound('carCrash').play(); LK.getSound('carCrash').play(); LK.showGameOver(); } } }; game.down = function (x, y, obj) { if (x < 2048 / 2) { car.moveLeft(); } else { car.moveRight(); } }; game.update = function () { if (LK.ticks % 60 == 0) { spawnObstacle(); } for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (obstacles[i].y > 2732) { obstacles.splice(i, 1); } } checkCollision(); };
/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine based on their usage in the code.
// For example, car, obstacle, and road assets will be initialized when they are used in the game logic.
// Car class to represent 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.lane = 1; // Start in the middle lane
self.moveLeft = function () {
if (self.lane > 0) {
self.lane--;
var targetX = self.x - 2048 / 3;
var moveInterval = LK.setInterval(function () {
if (self.x > targetX) {
self.x -= 10; // Adjust speed as needed
} else {
self.x = targetX;
LK.clearInterval(moveInterval);
LK.getSound('carMove').play();
}
}, 16); // Approximately 60 FPS
}
};
self.moveRight = function () {
if (self.lane < 2) {
self.lane++;
var targetX = self.x + 2048 / 3;
var moveInterval = LK.setInterval(function () {
if (self.x < targetX) {
self.x += 10; // Adjust speed as needed
} else {
self.x = targetX;
LK.clearInterval(moveInterval);
LK.getSound('carMove').play();
}
}, 16); // Approximately 60 FPS
}
};
});
// Obstacle class to represent traffic obstacles
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 a black background to represent a road
});
/****
* Game Code
****/
// Create lane indicators
var laneWidth = 2048 / 3;
for (var i = 1; i < 3; i++) {
var laneLine = LK.getAsset('road', {
width: 10,
height: 2732,
color: 0xFFFFFF
});
laneLine.x = laneWidth * i;
laneLine.y = 0;
game.addChild(laneLine);
}
var car = game.addChild(new Car());
car.x = 2048 / 2;
car.y = 2732 - 200;
var obstacles = [];
var spawnObstacle = function spawnObstacle() {
var lane = Math.floor(Math.random() * 3);
var obstacle = new Obstacle();
obstacle.x = 2048 / 3 * lane + 2048 / 6;
obstacle.y = -100;
obstacles.push(obstacle);
game.addChild(obstacle);
};
var checkCollision = function checkCollision() {
for (var i = obstacles.length - 1; i >= 0; i--) {
if (car.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('carCrash').play();
LK.getSound('carCrash').play();
LK.showGameOver();
}
}
};
game.down = function (x, y, obj) {
if (x < 2048 / 2) {
car.moveLeft();
} else {
car.moveRight();
}
};
game.update = function () {
if (LK.ticks % 60 == 0) {
spawnObstacle();
}
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].y > 2732) {
obstacles.splice(i, 1);
}
}
checkCollision();
};