/**** * Classes ****/ // Define a class for big flying enemies var BigEnemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); self.speed = Math.random() * 6 + 6; self.update = function () { self.x -= self.speed; if (self.x < -50) { self.destroy(); } }; }); // Define a class for enemies var Enemy = Container.expand(function () { var self = Container.call(this); // Increase enemy size var size = Math.random() * 0.5 + 0.5; var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5, scaleX: size, scaleY: size }); // Increase enemy speed self.speed = Math.random() * 4 + 4; self.update = function () { self.x -= self.speed; if (self.x < -50) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // 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 = 3; self.jumpHeight = 30; self.isJumping = false; self.velocityY = 0; self.update = function () { if (self.isJumping) { self.y += self.velocityY; self.velocityY += 0.6; // Decreased gravity effect by 30% if (self.y >= 2732 / 2) { // Ground level self.y = 2732 / 2; self.isJumping = false; self.velocityY = 0; self.canDoubleJump = false; // Reset double jump when player lands self.canDoubleDash = true; // Reset double dash when player lands self.canDoubleJump = false; // Reset double jump self.canDoubleDash = true; // Reset double dash when player lands self.isJumping = false; // Reset normal jump when player lands } } }; self.dashBack = function () { if (!self.isDashing || self.canDoubleDash) { if (self.isDashing && self.canDoubleDash) { self.canDoubleDash = false; self.x -= 350; // Second dash back is shorter } if (self.canDoubleDash) { self.canDoubleDash = false; } else { self.isDashing = true; } self.dashTimer = 100; LK.getSound('dash_sfx').play(); // Play dash sound effect self.oldX = self.x; self.x -= 300; // Increase dash distance } }; self.jump = function () { if (!self.isJumping && self.y >= 2732 / 2) { self.isJumping = true; self.velocityY = -self.jumpHeight * 1.2; } }; self.update = function () { if (self.isJumping) { self.y += self.velocityY; self.velocityY += 0.7; // Decreased gravity effect by 30% if (self.y >= 2732 / 2) { // Ground level self.y = 2732 / 2; self.isJumping = false; self.velocityY = 0; } } if (self.isDashing) { self.dashTimer--; if (self.dashTimer == 0) { // Increase dash cooldown self.isDashing = false; self.x = self.oldX; } } }; }); // Define a class for managing player health var PlayerHealth = Container.expand(function () { var self = Container.call(this); self.lives = 3; self.hearts = []; // Create heart icons for (var i = 0; i < self.lives; i++) { var heart = self.attachAsset('heart', { anchorX: 0.5, anchorY: 0.5 }); heart.x = 50 + i * 110; // Position hearts with some spacing heart.y = 50; self.hearts.push(heart); } // Method to decrease lives self.decreaseLife = function () { if (self.lives > 0) { self.lives--; self.hearts[self.lives].visible = false; } }; // Method to check if player is out of lives self.isGameOver = function () { return self.lives <= 0; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Initialize player health and display heart icons var playerHealth = game.addChild(new PlayerHealth()); playerHealth.hearts.forEach(function (heart, index) { heart.x = 100 + index * 110; // Position hearts with some spacing heart.y = 100; game.addChild(heart); }); // Draw white rectangles on screen to indicate where to touch var background = game.addChild(LK.getAsset('background', { anchorX: 0, anchorY: 0 })); background.x = 0; background.y = 0; var leftTouchArea = game.addChild(LK.getAsset('leftTouchArea', { anchorX: 0, anchorY: 0 })); var rightTouchArea = game.addChild(LK.getAsset('rightTouchArea', { anchorX: 0, anchorY: 0 })); leftTouchArea.alpha = 0.7; leftTouchArea.y = 1474; leftTouchArea.x = 137; rightTouchArea.alpha = 0.7; rightTouchArea.x = 1161; rightTouchArea.y = 1474; // Add jump and dash icons to the buttons var jumpIcon = game.addChild(LK.getAsset('player', { anchorX: 0.5, anchorY: 0.5 })); jumpIcon.x = rightTouchArea.x + rightTouchArea.width / 2; jumpIcon.y = rightTouchArea.y + rightTouchArea.height / 2; jumpIcon.scaleX = 2; jumpIcon.scaleY = 2; var dashIcon = game.addChild(LK.getAsset('player', { anchorX: 0.5, anchorY: 0.5 })); dashIcon.x = leftTouchArea.x + leftTouchArea.width / 2; dashIcon.y = leftTouchArea.y + leftTouchArea.height / 2; dashIcon.scaleX = 3; dashIcon.scaleY = 3; dashIcon.alpha = 0.7; // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; // Initialize enemies 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: 0x000000 }); // Add a new Text2 object to display the cooldown of Dash var cooldownText = new Text2('0', { anchorX: 0.5, anchorY: 0.5, size: 175, fill: 0x0000000 }); // Add the cooldown text to the game GUI at the middle of dash button game.addChild(cooldownText); cooldownText.x = leftTouchArea.x + leftTouchArea.width / 4; cooldownText.y = leftTouchArea.y + leftTouchArea.height / 2; // Add the score text to the game GUI at the top right of the screen game.addChild(scoreText); scoreText.x = 2048 / 2; scoreText.y = 100; // Add control instructions to the game screen var controlText = new Text2('Tap right to jump, tap left to dash back', { anchorX: 25, anchorY: 0.5, size: 100, fill: 0x000000 }); game.addChild(controlText); controlText.x = 2048 / 9; controlText.y = 2620; // Handle game updates game.update = function () { player.update(); // Update the cooldown text with the dash cooldown cooldownText.setText(player.isDashing ? player.dashTimer : 100); // Spawn enemies enemySpawnCounter++; if (enemySpawnCounter >= enemySpawnInterval) { var enemy; if (Math.random() < 0.2) { // 20% chance to spawn a big enemy enemy = new BigEnemy(); enemy.y = 2732 / 4; } else { enemy = new Enemy(); enemy.y = 2732 / 2; } enemy.x = 2096; enemies.push(enemy); game.addChild(enemy); // Randomize the spawn interval for the next enemy enemySpawnInterval = Math.floor(Math.random() * 100) + 50; enemySpawnCounter = 0; } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (player.intersects(enemies[j])) { LK.effects.flashScreen(0xff0000, 1000); playerHealth.decreaseLife(); if (playerHealth.isGameOver()) { LK.showGameOver(); } } else if (player.x > enemies[j].x && !enemies[j].passed) { enemies[j].passed = true; LK.setScore(LK.getScore() + (enemies[j] instanceof BigEnemy ? 2 : 1)); // Big enemies give 2 points scoreText.setText(LK.getScore()); } } }; // Play background music LK.playMusic('main_theme'); // Handle player jump game.down = function (x, y, obj) { if (x < 1024 && y > 1366) { player.dashBack(); } else { player.jump(); LK.getSound('jump_sfx').play(); // Play jump sound effect } };
/****
* Classes
****/
// Define a class for big flying enemies
var BigEnemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
self.speed = Math.random() * 6 + 6;
self.update = function () {
self.x -= self.speed;
if (self.x < -50) {
self.destroy();
}
};
});
// Define a class for enemies
var Enemy = Container.expand(function () {
var self = Container.call(this);
// Increase enemy size
var size = Math.random() * 0.5 + 0.5;
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: size,
scaleY: size
});
// Increase enemy speed
self.speed = Math.random() * 4 + 4;
self.update = function () {
self.x -= self.speed;
if (self.x < -50) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// 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 = 3;
self.jumpHeight = 30;
self.isJumping = false;
self.velocityY = 0;
self.update = function () {
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.6; // Decreased gravity effect by 30%
if (self.y >= 2732 / 2) {
// Ground level
self.y = 2732 / 2;
self.isJumping = false;
self.velocityY = 0;
self.canDoubleJump = false; // Reset double jump when player lands
self.canDoubleDash = true; // Reset double dash when player lands
self.canDoubleJump = false; // Reset double jump
self.canDoubleDash = true; // Reset double dash when player lands
self.isJumping = false; // Reset normal jump when player lands
}
}
};
self.dashBack = function () {
if (!self.isDashing || self.canDoubleDash) {
if (self.isDashing && self.canDoubleDash) {
self.canDoubleDash = false;
self.x -= 350; // Second dash back is shorter
}
if (self.canDoubleDash) {
self.canDoubleDash = false;
} else {
self.isDashing = true;
}
self.dashTimer = 100;
LK.getSound('dash_sfx').play(); // Play dash sound effect
self.oldX = self.x;
self.x -= 300; // Increase dash distance
}
};
self.jump = function () {
if (!self.isJumping && self.y >= 2732 / 2) {
self.isJumping = true;
self.velocityY = -self.jumpHeight * 1.2;
}
};
self.update = function () {
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.7; // Decreased gravity effect by 30%
if (self.y >= 2732 / 2) {
// Ground level
self.y = 2732 / 2;
self.isJumping = false;
self.velocityY = 0;
}
}
if (self.isDashing) {
self.dashTimer--;
if (self.dashTimer == 0) {
// Increase dash cooldown
self.isDashing = false;
self.x = self.oldX;
}
}
};
});
// Define a class for managing player health
var PlayerHealth = Container.expand(function () {
var self = Container.call(this);
self.lives = 3;
self.hearts = [];
// Create heart icons
for (var i = 0; i < self.lives; i++) {
var heart = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5
});
heart.x = 50 + i * 110; // Position hearts with some spacing
heart.y = 50;
self.hearts.push(heart);
}
// Method to decrease lives
self.decreaseLife = function () {
if (self.lives > 0) {
self.lives--;
self.hearts[self.lives].visible = false;
}
};
// Method to check if player is out of lives
self.isGameOver = function () {
return self.lives <= 0;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Initialize player health and display heart icons
var playerHealth = game.addChild(new PlayerHealth());
playerHealth.hearts.forEach(function (heart, index) {
heart.x = 100 + index * 110; // Position hearts with some spacing
heart.y = 100;
game.addChild(heart);
});
// Draw white rectangles on screen to indicate where to touch
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0
}));
background.x = 0;
background.y = 0;
var leftTouchArea = game.addChild(LK.getAsset('leftTouchArea', {
anchorX: 0,
anchorY: 0
}));
var rightTouchArea = game.addChild(LK.getAsset('rightTouchArea', {
anchorX: 0,
anchorY: 0
}));
leftTouchArea.alpha = 0.7;
leftTouchArea.y = 1474;
leftTouchArea.x = 137;
rightTouchArea.alpha = 0.7;
rightTouchArea.x = 1161;
rightTouchArea.y = 1474;
// Add jump and dash icons to the buttons
var jumpIcon = game.addChild(LK.getAsset('player', {
anchorX: 0.5,
anchorY: 0.5
}));
jumpIcon.x = rightTouchArea.x + rightTouchArea.width / 2;
jumpIcon.y = rightTouchArea.y + rightTouchArea.height / 2;
jumpIcon.scaleX = 2;
jumpIcon.scaleY = 2;
var dashIcon = game.addChild(LK.getAsset('player', {
anchorX: 0.5,
anchorY: 0.5
}));
dashIcon.x = leftTouchArea.x + leftTouchArea.width / 2;
dashIcon.y = leftTouchArea.y + leftTouchArea.height / 2;
dashIcon.scaleX = 3;
dashIcon.scaleY = 3;
dashIcon.alpha = 0.7;
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
// Initialize enemies
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: 0x000000
});
// Add a new Text2 object to display the cooldown of Dash
var cooldownText = new Text2('0', {
anchorX: 0.5,
anchorY: 0.5,
size: 175,
fill: 0x0000000
});
// Add the cooldown text to the game GUI at the middle of dash button
game.addChild(cooldownText);
cooldownText.x = leftTouchArea.x + leftTouchArea.width / 4;
cooldownText.y = leftTouchArea.y + leftTouchArea.height / 2;
// Add the score text to the game GUI at the top right of the screen
game.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 100;
// Add control instructions to the game screen
var controlText = new Text2('Tap right to jump, tap left to dash back', {
anchorX: 25,
anchorY: 0.5,
size: 100,
fill: 0x000000
});
game.addChild(controlText);
controlText.x = 2048 / 9;
controlText.y = 2620;
// Handle game updates
game.update = function () {
player.update();
// Update the cooldown text with the dash cooldown
cooldownText.setText(player.isDashing ? player.dashTimer : 100);
// Spawn enemies
enemySpawnCounter++;
if (enemySpawnCounter >= enemySpawnInterval) {
var enemy;
if (Math.random() < 0.2) {
// 20% chance to spawn a big enemy
enemy = new BigEnemy();
enemy.y = 2732 / 4;
} else {
enemy = new Enemy();
enemy.y = 2732 / 2;
}
enemy.x = 2096;
enemies.push(enemy);
game.addChild(enemy);
// Randomize the spawn interval for the next enemy
enemySpawnInterval = Math.floor(Math.random() * 100) + 50;
enemySpawnCounter = 0;
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (player.intersects(enemies[j])) {
LK.effects.flashScreen(0xff0000, 1000);
playerHealth.decreaseLife();
if (playerHealth.isGameOver()) {
LK.showGameOver();
}
} else if (player.x > enemies[j].x && !enemies[j].passed) {
enemies[j].passed = true;
LK.setScore(LK.getScore() + (enemies[j] instanceof BigEnemy ? 2 : 1)); // Big enemies give 2 points
scoreText.setText(LK.getScore());
}
}
};
// Play background music
LK.playMusic('main_theme');
// Handle player jump
game.down = function (x, y, obj) {
if (x < 1024 && y > 1366) {
player.dashBack();
} else {
player.jump();
LK.getSound('jump_sfx').play(); // Play jump sound effect
}
};