/**** * 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 Obstacle2 = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle2', { 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 / 20); // Adjust speed increment 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 / 20)) == 0) { // Decrease interval based on score var newObstacle; if (Math.random() > 0.5) { newObstacle = new Obstacle(); } else { newObstacle = new Obstacle2(); } 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
@@ -15,8 +15,22 @@
self.destroy();
}
};
});
+var Obstacle2 = Container.expand(function () {
+ var self = Container.call(this);
+ var obstacleGraphics = self.attachAsset('obstacle2', {
+ 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);
@@ -63,9 +77,14 @@
}
// Spawn new obstacles
if (LK.ticks % Math.max(30, 60 - Math.floor(score / 20)) == 0) {
// Decrease interval based on score
- var newObstacle = new Obstacle();
+ var newObstacle;
+ if (Math.random() > 0.5) {
+ newObstacle = new Obstacle();
+ } else {
+ newObstacle = new Obstacle2();
+ }
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);