Code edit (9 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Flappy Bird
User prompt
Create a simple 2D Flappy Bird-style game. Game requirements: - The player controls a small bird that continuously falls due to gravity. - Pressing spacebar or tapping makes the bird jump upward. - Obstacles (pipes) move from right to left across the screen. - Each pipe has a gap that the bird must pass through. - If the bird hits a pipe or the ground, the game ends. - Score increases each time the bird successfully passes a pipe. Game features: - Start screen with "Tap or Press Space to Start" - Game over screen with final score and restart option - Smooth animations and simple physics (gravity and jump) - Randomized pipe heights for difficulty - Clean, colorful 2D graphics Technical requirements: - Use simple and readable code - Include comments explaining the logic - Use basic physics for movement - Keep everything beginner-friendly Optional enhancements: - Add sound effects for jump and collision - Add background scrolling - Increase difficulty over time
User prompt
Please continue polishing my design document.
Initial prompt
Create a simple 2D Flappy Bird-style game. Game requirements: - The player controls a small bird that continuously falls due to gravity. - Pressing spacebar or tapping makes the bird jump upward. - Obstacles (pipes) move from right to left across the screen. - Each pipe has a gap that the bird must pass through. - If the bird hits a pipe or the ground, the game ends. - Score increases each time the bird successfully passes a pipe. Game features: - Start screen with "Tap or Press Space to Start" - Game over screen with final score and restart option - Smooth animations and simple physics (gravity and jump) - Randomized pipe heights for difficulty - Clean, colorful 2D graphics Technical requirements: - Use simple and readable code - Include comments explaining the logic - Use basic physics for movement - Keep everything beginner-friendly Optional enhancements: - Add sound effects for jump and collision - Add background scrolling - Increase difficulty over time
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.6;
self.jumpPower = -15;
self.maxFallSpeed = 20;
self.jump = function () {
self.velocityY = self.jumpPower;
LK.getSound('jump').play();
};
self.update = function () {
self.velocityY += self.gravity;
if (self.velocityY > self.maxFallSpeed) {
self.velocityY = self.maxFallSpeed;
}
self.y += self.velocityY;
var rotation = Math.min(self.velocityY * 0.05, 0.5);
birdGraphics.rotation = rotation;
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -8;
self.gapHeight = 200;
self.pipeWidth = 150;
self.scored = false;
var topPipe = self.attachAsset('pipeTop', {
anchorX: 0.5,
anchorY: 0.5
});
var bottomPipe = self.attachAsset('pipeBottom', {
anchorX: 0.5,
anchorY: 0.5
});
self.setPipeGap = function (gapY) {
var topPipeHeight = gapY - self.gapHeight / 2;
var bottomPipeY = gapY + self.gapHeight / 2;
var bottomPipeHeight = 2732 - bottomPipeY;
topPipe.height = Math.max(100, topPipeHeight);
topPipe.y = topPipe.height / 2;
bottomPipe.height = Math.max(100, bottomPipeHeight);
bottomPipe.y = bottomPipeY + bottomPipe.height / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird;
var pipes = [];
var gameStarted = false;
var gameOver = false;
var score = 0;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120;
var difficultyTimer = 0;
var backgroundGraphic = LK.getAsset('background', {
anchorX: 0,
anchorY: 0
});
game.addChild(backgroundGraphic);
var groundGraphic = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
}));
groundGraphic.y = 2732 - 100;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.setText('0');
LK.gui.top.addChild(scoreTxt);
var startTxt = new Text2('Tap to Start', {
size: 80,
fill: 0xFFFFFF
});
startTxt.anchor.set(0.5, 0.5);
startTxt.x = 1024;
startTxt.y = 1366;
game.addChild(startTxt);
function initGame() {
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366;
bird.velocityY = 0;
gameStarted = true;
gameOver = false;
score = 0;
pipes = [];
pipeSpawnTimer = 0;
difficultyTimer = 0;
scoreTxt.setText('0');
startTxt.visible = false;
LK.playMusic('bgmusic', {
loop: true
});
}
function spawnPipe() {
var minGapY = 300;
var maxGapY = 2200;
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
var pipe = game.addChild(new Pipe());
pipe.x = 2048 + 100;
pipe.y = 0;
pipe.setPipeGap(gapY);
pipes.push(pipe);
}
game.down = function (x, y, obj) {
if (!gameStarted) {
initGame();
} else if (!gameOver && bird) {
bird.jump();
}
};
game.update = function () {
if (!gameStarted) {
return;
}
if (gameOver) {
return;
}
if (bird) {
bird.update();
if (bird.y - 25 < 0 || bird.y + 25 > 2732 - 100) {
gameOver = true;
LK.getSound('collision').play();
LK.showGameOver();
return;
}
}
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
difficultyTimer++;
if (difficultyTimer > 0 && difficultyTimer % 600 === 0) {
pipeSpawnInterval = Math.max(90, pipeSpawnInterval - 5);
}
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
if (bird && bird.intersects(pipe)) {
gameOver = true;
LK.getSound('collision').play();
LK.showGameOver();
return;
}
if (bird && !pipe.scored && bird.x > pipe.x) {
pipe.scored = true;
score++;
LK.setScore(score);
scoreTxt.setText(String(score));
}
if (pipe.x < -200) {
pipe.destroy();
pipes.splice(i, 1);
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,185 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Bird = Container.expand(function () {
+ var self = Container.call(this);
+ var birdGraphics = self.attachAsset('bird', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.velocityY = 0;
+ self.gravity = 0.6;
+ self.jumpPower = -15;
+ self.maxFallSpeed = 20;
+ self.jump = function () {
+ self.velocityY = self.jumpPower;
+ LK.getSound('jump').play();
+ };
+ self.update = function () {
+ self.velocityY += self.gravity;
+ if (self.velocityY > self.maxFallSpeed) {
+ self.velocityY = self.maxFallSpeed;
+ }
+ self.y += self.velocityY;
+ var rotation = Math.min(self.velocityY * 0.05, 0.5);
+ birdGraphics.rotation = rotation;
+ };
+ return self;
+});
+var Pipe = Container.expand(function () {
+ var self = Container.call(this);
+ self.speed = -8;
+ self.gapHeight = 200;
+ self.pipeWidth = 150;
+ self.scored = false;
+ var topPipe = self.attachAsset('pipeTop', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var bottomPipe = self.attachAsset('pipeBottom', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.setPipeGap = function (gapY) {
+ var topPipeHeight = gapY - self.gapHeight / 2;
+ var bottomPipeY = gapY + self.gapHeight / 2;
+ var bottomPipeHeight = 2732 - bottomPipeY;
+ topPipe.height = Math.max(100, topPipeHeight);
+ topPipe.y = topPipe.height / 2;
+ bottomPipe.height = Math.max(100, bottomPipeHeight);
+ bottomPipe.y = bottomPipeY + bottomPipe.height / 2;
+ };
+ self.update = function () {
+ self.x += self.speed;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var bird;
+var pipes = [];
+var gameStarted = false;
+var gameOver = false;
+var score = 0;
+var pipeSpawnTimer = 0;
+var pipeSpawnInterval = 120;
+var difficultyTimer = 0;
+var backgroundGraphic = LK.getAsset('background', {
+ anchorX: 0,
+ anchorY: 0
+});
+game.addChild(backgroundGraphic);
+var groundGraphic = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 0
+}));
+groundGraphic.y = 2732 - 100;
+var scoreTxt = new Text2('0', {
+ size: 100,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+scoreTxt.setText('0');
+LK.gui.top.addChild(scoreTxt);
+var startTxt = new Text2('Tap to Start', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+startTxt.anchor.set(0.5, 0.5);
+startTxt.x = 1024;
+startTxt.y = 1366;
+game.addChild(startTxt);
+function initGame() {
+ bird = game.addChild(new Bird());
+ bird.x = 300;
+ bird.y = 1366;
+ bird.velocityY = 0;
+ gameStarted = true;
+ gameOver = false;
+ score = 0;
+ pipes = [];
+ pipeSpawnTimer = 0;
+ difficultyTimer = 0;
+ scoreTxt.setText('0');
+ startTxt.visible = false;
+ LK.playMusic('bgmusic', {
+ loop: true
+ });
+}
+function spawnPipe() {
+ var minGapY = 300;
+ var maxGapY = 2200;
+ var gapY = minGapY + Math.random() * (maxGapY - minGapY);
+ var pipe = game.addChild(new Pipe());
+ pipe.x = 2048 + 100;
+ pipe.y = 0;
+ pipe.setPipeGap(gapY);
+ pipes.push(pipe);
+}
+game.down = function (x, y, obj) {
+ if (!gameStarted) {
+ initGame();
+ } else if (!gameOver && bird) {
+ bird.jump();
+ }
+};
+game.update = function () {
+ if (!gameStarted) {
+ return;
+ }
+ if (gameOver) {
+ return;
+ }
+ if (bird) {
+ bird.update();
+ if (bird.y - 25 < 0 || bird.y + 25 > 2732 - 100) {
+ gameOver = true;
+ LK.getSound('collision').play();
+ LK.showGameOver();
+ return;
+ }
+ }
+ pipeSpawnTimer++;
+ if (pipeSpawnTimer >= pipeSpawnInterval) {
+ spawnPipe();
+ pipeSpawnTimer = 0;
+ }
+ difficultyTimer++;
+ if (difficultyTimer > 0 && difficultyTimer % 600 === 0) {
+ pipeSpawnInterval = Math.max(90, pipeSpawnInterval - 5);
+ }
+ for (var i = pipes.length - 1; i >= 0; i--) {
+ var pipe = pipes[i];
+ pipe.update();
+ if (bird && bird.intersects(pipe)) {
+ gameOver = true;
+ LK.getSound('collision').play();
+ LK.showGameOver();
+ return;
+ }
+ if (bird && !pipe.scored && bird.x > pipe.x) {
+ pipe.scored = true;
+ score++;
+ LK.setScore(score);
+ scoreTxt.setText(String(score));
+ }
+ if (pipe.x < -200) {
+ pipe.destroy();
+ pipes.splice(i, 1);
+ }
+ }
+};
\ No newline at end of file