Code edit (1 edits merged)
Please save this source code
User prompt
Add a main menu to the game, have a play button there, and when you press that button, the game starts.
User prompt
Prevent the coin from spinning and if we touch the border, we can move on to the next level.
User prompt
The platform should be at least 750 pixels apart from another platform.
User prompt
If the player is at level 12, there are 17 platforms. If the player is at level 1, there are 5 platforms, so level + 5 = number of platforms.
User prompt
Score and level should be below and on the top right of the game
User prompt
Let the score and level be one below the other
User prompt
When 1 coin is taken, it gives 1 score. The level and score should be in the middle. As the level increases, the platforms in the game length increase by 1
User prompt
reset level
User prompt
Delete the death border asset and code a border asset with unlimited height. Write a code to prevent the player from passing through this border asset, and if the player touches this border, do nothing.
User prompt
add more coins
User prompt
delete spikes
User prompt
Shorten the length of the death border asset and make its height unlimited
User prompt
Please fix the bug: 'ReferenceError: levelLength is not defined' in or related to this line: 'if (player.x >= levelLength + 50) {' Line Number: 404
User prompt
Make the death border asset length unlimited
User prompt
When the player clicks on the screen and is in the air and clicks again, he can jump
User prompt
If the player presses the space bar, the player asset will jump.
User prompt
Leave at least 1000 pixels between 2 platform assets
User prompt
spike should not occur at the start of the game
User prompt
Prevent the player asset from passing through the platform asset. The player must always be above the platform.
User prompt
The game platform must always be at the same height
User prompt
The player cannot pass through the ground
User prompt
There should be no jumping at the beginning of the timer
User prompt
Spikes should be slightly to the side of the ground, not above it.
User prompt
The player cannot pass through the ground, he must stay on it
/****
* 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 () {
if (!self.collected) {
coinGraphics.rotation += 0.1;
}
};
return self;
});
var DeathBorder = Container.expand(function () {
var self = Container.call(this);
var borderGraphics = self.attachAsset('deathBorder', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var FinishLine = Container.expand(function () {
var self = Container.call(this);
var finishGraphics = self.attachAsset('finishLine', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var Lava = Container.expand(function () {
var self = Container.call(this);
var lavaGraphics = self.attachAsset('lava', {
anchorX: 0,
anchorY: 0
});
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
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.jumpForce = -20;
self.gravity = 0.8;
self.groundY = 0;
self.isJumping = false;
self.jumpCount = 0;
self.maxJumps = 2;
self.jump = function () {
if ((self.isGrounded || self.jumpCount < self.maxJumps) && gameStarted) {
self.velocityY = self.jumpForce;
self.isGrounded = false;
self.isJumping = true;
self.jumpCount++;
LK.getSound('jump').play();
}
};
self.strongJump = function () {
if ((self.isGrounded || self.jumpCount < self.maxJumps) && gameStarted) {
self.velocityY = self.jumpForce * 1.5;
self.isGrounded = false;
self.isJumping = true;
self.jumpCount++;
LK.getSound('jump').play();
}
};
self.update = function () {
// Apply gravity
if (!self.isGrounded) {
self.velocityY += self.gravity;
}
// Update position
self.y += self.velocityY;
// Platform collision detection is handled in main game loop
};
return self;
});
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGraphics = self.attachAsset('spike', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Game variables
var player;
var platforms = [];
var spikes = [];
var coins = [];
var finishLine;
var deathBorder;
var cameraOffsetX = 0;
var gameSpeed = 16;
var currentLevel = storage.currentLevel || 1;
var levelComplete = false;
var isJumpPressed = false;
var jumpHoldTime = 0;
var maxJumpHoldTime = 15;
var gameStarted = false;
var countdownActive = false;
var countdownNumber = 3;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 50;
scoreTxt.y = 50;
LK.gui.topLeft.addChild(scoreTxt);
var levelTxt = new Text2('Level: ' + currentLevel, {
size: 60,
fill: 0xFFFFFF
});
levelTxt.anchor.set(1, 0);
levelTxt.x = -50;
levelTxt.y = 50;
LK.gui.topRight.addChild(levelTxt);
var countdownTxt = new Text2('3', {
size: 200,
fill: 0xFFFFFF
});
countdownTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(countdownTxt);
countdownTxt.alpha = 0;
// Initialize player
player = game.addChild(new Player());
player.x = 300;
player.y = 2200;
player.groundY = 2200;
// Level generation function
function generateLevel() {
// Clear existing level elements
for (var i = platforms.length - 1; i >= 0; i--) {
platforms[i].destroy();
}
for (var i = spikes.length - 1; i >= 0; i--) {
spikes[i].destroy();
}
for (var i = coins.length - 1; i >= 0; i--) {
coins[i].destroy();
}
if (finishLine) {
finishLine.destroy();
}
if (deathBorder) {
deathBorder.destroy();
}
platforms = [];
spikes = [];
coins = [];
// Generate floating platforms with gaps
var levelLength = 3000 + currentLevel * 500;
var currentX = 200;
var baseY = 2300;
var allSpikePositions = [];
// Create starting platform
var startPlatform = game.addChild(new Platform());
startPlatform.x = 200;
startPlatform.y = baseY;
platforms.push(startPlatform);
currentX = 700; // Start next platforms after gap
while (currentX < levelLength) {
// Keep all platforms at the same height
var platformY = baseY;
// Create platform
var platform = game.addChild(new Platform());
platform.x = currentX;
platform.y = platformY;
platforms.push(platform);
// Track spikes on this platform
var platformSpikes = [];
// Add spikes to the side of some platforms (reduced spawn rate)
if (currentX > 1500 && Math.random() < 0.2 + currentLevel * 0.05) {
var spike = game.addChild(new Spike());
// Position spike to the side of the platform (left or right)
var sideOffset = Math.random() < 0.5 ? -250 : 250; // 250 pixels to left or right
spike.x = currentX + sideOffset;
spike.y = platformY; // Same level as platform
spikes.push(spike);
platformSpikes.push(spike.x);
allSpikePositions.push(spike.x);
}
// Add coins with minimum distance from spikes
if (Math.random() < 0.6) {
var coinX = currentX + (Math.random() - 0.5) * 150;
var coinY = platformY - 120 - Math.random() * 80;
// Check distance from all spikes
var validPosition = true;
for (var s = 0; s < allSpikePositions.length; s++) {
if (Math.abs(coinX - allSpikePositions[s]) < 250) {
validPosition = false;
break;
}
}
// Only place coin if it's far enough from spikes
if (validPosition) {
var coin = game.addChild(new Coin());
coin.x = coinX;
coin.y = coinY;
coins.push(coin);
}
}
// Create significant gaps between platforms for parkour gameplay
currentX += 1000 + Math.random() * 300;
}
// Add infinite lava background
var lavaY = 2500;
var lavaX = 0;
while (lavaX < levelLength + 2048) {
var lava = game.addChild(new Lava());
lava.x = lavaX;
lava.y = lavaY;
lavaX += 2048; // Move to next lava tile position
}
// Create finish platform
var finishPlatform = game.addChild(new Platform());
finishPlatform.x = levelLength - 200;
finishPlatform.y = baseY;
platforms.push(finishPlatform);
// Add finish line
finishLine = game.addChild(new FinishLine());
finishLine.x = levelLength - 200;
finishLine.y = baseY - 40;
// Add death border at the end of finish line
deathBorder = game.addChild(new DeathBorder());
deathBorder.x = levelLength + 50;
deathBorder.y = baseY - 40;
// Reset player position
player.x = 200;
player.y = baseY - 100;
player.velocityY = 0;
player.isGrounded = true;
player.jumpCount = 0;
player.groundY = baseY;
cameraOffsetX = 0;
levelComplete = false;
startCountdown();
}
// Keyboard input handling for space bar
LK.on('keydown', function (event) {
if (event.code === 'Space' && !isJumpPressed) {
isJumpPressed = true;
jumpHoldTime = 0;
}
});
LK.on('keyup', function (event) {
if (event.code === 'Space' && isJumpPressed) {
if (jumpHoldTime < 5) {
player.jump();
} else {
player.strongJump();
}
isJumpPressed = false;
jumpHoldTime = 0;
}
});
// Input handling
game.down = function (x, y, obj) {
isJumpPressed = true;
jumpHoldTime = 0;
};
game.up = function (x, y, obj) {
if (isJumpPressed) {
if (jumpHoldTime < 5) {
player.jump();
} else {
player.strongJump();
}
isJumpPressed = false;
jumpHoldTime = 0;
}
};
// Game update loop
game.update = function () {
if (levelComplete || !gameStarted || countdownActive) return;
// Handle jump input
if (isJumpPressed) {
jumpHoldTime++;
if (jumpHoldTime >= maxJumpHoldTime) {
player.strongJump();
isJumpPressed = false;
jumpHoldTime = 0;
}
}
// Move player forward automatically
player.x += gameSpeed;
// Update camera
cameraOffsetX = player.x - 400;
game.x = -cameraOffsetX;
// Check platform collision
var onPlatform = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
// Check if player is within horizontal bounds of platform
if (player.x > platform.x - 200 && player.x < platform.x + 200) {
var platformTop = platform.y - 40;
var platformBottom = platform.y + 40;
var playerBottom = player.y;
var playerTop = player.y - 180; // Player height is 180
var previousPlayerBottom = player.y - player.velocityY;
// Check if player is intersecting with platform
if (playerBottom > platformTop && playerTop < platformBottom) {
// Player is overlapping with platform
if (player.velocityY >= 0) {
// Player is falling or stationary, place on top of platform
player.y = platformTop;
player.velocityY = 0;
player.isGrounded = true;
player.isJumping = false;
player.jumpCount = 0;
onPlatform = true;
} else {
// Player is moving up, stop at bottom of platform
player.y = platformBottom + 180;
player.velocityY = 0;
}
break;
}
// Check if player is landing on platform from above
else if (player.velocityY >= 0 && previousPlayerBottom <= platformTop && playerBottom >= platformTop) {
player.y = platformTop;
player.velocityY = 0;
player.isGrounded = true;
player.isJumping = false;
player.jumpCount = 0;
onPlatform = true;
break;
}
}
}
if (!onPlatform) {
player.isGrounded = false;
}
// Check spike collision
for (var i = 0; i < spikes.length; i++) {
var spike = spikes[i];
if (player.intersects(spike)) {
LK.getSound('death').play();
LK.effects.flashScreen(0xff0000, 500);
resetLevel();
return;
}
}
// Check death border collision
if (deathBorder && player.intersects(deathBorder)) {
LK.getSound('death').play();
LK.effects.flashScreen(0xff0000, 500);
resetLevel();
return;
}
// Check coin collection
for (var i = 0; i < coins.length; i++) {
var coin = coins[i];
if (!coin.collected && player.intersects(coin)) {
coin.collected = true;
coin.alpha = 0;
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
LK.getSound('coin').play();
}
}
// Check finish line
if (finishLine && player.intersects(finishLine)) {
levelComplete = true;
currentLevel++;
storage.currentLevel = currentLevel;
levelTxt.setText('Level: ' + currentLevel);
// Flash screen green and generate next level
LK.effects.flashScreen(0x00ff00, 1000);
LK.setTimeout(function () {
generateLevel();
}, 1000);
}
// Check if player fell into lava or fell below ground level
if (player.y > 2500 || player.y > player.groundY + 100) {
LK.getSound('death').play();
LK.effects.flashScreen(0xff0000, 500);
resetLevel();
}
};
function startCountdown() {
countdownActive = true;
gameStarted = false;
countdownNumber = 3;
countdownTxt.setText(countdownNumber.toString());
countdownTxt.alpha = 1;
countdownTxt.scaleX = 1;
countdownTxt.scaleY = 1;
function nextCountdown() {
// Scale up and fade out current number
tween(countdownTxt, {
scaleX: 2,
scaleY: 2,
alpha: 0
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinish() {
countdownNumber--;
if (countdownNumber > 0) {
// Show next number
countdownTxt.setText(countdownNumber.toString());
countdownTxt.alpha = 1;
countdownTxt.scaleX = 1;
countdownTxt.scaleY = 1;
LK.setTimeout(nextCountdown, 200);
} else {
// Show GO! and start game
countdownTxt.setText('GO!');
countdownTxt.alpha = 1;
countdownTxt.scaleX = 1;
countdownTxt.scaleY = 1;
tween(countdownTxt, {
scaleX: 2,
scaleY: 2,
alpha: 0
}, {
duration: 800,
easing: tween.easeOut,
onFinish: function onFinish() {
countdownActive = false;
gameStarted = true;
}
});
}
}
});
}
LK.setTimeout(nextCountdown, 200);
}
function resetLevel() {
LK.showGameOver();
}
// Game reset function (called when game restarts)
function resetGame() {
currentLevel = 1;
storage.currentLevel = currentLevel;
LK.setScore(0);
scoreTxt.setText('Score: 0');
levelTxt.setText('Level: ' + currentLevel);
generateLevel();
}
// Initialize first level
generateLevel(); ===================================================================
--- original.js
+++ change.js
@@ -64,21 +64,25 @@
self.jumpForce = -20;
self.gravity = 0.8;
self.groundY = 0;
self.isJumping = false;
+ self.jumpCount = 0;
+ self.maxJumps = 2;
self.jump = function () {
- if (self.isGrounded && gameStarted) {
+ if ((self.isGrounded || self.jumpCount < self.maxJumps) && gameStarted) {
self.velocityY = self.jumpForce;
self.isGrounded = false;
self.isJumping = true;
+ self.jumpCount++;
LK.getSound('jump').play();
}
};
self.strongJump = function () {
- if (self.isGrounded && gameStarted) {
+ if ((self.isGrounded || self.jumpCount < self.maxJumps) && gameStarted) {
self.velocityY = self.jumpForce * 1.5;
self.isGrounded = false;
self.isJumping = true;
+ self.jumpCount++;
LK.getSound('jump').play();
}
};
self.update = function () {
@@ -259,8 +263,9 @@
player.x = 200;
player.y = baseY - 100;
player.velocityY = 0;
player.isGrounded = true;
+ player.jumpCount = 0;
player.groundY = baseY;
cameraOffsetX = 0;
levelComplete = false;
startCountdown();
@@ -335,8 +340,9 @@
player.y = platformTop;
player.velocityY = 0;
player.isGrounded = true;
player.isJumping = false;
+ player.jumpCount = 0;
onPlatform = true;
} else {
// Player is moving up, stop at bottom of platform
player.y = platformBottom + 180;
@@ -349,8 +355,9 @@
player.y = platformTop;
player.velocityY = 0;
player.isGrounded = true;
player.isJumping = false;
+ player.jumpCount = 0;
onPlatform = true;
break;
}
}