/****
* 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,
scaleX: 1.2,
scaleY: 8
});
// Bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0,
scaleX: 1.2,
scaleY: 8
});
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;
});
var Tile = Container.expand(function (color, gridX, gridY) {
var self = Container.call(this);
} // ... mevcut class içeriğin
);
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
//var tween = LK.import("@upit/tween.v1");
// var tween = LK.import("@upit/tween.v1"); // bunu sil veya yorum satırı yap
function tween(obj, props) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var duration = options.duration || 300;
var frameRate = 60;
var totalFrames = Math.round(duration / (1000 / frameRate));
var keys = Object.keys(props);
var frame = 0;
var startValues = {};
for (var _i = 0, _keys = keys; _i < _keys.length; _i++) {
var key = _keys[_i];
startValues[key] = obj[key];
}
var interval = LK.setInterval(function () {
frame++;
for (var _i2 = 0, _keys2 = keys; _i2 < _keys2.length; _i2++) {
var key = _keys2[_i2];
var start = startValues[key];
var end = props[key];
var delta = end - start;
obj[key] = start + delta * frame / totalFrames;
}
if (frame >= totalFrames) {
LK.clearInterval(interval);
for (var _i3 = 0, _keys3 = keys; _i3 < _keys3.length; _i3++) {
var key = _keys3[_i3];
obj[key] = props[key];
}
if (options.onFinish) {
options.onFinish();
}
}
}, 1000 / frameRate);
}
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // 2 seconds at 60fps
var lastPipeScore = 0;
var nextPipeX = 2200; // Starting X position for next pipe
var pipeDistance = 350; // Distance between pipes
// 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(x) {
var pipe = new Pipe();
pipe.x = x || nextPipeX; // Use provided x or nextPipeX
// 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);
// Update nextPipeX for next pipe
nextPipeX += pipeDistance;
}
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(nextPipeX);
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(nextPipeX);
pipeSpawnTimer = 0;
}
}, 2000);
; /****
* 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,
scaleX: 1.2,
scaleY: 8
});
// Bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0,
scaleX: 1.2,
scaleY: 8
});
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;
});
var Tile = Container.expand(function (color, gridX, gridY) {
var self = Container.call(this);
} // ... mevcut class içeriğin
);
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
//var tween = LK.import("@upit/tween.v1");
// var tween = LK.import("@upit/tween.v1"); // bunu sil veya yorum satırı yap
function tween(obj, props) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var duration = options.duration || 300;
var frameRate = 60;
var totalFrames = Math.round(duration / (1000 / frameRate));
var keys = Object.keys(props);
var frame = 0;
var startValues = {};
for (var _i = 0, _keys = keys; _i < _keys.length; _i++) {
var key = _keys[_i];
startValues[key] = obj[key];
}
var interval = LK.setInterval(function () {
frame++;
for (var _i2 = 0, _keys2 = keys; _i2 < _keys2.length; _i2++) {
var key = _keys2[_i2];
var start = startValues[key];
var end = props[key];
var delta = end - start;
obj[key] = start + delta * frame / totalFrames;
}
if (frame >= totalFrames) {
LK.clearInterval(interval);
for (var _i3 = 0, _keys3 = keys; _i3 < _keys3.length; _i3++) {
var key = _keys3[_i3];
obj[key] = props[key];
}
if (options.onFinish) {
options.onFinish();
}
}
}, 1000 / frameRate);
}
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // 2 seconds at 60fps
var lastPipeScore = 0;
var nextPipeX = 2200; // Starting X position for next pipe
var pipeDistance = 350; // Distance between pipes
// 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(x) {
var pipe = new Pipe();
pipe.x = x || nextPipeX; // Use provided x or nextPipeX
// 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);
// Update nextPipeX for next pipe
nextPipeX += pipeDistance;
}
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(nextPipeX);
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(nextPipeX);
pipeSpawnTimer = 0;
}
}, 2000);
;