/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Car class representing 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.speed = 0;
self.maxSpeed = 10;
self.acceleration = 0.2;
self.deceleration = 0.1;
self.update = function () {
self.y -= self.speed;
if (self.speed > 0) {
self.speed -= self.deceleration;
}
};
});
// Coin class representing coins on the track
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move coin downwards
};
});
// Obstacle class representing obstacles on the track
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move obstacle downwards
};
});
// Track class representing the racing track
var Track = Container.expand(function () {
var self = Container.call(this);
var trackGraphics = self.attachAsset('track', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Logic for moving the track to simulate car movement
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Show a starting message
var startMessage = new Text2('Collect all treasures', {
size: 150,
fill: "#ffffff"
});
startMessage.anchor.set(0.5, 0);
startMessage.x = 2048 / 2;
startMessage.y = 2732 / 2;
game.addChild(startMessage);
// Remove the starting message after 3 seconds
LK.setTimeout(function () {
startMessage.destroy();
}, 3000);
// Initialize game elements
var playerCar = game.addChild(new Car());
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
var track = game.addChild(new Track());
track.x = 2048 / 2;
track.y = 2732 / 2;
var obstacles = [];
var coins = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to handle car movement
function handleMove(x, y, obj) {
playerCar.x = x;
}
// Function to update game state
game.update = function () {
playerCar.update();
track.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].y > 2732) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new obstacles
if (LK.ticks % 60 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
var newCoin = new Coin();
newCoin.x = Math.random() * 2048;
newCoin.y = -50;
coins.push(newCoin);
game.addChild(newCoin);
}
// Update coins
for (var i = coins.length - 1; i >= 0; i--) {
coins[i].update();
if (coins[i].y > 2732) {
coins[i].destroy();
coins.splice(i, 1);
}
if (playerCar.intersects(coins[i])) {
coins[i].destroy();
coins.splice(i, 1);
score += 10; // Increase score by 10 when player collects a coin
}
}
// Update score
scoreTxt.setText(score);
// Show a celebration after every 100 points
if (score % 100 == 0 && score > 0) {
var celebrationMessage = new Text2('Hooray! 100 points!', {
size: 150,
fill: "#ffffff"
});
celebrationMessage.anchor.set(0.5, 0);
celebrationMessage.x = 2048 / 2;
celebrationMessage.y = 2732 / 2;
game.addChild(celebrationMessage);
// Remove the celebration message after 3 seconds
LK.setTimeout(function () {
celebrationMessage.destroy();
}, 3000);
}
};
// Event listeners for touch controls
game.down = function (x, y, obj) {
handleMove(x, y, obj);
};
game.move = function (x, y, obj) {
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
// Stop car acceleration
playerCar.speed = 0;
};
A sports car from top view (2d). Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A stone from top view (2d). Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A chest for 2d game. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.