/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.update = function () {
self.x -= gameSpeed;
coinGraphics.rotation += 0.1;
coinGraphics.scaleX = 0.8 + Math.sin(LK.ticks * 0.1) * 0.2;
coinGraphics.scaleY = 0.8 + Math.sin(LK.ticks * 0.1) * 0.2;
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
self.update = function () {
self.x -= gameSpeed;
};
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0,
anchorY: 0
});
self.update = function () {
self.x -= gameSpeed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityY = 0;
self.isGrounded = false;
self.jumpPower = -25;
self.gravity = 1.2;
self.groundY = 0;
self.jump = function () {
if (self.isGrounded) {
self.velocityY = self.jumpPower;
self.isGrounded = false;
LK.getSound('jump').play();
}
};
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocityY = 0;
self.isGrounded = true;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var gameSpeed = 8;
var baseSpeed = 8;
var maxSpeed = 20;
var speedIncrement = 0.01;
var distance = 0;
var coins = 0;
var groundLevel = 2400;
var nextPlatformX = 0;
var nextObstacleX = 0;
var nextCoinX = 0;
var platforms = [];
var obstacles = [];
var gameCoins = [];
var player = game.addChild(new Player());
player.x = 300;
player.y = groundLevel;
player.groundY = groundLevel;
var scoreText = new Text2('Distance: 0', {
size: 60,
fill: '#FFFFFF'
});
scoreText.anchor.set(0, 0);
LK.gui.topLeft.addChild(scoreText);
var coinText = new Text2('Coins: 0', {
size: 60,
fill: '#FFD700'
});
coinText.anchor.set(0, 0);
coinText.y = 80;
LK.gui.topLeft.addChild(coinText);
var highScoreText = new Text2('Best: ' + (storage.highScore || 0), {
size: 50,
fill: '#CCCCCC'
});
highScoreText.anchor.set(1, 0);
LK.gui.topRight.addChild(highScoreText);
function generatePlatform() {
var platform = new Platform();
platform.x = nextPlatformX;
// Add some variety to platform heights
platform.y = groundLevel - Math.random() * 100;
platforms.push(platform);
game.addChild(platform);
// Increase spacing variety as speed increases
var spacing = Math.random() * 400 + 150;
// Add more challenging gaps at higher speeds
if (gameSpeed > 15) {
spacing += Math.random() * 200;
}
nextPlatformX += spacing;
}
function generateObstacle() {
// Increase obstacle frequency as speed increases
var obstacleChance = Math.min(0.3 + (gameSpeed - baseSpeed) * 0.02, 0.7);
if (Math.random() < obstacleChance) {
var obstacle = new Obstacle();
obstacle.x = nextObstacleX;
obstacle.y = groundLevel;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Vary spacing based on current speed
var spacing = Math.random() * 350 + 250;
if (gameSpeed > 15) {
spacing = Math.random() * 300 + 200;
}
nextObstacleX += spacing;
}
function generateCoin() {
// Maintain good coin distribution
if (Math.random() < 0.65) {
var coin = new Coin();
coin.x = nextCoinX;
// Place coins at various heights, including some challenging positions
coin.y = groundLevel - Math.random() * 300 - 80;
gameCoins.push(coin);
game.addChild(coin);
}
// Consistent coin spacing for better flow
nextCoinX += Math.random() * 180 + 120;
}
for (var i = 0; i < 20; i++) {
generatePlatform();
}
for (var i = 0; i < 10; i++) {
generateObstacle();
generateCoin();
}
var isJumping = false;
game.down = function (x, y, obj) {
player.jump();
isJumping = true;
};
game.up = function (x, y, obj) {
isJumping = false;
};
game.update = function () {
distance += gameSpeed;
if (gameSpeed < maxSpeed) {
gameSpeed += speedIncrement;
}
if (isJumping && player.velocityY > player.jumpPower * 0.5) {
player.velocityY = player.jumpPower * 0.5;
}
for (var i = platforms.length - 1; i >= 0; i--) {
var platform = platforms[i];
if (platform.x < -platform.width) {
platform.destroy();
platforms.splice(i, 1);
}
}
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
if (obstacle.x < -obstacle.width) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
if (player.intersects(obstacle)) {
var finalScore = Math.floor(distance / 10) + coins * 5;
LK.setScore(finalScore);
if (!storage.highScore || finalScore > storage.highScore) {
storage.highScore = finalScore;
}
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
return;
}
}
for (var i = gameCoins.length - 1; i >= 0; i--) {
var coin = gameCoins[i];
if (coin.x < -coin.width) {
coin.destroy();
gameCoins.splice(i, 1);
continue;
}
if (!coin.collected && player.intersects(coin)) {
coin.collected = true;
coins++;
LK.getSound('coin').play();
tween(coin, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
coin.destroy();
}
});
gameCoins.splice(i, 1);
coinText.setText('Coins: ' + coins);
}
}
if (player.y > 2732) {
var finalScore = Math.floor(distance / 10) + coins * 5;
LK.setScore(finalScore);
if (!storage.highScore || finalScore > storage.highScore) {
storage.highScore = finalScore;
}
LK.showGameOver();
return;
}
// Calculate world position based on distance traveled
var worldPosition = distance;
// Generate platforms ahead of the current world position
while (nextPlatformX < worldPosition + 3000) {
generatePlatform();
}
// Generate obstacles ahead of the current world position
while (nextObstacleX < worldPosition + 3000) {
generateObstacle();
}
// Generate coins ahead of the current world position
while (nextCoinX < worldPosition + 3000) {
generateCoin();
}
scoreText.setText('Distance: ' + Math.floor(distance / 10));
// Camera follows by moving the entire game world backwards
game.x = -worldPosition + 400;
// Keep player at consistent screen position by adjusting for camera movement
player.x = 300 + worldPosition;
};
LK.playMusic('bgmusic'); ===================================================================
--- original.js
+++ change.js
@@ -264,6 +264,8 @@
}
scoreText.setText('Distance: ' + Math.floor(distance / 10));
// Camera follows by moving the entire game world backwards
game.x = -worldPosition + 400;
+ // Keep player at consistent screen position by adjusting for camera movement
+ player.x = 300 + worldPosition;
};
LK.playMusic('bgmusic');
\ No newline at end of file