/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Asteroid = Container.expand(function () { var self = Container.call(this); var asteroidGraphics = self.attachAsset('asteroid', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.y += self.speed; }; return self; }); var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; }; return self; }); var EquipIcon = Container.expand(function () { var self = Container.call(this); var equipGraphics = self.attachAsset('shopIcon', { anchorX: 0.5, anchorY: 0.5, tint: 0x00ff00 // Set color to green }); self.down = function (x, y, obj) { console.log("Equip icon clicked. Showing gun selection box."); var gunSelectionBox = new GunSelectionBox(); game.addChild(gunSelectionBox); }; return self; }); var GunSelectionBox = Container.expand(function () { var self = Container.call(this); var boxGraphics = self.attachAsset('shopIcon', { anchorX: 0.5, anchorY: 0.5, tint: 0x333333 // Dark gray for the box }); self.x = 2048 / 2; self.y = 2732 / 2; self.width = 600; self.height = 800; var guns = ['Pistol', 'Rifle', 'Shotgun', 'Sniper']; var startY = -150; guns.forEach(function (gun, index) { var gunText = new Text2(gun, { size: 80, fill: 0xFFFFFF }); gunText.anchor.set(0.5, 0.5); gunText.x = 0; gunText.y = startY + index * 100; gunText.down = function (x, y, obj) { selectedGun = { name: gun }; console.log(gun + " selected!"); self.destroy(); // Close the selection box after selecting a gun }; self.addChild(gunText); }); return self; }); var HealthBar = Container.expand(function () { var self = Container.call(this); var barBackground = self.addChild(new Container()); var barForeground = self.addChild(new Container()); var backgroundGraphics = barBackground.attachAsset('healthBarBackground', { anchorX: 0, anchorY: 0, x: -150, y: -25, width: 300, height: 50 }); var foregroundGraphics = barForeground.attachAsset('healthBarForeground', { anchorX: 0, anchorY: 0, x: -150, y: -25, width: 300, height: 50 }); self.updateHealth = function (health) { barForeground.scaleX = health / 3; // Assuming max health is 3 if (health < 3) { foregroundGraphics.tint = 0xff0000; // Change color to red when health decreases } else { foregroundGraphics.tint = 0x61d695; // Reset color to original when health is full } }; return self; }); var HomeBase = Container.expand(function () { var self = Container.call(this); var baseGraphics = self.attachAsset('homeBase', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Mineral = Container.expand(function () { var self = Container.call(this); var mineralGraphics = self.attachAsset('mineral', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var ShopIcon = Container.expand(function () { var self = Container.call(this); var shopGraphics = self.attachAsset('shopIcon', { anchorX: 0.5, anchorY: 0.5 }); self.down = function (x, y, obj) { console.log("Shop icon clicked. Open shop to buy more guns."); var shopBox = new Container(); shopBox.x = 2048 / 2; shopBox.y = 2732 / 2; shopBox.width = 600; shopBox.height = 800; shopBox.attachAsset('shopIcon', { anchorX: 0.5, anchorY: 0.5 }); game.addChild(shopBox); var shopMenu = new Container(); var items = [{ name: 'Shotgun', price: 10 }, { name: 'Laser Gun', price: 20 }, { name: 'Rocket Launcher', price: 30 }, { name: 'Plasma Rifle', price: 40 }, { name: 'Sniper Cannon', price: 50 }]; var startY = 2732 / 2 - 100; items.forEach(function (item, index) { var itemText = new Text2('Buy ' + item.name + ': ' + item.price + ' Score', { size: 80, fill: 0xFFFFFF }); itemText.anchor.set(0.5, 0.5); itemText.x = 2048 / 2; itemText.y = startY + index * 100; itemText.down = function (x, y, obj) { if (score >= item.price) { score -= item.price; scoreTxt.setText('Score: ' + score); console.log(item.name + " purchased!"); shopMenu.destroy(); // Apply score multiplier based on the purchased gun if (item.name === 'Shotgun') { scoreMultiplier = 2; } else if (item.name === 'Laser Gun') { scoreMultiplier = 3; } else if (item.name === 'Rocket Launcher') { scoreMultiplier = 4; } } else { console.log("Not enough score to buy " + item.name + "."); } }; shopMenu.addChild(itemText); }); game.addChild(shopMenu); }; return self; }); var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.energy = 100; self.update = function () { // Update logic for spaceship }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Initialize assets used in this game. Scale them according to what is needed for the game. game.setBackgroundColor(0x000000); var homeBase = game.addChild(new HomeBase()); homeBase.x = 2048 / 2; homeBase.y = 2732 - 300; var spaceship = game.addChild(new Spaceship()); spaceship.x = 2048 / 2; spaceship.y = 2732 - 150; var asteroids = []; var minerals = []; var bullets = []; var score = 0; var energy = 100; var scoreMultiplier = 1; // Default score multiplier var selectedGun = null; // Initialize selectedGun variable var health = 3; // Initialize health for the spaceship var healthBar = game.addChild(new HealthBar()); healthBar.x = 2048 / 2; healthBar.y = 50; healthBar.updateHealth(health); var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var energyTxt = new Text2('Energy: 100', { size: 100, fill: 0xFFFFFF }); var shopIcon = game.addChild(new ShopIcon()); shopIcon.x = 2048 - 100; // Position shop icon at the bottom-right corner shopIcon.y = 2732 - 100; var equipIcon = game.addChild(new EquipIcon()); equipIcon.x = 2048 - 500; // Move equip icon further to the left equipIcon.y = 2732 - 200; // Move equip icon up energyTxt.anchor.set(0.5, 0); LK.gui.topRight.addChild(energyTxt); function handleMove(x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } } game.move = handleMove; game.down = function (x, y, obj) { dragNode = spaceship; handleMove(x, y, obj); var newBullet = new Bullet(); newBullet.x = spaceship.x; newBullet.y = spaceship.y - 50; bullets.push(newBullet); game.addChild(newBullet); }; game.up = function (x, y, obj) { dragNode = null; }; game.update = function () { if (LK.ticks % 60 == 0) { var newAsteroid = new Asteroid(); newAsteroid.x = Math.random() * 2048; newAsteroid.y = -50; asteroids.push(newAsteroid); game.addChild(newAsteroid); } for (var i = asteroids.length - 1; i >= 0; i--) { var asteroid = asteroids[i]; if (asteroid.y > 2732) { if (asteroid.intersects(homeBase)) { health -= 1; // Decrease health when asteroid reaches home base healthBar.updateHealth(health); if (health <= 0) { LK.showGameOver(); // Show game over screen and reset game state return; // Exit the update loop } } asteroid.destroy(); asteroids.splice(i, 1); continue; } if (spaceship.intersects(asteroid)) { health -= 1; // Decrease health when asteroid passes through spaceship healthBar.updateHealth(health); if (health <= 0) { LK.showGameOver(); // Show game over screen and reset game state return; // Exit the update loop } asteroid.destroy(); asteroids.splice(i, 1); continue; } } for (var k = bullets.length - 1; k >= 0; k--) { var bullet = bullets[k]; if (bullet.y < -50) { bullet.destroy(); bullets.splice(k, 1); continue; } for (var l = asteroids.length - 1; l >= 0; l--) { var asteroid = asteroids[l]; if (bullet.intersects(asteroid)) { score += 5 * scoreMultiplier; scoreTxt.setText('Score: ' + score); bullet.destroy(); bullets.splice(k, 1); asteroid.destroy(); asteroids.splice(l, 1); if (asteroids.length === 0) { // Advance to level 2 for (var m = 0; m < 10; m++) { // Increase number of asteroids var newAsteroid = new Asteroid(); newAsteroid.x = Math.random() * 2048; newAsteroid.y = -50; newAsteroid.speed = 5; // Increase speed for level 2 asteroids.push(newAsteroid); game.addChild(newAsteroid); } } break; } } } if (LK.ticks % 120 == 0) { var newMineral = new Mineral(); newMineral.x = Math.random() * 2048; newMineral.y = -50; minerals.push(newMineral); game.addChild(newMineral); } for (var j = minerals.length - 1; j >= 0; j--) { var mineral = minerals[j]; if (mineral.y > 2732) { mineral.destroy(); minerals.splice(j, 1); continue; } if (spaceship.intersects(mineral)) { score += 10; scoreTxt.setText('Score: ' + score); mineral.destroy(); minerals.splice(j, 1); } } }; LK.playMusic('bgMusic');
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
};
return self;
});
var EquipIcon = Container.expand(function () {
var self = Container.call(this);
var equipGraphics = self.attachAsset('shopIcon', {
anchorX: 0.5,
anchorY: 0.5,
tint: 0x00ff00 // Set color to green
});
self.down = function (x, y, obj) {
console.log("Equip icon clicked. Showing gun selection box.");
var gunSelectionBox = new GunSelectionBox();
game.addChild(gunSelectionBox);
};
return self;
});
var GunSelectionBox = Container.expand(function () {
var self = Container.call(this);
var boxGraphics = self.attachAsset('shopIcon', {
anchorX: 0.5,
anchorY: 0.5,
tint: 0x333333 // Dark gray for the box
});
self.x = 2048 / 2;
self.y = 2732 / 2;
self.width = 600;
self.height = 800;
var guns = ['Pistol', 'Rifle', 'Shotgun', 'Sniper'];
var startY = -150;
guns.forEach(function (gun, index) {
var gunText = new Text2(gun, {
size: 80,
fill: 0xFFFFFF
});
gunText.anchor.set(0.5, 0.5);
gunText.x = 0;
gunText.y = startY + index * 100;
gunText.down = function (x, y, obj) {
selectedGun = {
name: gun
};
console.log(gun + " selected!");
self.destroy(); // Close the selection box after selecting a gun
};
self.addChild(gunText);
});
return self;
});
var HealthBar = Container.expand(function () {
var self = Container.call(this);
var barBackground = self.addChild(new Container());
var barForeground = self.addChild(new Container());
var backgroundGraphics = barBackground.attachAsset('healthBarBackground', {
anchorX: 0,
anchorY: 0,
x: -150,
y: -25,
width: 300,
height: 50
});
var foregroundGraphics = barForeground.attachAsset('healthBarForeground', {
anchorX: 0,
anchorY: 0,
x: -150,
y: -25,
width: 300,
height: 50
});
self.updateHealth = function (health) {
barForeground.scaleX = health / 3; // Assuming max health is 3
if (health < 3) {
foregroundGraphics.tint = 0xff0000; // Change color to red when health decreases
} else {
foregroundGraphics.tint = 0x61d695; // Reset color to original when health is full
}
};
return self;
});
var HomeBase = Container.expand(function () {
var self = Container.call(this);
var baseGraphics = self.attachAsset('homeBase', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Mineral = Container.expand(function () {
var self = Container.call(this);
var mineralGraphics = self.attachAsset('mineral', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var ShopIcon = Container.expand(function () {
var self = Container.call(this);
var shopGraphics = self.attachAsset('shopIcon', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
console.log("Shop icon clicked. Open shop to buy more guns.");
var shopBox = new Container();
shopBox.x = 2048 / 2;
shopBox.y = 2732 / 2;
shopBox.width = 600;
shopBox.height = 800;
shopBox.attachAsset('shopIcon', {
anchorX: 0.5,
anchorY: 0.5
});
game.addChild(shopBox);
var shopMenu = new Container();
var items = [{
name: 'Shotgun',
price: 10
}, {
name: 'Laser Gun',
price: 20
}, {
name: 'Rocket Launcher',
price: 30
}, {
name: 'Plasma Rifle',
price: 40
}, {
name: 'Sniper Cannon',
price: 50
}];
var startY = 2732 / 2 - 100;
items.forEach(function (item, index) {
var itemText = new Text2('Buy ' + item.name + ': ' + item.price + ' Score', {
size: 80,
fill: 0xFFFFFF
});
itemText.anchor.set(0.5, 0.5);
itemText.x = 2048 / 2;
itemText.y = startY + index * 100;
itemText.down = function (x, y, obj) {
if (score >= item.price) {
score -= item.price;
scoreTxt.setText('Score: ' + score);
console.log(item.name + " purchased!");
shopMenu.destroy();
// Apply score multiplier based on the purchased gun
if (item.name === 'Shotgun') {
scoreMultiplier = 2;
} else if (item.name === 'Laser Gun') {
scoreMultiplier = 3;
} else if (item.name === 'Rocket Launcher') {
scoreMultiplier = 4;
}
} else {
console.log("Not enough score to buy " + item.name + ".");
}
};
shopMenu.addChild(itemText);
});
game.addChild(shopMenu);
};
return self;
});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.energy = 100;
self.update = function () {
// Update logic for spaceship
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Initialize assets used in this game. Scale them according to what is needed for the game.
game.setBackgroundColor(0x000000);
var homeBase = game.addChild(new HomeBase());
homeBase.x = 2048 / 2;
homeBase.y = 2732 - 300;
var spaceship = game.addChild(new Spaceship());
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 150;
var asteroids = [];
var minerals = [];
var bullets = [];
var score = 0;
var energy = 100;
var scoreMultiplier = 1; // Default score multiplier
var selectedGun = null; // Initialize selectedGun variable
var health = 3; // Initialize health for the spaceship
var healthBar = game.addChild(new HealthBar());
healthBar.x = 2048 / 2;
healthBar.y = 50;
healthBar.updateHealth(health);
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var energyTxt = new Text2('Energy: 100', {
size: 100,
fill: 0xFFFFFF
});
var shopIcon = game.addChild(new ShopIcon());
shopIcon.x = 2048 - 100; // Position shop icon at the bottom-right corner
shopIcon.y = 2732 - 100;
var equipIcon = game.addChild(new EquipIcon());
equipIcon.x = 2048 - 500; // Move equip icon further to the left
equipIcon.y = 2732 - 200; // Move equip icon up
energyTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(energyTxt);
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
dragNode = spaceship;
handleMove(x, y, obj);
var newBullet = new Bullet();
newBullet.x = spaceship.x;
newBullet.y = spaceship.y - 50;
bullets.push(newBullet);
game.addChild(newBullet);
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.update = function () {
if (LK.ticks % 60 == 0) {
var newAsteroid = new Asteroid();
newAsteroid.x = Math.random() * 2048;
newAsteroid.y = -50;
asteroids.push(newAsteroid);
game.addChild(newAsteroid);
}
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
if (asteroid.y > 2732) {
if (asteroid.intersects(homeBase)) {
health -= 1; // Decrease health when asteroid reaches home base
healthBar.updateHealth(health);
if (health <= 0) {
LK.showGameOver(); // Show game over screen and reset game state
return; // Exit the update loop
}
}
asteroid.destroy();
asteroids.splice(i, 1);
continue;
}
if (spaceship.intersects(asteroid)) {
health -= 1; // Decrease health when asteroid passes through spaceship
healthBar.updateHealth(health);
if (health <= 0) {
LK.showGameOver(); // Show game over screen and reset game state
return; // Exit the update loop
}
asteroid.destroy();
asteroids.splice(i, 1);
continue;
}
}
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
if (bullet.y < -50) {
bullet.destroy();
bullets.splice(k, 1);
continue;
}
for (var l = asteroids.length - 1; l >= 0; l--) {
var asteroid = asteroids[l];
if (bullet.intersects(asteroid)) {
score += 5 * scoreMultiplier;
scoreTxt.setText('Score: ' + score);
bullet.destroy();
bullets.splice(k, 1);
asteroid.destroy();
asteroids.splice(l, 1);
if (asteroids.length === 0) {
// Advance to level 2
for (var m = 0; m < 10; m++) {
// Increase number of asteroids
var newAsteroid = new Asteroid();
newAsteroid.x = Math.random() * 2048;
newAsteroid.y = -50;
newAsteroid.speed = 5; // Increase speed for level 2
asteroids.push(newAsteroid);
game.addChild(newAsteroid);
}
}
break;
}
}
}
if (LK.ticks % 120 == 0) {
var newMineral = new Mineral();
newMineral.x = Math.random() * 2048;
newMineral.y = -50;
minerals.push(newMineral);
game.addChild(newMineral);
}
for (var j = minerals.length - 1; j >= 0; j--) {
var mineral = minerals[j];
if (mineral.y > 2732) {
mineral.destroy();
minerals.splice(j, 1);
continue;
}
if (spaceship.intersects(mineral)) {
score += 10;
scoreTxt.setText('Score: ' + score);
mineral.destroy();
minerals.splice(j, 1);
}
}
};
LK.playMusic('bgMusic');