User prompt
Add game over sounds
User prompt
Add background for Avery music background
User prompt
Please fix the bug: 'ground is not defined' in or related to this line: 'character.groundY = ground.y;' Line Number: 155
User prompt
Add big Ground
User prompt
Add text left touch down character
User prompt
Add character down for player's left touch
User prompt
Add down
User prompt
Add unlimited coins
Code edit (1 edits merged)
Please save this source code
User prompt
Endless Runner Dash
Initial prompt
2d running
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 1.0
});
// Character state
self.isJumping = false;
self.isSliding = false;
self.jumpHeight = 600;
self.jumpSpeed = 0;
self.gravity = 40;
self.groundY = 0;
self.jump = function () {
if (!self.isJumping && !self.isSliding) {
self.isJumping = true;
self.jumpSpeed = -25;
LK.getSound('jump').play();
}
};
self.slide = function () {
if (!self.isSliding && !self.isJumping) {
self.isSliding = true;
characterGraphics.scaleY = 0.5;
// Return to normal after 500ms
LK.setTimeout(function () {
self.isSliding = false;
characterGraphics.scaleY = 1;
}, 500);
}
};
self.update = function () {
if (self.isJumping) {
self.y += self.jumpSpeed;
self.jumpSpeed += self.gravity / 60;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.isJumping = false;
self.jumpSpeed = 0;
}
}
};
return self;
});
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.update = function () {
self.x -= self.speed * 0.3; // Clouds move slower than obstacles
};
return self;
});
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.update = function () {
self.x -= self.speed;
// Rotate the coin slightly for animation
coinGraphics.rotation += 0.05;
};
return self;
});
var Obstacle = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'high'; // 'high', 'low', or 'flying'
self.speed = 0;
var obstacleGraphics;
if (self.type === 'high') {
obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
} else if (self.type === 'low') {
obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0,
scaleY: 0.5
});
} else if (self.type === 'flying') {
obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0,
scaleY: 0.8,
y: -250 // Fly a bit above ground
});
}
self.collisionWidth = obstacleGraphics.width * 0.8;
self.collisionHeight = obstacleGraphics.height * 0.9;
self.update = function () {
self.x -= self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Game configuration
var gameSpeed = 10;
var speedIncreaseRate = 0.0005;
var spawnRate = 120; // Frames between obstacle spawns
var coinSpawnChance = 0.4; // 40% chance to spawn a coin with each obstacle
var obstacleTypes = ['high', 'low', 'flying'];
var gameStarted = false;
var gameOver = false;
// Create ground
var ground = LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
});
ground.y = 2732 - ground.height;
game.addChild(ground);
// Create character
var character = new Character();
character.x = 400;
character.groundY = ground.y;
character.y = character.groundY;
game.addChild(character);
// Create arrays for game objects
var obstacles = [];
var coins = [];
var clouds = [];
// Cloud generation for background
function generateInitialClouds() {
for (var i = 0; i < 5; i++) {
var cloud = new Cloud();
cloud.x = Math.random() * 2048;
cloud.y = Math.random() * 500 + 200;
cloud.speed = gameSpeed;
cloud.alpha = 0.7 + Math.random() * 0.3;
cloud.scale.set(0.7 + Math.random() * 0.6);
clouds.push(cloud);
game.addChild(cloud);
}
}
// Create UI elements
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
size: 50,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0, 0);
highScoreTxt.alpha = 0.7;
LK.gui.topLeft.addChild(highScoreTxt);
// Move it to not overlap with the menu icon
highScoreTxt.x = 110;
var instructionsTxt = new Text2('Tap to jump, Swipe down to slide', {
size: 60,
fill: 0xFFFFFF
});
instructionsTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(instructionsTxt);
// Timer to hide instructions
LK.setTimeout(function () {
tween(instructionsTxt, {
alpha: 0
}, {
duration: 1000
});
}, 3000);
// Game state management
function startGame() {
if (!gameStarted) {
gameStarted = true;
LK.setScore(0);
scoreTxt.setText(LK.getScore());
generateInitialClouds();
// Play background music
LK.playMusic('gameBgMusic', {
fade: {
start: 0,
end: 0.3,
duration: 1000
}
});
}
}
function checkCollisions() {
var charBox = {
x: character.x - 80,
y: character.y - (character.isSliding ? 150 : 300),
width: 160,
height: character.isSliding ? 150 : 300
};
// Check obstacle collisions
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var obstacleBox = {
x: obstacle.x - obstacle.collisionWidth / 2,
y: obstacle.type === 'flying' ? ground.y - 250 - obstacle.collisionHeight : ground.y - obstacle.collisionHeight,
width: obstacle.collisionWidth,
height: obstacle.collisionHeight
};
if (charBox.x + charBox.width > obstacleBox.x && charBox.x < obstacleBox.x + obstacleBox.width && charBox.y + charBox.height > obstacleBox.y && charBox.y < obstacleBox.y + obstacleBox.height) {
if (!gameOver) {
LK.getSound('crash').play();
gameOver = true;
// Flash screen red
LK.effects.flashScreen(0xff0000, 500);
// Update high score if needed
if (LK.getScore() > storage.highScore) {
storage.highScore = LK.getScore();
highScoreTxt.setText('High Score: ' + storage.highScore);
}
// Show game over
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
}
}
// Check coin collisions
for (var j = coins.length - 1; j >= 0; j--) {
var coin = coins[j];
var coinBox = {
x: coin.x - 40,
y: coin.y - 40,
width: 80,
height: 80
};
if (charBox.x + charBox.width > coinBox.x && charBox.x < coinBox.x + coinBox.width && charBox.y + charBox.height > coinBox.y && charBox.y < coinBox.y + coinBox.height) {
// Collect coin
LK.getSound('collect').play();
LK.setScore(Infinity); // Set score to infinity for unlimited coins
scoreTxt.setText('∞'); // Display infinity symbol for unlimited coins
// Create sparkle effect
LK.effects.flashObject(coin, 0xFFFFFF, 200);
// Remove coin
coin.destroy();
coins.splice(j, 1);
}
}
}
function spawnObstacle() {
var type = obstacleTypes[Math.floor(Math.random() * obstacleTypes.length)];
var obstacle = new Obstacle(type);
obstacle.x = 2048 + 100;
obstacle.y = ground.y;
obstacle.speed = gameSpeed;
obstacles.push(obstacle);
game.addChild(obstacle);
// Possibly spawn a coin
if (Math.random() < coinSpawnChance) {
var coin = new Coin();
coin.speed = gameSpeed;
// Position the coin appropriately based on obstacle type
coin.x = 2048 + 100 + Math.random() * 200;
if (type === 'high') {
// Place coin above the high obstacle
coin.y = ground.y - 450;
} else if (type === 'low') {
// Place coin above the low obstacle
coin.y = ground.y - 350;
} else if (type === 'flying') {
// Place coin below the flying obstacle
coin.y = ground.y - 100;
}
coins.push(coin);
game.addChild(coin);
}
// Possibly spawn a cloud
if (Math.random() < 0.3) {
var cloud = new Cloud();
cloud.x = 2048 + 100;
cloud.y = Math.random() * 500 + 200;
cloud.speed = gameSpeed;
cloud.alpha = 0.7 + Math.random() * 0.3;
cloud.scale.set(0.7 + Math.random() * 0.6);
clouds.push(cloud);
game.addChild(cloud);
}
}
// Event handlers
game.down = function (x, y, obj) {
startGame();
if (x < 1024) {
// Check if touch is on the left side of the screen
character.slide();
} else {
character.jump();
}
};
// Special handler for slide gesture
var startY = 0;
var isDragging = false;
game.move = function (x, y, obj) {
if (!isDragging) {
startY = y;
isDragging = true;
} else if (y - startY > 200) {
// Swipe down detected
character.slide();
isDragging = false;
}
};
game.up = function (x, y, obj) {
isDragging = false;
};
// Game update function
game.update = function () {
if (!gameStarted || gameOver) {
return;
}
// Increase speed over time
gameSpeed += speedIncreaseRate;
// Update character
character.update();
// Spawn new obstacles
if (LK.ticks % spawnRate === 0) {
spawnObstacle();
}
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].speed = gameSpeed;
obstacles[i].update();
// Remove off-screen obstacles
if (obstacles[i].x < -200) {
obstacles[i].destroy();
obstacles.splice(i, 1);
// Score point for passing obstacle
if (!gameOver) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
}
}
}
// Update coins
for (var j = coins.length - 1; j >= 0; j--) {
coins[j].speed = gameSpeed;
coins[j].update();
// Remove off-screen coins
if (coins[j].x < -200) {
coins[j].destroy();
coins.splice(j, 1);
}
}
// Update clouds
for (var k = clouds.length - 1; k >= 0; k--) {
clouds[k].speed = gameSpeed;
clouds[k].update();
// Remove off-screen clouds
if (clouds[k].x < -400) {
clouds[k].destroy();
clouds.splice(k, 1);
}
}
// Check for collisions
checkCollisions();
// Adjust spawn rate based on speed
spawnRate = Math.max(60, 120 - Math.floor(gameSpeed) * 2);
}; ===================================================================
--- original.js
+++ change.js
@@ -303,10 +303,14 @@
}
// Event handlers
game.down = function (x, y, obj) {
startGame();
- character.jump();
- character.slide(); // Add slide action on down event
+ if (x < 1024) {
+ // Check if touch is on the left side of the screen
+ character.slide();
+ } else {
+ character.jump();
+ }
};
// Special handler for slide gesture
var startY = 0;
var isDragging = false;