/****
* 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.3;
self.flapPower = -12;
self.maxFallSpeed = 15;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add a small rotation animation when flapping
tween(birdGraphics, {
rotation: -0.3
}, {
duration: 150
});
tween(birdGraphics, {
rotation: 0
}, {
duration: 300
});
};
self.update = function () {
self.velocity += self.gravity;
if (self.velocity > self.maxFallSpeed) {
self.velocity = self.maxFallSpeed;
}
self.y += self.velocity;
// Rotate bird based on velocity
birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
};
return self;
});
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
self.speed = -4;
self.update = function () {
self.x += self.speed;
// Reset position for infinite scrolling
if (self.x <= -2048) {
self.x = 0;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -4;
self.scored = false;
// Create top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
// Create bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setup = function (gapY, gapSize) {
self.topPipe.y = gapY - gapSize / 2;
self.bottomPipe.y = gapY + gapSize / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game constants
var PIPE_GAP_SIZE = 300;
var PIPE_SPACING = 400;
var GROUND_Y = 2632;
// Game variables
var bird;
var pipes = [];
var grounds = [];
var gameStarted = false;
var pipeSpawnTimer = 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 START', {
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;
// Create ground segments for infinite scrolling
for (var i = 0; i < 2; i++) {
var ground = game.addChild(new Ground());
ground.x = i * 2048;
ground.y = GROUND_Y;
grounds.push(ground);
}
// Helper functions
function spawnPipe() {
var pipe = new Pipe();
var gapY = Math.random() * (GROUND_Y - 400 - PIPE_GAP_SIZE) + 200 + PIPE_GAP_SIZE / 2;
pipe.x = 2048 + 60;
pipe.setup(gapY, PIPE_GAP_SIZE);
pipes.push(pipe);
game.addChild(pipe);
}
function startGame() {
gameStarted = true;
instructionTxt.visible = false;
bird.flap();
}
function checkCollisions() {
// Check ground collision
if (bird.y + 22 >= GROUND_Y) {
return true;
}
// Check ceiling collision
if (bird.y - 22 <= 0) {
return true;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
var pipeLeft = pipe.x - 60;
var pipeRight = pipe.x + 60;
var birdLeft = bird.x - 30;
var birdRight = bird.x + 30;
var birdTop = bird.y - 22;
var birdBottom = bird.y + 22;
// Check if bird is within pipe's x range
if (birdRight > pipeLeft && birdLeft < pipeRight) {
// Check top pipe collision
if (birdTop < pipe.topPipe.y) {
return true;
}
// Check bottom pipe collision
if (birdBottom > pipe.bottomPipe.y) {
return true;
}
}
// Check for scoring
if (!pipe.scored && bird.x > pipe.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
}
return false;
}
// Event handlers
game.down = function (x, y, obj) {
if (!gameStarted) {
startGame();
} else {
bird.flap();
}
};
// Main game loop
game.update = function () {
if (!gameStarted) {
return;
}
// Update bird
bird.update();
// Update ground
for (var i = 0; i < grounds.length; i++) {
grounds[i].update();
}
// Update pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove pipes that are off-screen
if (pipe.x < -150) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Spawn new pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= 100) {
// Spawn every ~1.67 seconds at 60fps
spawnPipe();
pipeSpawnTimer = 0;
}
// Check collisions
if (checkCollisions()) {
LK.getSound('hit').play();
LK.showGameOver();
}
}; /****
* 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.3;
self.flapPower = -12;
self.maxFallSpeed = 15;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add a small rotation animation when flapping
tween(birdGraphics, {
rotation: -0.3
}, {
duration: 150
});
tween(birdGraphics, {
rotation: 0
}, {
duration: 300
});
};
self.update = function () {
self.velocity += self.gravity;
if (self.velocity > self.maxFallSpeed) {
self.velocity = self.maxFallSpeed;
}
self.y += self.velocity;
// Rotate bird based on velocity
birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
};
return self;
});
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
self.speed = -4;
self.update = function () {
self.x += self.speed;
// Reset position for infinite scrolling
if (self.x <= -2048) {
self.x = 0;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -4;
self.scored = false;
// Create top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
// Create bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setup = function (gapY, gapSize) {
self.topPipe.y = gapY - gapSize / 2;
self.bottomPipe.y = gapY + gapSize / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game constants
var PIPE_GAP_SIZE = 300;
var PIPE_SPACING = 400;
var GROUND_Y = 2632;
// Game variables
var bird;
var pipes = [];
var grounds = [];
var gameStarted = false;
var pipeSpawnTimer = 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 START', {
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;
// Create ground segments for infinite scrolling
for (var i = 0; i < 2; i++) {
var ground = game.addChild(new Ground());
ground.x = i * 2048;
ground.y = GROUND_Y;
grounds.push(ground);
}
// Helper functions
function spawnPipe() {
var pipe = new Pipe();
var gapY = Math.random() * (GROUND_Y - 400 - PIPE_GAP_SIZE) + 200 + PIPE_GAP_SIZE / 2;
pipe.x = 2048 + 60;
pipe.setup(gapY, PIPE_GAP_SIZE);
pipes.push(pipe);
game.addChild(pipe);
}
function startGame() {
gameStarted = true;
instructionTxt.visible = false;
bird.flap();
}
function checkCollisions() {
// Check ground collision
if (bird.y + 22 >= GROUND_Y) {
return true;
}
// Check ceiling collision
if (bird.y - 22 <= 0) {
return true;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
var pipeLeft = pipe.x - 60;
var pipeRight = pipe.x + 60;
var birdLeft = bird.x - 30;
var birdRight = bird.x + 30;
var birdTop = bird.y - 22;
var birdBottom = bird.y + 22;
// Check if bird is within pipe's x range
if (birdRight > pipeLeft && birdLeft < pipeRight) {
// Check top pipe collision
if (birdTop < pipe.topPipe.y) {
return true;
}
// Check bottom pipe collision
if (birdBottom > pipe.bottomPipe.y) {
return true;
}
}
// Check for scoring
if (!pipe.scored && bird.x > pipe.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
}
return false;
}
// Event handlers
game.down = function (x, y, obj) {
if (!gameStarted) {
startGame();
} else {
bird.flap();
}
};
// Main game loop
game.update = function () {
if (!gameStarted) {
return;
}
// Update bird
bird.update();
// Update ground
for (var i = 0; i < grounds.length; i++) {
grounds[i].update();
}
// Update pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove pipes that are off-screen
if (pipe.x < -150) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Spawn new pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= 100) {
// Spawn every ~1.67 seconds at 60fps
spawnPipe();
pipeSpawnTimer = 0;
}
// Check collisions
if (checkCollisions()) {
LK.getSound('hit').play();
LK.showGameOver();
}
};