/****
* Classes
****/
// Define a class for 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();
}
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Define a class for the player's car
var PlayerCar = Container.expand(function () {
var self = Container.call(this);
var carGraphics = self.attachAsset('playerCar', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for the player's car
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var playerCar;
var obstacles = [];
var score = 0;
var scoreTxt;
// Function to handle game updates
game.update = function () {
// Update player car
playerCar.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].speed = 5 + Math.floor(score / 10); // Increase speed based on score
obstacles[i].update();
if (playerCar.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else {
score += 1; // Increase score
scoreTxt.setText('Score: ' + score); // Update score display
}
}
// Spawn new obstacles
if (LK.ticks % Math.max(30, 60 - Math.floor(score / 10)) == 0) {
// Decrease interval based on score
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
};
// Function to handle player car movement
game.move = function (x, y, obj) {
playerCar.x = x;
playerCar.y = y;
};
// Initialize player car
playerCar = new PlayerCar();
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
game.addChild(playerCar);
// Initialize score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt); ===================================================================
--- original.js
+++ change.js
@@ -1,85 +1,90 @@
-/****
+/****
* Classes
-****/
+****/
// Define a class for 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();
- }
- };
+ 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();
+ }
+ };
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Define a class for the player's car
var PlayerCar = Container.expand(function () {
- var self = Container.call(this);
- var carGraphics = self.attachAsset('playerCar', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 10;
- self.update = function () {
- // Update logic for the player's car
- };
+ var self = Container.call(this);
+ var carGraphics = self.attachAsset('playerCar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 10;
+ self.update = function () {
+ // Update logic for the player's car
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Initialize game variables
var playerCar;
var obstacles = [];
var score = 0;
var scoreTxt;
// Function to handle game updates
game.update = function () {
- // Update player car
- playerCar.update();
- // Update obstacles
- for (var i = obstacles.length - 1; i >= 0; i--) {
- obstacles[i].update();
- 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);
- }
+ // Update player car
+ playerCar.update();
+ // Update obstacles
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ obstacles[i].speed = 5 + Math.floor(score / 10); // Increase speed based on score
+ obstacles[i].update();
+ if (playerCar.intersects(obstacles[i])) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ } else {
+ score += 1; // Increase score
+ scoreTxt.setText('Score: ' + score); // Update score display
+ }
+ }
+ // Spawn new obstacles
+ if (LK.ticks % Math.max(30, 60 - Math.floor(score / 10)) == 0) {
+ // Decrease interval based on score
+ var newObstacle = new Obstacle();
+ newObstacle.x = Math.random() * 2048;
+ newObstacle.y = -50;
+ obstacles.push(newObstacle);
+ game.addChild(newObstacle);
+ }
};
// Function to handle player car movement
game.move = function (x, y, obj) {
- playerCar.x = x;
- playerCar.y = y;
+ playerCar.x = x;
+ playerCar.y = y;
};
// Initialize player car
playerCar = new PlayerCar();
playerCar.x = 2048 / 2;
playerCar.y = 2732 - 200;
game.addChild(playerCar);
// Initialize score text
scoreTxt = new Text2('Score: 0', {
- size: 100,
- fill: "#ffffff"
+ size: 100,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
\ No newline at end of file