/****
* Classes
****/
// Bullet class representing the bullets shot by the player
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('Yellow_bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.x += self.speed;
if (self.y < -50) {
self.destroy();
} else {
for (var i = enemies.length - 1; i >= 0; i--) {
if (self.intersects(enemies[i])) {
enemies[i].destroy();
enemies.splice(i, 1);
enemiesDefeated++;
self.destroy(); // Destroy the bullet upon collision
LK.getSound('Player_hit').play();
// Chance to drop a powerup
if (Math.random() < 0.3 && enemies[i]) {
// 30% chance
var powerup = game.addChild(new Powerup());
powerup.x = enemies[i].x;
powerup.y = enemies[i].y;
items.push(powerup);
}
}
}
}
};
});
// Enemy class representing the enemies in the game
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 () {
self.x -= self.speed;
};
});
// EnemyAttack class representing the attack shot by the enemy
var EnemyAttack = Container.expand(function () {
var self = Container.call(this);
var attackGraphics = self.attachAsset('Enemy_attack', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// New EnemyWithAttack class representing a stationary enemy that shoots
var EnemyWithAttack = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('Enemy_2', {
anchorX: 0.5,
anchorY: 0.5
});
self.hp = 3; // Set enemy health to 3
self.update = function () {
// Logic for enemy attack
if (self.hp <= 0) {
self.destroy();
return;
}
if (LK.ticks % 120 === 0) {
// Shoot every 2 seconds
var attack = new EnemyAttack();
attack.x = self.x;
attack.y = self.y;
game.addChild(attack);
}
};
});
// Item class representing additional game elements
var Item = Container.expand(function () {
var self = Container.call(this);
// Removed heart icon attachment
self.update = function () {
// Logic for item behavior
};
});
// Update function to apply velocity and rotation
// Player class representing the main character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('Duck', {
anchorX: 0.5,
anchorY: 0.5
});
// Add heart icons above the Duck player to represent health
var heartIcons = [];
for (var i = 0; i < 3; i++) {
var heartIcon = LK.getAsset('Heart_icon', {
anchorX: 0.4,
anchorY: 0.5,
x: self.x + i * 110,
// Adjust spacing between hearts
y: self.y - 50 // Position above the Duck player
});
self.addChild(heartIcon);
heartIcons.push(heartIcon);
}
self.speed = 10;
self.update = function () {
// Apply gravity
self.speed += 0.5; // Gravity pulling down
self.y += self.speed; // Apply gravity to vertical speed
// Check if player is on the ground
if (self.y > ground.y - self.height / 2) {
self.y = ground.y - self.height / 2; // Correct position
self.speed = 0; // Stop moving downwards
}
// Check for shooting
if (LK.ticks % (enemiesDefeated >= 5 ? 30 : 60) === 0) {
self.shoot();
}
// Check for collision with enemies
for (var i = enemies.length - 1; i >= 0; i--) {
if (self.intersects(enemies[i])) {
enemies[i].destroy();
enemies.splice(i, 1);
// Handle player damage or game over logic
playerHealth--;
if (playerHealth >= 0) {
heartIcons[playerHealth].destroy(); // Remove a heart icon
}
if (playerHealth <= 0) {
// Stop all sounds and play game over sound
LK.stopMusic();
LK.getSound('Game_over_super_mario_world_soun').play();
// Display duck_death asset
var duckDeath = LK.getAsset('Duck_death', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
});
game.addChild(duckDeath);
// Trigger game over logic
LK.showGameOver();
}
LK.getSound('Enemy_hit').play();
}
}
};
self.shoot = function () {
if (bulletCount > 0) {
// Logic for shooting bullets
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
LK.getSound('Shoot').play();
bulletCount--;
if (bulletCount === 0) {
// Destroy all existing bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].destroy();
bullets.splice(i, 1);
}
bulletCount = 3; // Reload bullets
reloadTimeout = LK.setTimeout(function () {
LK.getSound('Reload').play();
for (var i = 0; i < bulletCount; i++) {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
bullets.push(bullet);
}
}, 3000);
}
}
};
});
// Powerup class representing powerup items
var Powerup = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('Powerup_1', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Logic for powerup behavior, e.g., floating down
self.y += 2;
// Check if player collects the powerup
if (self.intersects(player)) {
// Play sound when Powerup is collected
LK.getSound('Freeze').play();
// Delay the activation of the powerup effect by 2 seconds
LK.setTimeout(function () {
// Apply powerup effect
playerHealth = Math.min(playerHealth + 1, 3); // Increase health, max 3
heartIcons[playerHealth - 1].visible = true; // Show heart icon
// Add blue effect to player
LK.effects.flashObject(player, 0x0000ff, 1000); // Flash blue for 1 second
// Freeze effect: stop all enemies for 3 seconds
enemies.forEach(function (enemy) {
enemy.speed = 0;
});
LK.setTimeout(function () {
enemies.forEach(function (enemy) {
enemy.speed = 5; // Reset speed after freeze
});
}, 3000);
self.destroy();
}, 2000); // Activate after 2 seconds
}
// Destroy if off screen
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
//<Assets used in the game will automatically appear here>
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Add scenario image to the game background
var scenario = LK.getAsset('scenario', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366,
scaleX: 20.48,
scaleY: 27.32
});
game.addChild(scenario);
// Add falling danger sign effect with text after level 1
if (currentLevel > 1) {
var dangerSign = LK.getAsset('Danger', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: -100 // Start above the screen
});
game.addChild(dangerSign);
var dangerText = new Text2('Jump will be important!', {
size: 100,
fill: "#ff0000"
});
dangerText.anchor.set(0.5, 0);
dangerText.x = 1024;
dangerText.y = dangerSign.y - 50; // Position text above the danger sign
LK.gui.top.addChild(dangerText);
var fallSpeed = 10;
var dangerEffect = LK.setInterval(function () {
dangerSign.y += fallSpeed;
dangerText.y += fallSpeed;
if (dangerSign.y >= ground.y - ground.height / 2) {
LK.clearInterval(dangerEffect);
LK.setTimeout(function () {
dangerSign.destroy();
dangerText.destroy();
}, 2000); // Remove the danger sign and text after 2 seconds
}
}, 50);
}
var globalVariable; // Global variable declaration
// Function to handle level progression
function levelUp() {
// Update current level
currentLevel = points / 10 + 1; // Calculate current level based on points
// Increase difficulty by reducing enemy spawn interval
if (enemySpawnTimer) {
LK.clearInterval(enemySpawnTimer);
}
enemySpawnTimer = LK.setInterval(function () {
var enemy = game.addChild(new Enemy());
enemy.x = 2048 - enemy.width / 2; // Place the enemy on the right side of the screen
enemy.y = ground.y - ground.height / 2 - enemy.height / 2; // Place the enemy on the ground
enemies.push(enemy);
}, Math.max(1000, 3000 - points * 100 * (currentLevel > 2 ? 0.8 : 1) * (currentLevel === 2 ? 0.5 : 1))); // Adjust spawn rate for level 2
// Play level up sound
LK.getSound('Level_up').play();
// Add a new stationary enemy with 3 HP after level up
var stationaryEnemy = game.addChild(new EnemyWithAttack());
stationaryEnemy.x = 1024; // Center the enemy horizontally
stationaryEnemy.y = ground.y - ground.height / 2 - stationaryEnemy.height / 2; // Place the enemy on the ground
enemies.push(stationaryEnemy);
// Spawn EnemyWithAttack every 3 seconds
var enemyWithAttackSpawnTimer = LK.setInterval(function () {
var newEnemyWithAttack = game.addChild(new EnemyWithAttack());
newEnemyWithAttack.x = Math.random() * 2048; // Random x position
newEnemyWithAttack.y = ground.y - ground.height / 2 - newEnemyWithAttack.height / 2; // Place the enemy on the ground
enemies.push(newEnemyWithAttack);
}, 10000); // Spawn every 10 seconds
// Add falling danger sign effect with text on top after level up
if (!game.dangerSignSpawned) {
var dangerSign = LK.getAsset('Danger', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: -100 // Start above the screen
});
var dangerText = new Text2('Jump will be important!', {
size: 100,
fill: "#ff0000"
});
dangerText.anchor.set(0.5, 0.5);
dangerText.x = dangerSign.x - 600;
dangerText.y = dangerSign.y; // Position text on top of the danger sign
dangerSign.addChild(dangerText); // Add text as a child of the danger sign
game.addChild(dangerSign);
var fallSpeed = 10;
var dangerEffect = LK.setInterval(function () {
dangerSign.y += fallSpeed;
if (dangerSign.y >= ground.y - ground.height / 2) {
LK.clearInterval(dangerEffect);
LK.setTimeout(function () {
dangerSign.destroy();
game.dangerSignSpawned = false; // Reset flag after destruction
}, 2000); // Remove the danger sign after 2 seconds
}
}, 50);
game.dangerSignSpawned = true; // Set flag to prevent multiple spawns
}
var levelUpText = new Text2('Level Up!', {
size: 150,
fill: "#00ff00"
});
levelUpText.anchor.set(0.5, 0);
levelUpText.scale.set(0.1, 0.1); // Start small for scaling effect
LK.gui.top.addChild(levelUpText);
// Animate the level up text
var scaleUp = true;
var levelUpEffect = LK.setInterval(function () {
if (scaleUp) {
levelUpText.scale.x += 0.05;
levelUpText.scale.y += 0.05;
if (levelUpText.scale.x >= 1) {
scaleUp = false;
}
} else {
levelUpText.scale.x -= 0.05;
levelUpText.scale.y -= 0.05;
if (levelUpText.scale.x <= 0.1) {
scaleUp = true;
}
}
}, 50);
LK.setTimeout(function () {
LK.clearInterval(levelUpEffect);
levelUpText.destroy();
}, 2000); // Remove the message after 2 seconds
}
// Call levelUp function every time player reaches a new level
var levelCheckTimer = LK.setInterval(function () {
if (enemiesDefeated >= currentLevel * 10) {
// Level up when enemies defeated reaches the required count
levelUp();
}
}, 1000); // Check every second
// Initialize up button
var upButton = LK.getAsset('Up', {
anchorX: 0.5,
anchorY: 0.5,
x: 400,
y: 2600
});
game.addChild(upButton);
upButton.down = function (x, y, obj) {
if (player.y >= ground.y - player.height / 2) {
// If player is on the ground
player.speed = -20; // Apply upward force
LK.getSound('Jump').play();
// Add more dust effect when player jumps
for (var i = 0; i < 3; i++) {
var dust = LK.getAsset('dust', {
anchorX: 0.5,
anchorY: 0.5,
x: player.x + i * 20,
y: player.y + player.height / 2,
scaleX: 0.3,
scaleY: 0.3
});
game.addChild(dust);
// Update dust position to follow player's jump
dust.update = function () {
dust.y = player.y + player.height / 2;
};
// Remove dust effect after 500ms
LK.setTimeout(function () {
dust.destroy();
}, 500);
}
}
};
// Add a notice text warning about loud sound
var noticeText = new Text2('Warning! Loud Sound', {
size: 100,
fill: "#ff0000"
});
noticeText.anchor.set(0.5, 0);
LK.gui.top.addChild(noticeText);
LK.setTimeout(function () {
noticeText.destroy();
}, 5000); // Remove the notice after 5 seconds
// Add 'Your Goal' text on game over
var yourGoalText = new Text2('Your Goal: Survive!', {
size: 100,
fill: "#ffffff"
});
yourGoalText.anchor.set(0.5, 0);
yourGoalText.y = 200; // Position the text below the level text
LK.gui.bottom.addChild(yourGoalText);
// Add a button to skip the introduction;
var yourGoalText = new Text2('Your Goal: Survive!', {
size: 100,
fill: "#ffffff"
});
yourGoalText.anchor.set(0.5, 0);
yourGoalText.y = 200; // Position the text below the level text
LK.gui.bottom.addChild(yourGoalText);
// Display current level text
var levelText = new Text2('Level: ' + currentLevel, {
size: 100,
fill: "#ffffff"
});
levelText.anchor.set(0.5, 0);
LK.gui.top.addChild(levelText);
// Initialize ground
var ground = LK.getAsset('Ground', {
anchorX: 0.5,
anchorY: 1.0,
x: 1024,
y: 2500
});
game.addChild(ground);
// Update level text whenever the level changes
function updateLevelText() {
levelText.setText('Level: ' + currentLevel);
}
// Initialize player, enemies, and TV channels
var player = game.addChild(new Player());
player.x = 200; // Place the duck in left
player.y = ground.y - ground.height - player.height / 2;
// Add heart icons at the top of the screen to represent health
var heartIcons = [];
for (var i = 0; i < (currentLevel === 2 ? 2 : playerHealth); i++) {
var heartIcon = LK.getAsset('Heart_icon', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024 + i * 110,
y: 30
});
game.addChild(heartIcon);
heartIcons.push(heartIcon);
}
var enemies = [];
var items = []; // Initialize items array to track new game elements
var points = 0; // Initialize points variable to track player score
var currentLevel = 1; // Initialize currentLevel variable to track the player's level
updateLevelText(); // Update level text on initialization
var enemiesDefeated = 0; // Initialize enemiesDefeated variable to track defeated enemies
var bullets = []; // Initialize bullets array to track bullets
var Classic = 0; // Initialize Classic variable
var playerHealth = 3; // Initialize player health variable
var bulletCount = 3; // Initialize bullet count
var reloadTimeout; // Timeout for reloading bullets
var itemSpawnTimer = LK.setInterval(function () {
var item = game.addChild(new Item());
item.x = Math.random() * 2048; // Random x position
item.y = Math.random() * (ground.y - ground.height); // Random y position above ground
items.push(item);
}, 5000); // Spawn items every 5 seconds
var enemySpawnTimer = LK.setInterval(function () {
for (var i = 0; i < 3; i++) {
// Add multiple enemies
var enemy = game.addChild(new Enemy());
enemy.x = 2048 - enemy.width / 2 - i * 150; // Spread enemies horizontally
enemy.y = ground.y - ground.height / 2 - enemy.height / 2; // Place the enemy on the ground
enemies.push(enemy);
}
}, 1500); // Increase spawn rate by reducing interval
// Create TV channels
// Play background music during gameplay
LK.playMusic('Background_music');
// Add a button to skip the introduction;
Title game:duck with a gun. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Duck with a gun title logo. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Yellow_bullet pixel. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Red dot with white in the middle pixel. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Mana_icon pixel. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Enemy pixel. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
White_bullet_icon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
duck_death. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
duck_revive_in_game_over. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Continue_button_spending_gems. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Powerup_1. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Scenario. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Enemy pixel_2 more strong.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Danger sign. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.