User prompt
make sky black
User prompt
please add leader board ↪💡 Consider importing and using the following plugins: @upit/storage.v1
User prompt
make pipes bigger and wider the gaps
User prompt
Please fix the bug: 'TypeError: tween.to is not a function' in or related to this line: 'tween.to(self, 0.3, {' Line Number: 44 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
put coins between pipes gaps
Code edit (1 edits merged)
Please save this source code
User prompt
Flappy Rocket
Initial prompt
make me game a like flappy bird
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); // Create coin visual var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.collected = false; self.rotation = 0; // Animate the coin self.update = function () { self.x -= self.speed; self.rotation += 0.05; coinGraphics.rotation = self.rotation; }; // Flash and mark as collected self.collect = function () { if (!self.collected) { self.collected = true; LK.effects.flashObject(self, 0xffff00, 300); // Fade out and shrink tween.to(self, 0.3, { alpha: 0, scaleX: 0.1, scaleY: 0.1, onComplete: function onComplete() { self.visible = false; } }); } }; return self; }); var Pipe = Container.expand(function () { var self = Container.call(this); // Each pipe consists of top and bottom segments var topPipe = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 1.0 }); var bottomPipe = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.0 }); self.speed = 5; self.gapHeight = 300; self.scored = false; self.setGapPosition = function (gapY) { topPipe.y = gapY - self.gapHeight / 2; bottomPipe.y = gapY + self.gapHeight / 2; }; self.update = function () { self.x -= self.speed; }; self.getTopPipe = function () { return topPipe; }; self.getBottomPipe = function () { return bottomPipe; }; return self; }); var Rocket = Container.expand(function () { var self = Container.call(this); var rocketGraphics = self.attachAsset('rocket', { anchorX: 0.5, anchorY: 0.5 }); self.velocity = 0; self.gravity = 0.5; self.jumpForce = -10; self.rotation = 0; self.isDead = false; self.jump = function () { if (self.isDead) return; self.velocity = self.jumpForce; LK.getSound('jump').play(); }; self.update = function () { if (self.isDead) return; self.velocity += self.gravity; self.y += self.velocity; // Calculate rotation based on velocity (between -30 and 90 degrees) var targetRotation = Math.max(-Math.PI / 6, Math.min(Math.PI / 2, self.velocity * 0.04)); self.rotation = targetRotation; rocketGraphics.rotation = self.rotation; // Check boundaries if (self.y < 50) { self.y = 50; self.velocity = 0; } else if (self.y > 2732 - 50) { self.die(); } }; self.die = function () { if (self.isDead) return; self.isDead = true; LK.getSound('crash').play(); LK.effects.flashObject(self, 0xff0000, 500); // Trigger game over after a short delay LK.setTimeout(function () { LK.showGameOver(); }, 1000); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb }); /**** * Game Code ****/ // Game variables var gameStarted = false; var rocket; var pipes = []; var coins = []; var pipeSpawnTimer = 0; var pipeSpawnInterval = 120; // Frames between pipe spawns var minGapY = 400; var maxGapY = 2732 - 400; var lastIntersecting = false; var lastCoinCollected = false; // Create UI elements var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var instructionsTxt = new Text2('Tap to start\nThen tap to fly', { size: 80, fill: 0xFFFFFF }); instructionsTxt.anchor.set(0.5, 0.5); LK.gui.center.addChild(instructionsTxt); // Create background var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); // Create rocket function initGame() { // Create rocket if it doesn't exist if (!rocket) { rocket = new Rocket(); rocket.x = 400; rocket.y = 2732 / 2; game.addChild(rocket); } else { rocket.y = 2732 / 2; rocket.velocity = 0; rocket.rotation = 0; rocket.isDead = false; } // Clear existing pipes for (var i = 0; i < pipes.length; i++) { pipes[i].destroy(); } pipes = []; // Clear existing coins for (var i = 0; i < coins.length; i++) { coins[i].destroy(); } coins = []; // Reset variables pipeSpawnTimer = 0; LK.setScore(0); scoreTxt.setText('0'); gameStarted = false; instructionsTxt.visible = true; lastIntersecting = false; } // Initialize the game initGame(); // Play background music LK.playMusic('gameMusic', { fade: { start: 0, end: 0.6, duration: 1000 } }); // Handle tap to jump and start game game.down = function (x, y, obj) { if (!gameStarted) { gameStarted = true; instructionsTxt.visible = false; } if (rocket && !rocket.isDead) { rocket.jump(); } }; // Create a new pipe at the right edge of the screen function spawnPipe() { var pipe = new Pipe(); pipe.x = 2048 + 150; // Start just off the right side of the screen // Randomize gap position var gapY = minGapY + Math.random() * (maxGapY - minGapY); pipe.setGapPosition(gapY); game.addChild(pipe); pipes.push(pipe); // Add a coin in the middle of the gap (70% chance) if (Math.random() < 0.7) { var coin = new Coin(); coin.x = pipe.x; coin.y = gapY; // Position coin in the middle of the gap game.addChild(coin); coins.push(coin); } } // Check for collisions between rocket and pipes function checkCollisions() { if (!rocket || rocket.isDead || !gameStarted) return; var collisionDetected = false; var coinCollected = false; // Create rocket collision rectangle once var rocketBounds = new Rectangle(rocket.x - 40, // Half width rocket.y - 40, // Half height 80, // Width 80 // Height ); // Check collisions with pipes for (var i = 0; i < pipes.length; i++) { var pipe = pipes[i]; var topPipe = pipe.getTopPipe(); var bottomPipe = pipe.getBottomPipe(); // Convert pipe coordinates to game coordinates for collision detection var topGlobal = pipe.toGlobal(topPipe.position); var bottomGlobal = pipe.toGlobal(bottomPipe.position); var topGame = game.toLocal(topGlobal); var bottomGame = game.toLocal(bottomGlobal); // Create collision rectangles var topPipeBounds = new Rectangle(topGame.x - 75, // Half width topGame.y - 800, // Full height (anchored at bottom) 150, // Width 800 // Height ); var bottomPipeBounds = new Rectangle(bottomGame.x - 75, // Half width bottomGame.y, // Anchored at top 150, // Width 800 // Height ); // Check for collisions if (intersectRectangles(rocketBounds, topPipeBounds) || intersectRectangles(rocketBounds, bottomPipeBounds)) { collisionDetected = true; break; } // Check if rocket has passed this pipe pair if (!pipe.scored && pipe.x < rocket.x - 100) { pipe.scored = true; LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore().toString()); LK.getSound('score').play(); } } // Check for coin collisions for (var i = coins.length - 1; i >= 0; i--) { var coin = coins[i]; if (!coin.collected && coin.visible) { var coinBounds = new Rectangle(coin.x - 25, // Half width coin.y - 25, // Half height 50, // Width 50 // Height ); if (intersectRectangles(rocketBounds, coinBounds)) { coin.collect(); LK.setScore(LK.getScore() + 3); // Coins worth 3 points scoreTxt.setText(LK.getScore().toString()); LK.getSound('coin').play(); coinCollected = true; } } } // Check for state transition (not colliding -> colliding) if (!lastIntersecting && collisionDetected) { rocket.die(); } lastIntersecting = collisionDetected; } // Helper function to check if two rectangles intersect function intersectRectangles(rect1, rect2) { return !(rect1.x > rect2.x + rect2.width || rect1.x + rect1.width < rect2.x || rect1.y > rect2.y + rect2.height || rect1.y + rect1.height < rect2.y); } // Game update loop game.update = function () { if (gameStarted) { // Update rocket if (rocket) { rocket.update(); } // Update pipes for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].update(); // Remove pipes that have moved off screen if (pipes[i].x < -200) { pipes[i].destroy(); pipes.splice(i, 1); } } // Update coins for (var i = coins.length - 1; i >= 0; i--) { coins[i].update(); // Remove coins that have moved off screen if (coins[i].x < -100) { coins[i].destroy(); coins.splice(i, 1); } } // Spawn new pipes pipeSpawnTimer++; if (pipeSpawnTimer >= pipeSpawnInterval) { spawnPipe(); pipeSpawnTimer = 0; // Make the game slightly harder over time by decreasing the spawn interval pipeSpawnInterval = Math.max(80, pipeSpawnInterval - 1); } // Check for collisions checkCollisions(); // Adjust pipe speed based on score to increase difficulty var baseSpeed = 5; var speedIncrease = Math.min(5, Math.floor(LK.getScore() / 10)); for (var i = 0; i < pipes.length; i++) { pipes[i].speed = baseSpeed + speedIncrease; } // Update coin speeds to match pipes for (var i = 0; i < coins.length; i++) { coins[i].speed = baseSpeed + speedIncrease; } } };
===================================================================
--- original.js
+++ change.js
@@ -5,8 +5,42 @@
/****
* Classes
****/
+var Coin = Container.expand(function () {
+ var self = Container.call(this);
+ // Create coin visual
+ var coinGraphics = self.attachAsset('coin', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 5;
+ self.collected = false;
+ self.rotation = 0;
+ // Animate the coin
+ self.update = function () {
+ self.x -= self.speed;
+ self.rotation += 0.05;
+ coinGraphics.rotation = self.rotation;
+ };
+ // Flash and mark as collected
+ self.collect = function () {
+ if (!self.collected) {
+ self.collected = true;
+ LK.effects.flashObject(self, 0xffff00, 300);
+ // Fade out and shrink
+ tween.to(self, 0.3, {
+ alpha: 0,
+ scaleX: 0.1,
+ scaleY: 0.1,
+ onComplete: function onComplete() {
+ self.visible = false;
+ }
+ });
+ }
+ };
+ return self;
+});
var Pipe = Container.expand(function () {
var self = Container.call(this);
// Each pipe consists of top and bottom segments
var topPipe = self.attachAsset('pipe', {
@@ -93,13 +127,15 @@
// Game variables
var gameStarted = false;
var rocket;
var pipes = [];
+var coins = [];
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // Frames between pipe spawns
var minGapY = 400;
var maxGapY = 2732 - 400;
var lastIntersecting = false;
+var lastCoinCollected = false;
// Create UI elements
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
@@ -138,8 +174,13 @@
for (var i = 0; i < pipes.length; i++) {
pipes[i].destroy();
}
pipes = [];
+ // Clear existing coins
+ for (var i = 0; i < coins.length; i++) {
+ coins[i].destroy();
+ }
+ coins = [];
// Reset variables
pipeSpawnTimer = 0;
LK.setScore(0);
scoreTxt.setText('0');
@@ -175,13 +216,32 @@
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapY);
game.addChild(pipe);
pipes.push(pipe);
+ // Add a coin in the middle of the gap (70% chance)
+ if (Math.random() < 0.7) {
+ var coin = new Coin();
+ coin.x = pipe.x;
+ coin.y = gapY; // Position coin in the middle of the gap
+ game.addChild(coin);
+ coins.push(coin);
+ }
}
// Check for collisions between rocket and pipes
function checkCollisions() {
if (!rocket || rocket.isDead || !gameStarted) return;
var collisionDetected = false;
+ var coinCollected = false;
+ // Create rocket collision rectangle once
+ var rocketBounds = new Rectangle(rocket.x - 40,
+ // Half width
+ rocket.y - 40,
+ // Half height
+ 80,
+ // Width
+ 80 // Height
+ );
+ // Check collisions with pipes
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
var topPipe = pipe.getTopPipe();
var bottomPipe = pipe.getBottomPipe();
@@ -190,16 +250,8 @@
var bottomGlobal = pipe.toGlobal(bottomPipe.position);
var topGame = game.toLocal(topGlobal);
var bottomGame = game.toLocal(bottomGlobal);
// Create collision rectangles
- var rocketBounds = new Rectangle(rocket.x - 40,
- // Half width
- rocket.y - 40,
- // Half height
- 80,
- // Width
- 80 // Height
- );
var topPipeBounds = new Rectangle(topGame.x - 75,
// Half width
topGame.y - 800,
// Full height (anchored at bottom)
@@ -227,8 +279,29 @@
scoreTxt.setText(LK.getScore().toString());
LK.getSound('score').play();
}
}
+ // Check for coin collisions
+ for (var i = coins.length - 1; i >= 0; i--) {
+ var coin = coins[i];
+ if (!coin.collected && coin.visible) {
+ var coinBounds = new Rectangle(coin.x - 25,
+ // Half width
+ coin.y - 25,
+ // Half height
+ 50,
+ // Width
+ 50 // Height
+ );
+ if (intersectRectangles(rocketBounds, coinBounds)) {
+ coin.collect();
+ LK.setScore(LK.getScore() + 3); // Coins worth 3 points
+ scoreTxt.setText(LK.getScore().toString());
+ LK.getSound('coin').play();
+ coinCollected = true;
+ }
+ }
+ }
// Check for state transition (not colliding -> colliding)
if (!lastIntersecting && collisionDetected) {
rocket.die();
}
@@ -253,8 +326,17 @@
pipes[i].destroy();
pipes.splice(i, 1);
}
}
+ // Update coins
+ for (var i = coins.length - 1; i >= 0; i--) {
+ coins[i].update();
+ // Remove coins that have moved off screen
+ if (coins[i].x < -100) {
+ coins[i].destroy();
+ coins.splice(i, 1);
+ }
+ }
// Spawn new pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
spawnPipe();
@@ -269,6 +351,10 @@
var speedIncrease = Math.min(5, Math.floor(LK.getScore() / 10));
for (var i = 0; i < pipes.length; i++) {
pipes[i].speed = baseSpeed + speedIncrease;
}
+ // Update coin speeds to match pipes
+ for (var i = 0; i < coins.length; i++) {
+ coins[i].speed = baseSpeed + speedIncrease;
+ }
}
};
\ No newline at end of file