/**** * Classes ****/ // Assets will be automatically created and loaded by the LK engine based on their usage in the code. // Bird class to represent the player character var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = LK.getAsset('bird_frame_0', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(birdGraphics); self.velocity = 0; self.gravity = 0.5; self.lift = -10; self.update = function () { self.velocity += self.gravity; if (self.flapPressed) { self.velocity = self.lift; self.flapPressed = false; } self.y += self.velocity; if (self.y > 2732 - birdGraphics.height / 2) { self.y = 2732 - birdGraphics.height / 2; self.velocity = 0; } if (self.y < birdGraphics.height / 2) { self.y = birdGraphics.height / 2; self.velocity = 0; } }; self.flap = function () { self.flapPressed = true; LK.getSound('jump').play(); }; }); // Pipe class to represent obstacles var Pipe = Container.expand(function () { var self = Container.call(this); var pipeGraphics = LK.getAsset('pipe', { anchorX: 0.5, anchorY: 0.5, width: 150, height: 600 }); self.addChild(pipeGraphics); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -pipeGraphics.width / 2) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ var background = LK.getAsset('background', { anchorX: 0.0, anchorY: 0.0, width: 2048, height: 2732 }); game.addChild(background); var bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; bird.gravity = 0; LK.setTimeout(function () { bird.gravity = 0.5; }, 900); var pipes = []; var pipeSpeed = 5; // Speed of the pipes var pipeGap = 150; // Distance between upper and lower pipes var pipeInterval = 90; // Interval for pipe generation var ticks = 0; game.down = function (x, y, obj) { gameStarted = true; bird.flap(); }; var gameStarted = false; var startScreen = false; // Define startScreen variable var score = 0; var gameData = { highScore: 0, totalGamesPlayed: 0, totalScore: 0 }; // Function to save game data to local storage function saveGameData() { if (typeof localStorage !== 'undefined') { localStorage.setItem('gameData', JSON.stringify(gameData)); } } // Function to load game data from local storage function loadGameData() { var data = typeof localStorage !== 'undefined' ? localStorage.getItem('gameData') : null; if (data) { gameData = JSON.parse(data); } } // Load game data at the start loadGameData(); var scoreText = new Text2('0', { size: 100, fill: "#ffffff", x: 2048 / 2, y: 200 }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); game.update = function () { background.width = game.width; background.height = game.height; if (gameStarted && !startScreen) { bird.update(); if (bird.y >= 2732 - bird.height / 2 || bird.y <= bird.height / 2) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } if (ticks % pipeInterval === 0) { var pipe = new Pipe(); pipe.x = 2048; // Spawn off-screen to the right pipe.y = Math.random() * (2732 - pipe.height * 2) + pipe.height; // Adjusted random y position pipe.speed = -5 - Math.floor(score / 5); // Increase the speed of the pipes as the score increases pipes.push(pipe); game.addChild(pipe); } for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].update(); if (pipes[i].intersects(bird)) { LK.effects.flashScreen(0xff0000, 1000); // Update game data gameData.totalGamesPlayed++; gameData.totalScore += score; if (score > gameData.highScore) { gameData.highScore = score; } saveGameData(); LK.showGameOver('Score: ' + score + '\nHigh Score: ' + gameData.highScore); } if (pipes[i].x < bird.x && !pipes[i].scored) { score++; pipes[i].scored = true; } if (pipes[i].x < -pipes[i].width / 2) { pipes.splice(i, 1); } } scoreText.setText('Score: ' + score); ticks++; } };
/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Bird class to represent the player character
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = LK.getAsset('bird_frame_0', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(birdGraphics);
self.velocity = 0;
self.gravity = 0.5;
self.lift = -10;
self.update = function () {
self.velocity += self.gravity;
if (self.flapPressed) {
self.velocity = self.lift;
self.flapPressed = false;
}
self.y += self.velocity;
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.velocity = 0;
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.velocity = 0;
}
};
self.flap = function () {
self.flapPressed = true;
LK.getSound('jump').play();
};
});
// Pipe class to represent obstacles
var Pipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = LK.getAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5,
width: 150,
height: 600
});
self.addChild(pipeGraphics);
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -pipeGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var background = LK.getAsset('background', {
anchorX: 0.0,
anchorY: 0.0,
width: 2048,
height: 2732
});
game.addChild(background);
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
bird.gravity = 0;
LK.setTimeout(function () {
bird.gravity = 0.5;
}, 900);
var pipes = [];
var pipeSpeed = 5; // Speed of the pipes
var pipeGap = 150; // Distance between upper and lower pipes
var pipeInterval = 90; // Interval for pipe generation
var ticks = 0;
game.down = function (x, y, obj) {
gameStarted = true;
bird.flap();
};
var gameStarted = false;
var startScreen = false; // Define startScreen variable
var score = 0;
var gameData = {
highScore: 0,
totalGamesPlayed: 0,
totalScore: 0
};
// Function to save game data to local storage
function saveGameData() {
if (typeof localStorage !== 'undefined') {
localStorage.setItem('gameData', JSON.stringify(gameData));
}
}
// Function to load game data from local storage
function loadGameData() {
var data = typeof localStorage !== 'undefined' ? localStorage.getItem('gameData') : null;
if (data) {
gameData = JSON.parse(data);
}
}
// Load game data at the start
loadGameData();
var scoreText = new Text2('0', {
size: 100,
fill: "#ffffff",
x: 2048 / 2,
y: 200
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
game.update = function () {
background.width = game.width;
background.height = game.height;
if (gameStarted && !startScreen) {
bird.update();
if (bird.y >= 2732 - bird.height / 2 || bird.y <= bird.height / 2) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (ticks % pipeInterval === 0) {
var pipe = new Pipe();
pipe.x = 2048; // Spawn off-screen to the right
pipe.y = Math.random() * (2732 - pipe.height * 2) + pipe.height; // Adjusted random y position
pipe.speed = -5 - Math.floor(score / 5); // Increase the speed of the pipes as the score increases
pipes.push(pipe);
game.addChild(pipe);
}
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].intersects(bird)) {
LK.effects.flashScreen(0xff0000, 1000);
// Update game data
gameData.totalGamesPlayed++;
gameData.totalScore += score;
if (score > gameData.highScore) {
gameData.highScore = score;
}
saveGameData();
LK.showGameOver('Score: ' + score + '\nHigh Score: ' + gameData.highScore);
}
if (pipes[i].x < bird.x && !pipes[i].scored) {
score++;
pipes[i].scored = true;
}
if (pipes[i].x < -pipes[i].width / 2) {
pipes.splice(i, 1);
}
}
scoreText.setText('Score: ' + score);
ticks++;
}
};