/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
unlockedBirds: [1],
totalCoins: 0
});
/****
* 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.flapPower = -12;
self.isAlive = true;
self.flap = function () {
self.velocityY = self.flapPower;
LK.getSound('flap').play();
};
self.update = function () {
if (!self.isAlive) return;
self.velocityY += self.gravity;
self.lastY = self.y;
self.y += self.velocityY;
if (self.lastY - 25 >= 0 && self.y - 25 < 0) {
self.isAlive = false;
LK.showGameOver();
}
if (self.y - 25 < 0) {
self.y = 25;
self.velocityY = 0;
}
};
return self;
});
var BirdSelector = Container.expand(function () {
var self = Container.call(this);
self.birdId = 0;
self.isUnlocked = false;
self.isSelected = false;
var selectorGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
selectorGraphics.width = 80;
selectorGraphics.height = 67;
var lockText = new Text2('LOCKED', {
size: 40,
fill: 0xFF0000
});
lockText.anchor.set(0.5, 0.5);
lockText.visible = false;
self.addChild(lockText);
self.setLocked = function (locked) {
self.isUnlocked = !locked;
lockText.visible = locked;
selectorGraphics.alpha = locked ? 0.3 : 1;
};
return self;
});
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -6;
self.isCollected = false;
self.update = function () {
self.x += self.speed;
};
return self;
});
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
self.x = 0;
self.y = 2582;
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -6;
self.gapSize = 250;
self.hasBeenScored = false;
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setup = function (gapY) {
self.gapY = gapY;
topPipe.y = gapY - self.gapSize / 2;
bottomPipe.y = gapY + self.gapSize / 2;
topPipe.x = 50;
bottomPipe.x = 50;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var skyGradient = new Container();
game.addChild(skyGradient);
var topSky = LK.getAsset('skyTop', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
skyGradient.addChild(topSky);
var bottomSky = LK.getAsset('skyBottom', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 1366
});
skyGradient.addChild(bottomSky);
var gameStarted = false;
var gamePaused = false;
var menuContainer = new Container();
game.addChild(menuContainer);
var titleTxt = new Text2('FLAPPY BIRD', {
size: 200,
fill: 0xFFFFFF
});
titleTxt.anchor.set(0.5, 0.5);
titleTxt.x = 1024;
titleTxt.y = 600;
menuContainer.addChild(titleTxt);
var instructionsTxt = new Text2('TAP TO FLAP', {
size: 120,
fill: 0xFFFFFF
});
instructionsTxt.anchor.set(0.5, 0.5);
instructionsTxt.x = 1024;
instructionsTxt.y = 1100;
menuContainer.addChild(instructionsTxt);
var birdSelectionTxt = new Text2('SELECT BIRD', {
size: 100,
fill: 0xFFFFFF
});
birdSelectionTxt.anchor.set(0.5, 0.5);
birdSelectionTxt.x = 1024;
birdSelectionTxt.y = 1250;
menuContainer.addChild(birdSelectionTxt);
var birdSelectorsContainer = new Container();
menuContainer.addChild(birdSelectorsContainer);
var unlockedBirds = storage.unlockedBirds || [1];
var totalCoinsCollected = storage.totalCoins || 0;
var birdSelectors = [];
for (var b = 1; b <= 5; b++) {
var birdSelector = new BirdSelector();
birdSelectorsContainer.addChild(birdSelector);
birdSelector.birdId = b;
birdSelector.x = 300 + (b - 1) * 350;
birdSelector.y = 1350;
var coinsNeeded = (b - 1) * 50;
var isBirdUnlocked = false;
for (var ub = 0; ub < unlockedBirds.length; ub++) {
if (unlockedBirds[ub] === b) {
isBirdUnlocked = true;
break;
}
}
birdSelector.setLocked(!isBirdUnlocked);
if (b === 1) {
birdSelector.isSelected = true;
}
var costTxt = new Text2(coinsNeeded + ' coins', {
size: 30,
fill: 0xFFFFFF
});
costTxt.anchor.set(0.5, 0.5);
costTxt.y = 50;
birdSelector.addChild(costTxt);
birdSelectors.push(birdSelector);
}
var levelSelectionTxt = new Text2('SELECT LEVEL', {
size: 100,
fill: 0xFFFFFF
});
levelSelectionTxt.anchor.set(0.5, 0.5);
levelSelectionTxt.x = 1024;
levelSelectionTxt.y = 1500;
menuContainer.addChild(levelSelectionTxt);
var levelButtonsContainer = new Container();
menuContainer.addChild(levelButtonsContainer);
var levelButtons = [];
for (var l = 1; l <= 5; l++) {
var levelBtn = new Container();
levelButtonsContainer.addChild(levelBtn);
var btnGraphics = levelBtn.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
btnGraphics.width = 150;
btnGraphics.height = 120;
btnGraphics.tint = 0x3498db;
levelBtn.x = 300 + (l - 1) * 350;
levelBtn.y = 1600;
levelBtn.levelNumber = l;
levelBtn.isSelected = l === 1;
var btnTxt = new Text2(String(l), {
size: 80,
fill: 0xFFFFFF
});
btnTxt.anchor.set(0.5, 0.5);
levelBtn.addChild(btnTxt);
levelButtons.push(levelBtn);
}
var tapToStartTxt = new Text2('TAP TO START', {
size: 100,
fill: 0xFFD700
});
tapToStartTxt.anchor.set(0.5, 0.5);
tapToStartTxt.x = 1024;
tapToStartTxt.y = 1750;
menuContainer.addChild(tapToStartTxt);
var bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366;
bird.lastY = bird.y;
bird.lastWasCollidingWithPipe = false;
bird.visible = false;
var ground = game.addChild(new Ground());
ground.visible = false;
var pipes = [];
var coins = [];
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 200;
var nextGapPosition = 0;
var currentLevel = 1;
var pipsPassedInLevel = 0;
var pipsRequiredToLevelUp = 5;
var coinsCollected = 0;
var scoreTxt = new Text2('Score: 0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.y = 20;
LK.gui.top.addChild(scoreTxt);
scoreTxt.visible = false;
var coinsTxt = new Text2('Total Coins: 0', {
size: 80,
fill: 0xFFD700
});
coinsTxt.anchor.set(0.5, 0);
coinsTxt.y = 150;
LK.gui.top.addChild(coinsTxt);
coinsTxt.visible = false;
var gameScore = 0;
var levelTxt = new Text2('Level 1', {
size: 100,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
levelTxt.y = 150;
LK.gui.top.addChild(levelTxt);
levelTxt.visible = false;
function spawnPipe() {
var minGapY = 400;
var maxGapY = 1800;
var randomGapY = minGapY + Math.random() * (maxGapY - minGapY);
var newPipe = game.addChild(new Pipe());
newPipe.x = 2048 + 100;
var basePipeGapSize = newPipe.gapSize;
newPipe.gapSize = Math.max(120, basePipeGapSize - (currentLevel - 1) * 30);
newPipe.setup(randomGapY);
newPipe.lastX = newPipe.x;
newPipe.hasBeenScored = false;
pipes.push(newPipe);
var coinOffset = (Math.random() - 0.5) * 150;
var newCoin = game.addChild(new Coin());
newCoin.x = newPipe.x;
newCoin.y = randomGapY + coinOffset;
newCoin.lastX = newCoin.x;
newCoin.isCollected = false;
coins.push(newCoin);
}
game.down = function (x, y, obj) {
if (!gameStarted) {
var clickedBird = false;
for (var sb = 0; sb < birdSelectors.length; sb++) {
var selector = birdSelectors[sb];
var selectorBounds = selector.getBounds();
if (x >= selectorBounds.x && x <= selectorBounds.x + selectorBounds.width && y >= selectorBounds.y && y <= selectorBounds.y + selectorBounds.height) {
if (selector.isUnlocked) {
for (var dbs = 0; dbs < birdSelectors.length; dbs++) {
birdSelectors[dbs].isSelected = false;
birdSelectors[dbs].children[1].tint = 0xFFFFFF;
}
selector.isSelected = true;
selector.children[1].tint = 0xFFD700;
clickedBird = true;
} else {
var coinsNeeded = (selector.birdId - 1) * 50;
if (totalCoinsCollected >= coinsNeeded) {
var unlockedIndex = unlockedBirds.length;
unlockedBirds.push(selector.birdId);
storage.unlockedBirds = unlockedBirds;
selector.setLocked(false);
selector.isSelected = true;
for (var dus = 0; dus < birdSelectors.length; dus++) {
birdSelectors[dus].children[1].tint = 0xFFFFFF;
}
selector.children[1].tint = 0xFFD700;
clickedBird = true;
}
}
break;
}
}
var clickedLevel = false;
for (var b = 0; b < levelButtons.length; b++) {
var btn = levelButtons[b];
var buttonBounds = btn.getBounds();
if (x >= buttonBounds.x && x <= buttonBounds.x + buttonBounds.width && y >= buttonBounds.y && y <= buttonBounds.y + buttonBounds.height) {
for (var c = 0; c < levelButtons.length; c++) {
levelButtons[c].isSelected = false;
levelButtons[c].children[0].tint = 0xFFFFFF;
}
btn.isSelected = true;
btn.children[0].tint = 0xFFD700;
currentLevel = btn.levelNumber;
clickedLevel = true;
break;
}
}
if (!clickedBird && !clickedLevel) {
gameStarted = true;
gamePaused = false;
menuContainer.visible = false;
bird.visible = true;
ground.visible = true;
scoreTxt.visible = true;
levelTxt.visible = true;
coinsTxt.visible = true;
levelTxt.setText('Level ' + currentLevel);
pipsPassedInLevel = 0;
bird.gravity = 0.6 + (currentLevel - 1) * 0.15;
bird.flap();
}
} else if (bird.isAlive) {
bird.flap();
}
};
game.update = function () {
if (!gameStarted || gamePaused) return;
bird.update();
pipeSpawnTimer++;
var currentSpawnInterval = Math.max(80, pipeSpawnInterval - (currentLevel - 1) * 25);
if (pipeSpawnTimer >= currentSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
coin.update();
if (!coin.lastX) coin.lastX = coin.x;
if (coin.x < -100) {
coin.destroy();
coins.splice(i, 1);
}
if (bird.isAlive && !coin.isCollected) {
var coinCollisionDistance = Math.sqrt((bird.x - coin.x) * (bird.x - coin.x) + (bird.y - coin.y) * (bird.y - coin.y));
if (coinCollisionDistance < 50) {
coin.isCollected = true;
coinsCollected++;
totalCoinsCollected++;
storage.totalCoins = totalCoinsCollected;
coinsTxt.setText('Total Coins: ' + totalCoinsCollected);
LK.getSound('coin').play();
coin.destroy();
coins.splice(i, 1);
}
}
}
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
if (!pipe.lastX) pipe.lastX = pipe.x;
if (pipe.lastX > 300 && pipe.x <= 300 && !pipe.hasBeenScored) {
pipe.hasBeenScored = true;
gameScore++;
LK.setScore(gameScore);
pipsPassedInLevel++;
scoreTxt.setText('Score: ' + gameScore);
LK.getSound('score').play();
if (pipsPassedInLevel >= pipsRequiredToLevelUp) {
currentLevel++;
pipsPassedInLevel = 0;
levelTxt.setText('Level ' + currentLevel);
}
}
pipe.lastX = pipe.x;
if (pipe.x < -150) {
pipe.destroy();
pipes.splice(i, 1);
}
if (bird.isAlive && bird.lastWasCollidingWithPipe === undefined) {
bird.lastWasCollidingWithPipe = false;
}
if (bird.isAlive) {
var birdLeft = bird.x - 25;
var birdRight = bird.x + 25;
var birdTop = bird.y - 25;
var birdBottom = bird.y + 25;
var pipeLeft = pipe.x - 50;
var pipeRight = pipe.x + 50;
var isCollidingNow = false;
if (birdRight > pipeLeft && birdLeft < pipeRight) {
if (birdTop < pipe.gapY - pipe.gapSize / 2 || birdBottom > pipe.gapY + pipe.gapSize / 2) {
isCollidingNow = true;
}
}
if (!bird.lastWasCollidingWithPipe && isCollidingNow) {
bird.isAlive = false;
LK.showGameOver();
}
bird.lastWasCollidingWithPipe = isCollidingNow;
}
}
};
LK.playMusic('bgmusic', {
loop: true
}); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
unlockedBirds: [1],
totalCoins: 0
});
/****
* 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.flapPower = -12;
self.isAlive = true;
self.flap = function () {
self.velocityY = self.flapPower;
LK.getSound('flap').play();
};
self.update = function () {
if (!self.isAlive) return;
self.velocityY += self.gravity;
self.lastY = self.y;
self.y += self.velocityY;
if (self.lastY - 25 >= 0 && self.y - 25 < 0) {
self.isAlive = false;
LK.showGameOver();
}
if (self.y - 25 < 0) {
self.y = 25;
self.velocityY = 0;
}
};
return self;
});
var BirdSelector = Container.expand(function () {
var self = Container.call(this);
self.birdId = 0;
self.isUnlocked = false;
self.isSelected = false;
var selectorGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
selectorGraphics.width = 80;
selectorGraphics.height = 67;
var lockText = new Text2('LOCKED', {
size: 40,
fill: 0xFF0000
});
lockText.anchor.set(0.5, 0.5);
lockText.visible = false;
self.addChild(lockText);
self.setLocked = function (locked) {
self.isUnlocked = !locked;
lockText.visible = locked;
selectorGraphics.alpha = locked ? 0.3 : 1;
};
return self;
});
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -6;
self.isCollected = false;
self.update = function () {
self.x += self.speed;
};
return self;
});
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
self.x = 0;
self.y = 2582;
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -6;
self.gapSize = 250;
self.hasBeenScored = false;
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setup = function (gapY) {
self.gapY = gapY;
topPipe.y = gapY - self.gapSize / 2;
bottomPipe.y = gapY + self.gapSize / 2;
topPipe.x = 50;
bottomPipe.x = 50;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var skyGradient = new Container();
game.addChild(skyGradient);
var topSky = LK.getAsset('skyTop', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
skyGradient.addChild(topSky);
var bottomSky = LK.getAsset('skyBottom', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 1366
});
skyGradient.addChild(bottomSky);
var gameStarted = false;
var gamePaused = false;
var menuContainer = new Container();
game.addChild(menuContainer);
var titleTxt = new Text2('FLAPPY BIRD', {
size: 200,
fill: 0xFFFFFF
});
titleTxt.anchor.set(0.5, 0.5);
titleTxt.x = 1024;
titleTxt.y = 600;
menuContainer.addChild(titleTxt);
var instructionsTxt = new Text2('TAP TO FLAP', {
size: 120,
fill: 0xFFFFFF
});
instructionsTxt.anchor.set(0.5, 0.5);
instructionsTxt.x = 1024;
instructionsTxt.y = 1100;
menuContainer.addChild(instructionsTxt);
var birdSelectionTxt = new Text2('SELECT BIRD', {
size: 100,
fill: 0xFFFFFF
});
birdSelectionTxt.anchor.set(0.5, 0.5);
birdSelectionTxt.x = 1024;
birdSelectionTxt.y = 1250;
menuContainer.addChild(birdSelectionTxt);
var birdSelectorsContainer = new Container();
menuContainer.addChild(birdSelectorsContainer);
var unlockedBirds = storage.unlockedBirds || [1];
var totalCoinsCollected = storage.totalCoins || 0;
var birdSelectors = [];
for (var b = 1; b <= 5; b++) {
var birdSelector = new BirdSelector();
birdSelectorsContainer.addChild(birdSelector);
birdSelector.birdId = b;
birdSelector.x = 300 + (b - 1) * 350;
birdSelector.y = 1350;
var coinsNeeded = (b - 1) * 50;
var isBirdUnlocked = false;
for (var ub = 0; ub < unlockedBirds.length; ub++) {
if (unlockedBirds[ub] === b) {
isBirdUnlocked = true;
break;
}
}
birdSelector.setLocked(!isBirdUnlocked);
if (b === 1) {
birdSelector.isSelected = true;
}
var costTxt = new Text2(coinsNeeded + ' coins', {
size: 30,
fill: 0xFFFFFF
});
costTxt.anchor.set(0.5, 0.5);
costTxt.y = 50;
birdSelector.addChild(costTxt);
birdSelectors.push(birdSelector);
}
var levelSelectionTxt = new Text2('SELECT LEVEL', {
size: 100,
fill: 0xFFFFFF
});
levelSelectionTxt.anchor.set(0.5, 0.5);
levelSelectionTxt.x = 1024;
levelSelectionTxt.y = 1500;
menuContainer.addChild(levelSelectionTxt);
var levelButtonsContainer = new Container();
menuContainer.addChild(levelButtonsContainer);
var levelButtons = [];
for (var l = 1; l <= 5; l++) {
var levelBtn = new Container();
levelButtonsContainer.addChild(levelBtn);
var btnGraphics = levelBtn.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
btnGraphics.width = 150;
btnGraphics.height = 120;
btnGraphics.tint = 0x3498db;
levelBtn.x = 300 + (l - 1) * 350;
levelBtn.y = 1600;
levelBtn.levelNumber = l;
levelBtn.isSelected = l === 1;
var btnTxt = new Text2(String(l), {
size: 80,
fill: 0xFFFFFF
});
btnTxt.anchor.set(0.5, 0.5);
levelBtn.addChild(btnTxt);
levelButtons.push(levelBtn);
}
var tapToStartTxt = new Text2('TAP TO START', {
size: 100,
fill: 0xFFD700
});
tapToStartTxt.anchor.set(0.5, 0.5);
tapToStartTxt.x = 1024;
tapToStartTxt.y = 1750;
menuContainer.addChild(tapToStartTxt);
var bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366;
bird.lastY = bird.y;
bird.lastWasCollidingWithPipe = false;
bird.visible = false;
var ground = game.addChild(new Ground());
ground.visible = false;
var pipes = [];
var coins = [];
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 200;
var nextGapPosition = 0;
var currentLevel = 1;
var pipsPassedInLevel = 0;
var pipsRequiredToLevelUp = 5;
var coinsCollected = 0;
var scoreTxt = new Text2('Score: 0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.y = 20;
LK.gui.top.addChild(scoreTxt);
scoreTxt.visible = false;
var coinsTxt = new Text2('Total Coins: 0', {
size: 80,
fill: 0xFFD700
});
coinsTxt.anchor.set(0.5, 0);
coinsTxt.y = 150;
LK.gui.top.addChild(coinsTxt);
coinsTxt.visible = false;
var gameScore = 0;
var levelTxt = new Text2('Level 1', {
size: 100,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0.5, 0);
levelTxt.y = 150;
LK.gui.top.addChild(levelTxt);
levelTxt.visible = false;
function spawnPipe() {
var minGapY = 400;
var maxGapY = 1800;
var randomGapY = minGapY + Math.random() * (maxGapY - minGapY);
var newPipe = game.addChild(new Pipe());
newPipe.x = 2048 + 100;
var basePipeGapSize = newPipe.gapSize;
newPipe.gapSize = Math.max(120, basePipeGapSize - (currentLevel - 1) * 30);
newPipe.setup(randomGapY);
newPipe.lastX = newPipe.x;
newPipe.hasBeenScored = false;
pipes.push(newPipe);
var coinOffset = (Math.random() - 0.5) * 150;
var newCoin = game.addChild(new Coin());
newCoin.x = newPipe.x;
newCoin.y = randomGapY + coinOffset;
newCoin.lastX = newCoin.x;
newCoin.isCollected = false;
coins.push(newCoin);
}
game.down = function (x, y, obj) {
if (!gameStarted) {
var clickedBird = false;
for (var sb = 0; sb < birdSelectors.length; sb++) {
var selector = birdSelectors[sb];
var selectorBounds = selector.getBounds();
if (x >= selectorBounds.x && x <= selectorBounds.x + selectorBounds.width && y >= selectorBounds.y && y <= selectorBounds.y + selectorBounds.height) {
if (selector.isUnlocked) {
for (var dbs = 0; dbs < birdSelectors.length; dbs++) {
birdSelectors[dbs].isSelected = false;
birdSelectors[dbs].children[1].tint = 0xFFFFFF;
}
selector.isSelected = true;
selector.children[1].tint = 0xFFD700;
clickedBird = true;
} else {
var coinsNeeded = (selector.birdId - 1) * 50;
if (totalCoinsCollected >= coinsNeeded) {
var unlockedIndex = unlockedBirds.length;
unlockedBirds.push(selector.birdId);
storage.unlockedBirds = unlockedBirds;
selector.setLocked(false);
selector.isSelected = true;
for (var dus = 0; dus < birdSelectors.length; dus++) {
birdSelectors[dus].children[1].tint = 0xFFFFFF;
}
selector.children[1].tint = 0xFFD700;
clickedBird = true;
}
}
break;
}
}
var clickedLevel = false;
for (var b = 0; b < levelButtons.length; b++) {
var btn = levelButtons[b];
var buttonBounds = btn.getBounds();
if (x >= buttonBounds.x && x <= buttonBounds.x + buttonBounds.width && y >= buttonBounds.y && y <= buttonBounds.y + buttonBounds.height) {
for (var c = 0; c < levelButtons.length; c++) {
levelButtons[c].isSelected = false;
levelButtons[c].children[0].tint = 0xFFFFFF;
}
btn.isSelected = true;
btn.children[0].tint = 0xFFD700;
currentLevel = btn.levelNumber;
clickedLevel = true;
break;
}
}
if (!clickedBird && !clickedLevel) {
gameStarted = true;
gamePaused = false;
menuContainer.visible = false;
bird.visible = true;
ground.visible = true;
scoreTxt.visible = true;
levelTxt.visible = true;
coinsTxt.visible = true;
levelTxt.setText('Level ' + currentLevel);
pipsPassedInLevel = 0;
bird.gravity = 0.6 + (currentLevel - 1) * 0.15;
bird.flap();
}
} else if (bird.isAlive) {
bird.flap();
}
};
game.update = function () {
if (!gameStarted || gamePaused) return;
bird.update();
pipeSpawnTimer++;
var currentSpawnInterval = Math.max(80, pipeSpawnInterval - (currentLevel - 1) * 25);
if (pipeSpawnTimer >= currentSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
coin.update();
if (!coin.lastX) coin.lastX = coin.x;
if (coin.x < -100) {
coin.destroy();
coins.splice(i, 1);
}
if (bird.isAlive && !coin.isCollected) {
var coinCollisionDistance = Math.sqrt((bird.x - coin.x) * (bird.x - coin.x) + (bird.y - coin.y) * (bird.y - coin.y));
if (coinCollisionDistance < 50) {
coin.isCollected = true;
coinsCollected++;
totalCoinsCollected++;
storage.totalCoins = totalCoinsCollected;
coinsTxt.setText('Total Coins: ' + totalCoinsCollected);
LK.getSound('coin').play();
coin.destroy();
coins.splice(i, 1);
}
}
}
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
if (!pipe.lastX) pipe.lastX = pipe.x;
if (pipe.lastX > 300 && pipe.x <= 300 && !pipe.hasBeenScored) {
pipe.hasBeenScored = true;
gameScore++;
LK.setScore(gameScore);
pipsPassedInLevel++;
scoreTxt.setText('Score: ' + gameScore);
LK.getSound('score').play();
if (pipsPassedInLevel >= pipsRequiredToLevelUp) {
currentLevel++;
pipsPassedInLevel = 0;
levelTxt.setText('Level ' + currentLevel);
}
}
pipe.lastX = pipe.x;
if (pipe.x < -150) {
pipe.destroy();
pipes.splice(i, 1);
}
if (bird.isAlive && bird.lastWasCollidingWithPipe === undefined) {
bird.lastWasCollidingWithPipe = false;
}
if (bird.isAlive) {
var birdLeft = bird.x - 25;
var birdRight = bird.x + 25;
var birdTop = bird.y - 25;
var birdBottom = bird.y + 25;
var pipeLeft = pipe.x - 50;
var pipeRight = pipe.x + 50;
var isCollidingNow = false;
if (birdRight > pipeLeft && birdLeft < pipeRight) {
if (birdTop < pipe.gapY - pipe.gapSize / 2 || birdBottom > pipe.gapY + pipe.gapSize / 2) {
isCollidingNow = true;
}
}
if (!bird.lastWasCollidingWithPipe && isCollidingNow) {
bird.isAlive = false;
LK.showGameOver();
}
bird.lastWasCollidingWithPipe = isCollidingNow;
}
}
};
LK.playMusic('bgmusic', {
loop: true
});