User prompt
Don't add random colors make the it look better ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Better graphics
User prompt
When game over the text overlaps fix it
User prompt
Make the coins spawn on the ground and make the obstacles come faster after 100m
User prompt
Make the obstacles be on the ground
User prompt
Make the obstacles y level same as the player's y level
User prompt
Make the obstacles come to towards the player ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Make player on the ground
Code edit (1 edits merged)
Please save this source code
User prompt
Endless Dash
Initial prompt
Make a endless runner game
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = coinGraphics.width;
self.height = coinGraphics.height;
self.collected = false;
self.setup = function (lane) {
self.y = lanePositions[lane];
self.lane = lane;
self.speed = gameSpeed;
// Add spinning animation for coin
tween(coinGraphics, {
rotation: Math.PI * 2
}, {
duration: 1500,
easing: tween.linear,
onFinish: function onFinish() {
coinGraphics.rotation = 0;
if (self.parent) {
// Only restart if still in scene
self.setup(lane);
}
}
});
};
self.collect = function () {
if (self.collected) return;
self.collected = true;
LK.getSound('collect').play();
// Scale up and fade out effect
tween(self, {
scaleX: 1.5,
scaleY: 1.5,
alpha: 0
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.destroy();
}
});
};
self.update = function () {
if (self.collected) return true;
self.x -= self.speed;
// Check if coin is off screen
if (self.x < -self.width) {
self.destroy();
return true; // Return true if destroyed
}
return false; // Not destroyed
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var type = "high"; // Default type
var graphicsAsset;
self.setup = function (obstacleType, lane) {
type = obstacleType;
if (self.children.length > 0) {
self.removeChildren();
}
if (type === "high") {
graphicsAsset = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (type === "low") {
graphicsAsset = self.attachAsset('lowObstacle', {
anchorX: 0.5,
anchorY: 0.5
});
}
self.width = graphicsAsset.width;
self.height = graphicsAsset.height;
self.y = lanePositions[lane];
self.lane = lane;
self.type = type;
self.speed = gameSpeed;
};
self.update = function () {
self.x -= self.speed;
// Check if obstacle is off screen
if (self.x < -self.width) {
self.destroy();
return true; // Return true if destroyed
}
return false; // Not destroyed
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = playerGraphics.width;
self.height = playerGraphics.height;
self.lane = 1; // 0: top, 1: middle, 2: bottom
self.isJumping = false;
self.isSliding = false;
self.isDead = false;
self.jump = function () {
if (self.isJumping || self.isDead) return;
self.isJumping = true;
LK.getSound('jump').play();
var startY = self.y;
var jumpHeight = 250;
tween(self, {
y: startY - jumpHeight
}, {
duration: 350,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
y: startY
}, {
duration: 350,
easing: tween.easeIn,
onFinish: function onFinish() {
self.isJumping = false;
}
});
}
});
};
self.slide = function () {
if (self.isSliding || self.isDead) return;
self.isSliding = true;
var originalHeight = self.height;
tween(playerGraphics, {
scaleY: 0.5
}, {
duration: 200,
easing: tween.linear,
onFinish: function onFinish() {
LK.setTimeout(function () {
tween(playerGraphics, {
scaleY: 1
}, {
duration: 200,
easing: tween.linear,
onFinish: function onFinish() {
self.isSliding = false;
}
});
}, 400);
}
});
};
self.changeLane = function (direction) {
if (self.isDead) return;
// Direction: -1 for up, 1 for down
var newLane = self.lane + direction;
if (newLane < 0 || newLane > 2) return;
self.lane = newLane;
var targetY = lanePositions[self.lane];
tween(self, {
y: targetY
}, {
duration: 200,
easing: tween.easeOut
});
};
self.die = function () {
if (self.isDead) return;
self.isDead = true;
LK.getSound('hit').play();
LK.effects.flashObject(self, 0xff0000, 500);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game constants
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var lanePositions = [GAME_HEIGHT * 0.35, GAME_HEIGHT * 0.5, GAME_HEIGHT * 0.65];
var GROUND_Y = GAME_HEIGHT * 0.8;
var gameSpeed = 10;
var gameSpeedIncrement = 0.1;
var obstacleFrequency = 100; // Frames between obstacle generation
var coinFrequency = 150; // Frames between coin generation
var score = 0;
var distance = 0;
var isGameRunning = false;
// Game elements
var player;
var obstacles = [];
var coins = [];
var ground;
var ceiling;
var lastObstacleFrame = 0;
var lastCoinFrame = 0;
// GUI elements
var scoreTxt;
var distanceTxt;
var highScoreTxt;
// Game setup
function setupGame() {
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: GROUND_Y
}));
// Create ceiling
ceiling = game.addChild(LK.getAsset('ceiling', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
}));
// Create player
player = new Player();
player.x = GAME_WIDTH * 0.2;
player.y = lanePositions[1]; // Start in middle lane
game.addChild(player);
// Setup score text
scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 150;
scoreTxt.y = 100;
LK.gui.addChild(scoreTxt);
// Setup distance text
distanceTxt = new Text2('Distance: 0m', {
size: 80,
fill: 0xFFFFFF
});
distanceTxt.anchor.set(0, 0);
distanceTxt.x = 150;
distanceTxt.y = 200;
LK.gui.addChild(distanceTxt);
// Setup high score text
highScoreTxt = new Text2('High Score: ' + storage.highScore, {
size: 80,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(1, 0);
highScoreTxt.x = GAME_WIDTH - 150;
highScoreTxt.y = 100;
LK.gui.addChild(highScoreTxt);
// Reset game state
score = 0;
distance = 0;
gameSpeed = 10;
isGameRunning = true;
// Start background music
LK.playMusic('gameMusic');
}
// Create a new obstacle
function createObstacle() {
var obstacle = new Obstacle();
var lane = Math.floor(Math.random() * 3); // Random lane
var type = Math.random() > 0.5 ? "high" : "low"; // Random type
obstacle.setup(type, lane);
obstacle.x = GAME_WIDTH + obstacle.width;
game.addChild(obstacle);
obstacles.push(obstacle);
lastObstacleFrame = LK.ticks;
}
// Create a new coin
function createCoin() {
var coin = new Coin();
var lane = Math.floor(Math.random() * 3); // Random lane
coin.setup(lane);
coin.x = GAME_WIDTH + coin.width;
game.addChild(coin);
coins.push(coin);
lastCoinFrame = LK.ticks;
}
// Check for collisions
function checkCollisions() {
// Check obstacle collisions
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
if (player.intersects(obstacle)) {
// Check if player can pass:
// - If jumping over high obstacle
// - If sliding under low obstacle
var canPass = false;
if (obstacle.type === "high" && player.isJumping) {
canPass = true;
} else if (obstacle.type === "low" && player.isSliding) {
canPass = true;
} else if (player.lane !== obstacle.lane) {
canPass = true;
}
if (!canPass) {
player.die();
isGameRunning = false;
// Update high score
if (score > storage.highScore) {
storage.highScore = score;
highScoreTxt.setText('High Score: ' + storage.highScore);
}
}
}
}
// Check coin collisions
for (var j = 0; j < coins.length; j++) {
var coin = coins[j];
if (!coin.collected && player.intersects(coin)) {
coin.collect();
score += 10;
scoreTxt.setText('Score: ' + score);
}
}
}
// Update game function
function updateGame() {
if (!isGameRunning) return;
// Update distance and increase difficulty
distance += Math.floor(gameSpeed / 10);
distanceTxt.setText('Distance: ' + distance + 'm');
// Increase game speed
gameSpeed += gameSpeedIncrement * 0.01;
// Generate obstacles
if (LK.ticks - lastObstacleFrame > obstacleFrequency) {
createObstacle();
// Obstacles appear more frequently as the game progresses
obstacleFrequency = Math.max(50, 100 - Math.floor(distance / 500));
}
// Generate coins
if (LK.ticks - lastCoinFrame > coinFrequency) {
createCoin();
}
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].update()) {
obstacles.splice(i, 1);
}
}
// Update coins
for (var j = coins.length - 1; j >= 0; j--) {
if (coins[j].update()) {
coins.splice(j, 1);
}
}
// Check for collisions
checkCollisions();
}
// Handle swipe gestures
var startX = 0;
var startY = 0;
var isDragging = false;
var swipeThreshold = 50; // Minimum distance for a swipe
function handleDown(x, y, obj) {
startX = x;
startY = y;
isDragging = true;
}
function handleMove(x, y, obj) {
if (!isDragging || !isGameRunning) return;
var deltaX = x - startX;
var deltaY = y - startY;
var absX = Math.abs(deltaX);
var absY = Math.abs(deltaY);
// Only process significant movements
if (absX < swipeThreshold && absY < swipeThreshold) return;
// Determine swipe direction
if (absY > absX) {
// Vertical swipe
if (deltaY < 0) {
// Swipe up - jump
player.jump();
} else {
// Swipe down - slide
player.slide();
}
} else {
// Horizontal swipe
if (deltaY < -swipeThreshold) {
// Swipe up left/right - change lane up
player.changeLane(-1);
} else if (deltaY > swipeThreshold) {
// Swipe down left/right - change lane down
player.changeLane(1);
}
}
// Reset for next swipe
startX = x;
startY = y;
}
function handleUp(x, y, obj) {
isDragging = false;
}
// Set up event handlers
game.down = handleDown;
game.move = handleMove;
game.up = handleUp;
// Game update function
game.update = function () {
// Initialize game on first update
if (!player) {
setupGame();
}
// Update game state
updateGame();
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,432 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1", {
+ highScore: 0
+});
+
+/****
+* Classes
+****/
+var Coin = Container.expand(function () {
+ var self = Container.call(this);
+ var coinGraphics = self.attachAsset('coin', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.width = coinGraphics.width;
+ self.height = coinGraphics.height;
+ self.collected = false;
+ self.setup = function (lane) {
+ self.y = lanePositions[lane];
+ self.lane = lane;
+ self.speed = gameSpeed;
+ // Add spinning animation for coin
+ tween(coinGraphics, {
+ rotation: Math.PI * 2
+ }, {
+ duration: 1500,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ coinGraphics.rotation = 0;
+ if (self.parent) {
+ // Only restart if still in scene
+ self.setup(lane);
+ }
+ }
+ });
+ };
+ self.collect = function () {
+ if (self.collected) return;
+ self.collected = true;
+ LK.getSound('collect').play();
+ // Scale up and fade out effect
+ tween(self, {
+ scaleX: 1.5,
+ scaleY: 1.5,
+ alpha: 0
+ }, {
+ duration: 300,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ self.destroy();
+ }
+ });
+ };
+ self.update = function () {
+ if (self.collected) return true;
+ self.x -= self.speed;
+ // Check if coin is off screen
+ if (self.x < -self.width) {
+ self.destroy();
+ return true; // Return true if destroyed
+ }
+ return false; // Not destroyed
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var type = "high"; // Default type
+ var graphicsAsset;
+ self.setup = function (obstacleType, lane) {
+ type = obstacleType;
+ if (self.children.length > 0) {
+ self.removeChildren();
+ }
+ if (type === "high") {
+ graphicsAsset = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ } else if (type === "low") {
+ graphicsAsset = self.attachAsset('lowObstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ }
+ self.width = graphicsAsset.width;
+ self.height = graphicsAsset.height;
+ self.y = lanePositions[lane];
+ self.lane = lane;
+ self.type = type;
+ self.speed = gameSpeed;
+ };
+ self.update = function () {
+ self.x -= self.speed;
+ // Check if obstacle is off screen
+ if (self.x < -self.width) {
+ self.destroy();
+ return true; // Return true if destroyed
+ }
+ return false; // Not destroyed
+ };
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.width = playerGraphics.width;
+ self.height = playerGraphics.height;
+ self.lane = 1; // 0: top, 1: middle, 2: bottom
+ self.isJumping = false;
+ self.isSliding = false;
+ self.isDead = false;
+ self.jump = function () {
+ if (self.isJumping || self.isDead) return;
+ self.isJumping = true;
+ LK.getSound('jump').play();
+ var startY = self.y;
+ var jumpHeight = 250;
+ tween(self, {
+ y: startY - jumpHeight
+ }, {
+ duration: 350,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ tween(self, {
+ y: startY
+ }, {
+ duration: 350,
+ easing: tween.easeIn,
+ onFinish: function onFinish() {
+ self.isJumping = false;
+ }
+ });
+ }
+ });
+ };
+ self.slide = function () {
+ if (self.isSliding || self.isDead) return;
+ self.isSliding = true;
+ var originalHeight = self.height;
+ tween(playerGraphics, {
+ scaleY: 0.5
+ }, {
+ duration: 200,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ LK.setTimeout(function () {
+ tween(playerGraphics, {
+ scaleY: 1
+ }, {
+ duration: 200,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ self.isSliding = false;
+ }
+ });
+ }, 400);
+ }
+ });
+ };
+ self.changeLane = function (direction) {
+ if (self.isDead) return;
+ // Direction: -1 for up, 1 for down
+ var newLane = self.lane + direction;
+ if (newLane < 0 || newLane > 2) return;
+ self.lane = newLane;
+ var targetY = lanePositions[self.lane];
+ tween(self, {
+ y: targetY
+ }, {
+ duration: 200,
+ easing: tween.easeOut
+ });
+ };
+ self.die = function () {
+ if (self.isDead) return;
+ self.isDead = true;
+ LK.getSound('hit').play();
+ LK.effects.flashObject(self, 0xff0000, 500);
+ LK.setTimeout(function () {
+ LK.showGameOver();
+ }, 1000);
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game constants
+var GAME_WIDTH = 2048;
+var GAME_HEIGHT = 2732;
+var lanePositions = [GAME_HEIGHT * 0.35, GAME_HEIGHT * 0.5, GAME_HEIGHT * 0.65];
+var GROUND_Y = GAME_HEIGHT * 0.8;
+var gameSpeed = 10;
+var gameSpeedIncrement = 0.1;
+var obstacleFrequency = 100; // Frames between obstacle generation
+var coinFrequency = 150; // Frames between coin generation
+var score = 0;
+var distance = 0;
+var isGameRunning = false;
+// Game elements
+var player;
+var obstacles = [];
+var coins = [];
+var ground;
+var ceiling;
+var lastObstacleFrame = 0;
+var lastCoinFrame = 0;
+// GUI elements
+var scoreTxt;
+var distanceTxt;
+var highScoreTxt;
+// Game setup
+function setupGame() {
+ // Create ground
+ ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: GROUND_Y
+ }));
+ // Create ceiling
+ ceiling = game.addChild(LK.getAsset('ceiling', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 0
+ }));
+ // Create player
+ player = new Player();
+ player.x = GAME_WIDTH * 0.2;
+ player.y = lanePositions[1]; // Start in middle lane
+ game.addChild(player);
+ // Setup score text
+ scoreTxt = new Text2('Score: 0', {
+ size: 80,
+ fill: 0xFFFFFF
+ });
+ scoreTxt.anchor.set(0, 0);
+ scoreTxt.x = 150;
+ scoreTxt.y = 100;
+ LK.gui.addChild(scoreTxt);
+ // Setup distance text
+ distanceTxt = new Text2('Distance: 0m', {
+ size: 80,
+ fill: 0xFFFFFF
+ });
+ distanceTxt.anchor.set(0, 0);
+ distanceTxt.x = 150;
+ distanceTxt.y = 200;
+ LK.gui.addChild(distanceTxt);
+ // Setup high score text
+ highScoreTxt = new Text2('High Score: ' + storage.highScore, {
+ size: 80,
+ fill: 0xFFFFFF
+ });
+ highScoreTxt.anchor.set(1, 0);
+ highScoreTxt.x = GAME_WIDTH - 150;
+ highScoreTxt.y = 100;
+ LK.gui.addChild(highScoreTxt);
+ // Reset game state
+ score = 0;
+ distance = 0;
+ gameSpeed = 10;
+ isGameRunning = true;
+ // Start background music
+ LK.playMusic('gameMusic');
+}
+// Create a new obstacle
+function createObstacle() {
+ var obstacle = new Obstacle();
+ var lane = Math.floor(Math.random() * 3); // Random lane
+ var type = Math.random() > 0.5 ? "high" : "low"; // Random type
+ obstacle.setup(type, lane);
+ obstacle.x = GAME_WIDTH + obstacle.width;
+ game.addChild(obstacle);
+ obstacles.push(obstacle);
+ lastObstacleFrame = LK.ticks;
+}
+// Create a new coin
+function createCoin() {
+ var coin = new Coin();
+ var lane = Math.floor(Math.random() * 3); // Random lane
+ coin.setup(lane);
+ coin.x = GAME_WIDTH + coin.width;
+ game.addChild(coin);
+ coins.push(coin);
+ lastCoinFrame = LK.ticks;
+}
+// Check for collisions
+function checkCollisions() {
+ // Check obstacle collisions
+ for (var i = 0; i < obstacles.length; i++) {
+ var obstacle = obstacles[i];
+ if (player.intersects(obstacle)) {
+ // Check if player can pass:
+ // - If jumping over high obstacle
+ // - If sliding under low obstacle
+ var canPass = false;
+ if (obstacle.type === "high" && player.isJumping) {
+ canPass = true;
+ } else if (obstacle.type === "low" && player.isSliding) {
+ canPass = true;
+ } else if (player.lane !== obstacle.lane) {
+ canPass = true;
+ }
+ if (!canPass) {
+ player.die();
+ isGameRunning = false;
+ // Update high score
+ if (score > storage.highScore) {
+ storage.highScore = score;
+ highScoreTxt.setText('High Score: ' + storage.highScore);
+ }
+ }
+ }
+ }
+ // Check coin collisions
+ for (var j = 0; j < coins.length; j++) {
+ var coin = coins[j];
+ if (!coin.collected && player.intersects(coin)) {
+ coin.collect();
+ score += 10;
+ scoreTxt.setText('Score: ' + score);
+ }
+ }
+}
+// Update game function
+function updateGame() {
+ if (!isGameRunning) return;
+ // Update distance and increase difficulty
+ distance += Math.floor(gameSpeed / 10);
+ distanceTxt.setText('Distance: ' + distance + 'm');
+ // Increase game speed
+ gameSpeed += gameSpeedIncrement * 0.01;
+ // Generate obstacles
+ if (LK.ticks - lastObstacleFrame > obstacleFrequency) {
+ createObstacle();
+ // Obstacles appear more frequently as the game progresses
+ obstacleFrequency = Math.max(50, 100 - Math.floor(distance / 500));
+ }
+ // Generate coins
+ if (LK.ticks - lastCoinFrame > coinFrequency) {
+ createCoin();
+ }
+ // Update obstacles
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ if (obstacles[i].update()) {
+ obstacles.splice(i, 1);
+ }
+ }
+ // Update coins
+ for (var j = coins.length - 1; j >= 0; j--) {
+ if (coins[j].update()) {
+ coins.splice(j, 1);
+ }
+ }
+ // Check for collisions
+ checkCollisions();
+}
+// Handle swipe gestures
+var startX = 0;
+var startY = 0;
+var isDragging = false;
+var swipeThreshold = 50; // Minimum distance for a swipe
+function handleDown(x, y, obj) {
+ startX = x;
+ startY = y;
+ isDragging = true;
+}
+function handleMove(x, y, obj) {
+ if (!isDragging || !isGameRunning) return;
+ var deltaX = x - startX;
+ var deltaY = y - startY;
+ var absX = Math.abs(deltaX);
+ var absY = Math.abs(deltaY);
+ // Only process significant movements
+ if (absX < swipeThreshold && absY < swipeThreshold) return;
+ // Determine swipe direction
+ if (absY > absX) {
+ // Vertical swipe
+ if (deltaY < 0) {
+ // Swipe up - jump
+ player.jump();
+ } else {
+ // Swipe down - slide
+ player.slide();
+ }
+ } else {
+ // Horizontal swipe
+ if (deltaY < -swipeThreshold) {
+ // Swipe up left/right - change lane up
+ player.changeLane(-1);
+ } else if (deltaY > swipeThreshold) {
+ // Swipe down left/right - change lane down
+ player.changeLane(1);
+ }
+ }
+ // Reset for next swipe
+ startX = x;
+ startY = y;
+}
+function handleUp(x, y, obj) {
+ isDragging = false;
+}
+// Set up event handlers
+game.down = handleDown;
+game.move = handleMove;
+game.up = handleUp;
+// Game update function
+game.update = function () {
+ // Initialize game on first update
+ if (!player) {
+ setupGame();
+ }
+ // Update game state
+ updateGame();
+};
\ No newline at end of file