/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5
});
cloudGraphics.alpha = 0.7;
self.speed = Math.random() * 0.5 + 0.2;
self.update = function () {
self.x += self.speed;
if (self.x > 2048 + 100) {
self.x = -100;
self.y = Math.random() * 2732;
}
};
return self;
});
var Fruit = Container.expand(function (fruitType) {
var self = Container.call(this);
self.fruitType = fruitType || 'apple';
self.points = 10;
if (self.fruitType === 'orange') {
self.points = 15;
} else if (self.fruitType === 'banana') {
self.points = 20;
} else if (self.fruitType === 'grape') {
self.points = 25;
}
var fruitGraphics = self.attachAsset(self.fruitType, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 3 + 2;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Gun = Container.expand(function () {
var self = Container.call(this);
var gunGraphics = self.attachAsset('gun', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Sun = Container.expand(function () {
var self = Container.call(this);
var sunGraphics = self.attachAsset('sun', {
anchorX: 0.5,
anchorY: 0.5
});
sunGraphics.alpha = 0.8;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Add sun to the background
var sun = game.addChild(new Sun());
sun.x = 1800;
sun.y = 300;
// Add clouds to the background
var clouds = [];
for (var c = 0; c < 15; c++) {
var cloud = new Cloud();
cloud.x = Math.random() * 2048;
cloud.y = Math.random() * 2732;
clouds.push(cloud);
game.addChild(cloud);
}
var gun = game.addChild(new Gun());
gun.x = 2048 / 2;
gun.y = 2732 - 200;
var bullets = [];
var fruits = [];
var lives = 3;
var gameSpeed = 1;
var level = 1;
var lastLevelScore = 0;
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var livesTxt = new Text2('❤️❤️❤️', {
size: 60,
fill: 0xFFFFFF
});
livesTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(livesTxt);
var levelTxt = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(levelTxt);
levelTxt.x = 120; // Offset from left edge to avoid menu icon
var fruitTypes = ['apple', 'orange', 'banana', 'grape'];
var fruitSpawnTimer = 0;
var fruitSpawnRate = 120;
function spawnFruit() {
var randomType = fruitTypes[Math.floor(Math.random() * fruitTypes.length)];
var newFruit = new Fruit(randomType);
newFruit.x = Math.random() * (2048 - 300) + 150;
newFruit.y = -100;
newFruit.speed *= gameSpeed;
fruits.push(newFruit);
game.addChild(newFruit);
}
function shootBullet() {
var newBullet = new Bullet();
newBullet.x = gun.x;
newBullet.y = gun.y - 50;
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('shoot').play();
}
game.down = function (x, y, obj) {
shootBullet();
};
game.move = function (x, y, obj) {
gun.x = Math.max(150, Math.min(2048 - 150, x));
};
// Start background music
LK.playMusic('background');
game.update = function () {
fruitSpawnTimer++;
if (fruitSpawnTimer >= fruitSpawnRate / gameSpeed) {
spawnFruit();
fruitSpawnTimer = 0;
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.y < -100) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
// Check bullet-fruit collisions
for (var j = fruits.length - 1; j >= 0; j--) {
var fruit = fruits[j];
if (bullet.intersects(fruit)) {
var oldScore = LK.getScore();
LK.setScore(LK.getScore() + fruit.points);
var newScore = LK.getScore();
scoreTxt.setText('Score: ' + newScore);
// Check for level up every 20 points
var pointsGained = newScore - lastLevelScore;
if (pointsGained >= 20) {
level++;
lastLevelScore = newScore;
levelTxt.setText('Level: ' + level);
}
LK.effects.flashObject(fruit, 0xffffff, 200);
LK.getSound('hit').play();
bullet.destroy();
bullets.splice(i, 1);
fruit.destroy();
fruits.splice(j, 1);
break;
}
}
}
// Update fruits
for (var k = fruits.length - 1; k >= 0; k--) {
var fruit = fruits[k];
if (fruit.y > 2732 + 100) {
lives--;
var hearts = '';
for (var h = 0; h < lives; h++) {
hearts += '❤️';
}
livesTxt.setText(hearts);
if (lives <= 0) {
LK.showGameOver();
return;
}
fruit.destroy();
fruits.splice(k, 1);
}
}
// Increase game speed over time
if (LK.ticks % 1800 === 0) {
gameSpeed += 0.2;
fruitSpawnRate = Math.max(30, fruitSpawnRate - 5);
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5
});
cloudGraphics.alpha = 0.7;
self.speed = Math.random() * 0.5 + 0.2;
self.update = function () {
self.x += self.speed;
if (self.x > 2048 + 100) {
self.x = -100;
self.y = Math.random() * 2732;
}
};
return self;
});
var Fruit = Container.expand(function (fruitType) {
var self = Container.call(this);
self.fruitType = fruitType || 'apple';
self.points = 10;
if (self.fruitType === 'orange') {
self.points = 15;
} else if (self.fruitType === 'banana') {
self.points = 20;
} else if (self.fruitType === 'grape') {
self.points = 25;
}
var fruitGraphics = self.attachAsset(self.fruitType, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = Math.random() * 3 + 2;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Gun = Container.expand(function () {
var self = Container.call(this);
var gunGraphics = self.attachAsset('gun', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Sun = Container.expand(function () {
var self = Container.call(this);
var sunGraphics = self.attachAsset('sun', {
anchorX: 0.5,
anchorY: 0.5
});
sunGraphics.alpha = 0.8;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Add sun to the background
var sun = game.addChild(new Sun());
sun.x = 1800;
sun.y = 300;
// Add clouds to the background
var clouds = [];
for (var c = 0; c < 15; c++) {
var cloud = new Cloud();
cloud.x = Math.random() * 2048;
cloud.y = Math.random() * 2732;
clouds.push(cloud);
game.addChild(cloud);
}
var gun = game.addChild(new Gun());
gun.x = 2048 / 2;
gun.y = 2732 - 200;
var bullets = [];
var fruits = [];
var lives = 3;
var gameSpeed = 1;
var level = 1;
var lastLevelScore = 0;
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var livesTxt = new Text2('❤️❤️❤️', {
size: 60,
fill: 0xFFFFFF
});
livesTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(livesTxt);
var levelTxt = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(levelTxt);
levelTxt.x = 120; // Offset from left edge to avoid menu icon
var fruitTypes = ['apple', 'orange', 'banana', 'grape'];
var fruitSpawnTimer = 0;
var fruitSpawnRate = 120;
function spawnFruit() {
var randomType = fruitTypes[Math.floor(Math.random() * fruitTypes.length)];
var newFruit = new Fruit(randomType);
newFruit.x = Math.random() * (2048 - 300) + 150;
newFruit.y = -100;
newFruit.speed *= gameSpeed;
fruits.push(newFruit);
game.addChild(newFruit);
}
function shootBullet() {
var newBullet = new Bullet();
newBullet.x = gun.x;
newBullet.y = gun.y - 50;
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('shoot').play();
}
game.down = function (x, y, obj) {
shootBullet();
};
game.move = function (x, y, obj) {
gun.x = Math.max(150, Math.min(2048 - 150, x));
};
// Start background music
LK.playMusic('background');
game.update = function () {
fruitSpawnTimer++;
if (fruitSpawnTimer >= fruitSpawnRate / gameSpeed) {
spawnFruit();
fruitSpawnTimer = 0;
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.y < -100) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
// Check bullet-fruit collisions
for (var j = fruits.length - 1; j >= 0; j--) {
var fruit = fruits[j];
if (bullet.intersects(fruit)) {
var oldScore = LK.getScore();
LK.setScore(LK.getScore() + fruit.points);
var newScore = LK.getScore();
scoreTxt.setText('Score: ' + newScore);
// Check for level up every 20 points
var pointsGained = newScore - lastLevelScore;
if (pointsGained >= 20) {
level++;
lastLevelScore = newScore;
levelTxt.setText('Level: ' + level);
}
LK.effects.flashObject(fruit, 0xffffff, 200);
LK.getSound('hit').play();
bullet.destroy();
bullets.splice(i, 1);
fruit.destroy();
fruits.splice(j, 1);
break;
}
}
}
// Update fruits
for (var k = fruits.length - 1; k >= 0; k--) {
var fruit = fruits[k];
if (fruit.y > 2732 + 100) {
lives--;
var hearts = '';
for (var h = 0; h < lives; h++) {
hearts += '❤️';
}
livesTxt.setText(hearts);
if (lives <= 0) {
LK.showGameOver();
return;
}
fruit.destroy();
fruits.splice(k, 1);
}
}
// Increase game speed over time
if (LK.ticks % 1800 === 0) {
gameSpeed += 0.2;
fruitSpawnRate = Math.max(30, fruitSpawnRate - 5);
}
};