/**** * Classes ****/ // Define the Cactus class var Cactus = Container.expand(function () { var self = Container.call(this); var cactusGraphics = self.attachAsset('cactus', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); // Define the Floor class var Floor = Container.expand(function () { var self = Container.call(this); var floorGraphics = self.attachAsset('floor', { anchorX: 0.5, anchorY: 0.5 }); }); // Define the 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 () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Define the Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.jumpSpeed = -30; self.gravity = 1; self.isJumping = false; self.isFalling = false; self.update = function () { if (self.isJumping) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.jumpSpeed >= 0) { self.isJumping = false; self.isFalling = true; } } else if (self.isFalling) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.y >= game.groundLevel) { self.y = game.groundLevel; self.isFalling = false; self.jumpSpeed = -30; } } }; self.jump = function () { if (!self.isJumping && !self.isFalling) { self.isJumping = true; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xEDC9AF // Desert sand background }); /**** * Game Code ****/ // Initialize game variables game.groundLevel = 2500; game.obstacles = []; game.score = 0; // Create floor instance var floor = game.addChild(new Floor()); floor.x = 2048 / 2; floor.y = game.groundLevel + 25; // Adjust y position to align with ground level // Create looped cactus instances function createCactus() { var cactus = new Cactus(); cactus.x = 2048; cactus.y = game.groundLevel - 100; // Adjust y position to align with ground level game.addChild(cactus); return cactus; } var cacti = []; for (var i = 0; i < 5; i++) { var cactus = createCactus(); cactus.x += i * 500; // Space out cacti cacti.push(cactus); } // Create player instance var player = game.addChild(new Player()); player.x = 200; player.y = game.groundLevel; // Create score text var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to handle player jump game.down = function (x, y, obj) { player.jump(); }; // Function to spawn obstacles function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = 2048; obstacle.y = game.groundLevel; game.obstacles.push(obstacle); game.addChild(obstacle); } // Update game state game.update = function () { player.update(); for (var i = game.obstacles.length - 1; i >= 0; i--) { game.obstacles[i].update(); if (player.intersects(game.obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update cacti positions for (var j = cacti.length - 1; j >= 0; j--) { cacti[j].update(); if (cacti[j].x < -cacti[j].width) { cacti[j].x = 2048 + cacti[j].width; // Reset cactus position to the right } } if (LK.ticks % 60 == 0) { spawnObstacle(); game.score++; scoreTxt.setText(game.score); } };
===================================================================
--- original.js
+++ change.js
@@ -94,12 +94,22 @@
// Create floor instance
var floor = game.addChild(new Floor());
floor.x = 2048 / 2;
floor.y = game.groundLevel + 25; // Adjust y position to align with ground level
-// Create cactus instance
-var cactus = game.addChild(new Cactus());
-cactus.x = 1500;
-cactus.y = game.groundLevel - 100; // Adjust y position to align with ground level
+// Create looped cactus instances
+function createCactus() {
+ var cactus = new Cactus();
+ cactus.x = 2048;
+ cactus.y = game.groundLevel - 100; // Adjust y position to align with ground level
+ game.addChild(cactus);
+ return cactus;
+}
+var cacti = [];
+for (var i = 0; i < 5; i++) {
+ var cactus = createCactus();
+ cactus.x += i * 500; // Space out cacti
+ cacti.push(cactus);
+}
// Create player instance
var player = game.addChild(new Player());
player.x = 200;
player.y = game.groundLevel;
@@ -131,8 +141,15 @@
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
+ // Update cacti positions
+ for (var j = cacti.length - 1; j >= 0; j--) {
+ cacti[j].update();
+ if (cacti[j].x < -cacti[j].width) {
+ cacti[j].x = 2048 + cacti[j].width; // Reset cactus position to the right
+ }
+ }
if (LK.ticks % 60 == 0) {
spawnObstacle();
game.score++;
scoreTxt.setText(game.score);