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 = -48; // Jump velocity self.isOnGround = false; } }; // Update method self.update = function () { // Gravity self.velY += 4.2; 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; }); // 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 = 24; var minObstacleGap = 420; var maxObstacleGap = 700; var lastObstacleX = 0; var score = 0; var gameStarted = false; // Arrays var obstacles = []; // 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 var type = Math.random() < 0.6 ? 'spike' : 'block'; var obs; if (type === 'spike') { obs = new Spike(); obs.x = 2048 + 100; obs.y = groundY; } else { obs = new Block(); obs.x = 2048 + 100; obs.y = groundY; } obstacles.push(obs); game.addChild(obs); lastObstacleX = obs.x; } // 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 (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
@@ -1,6 +1,201 @@
-/****
+/****
+* 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 = -48; // Jump velocity
+ self.isOnGround = false;
+ }
+ };
+ // Update method
+ self.update = function () {
+ // Gravity
+ self.velY += 4.2;
+ 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;
+});
+// 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: 0x000000
-});
\ No newline at end of file
+ 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 = 24;
+var minObstacleGap = 420;
+var maxObstacleGap = 700;
+var lastObstacleX = 0;
+var score = 0;
+var gameStarted = false;
+// Arrays
+var obstacles = [];
+// 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
+ var type = Math.random() < 0.6 ? 'spike' : 'block';
+ var obs;
+ if (type === 'spike') {
+ obs = new Spike();
+ obs.x = 2048 + 100;
+ obs.y = groundY;
+ } else {
+ obs = new Block();
+ obs.x = 2048 + 100;
+ obs.y = groundY;
+ }
+ obstacles.push(obs);
+ game.addChild(obs);
+ lastObstacleX = obs.x;
+}
+// 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 (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();
\ No newline at end of file