/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Enemy Shell Class var EnemyShell = Container.expand(function () { var self = Container.call(this); var shell = self.attachAsset('enemyShell', { anchorX: 0.5, anchorY: 0.5 }); self.width = shell.width; self.height = shell.height; self.vx = 0; self.vy = 8; // Reduced speed self.update = function () { self.x += self.vx; self.y += self.vy; }; return self; }); // Enemy Tank Class var EnemyTank = Container.expand(function () { var self = Container.call(this); var tank = self.attachAsset('enemyTank', { anchorX: 0.5, anchorY: 0.5 }); self.width = tank.width; self.height = tank.height; // Movement self.speed = 6 + Math.random() * 2 + Math.floor(LK.getScore() / 10); // Escalate with score // Firing self.fireInterval = 120; // 2 seconds at 60fps self.fireCounter = 0; self.update = function () { self.y += self.speed; self.fireCounter++; if (self.fireCounter >= self.fireInterval) { self.fireCounter = 0; self.fire(); } }; self.fire = function () { var shell = new EnemyShell(); shell.x = self.x; shell.y = self.y + self.height / 2 + shell.height / 2; // Aim at player var dx = player.x - shell.x; var dy = player.y - shell.y; var mag = Math.sqrt(dx * dx + dy * dy); // Lowered speed for enemy shells var shellSpeed = 7 + Math.floor(LK.getScore() / 20); shell.vx = dx / mag * shellSpeed; shell.vy = dy / mag * shellSpeed; enemyShells.push(shell); game.addChild(shell); LK.getSound('enemyFire').play(); }; return self; }); // Explosion effect (for destroyed tanks) var Explosion = Container.expand(function () { var self = Container.call(this); var exp = self.attachAsset('explosion', { anchorX: 0.5, anchorY: 0.5 }); exp.alpha = 0.7; // Animate fade out and destroy tween(exp, { alpha: 0 }, { duration: 350, easing: tween.linear, onFinish: function onFinish() { self.destroy(); } }); return self; }); // Heart Drop Class (powerup) var HeartDrop = Container.expand(function () { var self = Container.call(this); var heart = self.attachAsset('heart', { anchorX: 0.5, anchorY: 0.5 }); self.width = heart.width; self.height = heart.height; self.speed = 7 + Math.random() * 2; self.update = function () { self.y += self.speed; }; return self; }); // Player Shell Class var PlayerShell = Container.expand(function () { var self = Container.call(this); var shell = self.attachAsset('playerShell', { anchorX: 0.5, anchorY: 0.5 }); self.width = shell.width; self.height = shell.height; self.speed = -32; // Upwards self.update = function () { self.y += self.speed; }; return self; }); // Player Tank Class var PlayerTank = Container.expand(function () { var self = Container.call(this); var tank = self.attachAsset('playerTank', { anchorX: 0.5, anchorY: 0.5 }); self.width = tank.width; self.height = tank.height; // Fire cooldown self.canFire = true; self.fireCooldown = 18; // frames (0.3s) self.cooldownCounter = 0; self.update = function () { if (!self.canFire) { self.cooldownCounter++; if (self.cooldownCounter >= self.fireCooldown) { self.canFire = true; self.cooldownCounter = 0; } } }; // Fire a shell self.fire = function () { if (self.canFire) { var shell = new PlayerShell(); shell.x = self.x; shell.y = self.y - self.height / 2 - shell.height / 2; playerShells.push(shell); game.addChild(shell); LK.getSound('fire').play(); self.canFire = false; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x22272b }); /**** * Game Code ****/ // Sound effects // Explosion: white ellipse (for flash effect) // Enemy shell: orange ellipse // Enemy tank: red box // Player shell: yellow ellipse // Player tank: green box // Game area var GAME_W = 2048; var GAME_H = 2732; // Player var player = new PlayerTank(); player.x = GAME_W / 2; player.y = GAME_H - 350; game.addChild(player); // Arrays for game objects var playerShells = []; var enemyTanks = []; var enemyShells = []; var heartDrops = []; // Array for heart drops // Score display var scoreTxt = new Text2('0', { size: 120, fill: "#fff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Player health var playerHealth = 5; var healthTxt = new Text2('❤❤❤❤❤', { size: 100, fill: 0xFF4444 }); healthTxt.anchor.set(0.5, 0); LK.gui.top.addChild(healthTxt); healthTxt.x = 300; healthTxt.y = 0; // Dragging var dragNode = null; // Spawn enemy timer var enemySpawnInterval = 90; // frames (1.5s) var enemySpawnCounter = 0; // Difficulty escalation function getEnemySpawnInterval() { var s = LK.getScore(); if (s < 10) return 90; if (s < 25) return 70; if (s < 50) return 55; return 40; } // Move handler (drag player tank) function handleMove(x, y, obj) { if (dragNode) { // Clamp to game area, avoid top 100px (menu) var minX = dragNode.width / 2; var maxX = GAME_W - dragNode.width / 2; var minY = 100 + dragNode.height / 2; var maxY = GAME_H - dragNode.height / 2; dragNode.x = Math.max(minX, Math.min(maxX, x)); dragNode.y = Math.max(minY, Math.min(maxY, y)); } } game.move = handleMove; game.down = function (x, y, obj) { // Only drag if touch is on player tank var local = player.toLocal(game.toGlobal({ x: x, y: y })); if (local.x > -player.width / 2 && local.x < player.width / 2 && local.y > -player.height / 2 && local.y < player.height / 2) { dragNode = player; } }; game.up = function (x, y, obj) { dragNode = null; }; // Tap to fire (anywhere) game.tap = function (x, y, obj) { player.fire(); }; // For compatibility, also fire on down if not dragging game.down = function (x, y, obj) { var local = player.toLocal(game.toGlobal({ x: x, y: y })); if (local.x > -player.width / 2 && local.x < player.width / 2 && local.y > -player.height / 2 && local.y < player.height / 2) { dragNode = player; } else { player.fire(); } }; // Main update loop game.update = function () { // Update player player.update(); // Update player shells for (var i = playerShells.length - 1; i >= 0; i--) { var shell = playerShells[i]; shell.update(); // Remove if off screen if (shell.y < -shell.height / 2) { shell.destroy(); playerShells.splice(i, 1); continue; } // Check collision with enemy tanks for (var j = enemyTanks.length - 1; j >= 0; j--) { var enemy = enemyTanks[j]; if (shell.intersects(enemy)) { // Explosion var exp = new Explosion(); exp.x = enemy.x; exp.y = enemy.y; game.addChild(exp); LK.getSound('explode').play(); // Remove both shell.destroy(); playerShells.splice(i, 1); enemy.destroy(); enemyTanks.splice(j, 1); // Score LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); break; } } } // Update enemy tanks for (var i = enemyTanks.length - 1; i >= 0; i--) { var enemy = enemyTanks[i]; enemy.update(); // Remove if off screen if (enemy.y > GAME_H + enemy.height / 2) { enemy.destroy(); enemyTanks.splice(i, 1); continue; } // Check collision with player if (enemy.intersects(player)) { // Explosion var exp = new Explosion(); exp.x = player.x; exp.y = player.y; game.addChild(exp); LK.getSound('explode').play(); LK.effects.flashScreen(0xff0000, 800); // Decrease health playerHealth--; // Update health display var hearts = ''; for (var h = 0; h < playerHealth; h++) hearts += '❤'; healthTxt.setText(hearts); if (playerHealth <= 0) { LK.showGameOver(); return; } // Remove enemy tank enemy.destroy(); enemyTanks.splice(i, 1); continue; } } // Update enemy shells for (var i = enemyShells.length - 1; i >= 0; i--) { var shell = enemyShells[i]; shell.update(); // Remove if off screen if (shell.y > GAME_H + shell.height / 2 || shell.x < -shell.width / 2 || shell.x > GAME_W + shell.width / 2) { shell.destroy(); enemyShells.splice(i, 1); continue; } // Check collision with player if (shell.intersects(player)) { // Explosion var exp = new Explosion(); exp.x = player.x; exp.y = player.y; game.addChild(exp); LK.getSound('explode').play(); LK.effects.flashScreen(0xff0000, 800); // Decrease health playerHealth--; // Update health display var hearts = ''; for (var h = 0; h < playerHealth; h++) hearts += '❤'; healthTxt.setText(hearts); if (playerHealth <= 0) { LK.showGameOver(); return; } // Remove shell shell.destroy(); enemyShells.splice(i, 1); continue; } } // Spawn enemies enemySpawnCounter++; if (enemySpawnCounter >= getEnemySpawnInterval()) { enemySpawnCounter = 0; var enemy = new EnemyTank(); // Random x, avoid edges var margin = 200; enemy.x = margin + Math.random() * (GAME_W - 2 * margin); enemy.y = -enemy.height / 2; enemyTanks.push(enemy); game.addChild(enemy); } // Rarely spawn heart drops (about 1 in 400 frames, ~every 6-7 seconds) if (Math.random() < 1 / 400) { var heart = new HeartDrop(); var margin = 200; heart.x = margin + Math.random() * (GAME_W - 2 * margin); heart.y = -heart.height / 2; heartDrops.push(heart); game.addChild(heart); } // Update heart drops for (var i = heartDrops.length - 1; i >= 0; i--) { var heart = heartDrops[i]; heart.update(); // Remove if off screen if (heart.y > GAME_H + heart.height / 2) { heart.destroy(); heartDrops.splice(i, 1); continue; } // Check collision with player if (heart.intersects(player)) { // Only heal if not at max health if (playerHealth < 5) { playerHealth++; var hearts = ''; for (var h = 0; h < playerHealth; h++) hearts += '❤'; healthTxt.setText(hearts); } heart.destroy(); heartDrops.splice(i, 1); continue; } } }; // Set initial score LK.setScore(0); scoreTxt.setText('0'); ; // Set initial health playerHealth = 5; var hearts = ''; for (var h = 0; h < playerHealth; h++) hearts += '❤'; healthTxt.setText(hearts);
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Enemy Shell Class
var EnemyShell = Container.expand(function () {
var self = Container.call(this);
var shell = self.attachAsset('enemyShell', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = shell.width;
self.height = shell.height;
self.vx = 0;
self.vy = 8; // Reduced speed
self.update = function () {
self.x += self.vx;
self.y += self.vy;
};
return self;
});
// Enemy Tank Class
var EnemyTank = Container.expand(function () {
var self = Container.call(this);
var tank = self.attachAsset('enemyTank', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = tank.width;
self.height = tank.height;
// Movement
self.speed = 6 + Math.random() * 2 + Math.floor(LK.getScore() / 10); // Escalate with score
// Firing
self.fireInterval = 120; // 2 seconds at 60fps
self.fireCounter = 0;
self.update = function () {
self.y += self.speed;
self.fireCounter++;
if (self.fireCounter >= self.fireInterval) {
self.fireCounter = 0;
self.fire();
}
};
self.fire = function () {
var shell = new EnemyShell();
shell.x = self.x;
shell.y = self.y + self.height / 2 + shell.height / 2;
// Aim at player
var dx = player.x - shell.x;
var dy = player.y - shell.y;
var mag = Math.sqrt(dx * dx + dy * dy);
// Lowered speed for enemy shells
var shellSpeed = 7 + Math.floor(LK.getScore() / 20);
shell.vx = dx / mag * shellSpeed;
shell.vy = dy / mag * shellSpeed;
enemyShells.push(shell);
game.addChild(shell);
LK.getSound('enemyFire').play();
};
return self;
});
// Explosion effect (for destroyed tanks)
var Explosion = Container.expand(function () {
var self = Container.call(this);
var exp = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
exp.alpha = 0.7;
// Animate fade out and destroy
tween(exp, {
alpha: 0
}, {
duration: 350,
easing: tween.linear,
onFinish: function onFinish() {
self.destroy();
}
});
return self;
});
// Heart Drop Class (powerup)
var HeartDrop = Container.expand(function () {
var self = Container.call(this);
var heart = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = heart.width;
self.height = heart.height;
self.speed = 7 + Math.random() * 2;
self.update = function () {
self.y += self.speed;
};
return self;
});
// Player Shell Class
var PlayerShell = Container.expand(function () {
var self = Container.call(this);
var shell = self.attachAsset('playerShell', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = shell.width;
self.height = shell.height;
self.speed = -32; // Upwards
self.update = function () {
self.y += self.speed;
};
return self;
});
// Player Tank Class
var PlayerTank = Container.expand(function () {
var self = Container.call(this);
var tank = self.attachAsset('playerTank', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = tank.width;
self.height = tank.height;
// Fire cooldown
self.canFire = true;
self.fireCooldown = 18; // frames (0.3s)
self.cooldownCounter = 0;
self.update = function () {
if (!self.canFire) {
self.cooldownCounter++;
if (self.cooldownCounter >= self.fireCooldown) {
self.canFire = true;
self.cooldownCounter = 0;
}
}
};
// Fire a shell
self.fire = function () {
if (self.canFire) {
var shell = new PlayerShell();
shell.x = self.x;
shell.y = self.y - self.height / 2 - shell.height / 2;
playerShells.push(shell);
game.addChild(shell);
LK.getSound('fire').play();
self.canFire = false;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x22272b
});
/****
* Game Code
****/
// Sound effects
// Explosion: white ellipse (for flash effect)
// Enemy shell: orange ellipse
// Enemy tank: red box
// Player shell: yellow ellipse
// Player tank: green box
// Game area
var GAME_W = 2048;
var GAME_H = 2732;
// Player
var player = new PlayerTank();
player.x = GAME_W / 2;
player.y = GAME_H - 350;
game.addChild(player);
// Arrays for game objects
var playerShells = [];
var enemyTanks = [];
var enemyShells = [];
var heartDrops = []; // Array for heart drops
// Score display
var scoreTxt = new Text2('0', {
size: 120,
fill: "#fff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Player health
var playerHealth = 5;
var healthTxt = new Text2('❤❤❤❤❤', {
size: 100,
fill: 0xFF4444
});
healthTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(healthTxt);
healthTxt.x = 300;
healthTxt.y = 0;
// Dragging
var dragNode = null;
// Spawn enemy timer
var enemySpawnInterval = 90; // frames (1.5s)
var enemySpawnCounter = 0;
// Difficulty escalation
function getEnemySpawnInterval() {
var s = LK.getScore();
if (s < 10) return 90;
if (s < 25) return 70;
if (s < 50) return 55;
return 40;
}
// Move handler (drag player tank)
function handleMove(x, y, obj) {
if (dragNode) {
// Clamp to game area, avoid top 100px (menu)
var minX = dragNode.width / 2;
var maxX = GAME_W - dragNode.width / 2;
var minY = 100 + dragNode.height / 2;
var maxY = GAME_H - dragNode.height / 2;
dragNode.x = Math.max(minX, Math.min(maxX, x));
dragNode.y = Math.max(minY, Math.min(maxY, y));
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
// Only drag if touch is on player tank
var local = player.toLocal(game.toGlobal({
x: x,
y: y
}));
if (local.x > -player.width / 2 && local.x < player.width / 2 && local.y > -player.height / 2 && local.y < player.height / 2) {
dragNode = player;
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Tap to fire (anywhere)
game.tap = function (x, y, obj) {
player.fire();
};
// For compatibility, also fire on down if not dragging
game.down = function (x, y, obj) {
var local = player.toLocal(game.toGlobal({
x: x,
y: y
}));
if (local.x > -player.width / 2 && local.x < player.width / 2 && local.y > -player.height / 2 && local.y < player.height / 2) {
dragNode = player;
} else {
player.fire();
}
};
// Main update loop
game.update = function () {
// Update player
player.update();
// Update player shells
for (var i = playerShells.length - 1; i >= 0; i--) {
var shell = playerShells[i];
shell.update();
// Remove if off screen
if (shell.y < -shell.height / 2) {
shell.destroy();
playerShells.splice(i, 1);
continue;
}
// Check collision with enemy tanks
for (var j = enemyTanks.length - 1; j >= 0; j--) {
var enemy = enemyTanks[j];
if (shell.intersects(enemy)) {
// Explosion
var exp = new Explosion();
exp.x = enemy.x;
exp.y = enemy.y;
game.addChild(exp);
LK.getSound('explode').play();
// Remove both
shell.destroy();
playerShells.splice(i, 1);
enemy.destroy();
enemyTanks.splice(j, 1);
// Score
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
}
// Update enemy tanks
for (var i = enemyTanks.length - 1; i >= 0; i--) {
var enemy = enemyTanks[i];
enemy.update();
// Remove if off screen
if (enemy.y > GAME_H + enemy.height / 2) {
enemy.destroy();
enemyTanks.splice(i, 1);
continue;
}
// Check collision with player
if (enemy.intersects(player)) {
// Explosion
var exp = new Explosion();
exp.x = player.x;
exp.y = player.y;
game.addChild(exp);
LK.getSound('explode').play();
LK.effects.flashScreen(0xff0000, 800);
// Decrease health
playerHealth--;
// Update health display
var hearts = '';
for (var h = 0; h < playerHealth; h++) hearts += '❤';
healthTxt.setText(hearts);
if (playerHealth <= 0) {
LK.showGameOver();
return;
}
// Remove enemy tank
enemy.destroy();
enemyTanks.splice(i, 1);
continue;
}
}
// Update enemy shells
for (var i = enemyShells.length - 1; i >= 0; i--) {
var shell = enemyShells[i];
shell.update();
// Remove if off screen
if (shell.y > GAME_H + shell.height / 2 || shell.x < -shell.width / 2 || shell.x > GAME_W + shell.width / 2) {
shell.destroy();
enemyShells.splice(i, 1);
continue;
}
// Check collision with player
if (shell.intersects(player)) {
// Explosion
var exp = new Explosion();
exp.x = player.x;
exp.y = player.y;
game.addChild(exp);
LK.getSound('explode').play();
LK.effects.flashScreen(0xff0000, 800);
// Decrease health
playerHealth--;
// Update health display
var hearts = '';
for (var h = 0; h < playerHealth; h++) hearts += '❤';
healthTxt.setText(hearts);
if (playerHealth <= 0) {
LK.showGameOver();
return;
}
// Remove shell
shell.destroy();
enemyShells.splice(i, 1);
continue;
}
}
// Spawn enemies
enemySpawnCounter++;
if (enemySpawnCounter >= getEnemySpawnInterval()) {
enemySpawnCounter = 0;
var enemy = new EnemyTank();
// Random x, avoid edges
var margin = 200;
enemy.x = margin + Math.random() * (GAME_W - 2 * margin);
enemy.y = -enemy.height / 2;
enemyTanks.push(enemy);
game.addChild(enemy);
}
// Rarely spawn heart drops (about 1 in 400 frames, ~every 6-7 seconds)
if (Math.random() < 1 / 400) {
var heart = new HeartDrop();
var margin = 200;
heart.x = margin + Math.random() * (GAME_W - 2 * margin);
heart.y = -heart.height / 2;
heartDrops.push(heart);
game.addChild(heart);
}
// Update heart drops
for (var i = heartDrops.length - 1; i >= 0; i--) {
var heart = heartDrops[i];
heart.update();
// Remove if off screen
if (heart.y > GAME_H + heart.height / 2) {
heart.destroy();
heartDrops.splice(i, 1);
continue;
}
// Check collision with player
if (heart.intersects(player)) {
// Only heal if not at max health
if (playerHealth < 5) {
playerHealth++;
var hearts = '';
for (var h = 0; h < playerHealth; h++) hearts += '❤';
healthTxt.setText(hearts);
}
heart.destroy();
heartDrops.splice(i, 1);
continue;
}
}
};
// Set initial score
LK.setScore(0);
scoreTxt.setText('0');
;
// Set initial health
playerHealth = 5;
var hearts = '';
for (var h = 0; h < playerHealth; h++) hearts += '❤';
healthTxt.setText(hearts);