/****
* Classes
****/
var Fly = Container.expand(function () {
var self = Container.call(this);
var flyGraphics = self.attachAsset('fly', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = 0;
self.gravity = 0.8;
self.jumpPower = -15;
self.jump = function () {
self.velocity = self.jumpPower;
LK.getSound('jump').play();
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate fly based on velocity for realistic physics
// When falling (positive velocity), rotate down
// When rising (negative velocity), rotate up
flyGraphics.rotation = self.velocity * 0.03; // Scale rotation based on velocity
// Prevent fly from going off screen
if (self.y < 50) {
self.y = 50;
self.velocity = 0;
}
if (self.y > 2680) {
self.y = 2680;
self.velocity = 0;
}
};
return self;
});
var ObstaclePair = Container.expand(function () {
var self = Container.call(this);
var gapSize = 600;
var gapPosition = Math.random() * 1000 + 600; // Random gap between 600-1600
var topObstacle = self.attachAsset('obstacleTop', {
anchorX: 0.5,
anchorY: 0
});
topObstacle.y = 0; // Attach to top of screen
topObstacle.height = gapPosition - gapSize / 2; // Extend down to gap start
var bottomObstacle = self.attachAsset('obstacleBottom', {
anchorX: 0.5,
anchorY: 0
});
bottomObstacle.y = gapPosition + gapSize / 2; // Start at gap end
bottomObstacle.height = 2732 - (gapPosition + gapSize / 2); // Extend to bottom of screen
self.speed = -5;
self.scored = false;
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var fly = game.addChild(new Fly());
fly.x = 300;
fly.y = 1366;
var obstacles = [];
var obstacleSpacing = 700;
var nextObstacleX = 2048;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Spawn obstacles continuously
function spawnObstacle() {
var obstacle = new ObstaclePair();
obstacle.x = nextObstacleX;
obstacles.push(obstacle);
game.addChild(obstacle);
nextObstacleX += obstacleSpacing;
}
// Initial obstacles to fill screen
for (var i = 0; i < 6; i++) {
spawnObstacle();
}
game.down = function (x, y, obj) {
fly.jump();
};
game.update = function () {
// Continuously spawn new obstacles to maintain flow
var rightmostObstacle = -999999;
for (var j = 0; j < obstacles.length; j++) {
if (obstacles[j].x > rightmostObstacle) {
rightmostObstacle = obstacles[j].x;
}
}
// Keep spawning obstacles to maintain continuous flow
while (rightmostObstacle < 2048 + obstacleSpacing) {
var obstacle = new ObstaclePair();
obstacle.x = rightmostObstacle + obstacleSpacing;
obstacles.push(obstacle);
game.addChild(obstacle);
rightmostObstacle = obstacle.x;
}
// Check collisions and scoring
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
// Remove obstacles that are off screen
if (obstacle.x < -200) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Check for scoring
if (!obstacle.scored && obstacle.x < fly.x) {
obstacle.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
// Check for collision
if (fly.intersects(obstacle)) {
LK.showGameOver();
return;
}
}
// Check if fly hits ground or ceiling
if (fly.y >= 2680 || fly.y <= 50) {
LK.showGameOver();
return;
}
}; /****
* Classes
****/
var Fly = Container.expand(function () {
var self = Container.call(this);
var flyGraphics = self.attachAsset('fly', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = 0;
self.gravity = 0.8;
self.jumpPower = -15;
self.jump = function () {
self.velocity = self.jumpPower;
LK.getSound('jump').play();
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate fly based on velocity for realistic physics
// When falling (positive velocity), rotate down
// When rising (negative velocity), rotate up
flyGraphics.rotation = self.velocity * 0.03; // Scale rotation based on velocity
// Prevent fly from going off screen
if (self.y < 50) {
self.y = 50;
self.velocity = 0;
}
if (self.y > 2680) {
self.y = 2680;
self.velocity = 0;
}
};
return self;
});
var ObstaclePair = Container.expand(function () {
var self = Container.call(this);
var gapSize = 600;
var gapPosition = Math.random() * 1000 + 600; // Random gap between 600-1600
var topObstacle = self.attachAsset('obstacleTop', {
anchorX: 0.5,
anchorY: 0
});
topObstacle.y = 0; // Attach to top of screen
topObstacle.height = gapPosition - gapSize / 2; // Extend down to gap start
var bottomObstacle = self.attachAsset('obstacleBottom', {
anchorX: 0.5,
anchorY: 0
});
bottomObstacle.y = gapPosition + gapSize / 2; // Start at gap end
bottomObstacle.height = 2732 - (gapPosition + gapSize / 2); // Extend to bottom of screen
self.speed = -5;
self.scored = false;
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var fly = game.addChild(new Fly());
fly.x = 300;
fly.y = 1366;
var obstacles = [];
var obstacleSpacing = 700;
var nextObstacleX = 2048;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Spawn obstacles continuously
function spawnObstacle() {
var obstacle = new ObstaclePair();
obstacle.x = nextObstacleX;
obstacles.push(obstacle);
game.addChild(obstacle);
nextObstacleX += obstacleSpacing;
}
// Initial obstacles to fill screen
for (var i = 0; i < 6; i++) {
spawnObstacle();
}
game.down = function (x, y, obj) {
fly.jump();
};
game.update = function () {
// Continuously spawn new obstacles to maintain flow
var rightmostObstacle = -999999;
for (var j = 0; j < obstacles.length; j++) {
if (obstacles[j].x > rightmostObstacle) {
rightmostObstacle = obstacles[j].x;
}
}
// Keep spawning obstacles to maintain continuous flow
while (rightmostObstacle < 2048 + obstacleSpacing) {
var obstacle = new ObstaclePair();
obstacle.x = rightmostObstacle + obstacleSpacing;
obstacles.push(obstacle);
game.addChild(obstacle);
rightmostObstacle = obstacle.x;
}
// Check collisions and scoring
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
// Remove obstacles that are off screen
if (obstacle.x < -200) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Check for scoring
if (!obstacle.scored && obstacle.x < fly.x) {
obstacle.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
// Check for collision
if (fly.intersects(obstacle)) {
LK.showGameOver();
return;
}
}
// Check if fly hits ground or ceiling
if (fly.y >= 2680 || fly.y <= 50) {
LK.showGameOver();
return;
}
};