User prompt
Make the blocks rendered in a structure shape so the player can jump on and collect minicoins
User prompt
Add a background asset
User prompt
Slow down the jump of the player too
User prompt
Slow down the speed of the player
Code edit (1 edits merged)
Please save this source code
User prompt
Jump Cube Run
Initial prompt
A platformer game that you jump over obstacles like a spike or a block and you play as a cube
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Block obstacle
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGfx = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 1
});
self.width = blockGfx.width;
self.height = blockGfx.height;
self.type = 'block';
self.update = function () {
self.x -= obstacleSpeed;
};
return self;
});
// Player Cube
var Cube = Container.expand(function () {
var self = Container.call(this);
var cubeGfx = self.attachAsset('cube', {
anchorX: 0.5,
anchorY: 1
});
self.width = cubeGfx.width;
self.height = cubeGfx.height;
self.velY = 0;
self.isOnGround = false;
// Jump method
self.jump = function () {
if (self.isOnGround) {
self.velY = -32; // Reduced jump velocity for slower jump
self.isOnGround = false;
}
};
// Update method
self.update = function () {
// Gravity
self.velY += 2.8; // Reduced gravity for slower jump arc
if (self.velY > 60) self.velY = 60;
self.y += self.velY;
// Prevent falling below ground
if (self.y > groundY) {
self.y = groundY;
self.velY = 0;
self.isOnGround = true;
}
};
return self;
});
// Minicoin collectible
var Minicoin = Container.expand(function () {
var self = Container.call(this);
var coinGfx = self.attachAsset('minicoin', {
anchorX: 0.5,
anchorY: 1
});
self.width = coinGfx.width;
self.height = coinGfx.height;
self.type = 'minicoin';
self.collected = false;
self.update = function () {
self.x -= obstacleSpeed;
};
return self;
});
// Spike obstacle
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGfx = self.attachAsset('spike', {
anchorX: 0.5,
anchorY: 1
});
self.width = spikeGfx.width;
self.height = spikeGfx.height;
self.type = 'spike';
self.update = function () {
self.x -= obstacleSpeed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf0f0f0
});
/****
* Game Code
****/
// Block obstacle
// Spike obstacle
// Ground
// Cube player
// Constants
var groundHeight = 80;
var groundY = 2732 - groundHeight;
var startX = 400;
var startY = groundY;
var obstacleSpeed = 12;
var minObstacleGap = 420;
var maxObstacleGap = 700;
var lastObstacleX = 0;
var score = 0;
var gameStarted = false;
// Arrays
var obstacles = [];
// Add background
var background = LK.getAsset('background', {
anchorX: 0,
anchorY: 0
});
background.x = 0;
background.y = 0;
game.addChild(background);
// Add ground
var ground = LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
});
ground.x = 0;
ground.y = groundY;
game.addChild(ground);
// Add player cube
var cube = new Cube();
cube.x = startX;
cube.y = startY;
game.addChild(cube);
// Score text
var scoreTxt = new Text2('0', {
size: 120,
fill: 0x222222
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Helper: spawn obstacle
function spawnObstacle() {
// Randomly choose spike or block structure
var type = Math.random() < 0.6 ? 'spike' : 'block';
if (type === 'spike') {
var obs = new Spike();
obs.x = 2048 + 100;
obs.y = groundY;
obstacles.push(obs);
game.addChild(obs);
lastObstacleX = obs.x;
} else {
// Structure: randomize height and length
var platformHeight = Math.floor(Math.random() * 2) + 1; // 1 or 2 blocks high
var platformLength = Math.floor(Math.random() * 3) + 2; // 2 to 4 blocks long
var baseX = 2048 + 100;
var blockW = 120;
var blockH = 120;
var minicoinsOnTop = Math.random() < 0.8; // 80% chance to spawn coins
for (var i = 0; i < platformLength; ++i) {
for (var j = 0; j < platformHeight; ++j) {
var block = new Block();
block.x = baseX + i * blockW;
block.y = groundY - j * blockH;
obstacles.push(block);
game.addChild(block);
lastObstacleX = block.x;
}
// Place minicoins on top of the structure
if (minicoinsOnTop && platformHeight > 0) {
var coin = new Minicoin();
coin.x = baseX + i * blockW;
coin.y = groundY - platformHeight * blockH - 10; // 10px above top block
obstacles.push(coin);
game.addChild(coin);
}
}
}
}
// Helper: reset game state
function resetGame() {
// Remove obstacles
for (var i = 0; i < obstacles.length; ++i) {
obstacles[i].destroy();
}
obstacles.length = 0;
cube.x = startX;
cube.y = startY;
cube.velY = 0;
cube.isOnGround = true;
lastObstacleX = 1200;
score = 0;
scoreTxt.setText(score);
gameStarted = false;
}
// Start game on first tap
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
return;
}
cube.jump();
};
// Main update loop
game.update = function () {
if (!gameStarted) return;
// Update player
cube.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; --i) {
var obs = obstacles[i];
obs.update();
// Remove if off screen
if (obs.x < -200) {
obs.destroy();
obstacles.splice(i, 1);
continue;
}
// Collision detection
if (obs.type === 'minicoin') {
if (!obs.collected && cube.intersects(obs)) {
obs.collected = true;
score += 5; // Minicoin gives 5 points
scoreTxt.setText(score);
obs.destroy();
obstacles.splice(i, 1);
continue;
}
} else {
if (cube.intersects(obs)) {
// Flash screen and game over
LK.effects.flashScreen(0xff0000, 800);
LK.showGameOver();
return;
}
// Score: passed obstacle
if (!obs.passed && obs.x + obs.width / 2 < cube.x - cube.width / 2) {
obs.passed = true;
score += 1;
scoreTxt.setText(score);
}
}
}
// Spawn new obstacles
if (obstacles.length === 0 || 2048 - lastObstacleX > minObstacleGap + Math.random() * (maxObstacleGap - minObstacleGap)) {
spawnObstacle();
}
};
// Reset game on game over
LK.on('gameover', function () {
resetGame();
});
// Initial state
resetGame(); ===================================================================
--- original.js
+++ change.js
@@ -53,8 +53,24 @@
}
};
return self;
});
+// Minicoin collectible
+var Minicoin = Container.expand(function () {
+ var self = Container.call(this);
+ var coinGfx = self.attachAsset('minicoin', {
+ anchorX: 0.5,
+ anchorY: 1
+ });
+ self.width = coinGfx.width;
+ self.height = coinGfx.height;
+ self.type = 'minicoin';
+ self.collected = false;
+ self.update = function () {
+ self.x -= obstacleSpeed;
+ };
+ return self;
+});
// Spike obstacle
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGfx = self.attachAsset('spike', {
@@ -126,23 +142,44 @@
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Helper: spawn obstacle
function spawnObstacle() {
- // Randomly choose spike or block
+ // Randomly choose spike or block structure
var type = Math.random() < 0.6 ? 'spike' : 'block';
- var obs;
if (type === 'spike') {
- obs = new Spike();
+ var obs = new Spike();
obs.x = 2048 + 100;
obs.y = groundY;
+ obstacles.push(obs);
+ game.addChild(obs);
+ lastObstacleX = obs.x;
} else {
- obs = new Block();
- obs.x = 2048 + 100;
- obs.y = groundY;
+ // Structure: randomize height and length
+ var platformHeight = Math.floor(Math.random() * 2) + 1; // 1 or 2 blocks high
+ var platformLength = Math.floor(Math.random() * 3) + 2; // 2 to 4 blocks long
+ var baseX = 2048 + 100;
+ var blockW = 120;
+ var blockH = 120;
+ var minicoinsOnTop = Math.random() < 0.8; // 80% chance to spawn coins
+ for (var i = 0; i < platformLength; ++i) {
+ for (var j = 0; j < platformHeight; ++j) {
+ var block = new Block();
+ block.x = baseX + i * blockW;
+ block.y = groundY - j * blockH;
+ obstacles.push(block);
+ game.addChild(block);
+ lastObstacleX = block.x;
+ }
+ // Place minicoins on top of the structure
+ if (minicoinsOnTop && platformHeight > 0) {
+ var coin = new Minicoin();
+ coin.x = baseX + i * blockW;
+ coin.y = groundY - platformHeight * blockH - 10; // 10px above top block
+ obstacles.push(coin);
+ game.addChild(coin);
+ }
+ }
}
- obstacles.push(obs);
- game.addChild(obs);
- lastObstacleX = obs.x;
}
// Helper: reset game state
function resetGame() {
// Remove obstacles
@@ -182,20 +219,31 @@
obstacles.splice(i, 1);
continue;
}
// Collision detection
- if (cube.intersects(obs)) {
- // Flash screen and game over
- LK.effects.flashScreen(0xff0000, 800);
- LK.showGameOver();
- return;
+ if (obs.type === 'minicoin') {
+ if (!obs.collected && cube.intersects(obs)) {
+ obs.collected = true;
+ score += 5; // Minicoin gives 5 points
+ scoreTxt.setText(score);
+ obs.destroy();
+ obstacles.splice(i, 1);
+ continue;
+ }
+ } else {
+ if (cube.intersects(obs)) {
+ // Flash screen and game over
+ LK.effects.flashScreen(0xff0000, 800);
+ LK.showGameOver();
+ return;
+ }
+ // Score: passed obstacle
+ if (!obs.passed && obs.x + obs.width / 2 < cube.x - cube.width / 2) {
+ obs.passed = true;
+ score += 1;
+ scoreTxt.setText(score);
+ }
}
- // Score: passed obstacle
- if (!obs.passed && obs.x + obs.width / 2 < cube.x - cube.width / 2) {
- obs.passed = true;
- score += 1;
- scoreTxt.setText(score);
- }
}
// Spawn new obstacles
if (obstacles.length === 0 || 2048 - lastObstacleX > minObstacleGap + Math.random() * (maxObstacleGap - minObstacleGap)) {
spawnObstacle();