/****
* 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.flapPower = -12;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add a little bounce animation
tween(birdGraphics, {
rotation: -0.3
}, {
duration: 100
});
tween(birdGraphics, {
rotation: 0
}, {
duration: 200
});
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate bird based on velocity
birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
// Check ground collision
if (self.y > 2732 - 150 && !invulnerable) {
lives--;
livesText.setText('Lives: ' + lives);
if (lives <= 0) {
skullText.alpha = 1; // Show skull emoji
tween(skullText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 500
});
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
} else {
// Reset bird position and velocity
self.y = 1366;
self.velocity = 0;
// Start invulnerability period
invulnerable = true;
invulnerabilityTimer = 0;
// Visual feedback for invulnerability
tween(birdGraphics, {
alpha: 0.5
}, {
duration: 100
});
LK.effects.flashScreen(0xffffff, 250);
}
}
// Check ceiling collision
if (self.y < 0) {
self.y = 0;
self.velocity = 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.setGapPosition = function (gapY, gapSize) {
self.topPipe.y = gapY - gapSize / 2;
self.bottomPipe.y = gapY + gapSize / 2;
};
self.update = function () {
self.x += self.speed;
// Remove pipe when off screen
if (self.x < -100) {
self.shouldRemove = true;
}
// Check scoring
if (!self.scored && self.x < bird.x) {
self.scored = true;
LK.setScore(LK.getScore() + 1);
scoreText.setText(LK.getScore());
LK.getSound('score').play();
}
};
self.checkCollision = function (bird) {
var birdBounds = {
x: bird.x - 30,
y: bird.y - 30,
width: 60,
height: 60
};
var topPipeBounds = {
x: self.x - 60,
y: self.topPipe.y - 800,
width: 120,
height: 800
};
var bottomPipeBounds = {
x: self.x - 60,
y: self.bottomPipe.y,
width: 120,
height: 800
};
return birdBounds.x < topPipeBounds.x + topPipeBounds.width && birdBounds.x + birdBounds.width > topPipeBounds.x && birdBounds.y < topPipeBounds.y + topPipeBounds.height && birdBounds.y + birdBounds.height > topPipeBounds.y || birdBounds.x < bottomPipeBounds.x + bottomPipeBounds.width && birdBounds.x + birdBounds.width > bottomPipeBounds.x && birdBounds.y < bottomPipeBounds.y + bottomPipeBounds.height && birdBounds.y + birdBounds.height > bottomPipeBounds.y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var bird;
var pipes = [];
var ground;
var scoreText;
var livesText;
var lives = 10;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 90; // Spawn pipe every 1.5 seconds at 60fps
var gapSize = 300;
var invulnerable = false;
var invulnerabilityTimer = 0;
var invulnerabilityDuration = 60; // 1 second at 60fps
var skullText;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 2732 - 100
}));
// Create bird
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366; // Middle of screen
// Create score display
scoreText = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreText.y = 100;
// Create star icon next to score
var starIcon = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
LK.gui.top.addChild(starIcon);
starIcon.x = scoreText.x - 80;
starIcon.y = scoreText.y + 60;
// Create lives display
livesText = new Text2('Lives: 10', {
size: 80,
fill: 0xFFFFFF
});
livesText.anchor.set(1, 0);
LK.gui.topRight.addChild(livesText);
livesText.x = -120;
livesText.y = 50;
// Create skull emoji (initially hidden)
skullText = new Text2('đ', {
size: 150,
fill: 0xFF0000
});
skullText.anchor.set(0.5, 0.5);
LK.gui.center.addChild(skullText);
skullText.alpha = 0; // Initially hidden
// Input handling
game.down = function (x, y, obj) {
bird.flap();
};
// Spawn pipes
function spawnPipe() {
var pipe = game.addChild(new Pipe());
pipe.x = 2148; // Start off screen to the right
// Random gap position
var minGapY = gapSize / 2 + 100;
var maxGapY = 2732 - 100 - gapSize / 2;
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapY, gapSize);
pipes.push(pipe);
}
// Main game loop
game.update = function () {
// Spawn pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
// Update pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
// Check collision
if (pipe.checkCollision(bird) && !invulnerable) {
lives--;
livesText.setText('Lives: ' + lives);
if (lives <= 0) {
skullText.alpha = 1; // Show skull emoji
tween(skullText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 500
});
LK.effects.flashScreen(0xff0000, 500);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
return;
} else {
// Reset bird position and velocity
bird.y = 1366;
bird.velocity = 0;
// Start invulnerability period
invulnerable = true;
invulnerabilityTimer = 0;
// Visual feedback for invulnerability
tween(bird.children[0], {
alpha: 0.5
}, {
duration: 100
});
LK.effects.flashScreen(0xff0000, 500);
}
}
// Remove pipes that are off screen
if (pipe.shouldRemove) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Handle invulnerability timer
if (invulnerable) {
invulnerabilityTimer++;
// Flash effect during invulnerability
if (invulnerabilityTimer % 10 < 5) {
bird.children[0].alpha = 0.3;
} else {
bird.children[0].alpha = 0.7;
}
// End invulnerability after duration
if (invulnerabilityTimer >= invulnerabilityDuration) {
invulnerable = false;
invulnerabilityTimer = 0;
tween(bird.children[0], {
alpha: 1
}, {
duration: 200
});
}
}
}; /****
* 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.flapPower = -12;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add a little bounce animation
tween(birdGraphics, {
rotation: -0.3
}, {
duration: 100
});
tween(birdGraphics, {
rotation: 0
}, {
duration: 200
});
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate bird based on velocity
birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
// Check ground collision
if (self.y > 2732 - 150 && !invulnerable) {
lives--;
livesText.setText('Lives: ' + lives);
if (lives <= 0) {
skullText.alpha = 1; // Show skull emoji
tween(skullText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 500
});
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
} else {
// Reset bird position and velocity
self.y = 1366;
self.velocity = 0;
// Start invulnerability period
invulnerable = true;
invulnerabilityTimer = 0;
// Visual feedback for invulnerability
tween(birdGraphics, {
alpha: 0.5
}, {
duration: 100
});
LK.effects.flashScreen(0xffffff, 250);
}
}
// Check ceiling collision
if (self.y < 0) {
self.y = 0;
self.velocity = 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.setGapPosition = function (gapY, gapSize) {
self.topPipe.y = gapY - gapSize / 2;
self.bottomPipe.y = gapY + gapSize / 2;
};
self.update = function () {
self.x += self.speed;
// Remove pipe when off screen
if (self.x < -100) {
self.shouldRemove = true;
}
// Check scoring
if (!self.scored && self.x < bird.x) {
self.scored = true;
LK.setScore(LK.getScore() + 1);
scoreText.setText(LK.getScore());
LK.getSound('score').play();
}
};
self.checkCollision = function (bird) {
var birdBounds = {
x: bird.x - 30,
y: bird.y - 30,
width: 60,
height: 60
};
var topPipeBounds = {
x: self.x - 60,
y: self.topPipe.y - 800,
width: 120,
height: 800
};
var bottomPipeBounds = {
x: self.x - 60,
y: self.bottomPipe.y,
width: 120,
height: 800
};
return birdBounds.x < topPipeBounds.x + topPipeBounds.width && birdBounds.x + birdBounds.width > topPipeBounds.x && birdBounds.y < topPipeBounds.y + topPipeBounds.height && birdBounds.y + birdBounds.height > topPipeBounds.y || birdBounds.x < bottomPipeBounds.x + bottomPipeBounds.width && birdBounds.x + birdBounds.width > bottomPipeBounds.x && birdBounds.y < bottomPipeBounds.y + bottomPipeBounds.height && birdBounds.y + birdBounds.height > bottomPipeBounds.y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var bird;
var pipes = [];
var ground;
var scoreText;
var livesText;
var lives = 10;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 90; // Spawn pipe every 1.5 seconds at 60fps
var gapSize = 300;
var invulnerable = false;
var invulnerabilityTimer = 0;
var invulnerabilityDuration = 60; // 1 second at 60fps
var skullText;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 2732 - 100
}));
// Create bird
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366; // Middle of screen
// Create score display
scoreText = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreText.y = 100;
// Create star icon next to score
var starIcon = LK.getAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
LK.gui.top.addChild(starIcon);
starIcon.x = scoreText.x - 80;
starIcon.y = scoreText.y + 60;
// Create lives display
livesText = new Text2('Lives: 10', {
size: 80,
fill: 0xFFFFFF
});
livesText.anchor.set(1, 0);
LK.gui.topRight.addChild(livesText);
livesText.x = -120;
livesText.y = 50;
// Create skull emoji (initially hidden)
skullText = new Text2('đ', {
size: 150,
fill: 0xFF0000
});
skullText.anchor.set(0.5, 0.5);
LK.gui.center.addChild(skullText);
skullText.alpha = 0; // Initially hidden
// Input handling
game.down = function (x, y, obj) {
bird.flap();
};
// Spawn pipes
function spawnPipe() {
var pipe = game.addChild(new Pipe());
pipe.x = 2148; // Start off screen to the right
// Random gap position
var minGapY = gapSize / 2 + 100;
var maxGapY = 2732 - 100 - gapSize / 2;
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapY, gapSize);
pipes.push(pipe);
}
// Main game loop
game.update = function () {
// Spawn pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
// Update pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
// Check collision
if (pipe.checkCollision(bird) && !invulnerable) {
lives--;
livesText.setText('Lives: ' + lives);
if (lives <= 0) {
skullText.alpha = 1; // Show skull emoji
tween(skullText, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 500
});
LK.effects.flashScreen(0xff0000, 500);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
return;
} else {
// Reset bird position and velocity
bird.y = 1366;
bird.velocity = 0;
// Start invulnerability period
invulnerable = true;
invulnerabilityTimer = 0;
// Visual feedback for invulnerability
tween(bird.children[0], {
alpha: 0.5
}, {
duration: 100
});
LK.effects.flashScreen(0xff0000, 500);
}
}
// Remove pipes that are off screen
if (pipe.shouldRemove) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Handle invulnerability timer
if (invulnerable) {
invulnerabilityTimer++;
// Flash effect during invulnerability
if (invulnerabilityTimer % 10 < 5) {
bird.children[0].alpha = 0.3;
} else {
bird.children[0].alpha = 0.7;
}
// End invulnerability after duration
if (invulnerabilityTimer >= invulnerabilityDuration) {
invulnerable = false;
invulnerabilityTimer = 0;
tween(bird.children[0], {
alpha: 1
}, {
duration: 200
});
}
}
};