/**** * Classes ****/ // Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 50; self.update = function () { self.x += self.speed; if (self.x > 2048 + 50) { self.destroy(); } }; }); // Clouds class var Clouds = Container.expand(function () { var self = Container.call(this); var cloudsGraphics = self.attachAsset('clouds', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -0.5; self.scale.set(0.5 + Math.random() * 1.5); // Randomize scale up to 2x (0.5 + Math.random() * 1.5) self.y = Math.random() * (2732 / 3); // Limit placement to upper 1/3 of screen self.update = function () { self.x += self.speed; if (self.x < -self.width) { self.x = 2048 + self.width; self.scale.set(1 + Math.random()); // Randomize scale again when repositioned self.y = Math.random() * (2732 / 3); // Randomize y position again when repositioned } }; }); // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyType = Math.floor(Math.random() * 3); if (enemyType == 0) { var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -2; } else if (enemyType == 1) { var enemyGraphics = self.attachAsset('enemy2', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -7; } else { var enemyGraphics = self.attachAsset('enemy3', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; } self.update = function () { self.x += self.speed; if (self.x < -50) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.jumpPower = -20; self.isJumping = false; self.isFalling = false; // Track if the hero is coming down from a jump self.isImmune = false; // Track if the hero is immune self.hasGun = false; // Track if the hero has a gun self.bullets = 0; // Track the number of bullets the hero has self.update = function () { if (self.isJumping) { self.y += self.jumpPower; self.jumpPower += 0.4; // Adjust gravity effect if (self.y >= 2732 * 2 / 3 - self.height) { // Ground level self.y = 2732 * 2 / 3 - self.height; self.isJumping = false; self.isFalling = false; // The hero is no longer falling self.jumpPower = -20; // Adjust jump power } else if (self.jumpPower > 0) { self.isFalling = true; // The hero is falling } } }; self.jump = function () { if (!self.isJumping) { self.isJumping = true; } }; self.makeImmune = function () { self.isImmune = true; LK.setTimeout(function () { self.isImmune = false; }, 3000); // Make the hero immune for 3 seconds }; self.giveGun = function () { self.hasGun = true; self.bullets = 10; }; }); // Pothole class var Pothole = Container.expand(function () { var self = Container.call(this); var potholeGraphics = self.attachAsset('pothole', { anchorX: 0.5, anchorY: 0.5, color: 0x8B4513 // Brown color }); self.update = function () { self.x -= 6; if (self.x < -50) { self.destroy(); } }; }); // PowerUp class var PowerUp = Container.expand(function () { var self = Container.call(this); var powerUpType = Math.floor(Math.random() * 3); if (powerUpType == 0) { var powerUpGraphics = self.attachAsset('powerUp', { anchorX: 0.5, anchorY: 0.5 }); self.value = 50; } else if (powerUpType == 1) { var powerUpGraphics = self.attachAsset('powerUp2', { anchorX: 0.5, anchorY: 0.5 }); self.value = 100; } else { var powerUpGraphics = self.attachAsset('powerUp3', { anchorX: 0.5, anchorY: 0.5 }); self.value = 150; } self.update = function () { self.x -= 5; if (self.x < -50) { self.destroy(); } }; }); // PowerUpTimer class var PowerUpTimer = Container.expand(function () { var self = Container.call(this); var timerBarGraphics = self.attachAsset('timerBar', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { if (self.width > 0) { self.width -= 1; } else { self.destroy(); } }; }); // Sky class var Sky = Container.expand(function () { var self = Container.call(this); var skyGraphics = self.attachAsset('sky', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Sky movement logic (if any) }; }); // Treasure class var Treasure = Container.expand(function (type) { var self = Container.call(this); var treasureType = type || 'treasure'; if (treasureType === 'treasure') { var treasureGraphics = self.attachAsset('treasure', { anchorX: 0.5, anchorY: 0.5 }); self.value = 10; } else if (treasureType === 'treasure2') { var treasureGraphics = self.attachAsset('treasure2', { anchorX: 0.5, anchorY: 0.5 }); self.value = 20; } else { var treasureGraphics = self.attachAsset('treasure3', { anchorX: 0.5, anchorY: 0.5 }); self.value = 30; } self.update = function () { self.x -= 5; if (self.x < -50) { self.destroy(); } }; }); // Trees class var Trees = Container.expand(function () { var self = Container.call(this); var treesGraphics = self.attachAsset('trees', { anchorX: 0.5, anchorY: 1.0 // Base of tree should be above ground level }); self.tint = 0x008000; self.speed = -3; self.scale.set(0.5 + Math.random()); // Randomize scale from 0.5 to 1.5 self.y = 2732 * 2 / 3 - (200 + Math.random() * 100); // Position the base of the tree above ground level self.update = function () { self.x += self.speed; if (self.x < -self.width) { self.x = 2048 + self.width; self.scale.set(0.5 + Math.random()); // Randomize scale again when repositioned } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x0000FF //Init game with blue background }); /**** * Game Code ****/ // Play background music in a loop LK.getSound('backgroundMusic').play(); function spawnCloud() { if (game.children.filter(function (child) { return child instanceof Clouds; }).length < 3) { var cloud = new Clouds(); cloud.x = 2048 + cloud.width; game.addChild(cloud); } } var sky = game.addChild(new Sky()); sky.x = 2048 / 2; sky.y = 2732 / 2; var trees = game.addChild(new Trees()); var clouds = game.addChild(new Clouds()); clouds.x = 0; clouds.y = 2732 / 4; var ground = game.addChild(LK.getAsset('ground', { x: 0, y: 2732 * 2 / 3 - 120 })); var hero = new Hero(); hero.x = 200; hero.y = 2732 * 2 / 3 - hero.height - 10; game.addChild(hero); var enemies = []; var potholes = []; var treasures = []; var powerUps = []; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var score = 0; function spawnEnemy() { var enemy = new Enemy(); enemy.x = 2048; enemy.y = 2732 * 2 / 3 - enemy.height - 10; enemies.push(enemy); game.addChild(enemy); } function spawnPothole() { var pothole = new Pothole(); pothole.x = 2048; pothole.y = 2732 - Math.random() * (2732 / 3.5); // Randomize pothole spawn height between bottom and 1/4 up pothole.scale.set(0.5 + Math.random()); // Randomize scale from 0.5 to 1.5 potholes.push(pothole); game.addChild(pothole); } function spawnTreasure() { var randomValue = Math.random(); var treasureType; if (randomValue < 0.7) { treasureType = 'treasure'; } else if (randomValue < 0.9) { treasureType = 'treasure2'; } else { treasureType = 'treasure3'; } var treasure = new Treasure(treasureType); treasure.x = 2048; treasure.y = 2732 * 2 / 3 - treasure.height - 350; // Increase the spawn height of treasures treasures.push(treasure); game.addChild(treasure); } function spawnPowerUp() { var powerUp = new PowerUp(); powerUp.x = 2048; powerUp.y = 2732 * 2 / 3 - powerUp.height - 300; // Increase the spawn height of power-ups powerUps.push(powerUp); game.addChild(powerUp); } game.down = function (x, y, obj) { if (hero.hasGun && hero.bullets > 0) { // Fire a bullet var bullet = new Bullet(); bullet.x = hero.x; bullet.y = hero.y; game.addChild(bullet); hero.bullets--; // Play gunfire sound LK.getSound('shoot').play(); if (hero.bullets == 0) { hero.hasGun = false; } } else { hero.jump(); } }; game.update = function () { hero.update(); game.removeChild(hero); game.addChild(hero); sky.update(); trees.update(); if (LK.ticks % 180 == 0) { var tree = new Trees(); tree.x = 2048 + tree.width; game.addChild(tree); } clouds.update(); for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (hero.intersects(enemies[i])) { if (hero.isFalling) { // The hero kills the enemy if it is falling enemies[i].destroy(); enemies.splice(i, 1); } else if (!hero.isImmune) { // The hero dies if it is not falling and not immune LK.effects.flashScreen(0xff0000, 1000); LK.getSound('playerDeath').play(); LK.showGameOver(); } } // Check for bullet collision with enemies for (var j = game.children.length - 1; j >= 0; j--) { if (game.children[j] instanceof Bullet) { if (enemies[i].intersects(game.children[j])) { // Destroy the enemy and the bullet game.children[j].destroy(); enemies[i].destroy(); enemies.splice(i, 1); // Play explosion sound when bullet hits enemy LK.getSound('explosion').play(); break; // Ensure only one enemy is killed per bullet and bullet is destroyed } } } } for (var i = game.children.length - 1; i >= 0; i--) { if (game.children[i] instanceof Pothole) { game.children[i].update(); // Potholes no longer harm the player } } for (var i = treasures.length - 1; i >= 0; i--) { treasures[i].update(); if (hero.intersects(treasures[i])) { score += treasures[i].value; scoreTxt.setText(score); LK.getSound('treasureCollect').play(); treasures[i].destroy(); treasures.splice(i, 1); } } for (var i = powerUps.length - 1; i >= 0; i--) { powerUps[i].update(); if (hero.intersects(powerUps[i])) { var powerUpTimer = new PowerUpTimer(); powerUpTimer.x = 2048 * (i + 1) / (powerUps.length + 1); powerUpTimer.y = 50; game.addChild(powerUpTimer); LK.getSound('powerUpCollect').play(); if (powerUps[i].value == 150) { // If the power up is powerUp3, make the hero immune hero.makeImmune(); } else if (powerUps[i].value == 100) { // If the power up is powerUp2, give the hero a gun hero.giveGun(); } else { // If the power up is powerUp, destroy all enemies and the powerup, flash screen red and play explosion sound for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].destroy(); enemies.splice(j, 1); } LK.effects.flashScreen(0xff0000, 100); LK.getSound('explosion').play(); powerUps[i].destroy(); powerUps.splice(i, 1); score += 50; scoreTxt.setText(score); } if (powerUps[i]) { powerUps[i].destroy(); powerUps.splice(i, 1); } } } if (LK.ticks % 60 == 0) { spawnEnemy(); } if (Math.random() < 0.05) { // Approximately every 20 ticks on average spawnPothole(); } if (LK.ticks % 180 == 0) { spawnTreasure(); } if (LK.ticks % 240 == 0) { spawnPowerUp(); } if (LK.ticks % 300 == 0) { spawnCloud(); } };
/****
* Classes
****/
// Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 50;
self.update = function () {
self.x += self.speed;
if (self.x > 2048 + 50) {
self.destroy();
}
};
});
// Clouds class
var Clouds = Container.expand(function () {
var self = Container.call(this);
var cloudsGraphics = self.attachAsset('clouds', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -0.5;
self.scale.set(0.5 + Math.random() * 1.5); // Randomize scale up to 2x (0.5 + Math.random() * 1.5)
self.y = Math.random() * (2732 / 3); // Limit placement to upper 1/3 of screen
self.update = function () {
self.x += self.speed;
if (self.x < -self.width) {
self.x = 2048 + self.width;
self.scale.set(1 + Math.random()); // Randomize scale again when repositioned
self.y = Math.random() * (2732 / 3); // Randomize y position again when repositioned
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyType = Math.floor(Math.random() * 3);
if (enemyType == 0) {
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -2;
} else if (enemyType == 1) {
var enemyGraphics = self.attachAsset('enemy2', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -7;
} else {
var enemyGraphics = self.attachAsset('enemy3', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
}
self.update = function () {
self.x += self.speed;
if (self.x < -50) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.jumpPower = -20;
self.isJumping = false;
self.isFalling = false; // Track if the hero is coming down from a jump
self.isImmune = false; // Track if the hero is immune
self.hasGun = false; // Track if the hero has a gun
self.bullets = 0; // Track the number of bullets the hero has
self.update = function () {
if (self.isJumping) {
self.y += self.jumpPower;
self.jumpPower += 0.4; // Adjust gravity effect
if (self.y >= 2732 * 2 / 3 - self.height) {
// Ground level
self.y = 2732 * 2 / 3 - self.height;
self.isJumping = false;
self.isFalling = false; // The hero is no longer falling
self.jumpPower = -20; // Adjust jump power
} else if (self.jumpPower > 0) {
self.isFalling = true; // The hero is falling
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
}
};
self.makeImmune = function () {
self.isImmune = true;
LK.setTimeout(function () {
self.isImmune = false;
}, 3000); // Make the hero immune for 3 seconds
};
self.giveGun = function () {
self.hasGun = true;
self.bullets = 10;
};
});
// Pothole class
var Pothole = Container.expand(function () {
var self = Container.call(this);
var potholeGraphics = self.attachAsset('pothole', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x8B4513 // Brown color
});
self.update = function () {
self.x -= 6;
if (self.x < -50) {
self.destroy();
}
};
});
// PowerUp class
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpType = Math.floor(Math.random() * 3);
if (powerUpType == 0) {
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 50;
} else if (powerUpType == 1) {
var powerUpGraphics = self.attachAsset('powerUp2', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 100;
} else {
var powerUpGraphics = self.attachAsset('powerUp3', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 150;
}
self.update = function () {
self.x -= 5;
if (self.x < -50) {
self.destroy();
}
};
});
// PowerUpTimer class
var PowerUpTimer = Container.expand(function () {
var self = Container.call(this);
var timerBarGraphics = self.attachAsset('timerBar', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (self.width > 0) {
self.width -= 1;
} else {
self.destroy();
}
};
});
// Sky class
var Sky = Container.expand(function () {
var self = Container.call(this);
var skyGraphics = self.attachAsset('sky', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Sky movement logic (if any)
};
});
// Treasure class
var Treasure = Container.expand(function (type) {
var self = Container.call(this);
var treasureType = type || 'treasure';
if (treasureType === 'treasure') {
var treasureGraphics = self.attachAsset('treasure', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 10;
} else if (treasureType === 'treasure2') {
var treasureGraphics = self.attachAsset('treasure2', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 20;
} else {
var treasureGraphics = self.attachAsset('treasure3', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 30;
}
self.update = function () {
self.x -= 5;
if (self.x < -50) {
self.destroy();
}
};
});
// Trees class
var Trees = Container.expand(function () {
var self = Container.call(this);
var treesGraphics = self.attachAsset('trees', {
anchorX: 0.5,
anchorY: 1.0 // Base of tree should be above ground level
});
self.tint = 0x008000;
self.speed = -3;
self.scale.set(0.5 + Math.random()); // Randomize scale from 0.5 to 1.5
self.y = 2732 * 2 / 3 - (200 + Math.random() * 100); // Position the base of the tree above ground level
self.update = function () {
self.x += self.speed;
if (self.x < -self.width) {
self.x = 2048 + self.width;
self.scale.set(0.5 + Math.random()); // Randomize scale again when repositioned
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0000FF //Init game with blue background
});
/****
* Game Code
****/
// Play background music in a loop
LK.getSound('backgroundMusic').play();
function spawnCloud() {
if (game.children.filter(function (child) {
return child instanceof Clouds;
}).length < 3) {
var cloud = new Clouds();
cloud.x = 2048 + cloud.width;
game.addChild(cloud);
}
}
var sky = game.addChild(new Sky());
sky.x = 2048 / 2;
sky.y = 2732 / 2;
var trees = game.addChild(new Trees());
var clouds = game.addChild(new Clouds());
clouds.x = 0;
clouds.y = 2732 / 4;
var ground = game.addChild(LK.getAsset('ground', {
x: 0,
y: 2732 * 2 / 3 - 120
}));
var hero = new Hero();
hero.x = 200;
hero.y = 2732 * 2 / 3 - hero.height - 10;
game.addChild(hero);
var enemies = [];
var potholes = [];
var treasures = [];
var powerUps = [];
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var score = 0;
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 * 2 / 3 - enemy.height - 10;
enemies.push(enemy);
game.addChild(enemy);
}
function spawnPothole() {
var pothole = new Pothole();
pothole.x = 2048;
pothole.y = 2732 - Math.random() * (2732 / 3.5); // Randomize pothole spawn height between bottom and 1/4 up
pothole.scale.set(0.5 + Math.random()); // Randomize scale from 0.5 to 1.5
potholes.push(pothole);
game.addChild(pothole);
}
function spawnTreasure() {
var randomValue = Math.random();
var treasureType;
if (randomValue < 0.7) {
treasureType = 'treasure';
} else if (randomValue < 0.9) {
treasureType = 'treasure2';
} else {
treasureType = 'treasure3';
}
var treasure = new Treasure(treasureType);
treasure.x = 2048;
treasure.y = 2732 * 2 / 3 - treasure.height - 350; // Increase the spawn height of treasures
treasures.push(treasure);
game.addChild(treasure);
}
function spawnPowerUp() {
var powerUp = new PowerUp();
powerUp.x = 2048;
powerUp.y = 2732 * 2 / 3 - powerUp.height - 300; // Increase the spawn height of power-ups
powerUps.push(powerUp);
game.addChild(powerUp);
}
game.down = function (x, y, obj) {
if (hero.hasGun && hero.bullets > 0) {
// Fire a bullet
var bullet = new Bullet();
bullet.x = hero.x;
bullet.y = hero.y;
game.addChild(bullet);
hero.bullets--;
// Play gunfire sound
LK.getSound('shoot').play();
if (hero.bullets == 0) {
hero.hasGun = false;
}
} else {
hero.jump();
}
};
game.update = function () {
hero.update();
game.removeChild(hero);
game.addChild(hero);
sky.update();
trees.update();
if (LK.ticks % 180 == 0) {
var tree = new Trees();
tree.x = 2048 + tree.width;
game.addChild(tree);
}
clouds.update();
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (hero.intersects(enemies[i])) {
if (hero.isFalling) {
// The hero kills the enemy if it is falling
enemies[i].destroy();
enemies.splice(i, 1);
} else if (!hero.isImmune) {
// The hero dies if it is not falling and not immune
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('playerDeath').play();
LK.showGameOver();
}
}
// Check for bullet collision with enemies
for (var j = game.children.length - 1; j >= 0; j--) {
if (game.children[j] instanceof Bullet) {
if (enemies[i].intersects(game.children[j])) {
// Destroy the enemy and the bullet
game.children[j].destroy();
enemies[i].destroy();
enemies.splice(i, 1);
// Play explosion sound when bullet hits enemy
LK.getSound('explosion').play();
break; // Ensure only one enemy is killed per bullet and bullet is destroyed
}
}
}
}
for (var i = game.children.length - 1; i >= 0; i--) {
if (game.children[i] instanceof Pothole) {
game.children[i].update();
// Potholes no longer harm the player
}
}
for (var i = treasures.length - 1; i >= 0; i--) {
treasures[i].update();
if (hero.intersects(treasures[i])) {
score += treasures[i].value;
scoreTxt.setText(score);
LK.getSound('treasureCollect').play();
treasures[i].destroy();
treasures.splice(i, 1);
}
}
for (var i = powerUps.length - 1; i >= 0; i--) {
powerUps[i].update();
if (hero.intersects(powerUps[i])) {
var powerUpTimer = new PowerUpTimer();
powerUpTimer.x = 2048 * (i + 1) / (powerUps.length + 1);
powerUpTimer.y = 50;
game.addChild(powerUpTimer);
LK.getSound('powerUpCollect').play();
if (powerUps[i].value == 150) {
// If the power up is powerUp3, make the hero immune
hero.makeImmune();
} else if (powerUps[i].value == 100) {
// If the power up is powerUp2, give the hero a gun
hero.giveGun();
} else {
// If the power up is powerUp, destroy all enemies and the powerup, flash screen red and play explosion sound
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].destroy();
enemies.splice(j, 1);
}
LK.effects.flashScreen(0xff0000, 100);
LK.getSound('explosion').play();
powerUps[i].destroy();
powerUps.splice(i, 1);
score += 50;
scoreTxt.setText(score);
}
if (powerUps[i]) {
powerUps[i].destroy();
powerUps.splice(i, 1);
}
}
}
if (LK.ticks % 60 == 0) {
spawnEnemy();
}
if (Math.random() < 0.05) {
// Approximately every 20 ticks on average
spawnPothole();
}
if (LK.ticks % 180 == 0) {
spawnTreasure();
}
if (LK.ticks % 240 == 0) {
spawnPowerUp();
}
if (LK.ticks % 300 == 0) {
spawnCloud();
}
};
toy gun. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
cartoon bullet, sideways, flying right, with empty background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
suit of armour, no background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
gold coin. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
cartoon bomb, no shadow, no background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
gold bar. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
treasure chest. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
cloud in bright blue sky.
bright blue sky over a grassy meadow.
dark green tree, plain background. Single Game Texture. In-Game asset. 2d. Blank background. No shadows.
a tall thin bunch of grass. Single Game Texture. In-Game asset. 2d. Blank background. No shadows.