/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Car class representing the player's vehicle
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 () {
if (mouseDown) {
// Increase the car's y position up to the center of the screen
if (self.y > 2732 / 2) {
self.y -= self.speed;
}
self.speed = 20;
} else {
// Revert the car's y position back to original when mouse button is not held
if (self.y < 2400) {
self.y += self.speed;
}
self.speed = 10;
}
};
});
// 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.speed = 5;
self.update = function () {
self.y -= self.speed;
if (self.y > 2732) {
self.y = -100; // Reset position to top
}
};
});
// Rival class representing rival cars
var Rival = Container.expand(function () {
var self = Container.call(this);
var rivalGraphics = self.attachAsset('rival', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.hitCount = 0; // Number of hits
self.update = function () {
self.y -= self.speed;
if (self.y > 2732) {
self.y = -100; // Reset position to top
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize two backgrounds
var background1 = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
}));
var background2 = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: -1366
}));
// Initialize player car
var playerCar = game.addChild(new Car());
playerCar.x = 1024; // Center horizontally
playerCar.y = 2400; // Near the bottom
// Initialize score
var score = 0;
// Create a Text2 object to display the score
var scoreText = new Text2('Score: ' + score, {
size: 100,
fill: 0x000000
});
// Set the anchor point to the top right corner
scoreText.anchor.set(1, 0);
// Add the score text to the top right corner of the GUI overlay
LK.gui.topRight.addChild(scoreText);
// 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);
}
// Initialize rivals
var rivals = [];
for (var i = 0; i < 3; i++) {
var rival = new Rival();
rival.x = Math.random() * 2048;
rival.y = Math.random() * 2732;
rivals.push(rival);
game.addChild(rival);
}
// Handle player car movement
var mouseDown = false;
game.move = function (x, y, obj) {
playerCar.x = x;
};
game.down = function (x, y, obj) {
mouseDown = true;
};
game.up = function (x, y, obj) {
mouseDown = false;
};
// Update game logic
game.update = function () {
// Move the backgrounds down
background1.y += 5;
background2.y += 5;
// Loop the backgrounds
if (background1.y > 4519.72) {
background1.y = background2.y - 4519.72;
}
if (background2.y > 4519.72) {
background2.y = background1.y - 4519.72;
}
// Update obstacles
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].speed = 5;
obstacles[i].update();
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
score -= 1; // Decrease score when colliding with an obstacle
scoreText.setText('Score: ' + score); // Update the score text
if (score < 0) {
LK.showGameOver(); // Game over when score is less than 0
}
obstacles[i].destroy(); // Destroy obstacle when player car collides with it
obstacles.splice(i, 1); // Remove obstacle from the array
i--; // Decrement index to account for the removed obstacle
}
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
score -= 1; // Decrease score when colliding with an obstacle
scoreText.setText('Score: ' + score); // Update the score text
if (score < 0) {
LK.showGameOver(); // Game over when score is less than 0
}
obstacles[i].destroy(); // Destroy obstacle when player car collides with it
obstacles.splice(i, 1); // Remove obstacle from the array
i--; // Decrement index to account for the removed obstacle
}
}
// Update rivals
for (var i = 0; i < rivals.length; i++) {
rivals[i].speed = 8;
rivals[i].update();
if (playerCar.intersects(rivals[i])) {
score += 1; // Increase score when colliding with a rival
scoreText.setText('Score: ' + score); // Update the score text
rivals[i].hitCount += 1; // Increase hitCount
if (rivals[i].hitCount == 2) {
rivals[i].destroy(); // Destroy rival when hitCount is 2
rivals.splice(i, 1); // Remove rival from the array
i--; // Decrement index to account for the removed rival
}
}
}
// Create a timer to generate new obstacles and rivals
if (LK.ticks % 60 == 0) {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = 2732;
obstacles.push(obstacle);
game.addChild(obstacle);
var rival = new Rival();
rival.x = Math.random() * 2048;
rival.y = 2732;
rivals.push(rival);
game.addChild(rival);
}
}; /****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Car class representing the player's vehicle
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 () {
if (mouseDown) {
// Increase the car's y position up to the center of the screen
if (self.y > 2732 / 2) {
self.y -= self.speed;
}
self.speed = 20;
} else {
// Revert the car's y position back to original when mouse button is not held
if (self.y < 2400) {
self.y += self.speed;
}
self.speed = 10;
}
};
});
// 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.speed = 5;
self.update = function () {
self.y -= self.speed;
if (self.y > 2732) {
self.y = -100; // Reset position to top
}
};
});
// Rival class representing rival cars
var Rival = Container.expand(function () {
var self = Container.call(this);
var rivalGraphics = self.attachAsset('rival', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.hitCount = 0; // Number of hits
self.update = function () {
self.y -= self.speed;
if (self.y > 2732) {
self.y = -100; // Reset position to top
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize two backgrounds
var background1 = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
}));
var background2 = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: -1366
}));
// Initialize player car
var playerCar = game.addChild(new Car());
playerCar.x = 1024; // Center horizontally
playerCar.y = 2400; // Near the bottom
// Initialize score
var score = 0;
// Create a Text2 object to display the score
var scoreText = new Text2('Score: ' + score, {
size: 100,
fill: 0x000000
});
// Set the anchor point to the top right corner
scoreText.anchor.set(1, 0);
// Add the score text to the top right corner of the GUI overlay
LK.gui.topRight.addChild(scoreText);
// 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);
}
// Initialize rivals
var rivals = [];
for (var i = 0; i < 3; i++) {
var rival = new Rival();
rival.x = Math.random() * 2048;
rival.y = Math.random() * 2732;
rivals.push(rival);
game.addChild(rival);
}
// Handle player car movement
var mouseDown = false;
game.move = function (x, y, obj) {
playerCar.x = x;
};
game.down = function (x, y, obj) {
mouseDown = true;
};
game.up = function (x, y, obj) {
mouseDown = false;
};
// Update game logic
game.update = function () {
// Move the backgrounds down
background1.y += 5;
background2.y += 5;
// Loop the backgrounds
if (background1.y > 4519.72) {
background1.y = background2.y - 4519.72;
}
if (background2.y > 4519.72) {
background2.y = background1.y - 4519.72;
}
// Update obstacles
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].speed = 5;
obstacles[i].update();
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
score -= 1; // Decrease score when colliding with an obstacle
scoreText.setText('Score: ' + score); // Update the score text
if (score < 0) {
LK.showGameOver(); // Game over when score is less than 0
}
obstacles[i].destroy(); // Destroy obstacle when player car collides with it
obstacles.splice(i, 1); // Remove obstacle from the array
i--; // Decrement index to account for the removed obstacle
}
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
score -= 1; // Decrease score when colliding with an obstacle
scoreText.setText('Score: ' + score); // Update the score text
if (score < 0) {
LK.showGameOver(); // Game over when score is less than 0
}
obstacles[i].destroy(); // Destroy obstacle when player car collides with it
obstacles.splice(i, 1); // Remove obstacle from the array
i--; // Decrement index to account for the removed obstacle
}
}
// Update rivals
for (var i = 0; i < rivals.length; i++) {
rivals[i].speed = 8;
rivals[i].update();
if (playerCar.intersects(rivals[i])) {
score += 1; // Increase score when colliding with a rival
scoreText.setText('Score: ' + score); // Update the score text
rivals[i].hitCount += 1; // Increase hitCount
if (rivals[i].hitCount == 2) {
rivals[i].destroy(); // Destroy rival when hitCount is 2
rivals.splice(i, 1); // Remove rival from the array
i--; // Decrement index to account for the removed rival
}
}
}
// Create a timer to generate new obstacles and rivals
if (LK.ticks % 60 == 0) {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = 2732;
obstacles.push(obstacle);
game.addChild(obstacle);
var rival = new Rival();
rival.x = Math.random() * 2048;
rival.y = 2732;
rivals.push(rival);
game.addChild(rival);
}
};