/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class
var Car = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('car', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Car movement logic
if (self.y > 2732) {
self.y = -carGraphics.height;
}
self.y += self.speed;
};
});
// Obstacle class
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 () {
// Obstacle movement logic
if (self.y > 2732) {
self.y = -obstacleGraphics.height;
self.x = Math.random() * 2048;
}
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 = game.addChild(new Car());
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
// Initialize obstacles
var obstacles = [];
for (var i = 0; i < 5; i++) {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * 2732;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Handle touch events for car movement
game.down = function (x, y, obj) {
playerCar.x = x;
playerCar.y = y;
};
game.move = function (x, y, obj) {
playerCar.x = x;
playerCar.y = y;
};
game.up = function (x, y, obj) {
// No action needed on touch up
};
// Update game state
game.update = function () {
playerCar.update();
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
if (playerCar.intersects(obstacles[i])) {
// Flash screen red for 1 second (1000ms) to show collision
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
}
};