Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'sprite')' in or related to this line: 'var topPipe = LK.create.sprite('greenTile');' Line Number: 248
Code edit (1 edits merged)
Please save this source code
User prompt
Flappy Bird
Initial prompt
const gravity = 0.5; const jumpPower = -10; let bird; let birdY = 200; let birdVelocity = 0; let pipes = []; let pipeSpeed = 2; let pipeGap = 120; let pipeWidth = 50; let score = 0; let gameOver = false; function createBird() { bird = LK.create.sprite('blueTile'); // Kuşu temsil eden blok bird.pos(100, birdY); bird.size(40, 30); } function createPipe(x) { // Boru üst parça let topHeight = Math.random() * 150 + 50; let topPipe = LK.create.sprite('greenTile'); topPipe.pos(x, 0); topPipe.size(pipeWidth, topHeight); // Boru alt parça let bottomPipe = LK.create.sprite('greenTile'); bottomPipe.pos(x, topHeight + pipeGap); bottomPipe.size(pipeWidth, 400 - (topHeight + pipeGap)); pipes.push({top: topPipe, bottom: bottomPipe, passed:false}); } function resetGame() { // Boruları temizle pipes.forEach(p => { LK.remove(p.top); LK.remove(p.bottom); }); pipes = []; score = 0; birdY = 200; birdVelocity = 0; gameOver = false; createBird(); createPipe(400); createPipe(600); createPipe(800); } function update() { if(gameOver) return; birdVelocity += gravity; birdY += birdVelocity; if(birdY > 370 || birdY < 0) { gameOver = true; } bird.pos(bird.x, birdY); // Boruları hareket ettir pipes.forEach(p => { p.top.pos(p.top.x - pipeSpeed, p.top.y); p.bottom.pos(p.bottom.x - pipeSpeed, p.bottom.y); // Boru ekran dışına çıkarsa yenisi gelsin if(p.top.x < -pipeWidth) { LK.remove(p.top); LK.remove(p.bottom); pipes.shift(); let lastX = pipes.length > 0 ? pipes[pipes.length -1].top.x : 800; createPipe(lastX + 200); } // Kuş boruyu geçtiyse skor artar if(!p.passed && p.top.x + pipeWidth < bird.x) { score++; p.passed = true; console.log("Puan: " + score); } // Çarpma kontrolü (basit AABB) if(collides(bird, p.top) || collides(bird, p.bottom)) { gameOver = true; } }); } function collides(a, b) { return !(a.x + a.width < b.x || a.x > b.x + b.width || a.y + a.height < b.y || a.y > b.y + b.height); } // Kuş zıplaması LK.on('click', () => { if(gameOver) { resetGame(); } else { birdVelocity = jumpPower; } }); resetGame(); LK.ticker(() => { update(); });
/****
* 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.velocity = 0;
self.gravity = 0.8;
self.jumpPower = -15;
self.maxFallSpeed = 12;
self.update = function () {
// Apply gravity
self.velocity += self.gravity;
// Limit fall speed
if (self.velocity > self.maxFallSpeed) {
self.velocity = self.maxFallSpeed;
}
// Update position
self.y += self.velocity;
// Rotate bird based on velocity
birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
};
self.jump = function () {
self.velocity = self.jumpPower;
LK.getSound('jump').play();
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.gapSize = 300;
self.speed = -4;
self.scored = false;
// Top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
// Bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setGapPosition = function (gapCenterY) {
self.topPipe.y = gapCenterY - self.gapSize / 2;
self.bottomPipe.y = gapCenterY + self.gapSize / 2;
};
self.update = function () {
self.x += self.speed;
};
self.getBounds = function () {
return {
x: self.x - 60,
width: 120,
topY: self.topPipe.y,
bottomY: self.bottomPipe.y
};
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // 2 seconds at 60fps
var lastPipeScore = 0;
// UI Elements
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var instructionTxt = new Text2('TAP TO JUMP', {
size: 60,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(instructionTxt);
// Initialize bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen height
// Initialize ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
}));
ground.x = 0;
ground.y = 2632; // Near bottom of screen
// Game input handling
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
instructionTxt.visible = false;
}
bird.jump();
};
// Pipe management functions
function createPipe() {
var pipe = new Pipe();
pipe.x = 2200; // Start off-screen right
// Random gap position (avoid too high or too low)
var minGapY = 400;
var maxGapY = 2200;
var gapCenterY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapCenterY);
pipes.push(pipe);
game.addChild(pipe);
}
function checkCollisions() {
var birdBounds = {
x: bird.x - 30,
y: bird.y - 30,
width: 60,
height: 60
};
// Check ground collision
if (bird.y + 30 >= ground.y) {
return true;
}
// Check ceiling collision
if (bird.y - 30 <= 0) {
return true;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
var pipeBounds = pipe.getBounds();
// Check if bird overlaps with pipe horizontally
if (birdBounds.x < pipeBounds.x + pipeBounds.width && birdBounds.x + birdBounds.width > pipeBounds.x) {
// Check if bird hits top or bottom pipe
if (birdBounds.y < pipeBounds.topY || birdBounds.y + birdBounds.height > pipeBounds.bottomY) {
return true;
}
}
}
return false;
}
function updateScore() {
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
// Check if bird passed through pipe
if (!pipe.scored && pipe.x + 60 < bird.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
}
}
function cleanupPipes() {
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
// Remove pipes that are off-screen
if (pipe.x < -200) {
pipe.destroy();
pipes.splice(i, 1);
}
}
}
// Main game loop
game.update = function () {
if (!gameStarted) {
return;
}
// Update bird
bird.update();
// Update pipes
for (var i = 0; i < pipes.length; i++) {
pipes[i].update();
}
// Spawn new pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
createPipe();
pipeSpawnTimer = 0;
}
// Check collisions
if (checkCollisions()) {
LK.getSound('crash').play();
LK.showGameOver();
return;
}
// Update score
updateScore();
// Cleanup off-screen pipes
cleanupPipes();
};
// Initialize first pipe after delay
LK.setTimeout(function () {
if (gameStarted) {
createPipe();
}
}, 2000); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,216 @@
-/****
+/****
+* 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.velocity = 0;
+ self.gravity = 0.8;
+ self.jumpPower = -15;
+ self.maxFallSpeed = 12;
+ self.update = function () {
+ // Apply gravity
+ self.velocity += self.gravity;
+ // Limit fall speed
+ if (self.velocity > self.maxFallSpeed) {
+ self.velocity = self.maxFallSpeed;
+ }
+ // Update position
+ self.y += self.velocity;
+ // Rotate bird based on velocity
+ birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
+ };
+ self.jump = function () {
+ self.velocity = self.jumpPower;
+ LK.getSound('jump').play();
+ };
+ return self;
+});
+var Pipe = Container.expand(function () {
+ var self = Container.call(this);
+ self.gapSize = 300;
+ self.speed = -4;
+ self.scored = false;
+ // Top pipe
+ self.topPipe = self.attachAsset('pipe', {
+ anchorX: 0.5,
+ anchorY: 1
+ });
+ // Bottom pipe
+ self.bottomPipe = self.attachAsset('pipe', {
+ anchorX: 0.5,
+ anchorY: 0
+ });
+ self.setGapPosition = function (gapCenterY) {
+ self.topPipe.y = gapCenterY - self.gapSize / 2;
+ self.bottomPipe.y = gapCenterY + self.gapSize / 2;
+ };
+ self.update = function () {
+ self.x += self.speed;
+ };
+ self.getBounds = function () {
+ return {
+ x: self.x - 60,
+ width: 120,
+ topY: self.topPipe.y,
+ bottomY: self.bottomPipe.y
+ };
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game variables
+var bird;
+var pipes = [];
+var ground;
+var gameStarted = false;
+var pipeSpawnTimer = 0;
+var pipeSpawnInterval = 120; // 2 seconds at 60fps
+var lastPipeScore = 0;
+// UI Elements
+var scoreTxt = new Text2('0', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+var instructionTxt = new Text2('TAP TO JUMP', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+instructionTxt.anchor.set(0.5, 0.5);
+LK.gui.center.addChild(instructionTxt);
+// Initialize bird
+bird = game.addChild(new Bird());
+bird.x = 400;
+bird.y = 1366; // Center of screen height
+// Initialize ground
+ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 0
+}));
+ground.x = 0;
+ground.y = 2632; // Near bottom of screen
+// Game input handling
+game.down = function (x, y, obj) {
+ if (!gameStarted) {
+ gameStarted = true;
+ instructionTxt.visible = false;
+ }
+ bird.jump();
+};
+// Pipe management functions
+function createPipe() {
+ var pipe = new Pipe();
+ pipe.x = 2200; // Start off-screen right
+ // Random gap position (avoid too high or too low)
+ var minGapY = 400;
+ var maxGapY = 2200;
+ var gapCenterY = minGapY + Math.random() * (maxGapY - minGapY);
+ pipe.setGapPosition(gapCenterY);
+ pipes.push(pipe);
+ game.addChild(pipe);
+}
+function checkCollisions() {
+ var birdBounds = {
+ x: bird.x - 30,
+ y: bird.y - 30,
+ width: 60,
+ height: 60
+ };
+ // Check ground collision
+ if (bird.y + 30 >= ground.y) {
+ return true;
+ }
+ // Check ceiling collision
+ if (bird.y - 30 <= 0) {
+ return true;
+ }
+ // Check pipe collisions
+ for (var i = 0; i < pipes.length; i++) {
+ var pipe = pipes[i];
+ var pipeBounds = pipe.getBounds();
+ // Check if bird overlaps with pipe horizontally
+ if (birdBounds.x < pipeBounds.x + pipeBounds.width && birdBounds.x + birdBounds.width > pipeBounds.x) {
+ // Check if bird hits top or bottom pipe
+ if (birdBounds.y < pipeBounds.topY || birdBounds.y + birdBounds.height > pipeBounds.bottomY) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+function updateScore() {
+ for (var i = 0; i < pipes.length; i++) {
+ var pipe = pipes[i];
+ // Check if bird passed through pipe
+ if (!pipe.scored && pipe.x + 60 < bird.x) {
+ pipe.scored = true;
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText(LK.getScore());
+ LK.getSound('score').play();
+ }
+ }
+}
+function cleanupPipes() {
+ for (var i = pipes.length - 1; i >= 0; i--) {
+ var pipe = pipes[i];
+ // Remove pipes that are off-screen
+ if (pipe.x < -200) {
+ pipe.destroy();
+ pipes.splice(i, 1);
+ }
+ }
+}
+// Main game loop
+game.update = function () {
+ if (!gameStarted) {
+ return;
+ }
+ // Update bird
+ bird.update();
+ // Update pipes
+ for (var i = 0; i < pipes.length; i++) {
+ pipes[i].update();
+ }
+ // Spawn new pipes
+ pipeSpawnTimer++;
+ if (pipeSpawnTimer >= pipeSpawnInterval) {
+ createPipe();
+ pipeSpawnTimer = 0;
+ }
+ // Check collisions
+ if (checkCollisions()) {
+ LK.getSound('crash').play();
+ LK.showGameOver();
+ return;
+ }
+ // Update score
+ updateScore();
+ // Cleanup off-screen pipes
+ cleanupPipes();
+};
+// Initialize first pipe after delay
+LK.setTimeout(function () {
+ if (gameStarted) {
+ createPipe();
+ }
+}, 2000);
\ No newline at end of file