/**** * 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.xSpeed = 10.9375; self.ySpeed = -20; self.gravity = 1; self.lift = -15; self.flap = function () { self.ySpeed = self.lift * 1.5; }; self._update_migrated = function () { if (game.isMouseDown) { self.ySpeed += self.gravity / 3; } else { self.ySpeed += self.gravity; } self.y += self.ySpeed; self.x += self.xSpeed; if (self.y <= 0 || self.y >= 2732) { self.speed = -self.speed; } var targetRotation = Math.atan2(self.ySpeed, self.xSpeed * self.scale.x) / 2; birdGraphics.rotation += (targetRotation - birdGraphics.rotation) / 10; }; self.lastFlowerCollision = {}; self.flip = function () { self.scale.x *= -1; }; }); var Flower = Container.expand(function () { var self = Container.call(this); var flowerStem = self.attachAsset('flowerStem', { anchorX: 0.5, anchorY: 0.5 }); var flowerHead = self.attachAsset('flower', { anchorX: 0.5, anchorY: 0.5 }); flowerHead.y = -1400; }); var FlowerObstacle = Container.expand(function () { var self = Container.call(this); var flowerHead = self.attachAsset('flower', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self._move_migrated = function (speed) { self.y += speed; }; return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); obstacleGraphics.rotation = Math.PI / 4; self.speed = 5; self._move_migrated = function (speed) { self.y += speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var tutorialTextWhite = new Text2('Tap to Flap\nHold to Float', { size: 150, fill: '#ffffff', font: 'Impact', align: 'center' }); tutorialTextWhite.anchor.set(.5, 1); tutorialTextWhite.x = -4; tutorialTextWhite.y = -62; LK.gui.bottom.addChild(tutorialTextWhite); var tutorialText = new Text2('Tap to Flap\nHold to Float', { size: 150, fill: '#3a84f7', font: 'Impact', dropShadow: true, dropShadowColor: '#222a9a', dropShadowBlur: 5, dropShadowDistance: 7, dropShadowAngle: 0, align: 'center' }); tutorialText.anchor.set(.5, 1); tutorialText.y = -50; LK.gui.bottom.addChild(tutorialText); game.score = 0; game.obstacleSpeed = 5; game.obstacleSpeedIncrease = 0.005; LK.playMusic('bgMusic'); game.checkObstacleCollision = function (obstacles) { for (var i = 0; i < obstacles.length; i++) { obstacles[i]._move_migrated(); var dist = Math.sqrt(Math.pow(bird.x - obstacles[i].x, 2) + Math.pow(bird.y - obstacles[i].y, 2)); if (dist < 280) { // Play obstacle hit sound LK.getSound('obstacleHit').play(); LK.setScore(game.score); LK.showGameOver(); } } }; game.setBackgroundColor(0xadd8e6); var scoreText = new Text2('0', { size: 150, fill: '#3a84f7', font: 'Impact', dropShadow: true, dropShadowColor: '#222a9a', dropShadowBlur: 5, dropShadowDistance: 7, dropShadowAngle: 0 }); scoreText.anchor.set(.5, 0); LK.gui.top.addChild(scoreText); var scoreText2 = new Text2('0', { size: 150, fill: '#ffffff', font: 'Impact' }); scoreText2.anchor.set(.5, 0); scoreText2.x = -4; scoreText2.y = -5; LK.gui.top.addChild(scoreText2); LK.gui.top.addChild(scoreText); var bird = game.addChild(new Bird()); var leftWall = game.addChild(new Flower()); leftWall.x = 0; leftWall.y = 1366; var rightWall = game.addChild(new Flower()); rightWall.x = 2048; rightWall.y = 1366; var leftObstacles = [], rightObstacles = [], leftFlowers = [], rightFlowers = []; var obstacleSpawnRandomness = 120; var obstacleSpawnRandomnessDecrease = 0.025 * (2 / 3); var obstacleSpawnY = -500; var leftObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness; var rightObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness; bird.x = 1024; bird.y = 1366; game.isMouseDown = false; game.down = function (x, y, obj) { bird.flap(); game.isMouseDown = true; }; game.up = function (x, y, obj) { game.isMouseDown = false; }; game.update = function () { bird._update_migrated(); if (game.score > 2) { tutorialText.y += 5; tutorialTextWhite.y += 5; } scoreText.setText(game.score); scoreText2.setText(game.score); game.obstacleSpeed += game.obstacleSpeedIncrease; obstacleSpawnRandomness -= obstacleSpawnRandomnessDecrease; if (obstacleSpawnRandomness < 20) { obstacleSpawnRandomness = 20; } if (LK.ticks >= leftObstacleSpawnTime) { var newObstacle = game.addChildAt(new Obstacle(), 0); newObstacle.x = 0; newObstacle.y = obstacleSpawnY; leftObstacles.push(newObstacle); if (leftObstacles.length > 1) { var prevObstacle = leftObstacles[leftObstacles.length - 2]; var flowerY = (prevObstacle.y + newObstacle.y) / 2; var flower = game.addChildAt(new FlowerObstacle(), 0); flower.x = 400; flower.y = flowerY; leftFlowers.push(flower); } leftObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness; } if (LK.ticks >= rightObstacleSpawnTime) { var newObstacle = game.addChildAt(new Obstacle(), 0); newObstacle.x = 2048; newObstacle.y = -newObstacle.height; rightObstacles.push(newObstacle); // Add flower between obstacles if (rightObstacles.length > 1) { var prevObstacle = rightObstacles[rightObstacles.length - 2]; var flowerY = (prevObstacle.y + newObstacle.y) / 2; var flower = game.addChildAt(new FlowerObstacle(), 0); flower.x = 1648; flower.y = flowerY; rightFlowers.push(flower); } rightObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness; } if (bird.intersects(leftWall) && bird.xSpeed < 0 || bird.intersects(rightWall) && bird.xSpeed > 0) { bird.xSpeed = -bird.xSpeed; bird.flip(); game.score++; LK.setScore(game.score); } for (var i = leftObstacles.length - 1; i >= 0; i--) { leftObstacles[i]._move_migrated(game.obstacleSpeed); if (leftObstacles[i].y > 3232) { leftObstacles[i].destroy(); leftObstacles.splice(i, 1); } } // Move and remove flowers that are off-screen for (var i = leftFlowers.length - 1; i >= 0; i--) { leftFlowers[i]._move_migrated(game.obstacleSpeed); if (leftFlowers[i].y > 3232) { leftFlowers[i].destroy(); leftFlowers.splice(i, 1); } } for (var i = rightObstacles.length - 1; i >= 0; i--) { rightObstacles[i]._move_migrated(game.obstacleSpeed); if (rightObstacles[i].y > 3232) { rightObstacles[i].destroy(); rightObstacles.splice(i, 1); } } // Move and remove flowers that are off-screen for (var i = rightFlowers.length - 1; i >= 0; i--) { rightFlowers[i]._move_migrated(game.obstacleSpeed); if (rightFlowers[i].y > 3232) { rightFlowers[i].destroy(); rightFlowers.splice(i, 1); } } // Check flower collisions game.checkFlowerCollision = function (flowers) { for (var i = 0; i < flowers.length; i++) { var dist = Math.sqrt(Math.pow(bird.x - flowers[i].x, 2) + Math.pow(bird.y - flowers[i].y, 2)); // Using a unique key for each flower to prevent multiple collisions var flowerKey = flowers[i].x + "_" + flowers[i].y; if (dist < 220 && !bird.lastFlowerCollision[flowerKey]) { // Set score to add exactly 2 points (not more) game.score = game.score + 2; // Update score display scoreText.setText(game.score); scoreText2.setText(game.score); // Play flower collection sound LK.getSound('flowerCollect').play(); // Flash the flower gold var currentFlower = flowers[i]; // Store reference to current flower if (currentFlower) { tween(currentFlower, { alpha: 0.5 }, { duration: 200, onFinish: function onFinish() { // Check if flower still exists before tweening again if (currentFlower && currentFlower.parent) { tween(currentFlower, { alpha: 1 }, { duration: 200 }); } } }); } // Track this collision bird.lastFlowerCollision[flowerKey] = true; } } }; game.checkObstacleCollision(leftObstacles); game.checkObstacleCollision(rightObstacles); game.checkFlowerCollision(leftFlowers); game.checkFlowerCollision(rightFlowers); if (bird.y < 0 || bird.y > 2732) { // Play obstacle hit sound when bird hits boundaries LK.getSound('obstacleHit').play(); LK.setScore(game.score); 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.xSpeed = 10.9375;
self.ySpeed = -20;
self.gravity = 1;
self.lift = -15;
self.flap = function () {
self.ySpeed = self.lift * 1.5;
};
self._update_migrated = function () {
if (game.isMouseDown) {
self.ySpeed += self.gravity / 3;
} else {
self.ySpeed += self.gravity;
}
self.y += self.ySpeed;
self.x += self.xSpeed;
if (self.y <= 0 || self.y >= 2732) {
self.speed = -self.speed;
}
var targetRotation = Math.atan2(self.ySpeed, self.xSpeed * self.scale.x) / 2;
birdGraphics.rotation += (targetRotation - birdGraphics.rotation) / 10;
};
self.lastFlowerCollision = {};
self.flip = function () {
self.scale.x *= -1;
};
});
var Flower = Container.expand(function () {
var self = Container.call(this);
var flowerStem = self.attachAsset('flowerStem', {
anchorX: 0.5,
anchorY: 0.5
});
var flowerHead = self.attachAsset('flower', {
anchorX: 0.5,
anchorY: 0.5
});
flowerHead.y = -1400;
});
var FlowerObstacle = Container.expand(function () {
var self = Container.call(this);
var flowerHead = self.attachAsset('flower', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self._move_migrated = function (speed) {
self.y += speed;
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
obstacleGraphics.rotation = Math.PI / 4;
self.speed = 5;
self._move_migrated = function (speed) {
self.y += speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var tutorialTextWhite = new Text2('Tap to Flap\nHold to Float', {
size: 150,
fill: '#ffffff',
font: 'Impact',
align: 'center'
});
tutorialTextWhite.anchor.set(.5, 1);
tutorialTextWhite.x = -4;
tutorialTextWhite.y = -62;
LK.gui.bottom.addChild(tutorialTextWhite);
var tutorialText = new Text2('Tap to Flap\nHold to Float', {
size: 150,
fill: '#3a84f7',
font: 'Impact',
dropShadow: true,
dropShadowColor: '#222a9a',
dropShadowBlur: 5,
dropShadowDistance: 7,
dropShadowAngle: 0,
align: 'center'
});
tutorialText.anchor.set(.5, 1);
tutorialText.y = -50;
LK.gui.bottom.addChild(tutorialText);
game.score = 0;
game.obstacleSpeed = 5;
game.obstacleSpeedIncrease = 0.005;
LK.playMusic('bgMusic');
game.checkObstacleCollision = function (obstacles) {
for (var i = 0; i < obstacles.length; i++) {
obstacles[i]._move_migrated();
var dist = Math.sqrt(Math.pow(bird.x - obstacles[i].x, 2) + Math.pow(bird.y - obstacles[i].y, 2));
if (dist < 280) {
// Play obstacle hit sound
LK.getSound('obstacleHit').play();
LK.setScore(game.score);
LK.showGameOver();
}
}
};
game.setBackgroundColor(0xadd8e6);
var scoreText = new Text2('0', {
size: 150,
fill: '#3a84f7',
font: 'Impact',
dropShadow: true,
dropShadowColor: '#222a9a',
dropShadowBlur: 5,
dropShadowDistance: 7,
dropShadowAngle: 0
});
scoreText.anchor.set(.5, 0);
LK.gui.top.addChild(scoreText);
var scoreText2 = new Text2('0', {
size: 150,
fill: '#ffffff',
font: 'Impact'
});
scoreText2.anchor.set(.5, 0);
scoreText2.x = -4;
scoreText2.y = -5;
LK.gui.top.addChild(scoreText2);
LK.gui.top.addChild(scoreText);
var bird = game.addChild(new Bird());
var leftWall = game.addChild(new Flower());
leftWall.x = 0;
leftWall.y = 1366;
var rightWall = game.addChild(new Flower());
rightWall.x = 2048;
rightWall.y = 1366;
var leftObstacles = [],
rightObstacles = [],
leftFlowers = [],
rightFlowers = [];
var obstacleSpawnRandomness = 120;
var obstacleSpawnRandomnessDecrease = 0.025 * (2 / 3);
var obstacleSpawnY = -500;
var leftObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
var rightObstacleSpawnTime = Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
bird.x = 1024;
bird.y = 1366;
game.isMouseDown = false;
game.down = function (x, y, obj) {
bird.flap();
game.isMouseDown = true;
};
game.up = function (x, y, obj) {
game.isMouseDown = false;
};
game.update = function () {
bird._update_migrated();
if (game.score > 2) {
tutorialText.y += 5;
tutorialTextWhite.y += 5;
}
scoreText.setText(game.score);
scoreText2.setText(game.score);
game.obstacleSpeed += game.obstacleSpeedIncrease;
obstacleSpawnRandomness -= obstacleSpawnRandomnessDecrease;
if (obstacleSpawnRandomness < 20) {
obstacleSpawnRandomness = 20;
}
if (LK.ticks >= leftObstacleSpawnTime) {
var newObstacle = game.addChildAt(new Obstacle(), 0);
newObstacle.x = 0;
newObstacle.y = obstacleSpawnY;
leftObstacles.push(newObstacle);
if (leftObstacles.length > 1) {
var prevObstacle = leftObstacles[leftObstacles.length - 2];
var flowerY = (prevObstacle.y + newObstacle.y) / 2;
var flower = game.addChildAt(new FlowerObstacle(), 0);
flower.x = 400;
flower.y = flowerY;
leftFlowers.push(flower);
}
leftObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
}
if (LK.ticks >= rightObstacleSpawnTime) {
var newObstacle = game.addChildAt(new Obstacle(), 0);
newObstacle.x = 2048;
newObstacle.y = -newObstacle.height;
rightObstacles.push(newObstacle);
// Add flower between obstacles
if (rightObstacles.length > 1) {
var prevObstacle = rightObstacles[rightObstacles.length - 2];
var flowerY = (prevObstacle.y + newObstacle.y) / 2;
var flower = game.addChildAt(new FlowerObstacle(), 0);
flower.x = 1648;
flower.y = flowerY;
rightFlowers.push(flower);
}
rightObstacleSpawnTime += Math.floor(Math.random() * obstacleSpawnRandomness) + obstacleSpawnRandomness;
}
if (bird.intersects(leftWall) && bird.xSpeed < 0 || bird.intersects(rightWall) && bird.xSpeed > 0) {
bird.xSpeed = -bird.xSpeed;
bird.flip();
game.score++;
LK.setScore(game.score);
}
for (var i = leftObstacles.length - 1; i >= 0; i--) {
leftObstacles[i]._move_migrated(game.obstacleSpeed);
if (leftObstacles[i].y > 3232) {
leftObstacles[i].destroy();
leftObstacles.splice(i, 1);
}
}
// Move and remove flowers that are off-screen
for (var i = leftFlowers.length - 1; i >= 0; i--) {
leftFlowers[i]._move_migrated(game.obstacleSpeed);
if (leftFlowers[i].y > 3232) {
leftFlowers[i].destroy();
leftFlowers.splice(i, 1);
}
}
for (var i = rightObstacles.length - 1; i >= 0; i--) {
rightObstacles[i]._move_migrated(game.obstacleSpeed);
if (rightObstacles[i].y > 3232) {
rightObstacles[i].destroy();
rightObstacles.splice(i, 1);
}
}
// Move and remove flowers that are off-screen
for (var i = rightFlowers.length - 1; i >= 0; i--) {
rightFlowers[i]._move_migrated(game.obstacleSpeed);
if (rightFlowers[i].y > 3232) {
rightFlowers[i].destroy();
rightFlowers.splice(i, 1);
}
}
// Check flower collisions
game.checkFlowerCollision = function (flowers) {
for (var i = 0; i < flowers.length; i++) {
var dist = Math.sqrt(Math.pow(bird.x - flowers[i].x, 2) + Math.pow(bird.y - flowers[i].y, 2));
// Using a unique key for each flower to prevent multiple collisions
var flowerKey = flowers[i].x + "_" + flowers[i].y;
if (dist < 220 && !bird.lastFlowerCollision[flowerKey]) {
// Set score to add exactly 2 points (not more)
game.score = game.score + 2;
// Update score display
scoreText.setText(game.score);
scoreText2.setText(game.score);
// Play flower collection sound
LK.getSound('flowerCollect').play();
// Flash the flower gold
var currentFlower = flowers[i]; // Store reference to current flower
if (currentFlower) {
tween(currentFlower, {
alpha: 0.5
}, {
duration: 200,
onFinish: function onFinish() {
// Check if flower still exists before tweening again
if (currentFlower && currentFlower.parent) {
tween(currentFlower, {
alpha: 1
}, {
duration: 200
});
}
}
});
}
// Track this collision
bird.lastFlowerCollision[flowerKey] = true;
}
}
};
game.checkObstacleCollision(leftObstacles);
game.checkObstacleCollision(rightObstacles);
game.checkFlowerCollision(leftFlowers);
game.checkFlowerCollision(rightFlowers);
if (bird.y < 0 || bird.y > 2732) {
// Play obstacle hit sound when bird hits boundaries
LK.getSound('obstacleHit').play();
LK.setScore(game.score);
LK.showGameOver();
}
};
Create a flower. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Create some cactus. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Create a butterfly. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows