/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Define a class for enemies
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
if (player.isInAir) {
// Only move when the player is in the air
self.x -= self.speed;
}
if (self.x < -50) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Import the tween plugin
// Define a class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
self.gravity = 4; // Decrease gravity effect
self.isInAir = false; // Variable to track if the player is in the air or not
self.update = function () {
// Apply gravity to the player
self.y += self.gravity;
// Ensure the player doesn't fall below the ground level
if (self.y > 2732 / 2) {
self.y = 2732 / 2;
self.isInAir = false; // Player is not in the air
self.isJumping = false; // Player is not jumping
} else {
self.isInAir = true; // Player is in the air
}
};
});
/****
* Initialize Game
****/
// Add tween to play again button to make it scale up and down
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Variables to track jump power and hold time
//<Assets used in the game will automatically appear here>
// Import the tween plugin
// Add tween to play again button to make it scale up and down
LK.on('gameOver', function () {
var playAgainButton = LK.getAsset('playAgainButton', {});
tween(playAgainButton, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 500,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(playAgainButton, {
scaleX: 1,
scaleY: 1
}, {
duration: 500,
easing: tween.easeInOut
});
}
});
});
var jumpPower = 500;
var holdTime = 0;
var maxHoldTime = 2; // Maximum hold time in seconds
var maxJumpHeight = 1500; // Maximum jump height in pixels
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0
}));
background.x = 0;
background.y = 0;
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
var enemies = [];
var enemySpawnInterval = 100;
var enemySpawnCounter = 0;
// Create a new Text2 object to display the score
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
// Add the score text to the game GUI at the top center of the screen
LK.gui.top.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 50;
// Create a new Text2 object to display the current score
var currentScoreText = new Text2('Current Score: 0', {
size: 50,
fill: 0xFFFFFF
});
// Add the current score text to the game GUI at the top center of the screen
LK.gui.top.addChild(currentScoreText);
currentScoreText.x = 2048 / 2;
currentScoreText.y = 100;
// Create a new Container object to display the hold time as a bar
var holdTimeBar = new Container({
width: 0,
height: 50,
color: 0xFFFFFF,
shape: 'box'
});
// Add the hold time bar to the game GUI at the bottom of the screen
LK.gui.bottom.addChild(holdTimeBar);
holdTimeBar.x = 0;
holdTimeBar.y = 0;
// Play background music only once when the game starts
if (!game.musicPlayed) {
LK.playMusic('music');
game.musicPlayed = true;
}
// Handle game updates
// Handle touch down to start increasing jump power
game.down = function (x, y, obj) {
holdTime = 0; // Reset hold time
};
// Handle touch up to execute jump based on accumulated power
game.up = function (x, y, obj) {
if (!player.isJumping) {
jumpPower = Math.min(holdTime / maxHoldTime, 1) * maxJumpHeight;
player.y -= jumpPower; // Make the player jump
player.isJumping = true; // Player is jumping
holdTime = 0; // Reset hold time after jump
}
};
game.update = function () {
player.update();
if (enemies.length <= 2) {
enemySpawnCounter++;
if (enemySpawnCounter >= enemySpawnInterval) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 / 2;
enemies.push(enemy);
game.addChild(enemy);
// Randomize the spawn interval for the next enemy
enemySpawnInterval = Math.floor(Math.random() * 150) + 50;
enemySpawnCounter = 0;
}
}
// Update hold time while the screen is being held down
if (holdTime < maxHoldTime) {
holdTime += 1 / 60; // Increment hold time by 1/60th of a second per frame
holdTimeBar.width = holdTime / maxHoldTime * 2048; // Update the width of the hold time bar based on the current hold time
}
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (player.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else if (player.x > enemies[j].x && !enemies[j].passed && player.isInAir) {
enemies[j].passed = true;
LK.setScore(LK.getScore() + 1);
scoreText.setText(LK.getScore());
// Update the current score text
currentScoreText.setText('Current Score: ' + LK.getScore());
// Play sound effect when score increases
LK.getSound('sounds').play();
// Create and display a 'Perfect' image object
var perfectImage = LK.getAsset('Perfect', {
anchorX: 0.5,
anchorY: 0.5
});
LK.gui.center.addChild(perfectImage);
perfectImage.x = 2048 / 2;
perfectImage.y = 2732 / 2; // Center of the screen
// Tween to fade out and remove the perfect image after 0.5 seconds
tween(perfectImage, {
alpha: 0
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
perfectImage.destroy();
}
});
} else if (enemies[j].x < 0) {
enemies[j].destroy();
enemies.splice(j, 1);
if (enemies.length == 0) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 / 2;
enemies.push(enemy);
game.addChild(enemy);
}
}
}
};
// Handle player jump /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Define a class for enemies
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
if (player.isInAir) {
// Only move when the player is in the air
self.x -= self.speed;
}
if (self.x < -50) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Import the tween plugin
// Define a class for the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
self.gravity = 4; // Decrease gravity effect
self.isInAir = false; // Variable to track if the player is in the air or not
self.update = function () {
// Apply gravity to the player
self.y += self.gravity;
// Ensure the player doesn't fall below the ground level
if (self.y > 2732 / 2) {
self.y = 2732 / 2;
self.isInAir = false; // Player is not in the air
self.isJumping = false; // Player is not jumping
} else {
self.isInAir = true; // Player is in the air
}
};
});
/****
* Initialize Game
****/
// Add tween to play again button to make it scale up and down
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Variables to track jump power and hold time
//<Assets used in the game will automatically appear here>
// Import the tween plugin
// Add tween to play again button to make it scale up and down
LK.on('gameOver', function () {
var playAgainButton = LK.getAsset('playAgainButton', {});
tween(playAgainButton, {
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 500,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(playAgainButton, {
scaleX: 1,
scaleY: 1
}, {
duration: 500,
easing: tween.easeInOut
});
}
});
});
var jumpPower = 500;
var holdTime = 0;
var maxHoldTime = 2; // Maximum hold time in seconds
var maxJumpHeight = 1500; // Maximum jump height in pixels
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0
}));
background.x = 0;
background.y = 0;
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
var enemies = [];
var enemySpawnInterval = 100;
var enemySpawnCounter = 0;
// Create a new Text2 object to display the score
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
// Add the score text to the game GUI at the top center of the screen
LK.gui.top.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 50;
// Create a new Text2 object to display the current score
var currentScoreText = new Text2('Current Score: 0', {
size: 50,
fill: 0xFFFFFF
});
// Add the current score text to the game GUI at the top center of the screen
LK.gui.top.addChild(currentScoreText);
currentScoreText.x = 2048 / 2;
currentScoreText.y = 100;
// Create a new Container object to display the hold time as a bar
var holdTimeBar = new Container({
width: 0,
height: 50,
color: 0xFFFFFF,
shape: 'box'
});
// Add the hold time bar to the game GUI at the bottom of the screen
LK.gui.bottom.addChild(holdTimeBar);
holdTimeBar.x = 0;
holdTimeBar.y = 0;
// Play background music only once when the game starts
if (!game.musicPlayed) {
LK.playMusic('music');
game.musicPlayed = true;
}
// Handle game updates
// Handle touch down to start increasing jump power
game.down = function (x, y, obj) {
holdTime = 0; // Reset hold time
};
// Handle touch up to execute jump based on accumulated power
game.up = function (x, y, obj) {
if (!player.isJumping) {
jumpPower = Math.min(holdTime / maxHoldTime, 1) * maxJumpHeight;
player.y -= jumpPower; // Make the player jump
player.isJumping = true; // Player is jumping
holdTime = 0; // Reset hold time after jump
}
};
game.update = function () {
player.update();
if (enemies.length <= 2) {
enemySpawnCounter++;
if (enemySpawnCounter >= enemySpawnInterval) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 / 2;
enemies.push(enemy);
game.addChild(enemy);
// Randomize the spawn interval for the next enemy
enemySpawnInterval = Math.floor(Math.random() * 150) + 50;
enemySpawnCounter = 0;
}
}
// Update hold time while the screen is being held down
if (holdTime < maxHoldTime) {
holdTime += 1 / 60; // Increment hold time by 1/60th of a second per frame
holdTimeBar.width = holdTime / maxHoldTime * 2048; // Update the width of the hold time bar based on the current hold time
}
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (player.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else if (player.x > enemies[j].x && !enemies[j].passed && player.isInAir) {
enemies[j].passed = true;
LK.setScore(LK.getScore() + 1);
scoreText.setText(LK.getScore());
// Update the current score text
currentScoreText.setText('Current Score: ' + LK.getScore());
// Play sound effect when score increases
LK.getSound('sounds').play();
// Create and display a 'Perfect' image object
var perfectImage = LK.getAsset('Perfect', {
anchorX: 0.5,
anchorY: 0.5
});
LK.gui.center.addChild(perfectImage);
perfectImage.x = 2048 / 2;
perfectImage.y = 2732 / 2; // Center of the screen
// Tween to fade out and remove the perfect image after 0.5 seconds
tween(perfectImage, {
alpha: 0
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
perfectImage.destroy();
}
});
} else if (enemies[j].x < 0) {
enemies[j].destroy();
enemies.splice(j, 1);
if (enemies.length == 0) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 / 2;
enemies.push(enemy);
game.addChild(enemy);
}
}
}
};
// Handle player jump