/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Dinosaur = Container.expand(function () { var self = Container.call(this); var dinoGraphics = self.attachAsset('dino', { anchorX: 0.5, anchorY: 1.0 }); self.isJumping = false; self.jumpPower = 0; self.gravity = 1.2; self.groundY = 0; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.jumpPower = -20; LK.getSound('jump').play(); } }; self.update = function () { if (self.isJumping) { self.jumpPower += self.gravity; self.y += self.jumpPower; if (self.y >= self.groundY) { self.y = self.groundY; self.isJumping = false; self.jumpPower = 0; } } }; return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var cactusGraphics = self.attachAsset('cactus', { anchorX: 0.5, anchorY: 1.0 }); self.speed = gameSpeed; self.update = function () { self.x -= self.speed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var gameSpeed = 8; var obstacles = []; var spawnTimer = 0; var spawnInterval = 90; var speedIncreaseTimer = 0; var distanceScore = 0; // Create ground var ground = game.addChild(LK.getAsset('ground', { anchorX: 0, anchorY: 0, x: 0, y: 2600 })); // Create dinosaur var dinosaur = game.addChild(new Dinosaur()); dinosaur.x = 300; dinosaur.y = 2600; dinosaur.groundY = 2600; // Create score display var scoreTxt = new Text2('0', { size: 80, fill: 0x000000 }); scoreTxt.anchor.set(0, 0); LK.gui.topLeft.addChild(scoreTxt); scoreTxt.x = 120; scoreTxt.y = 50; // Touch controls game.down = function (x, y, obj) { dinosaur.jump(); }; // Main game update game.update = function () { // Update distance score distanceScore += 1; LK.setScore(Math.floor(distanceScore / 10)); scoreTxt.setText('Score: ' + LK.getScore()); // Increase game speed over time speedIncreaseTimer++; if (speedIncreaseTimer >= 300) { // Every 5 seconds gameSpeed += 0.5; speedIncreaseTimer = 0; } // Spawn obstacles spawnTimer++; if (spawnTimer >= spawnInterval) { var obstacle = new Obstacle(); obstacle.x = 2200; obstacle.y = 2600; obstacle.speed = gameSpeed; obstacles.push(obstacle); game.addChild(obstacle); spawnTimer = 0; // Randomize next spawn interval spawnInterval = 60 + Math.random() * 80; } // Update and check obstacles for (var i = obstacles.length - 1; i >= 0; i--) { var obstacle = obstacles[i]; // Track last position for collision detection if (obstacle.lastX === undefined) obstacle.lastX = obstacle.x; if (obstacle.lastColliding === undefined) obstacle.lastColliding = false; // Remove obstacles that are off screen if (obstacle.lastX >= -50 && obstacle.x < -50) { obstacle.destroy(); obstacles.splice(i, 1); continue; } // Check collision with dinosaur var currentColliding = obstacle.intersects(dinosaur); if (!obstacle.lastColliding && currentColliding) { // Collision detected - game over LK.getSound('hit').play(); LK.effects.flashScreen(0xFF0000, 500); LK.showGameOver(); return; } // Update last known states obstacle.lastX = obstacle.x; obstacle.lastColliding = currentColliding; } };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,144 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Dinosaur = Container.expand(function () {
+ var self = Container.call(this);
+ var dinoGraphics = self.attachAsset('dino', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.isJumping = false;
+ self.jumpPower = 0;
+ self.gravity = 1.2;
+ self.groundY = 0;
+ self.jump = function () {
+ if (!self.isJumping) {
+ self.isJumping = true;
+ self.jumpPower = -20;
+ LK.getSound('jump').play();
+ }
+ };
+ self.update = function () {
+ if (self.isJumping) {
+ self.jumpPower += self.gravity;
+ self.y += self.jumpPower;
+ if (self.y >= self.groundY) {
+ self.y = self.groundY;
+ self.isJumping = false;
+ self.jumpPower = 0;
+ }
+ }
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var cactusGraphics = self.attachAsset('cactus', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.speed = gameSpeed;
+ self.update = function () {
+ self.x -= self.speed;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var gameSpeed = 8;
+var obstacles = [];
+var spawnTimer = 0;
+var spawnInterval = 90;
+var speedIncreaseTimer = 0;
+var distanceScore = 0;
+// Create ground
+var ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 2600
+}));
+// Create dinosaur
+var dinosaur = game.addChild(new Dinosaur());
+dinosaur.x = 300;
+dinosaur.y = 2600;
+dinosaur.groundY = 2600;
+// Create score display
+var scoreTxt = new Text2('0', {
+ size: 80,
+ fill: 0x000000
+});
+scoreTxt.anchor.set(0, 0);
+LK.gui.topLeft.addChild(scoreTxt);
+scoreTxt.x = 120;
+scoreTxt.y = 50;
+// Touch controls
+game.down = function (x, y, obj) {
+ dinosaur.jump();
+};
+// Main game update
+game.update = function () {
+ // Update distance score
+ distanceScore += 1;
+ LK.setScore(Math.floor(distanceScore / 10));
+ scoreTxt.setText('Score: ' + LK.getScore());
+ // Increase game speed over time
+ speedIncreaseTimer++;
+ if (speedIncreaseTimer >= 300) {
+ // Every 5 seconds
+ gameSpeed += 0.5;
+ speedIncreaseTimer = 0;
+ }
+ // Spawn obstacles
+ spawnTimer++;
+ if (spawnTimer >= spawnInterval) {
+ var obstacle = new Obstacle();
+ obstacle.x = 2200;
+ obstacle.y = 2600;
+ obstacle.speed = gameSpeed;
+ obstacles.push(obstacle);
+ game.addChild(obstacle);
+ spawnTimer = 0;
+ // Randomize next spawn interval
+ spawnInterval = 60 + Math.random() * 80;
+ }
+ // Update and check obstacles
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ var obstacle = obstacles[i];
+ // Track last position for collision detection
+ if (obstacle.lastX === undefined) obstacle.lastX = obstacle.x;
+ if (obstacle.lastColliding === undefined) obstacle.lastColliding = false;
+ // Remove obstacles that are off screen
+ if (obstacle.lastX >= -50 && obstacle.x < -50) {
+ obstacle.destroy();
+ obstacles.splice(i, 1);
+ continue;
+ }
+ // Check collision with dinosaur
+ var currentColliding = obstacle.intersects(dinosaur);
+ if (!obstacle.lastColliding && currentColliding) {
+ // Collision detected - game over
+ LK.getSound('hit').play();
+ LK.effects.flashScreen(0xFF0000, 500);
+ LK.showGameOver();
+ return;
+ }
+ // Update last known states
+ obstacle.lastX = obstacle.x;
+ obstacle.lastColliding = currentColliding;
+ }
+};
\ No newline at end of file