/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Lane = Container.expand(function () {
var self = Container.call(this);
var laneGraphics = self.attachAsset('lane', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += scrollSpeed;
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed + scrollSpeed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.maxSpeed = 8;
self.acceleration = 0.3;
self.deceleration = 0.5;
self.isAccelerating = false;
self.invulnerable = false;
self.invulnerableTime = 0;
self.startAcceleration = function () {
self.isAccelerating = true;
LK.getSound('engine').play();
};
self.stopAcceleration = function () {
self.isAccelerating = false;
LK.getSound('brake').play();
};
self.update = function () {
if (self.isAccelerating) {
self.speed = Math.min(self.speed + self.acceleration, self.maxSpeed);
} else {
self.speed = Math.max(self.speed - self.deceleration, 0);
}
// Handle invulnerability
if (self.invulnerable) {
self.invulnerableTime--;
playerGraphics.alpha = self.invulnerableTime % 10 < 5 ? 0.5 : 1.0;
if (self.invulnerableTime <= 0) {
self.invulnerable = false;
playerGraphics.alpha = 1.0;
}
}
};
self.makeInvulnerable = function (duration) {
self.invulnerable = true;
self.invulnerableTime = duration;
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.rotationSpeed = 0.1;
self.update = function () {
self.y += self.speed + scrollSpeed;
powerupGraphics.rotation += self.rotationSpeed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c5f2d
});
/****
* Game Code
****/
// Game variables
var player;
var obstacles = [];
var powerups = [];
var lanes = [];
var scrollSpeed = 4;
var gameSpeed = 1;
var spawnTimer = 0;
var powerupTimer = 0;
var distance = 0;
var lanePositions = [512, 768, 1024, 1280, 1536]; // 5 lanes
var nextLevelDistance = 1000;
// Background setup
var backgroundImage = game.addChild(LK.getAsset('Background', {
anchorX: 0,
anchorY: 0,
scaleX: 20.48,
scaleY: 27.32
}));
backgroundImage.x = 0;
backgroundImage.y = 0;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 120;
scoreTxt.y = 50;
LK.gui.topLeft.addChild(scoreTxt);
var speedTxt = new Text2('Speed: 0', {
size: 50,
fill: 0xFFFFFF
});
speedTxt.anchor.set(1, 0);
speedTxt.x = -50;
speedTxt.y = 50;
LK.gui.topRight.addChild(speedTxt);
var distanceTxt = new Text2('Distance: 0m', {
size: 50,
fill: 0xFFFFFF
});
distanceTxt.anchor.set(0.5, 0);
distanceTxt.x = 0;
distanceTxt.y = 50;
LK.gui.top.addChild(distanceTxt);
// Initialize player
player = game.addChild(new Player());
player.x = 1024; // Center lane
player.y = 2200;
// Initialize lane markers
function createLaneMarkers() {
for (var i = 0; i < 8; i++) {
var lane = game.addChild(new Lane());
lane.x = 1024;
lane.y = i * 400 - 200;
lanes.push(lane);
}
}
createLaneMarkers();
// Spawn obstacle
function spawnObstacle() {
var obstacle = game.addChild(new Obstacle());
obstacle.x = lanePositions[Math.floor(Math.random() * lanePositions.length)];
obstacle.y = -100;
obstacles.push(obstacle);
}
// Spawn powerup
function spawnPowerUp() {
var powerup = game.addChild(new PowerUp());
powerup.x = lanePositions[Math.floor(Math.random() * lanePositions.length)];
powerup.y = -100;
powerups.push(powerup);
}
// Handle player movement
function handlePlayerMovement(x, y, obj) {
// Calculate which lane the touch is in
var targetLane = 2; // Default to center
if (x < 640) targetLane = 0;else if (x < 896) targetLane = 1;else if (x < 1152) targetLane = 2;else if (x < 1408) targetLane = 3;else targetLane = 4;
// Move player to target lane
tween(player, {
x: lanePositions[targetLane]
}, {
duration: 200,
easing: tween.easeOut
});
}
// Game controls
game.down = function (x, y, obj) {
player.startAcceleration();
handlePlayerMovement(x, y, obj);
};
game.up = function (x, y, obj) {
player.stopAcceleration();
};
game.move = function (x, y, obj) {
if (player.isAccelerating) {
handlePlayerMovement(x, y, obj);
}
};
// Main game loop
game.update = function () {
// Update distance and score
distance += scrollSpeed;
LK.setScore(Math.floor(distance / 10));
// Update UI
scoreTxt.setText('Score: ' + LK.getScore());
speedTxt.setText('Speed: ' + Math.floor(player.speed));
distanceTxt.setText('Distance: ' + Math.floor(distance / 10) + 'm');
// Increase difficulty over time
if (distance > nextLevelDistance) {
gameSpeed += 0.1;
scrollSpeed = Math.min(scrollSpeed + 0.5, 10);
nextLevelDistance += 1000;
}
// Spawn obstacles
spawnTimer++;
if (spawnTimer > Math.max(60 - gameSpeed * 5, 20)) {
spawnObstacle();
spawnTimer = 0;
}
// Spawn powerups
powerupTimer++;
if (powerupTimer > 300) {
spawnPowerUp();
powerupTimer = 0;
}
// Update and check obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
// Track last position for off-screen detection
if (obstacle.lastY === undefined) obstacle.lastY = obstacle.y;
// Remove off-screen obstacles
if (obstacle.lastY < 2800 && obstacle.y >= 2800) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Check collision with player
if (!player.invulnerable && obstacle.intersects(player)) {
LK.getSound('crash').play();
LK.effects.flashScreen(0xff0000, 500);
LK.showGameOver();
return;
}
obstacle.lastY = obstacle.y;
}
// Update and check powerups
for (var j = powerups.length - 1; j >= 0; j--) {
var powerup = powerups[j];
// Track last position for off-screen detection
if (powerup.lastY === undefined) powerup.lastY = powerup.y;
// Remove off-screen powerups
if (powerup.lastY < 2800 && powerup.y >= 2800) {
powerup.destroy();
powerups.splice(j, 1);
continue;
}
// Check collection
if (powerup.intersects(player)) {
LK.getSound('powerup').play();
LK.setScore(LK.getScore() + 50);
player.makeInvulnerable(180); // 3 seconds of invulnerability
powerup.destroy();
powerups.splice(j, 1);
continue;
}
powerup.lastY = powerup.y;
}
// Update and recycle lane markers
for (var k = lanes.length - 1; k >= 0; k--) {
var lane = lanes[k];
// Track last position for recycling
if (lane.lastY === undefined) lane.lastY = lane.y;
// Recycle lane markers that go off screen
if (lane.lastY < 2800 && lane.y >= 2800) {
lane.y = -200;
}
lane.lastY = lane.y;
}
// Win condition - reach 5000m
if (distance >= 50000) {
LK.showYouWin();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -108,8 +108,17 @@
var powerupTimer = 0;
var distance = 0;
var lanePositions = [512, 768, 1024, 1280, 1536]; // 5 lanes
var nextLevelDistance = 1000;
+// Background setup
+var backgroundImage = game.addChild(LK.getAsset('Background', {
+ anchorX: 0,
+ anchorY: 0,
+ scaleX: 20.48,
+ scaleY: 27.32
+}));
+backgroundImage.x = 0;
+backgroundImage.y = 0;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF