/**** * Plugins ****/ var storage = LK.import("@upit/storage.v1", { selectedShip: 0 }); var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Asteroid class 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; if (self.y > 2732 + asteroidGraphics.height) { self.destroy(); } }; }); // Enemy class 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.y += self.speed; if (self.y > 2732 + enemyGraphics.height) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Hero class var Hero = Container.expand(function () { var self = Container.call(this); self.lives = 5; self.lastHit = 0; self.invulnerable = false; self.shipType = storage.selectedShip || 0; var shipAssets = ['hero', 'hero2', 'hero3']; var heroGraphics = self.attachAsset(shipAssets[self.shipType], { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.changeShip = function (shipIndex) { self.removeChild(heroGraphics); self.shipType = shipIndex; heroGraphics = self.attachAsset(shipAssets[shipIndex], { anchorX: 0.5, anchorY: 0.5 }); }; self.update = function () { // Handle invulnerability flashing if (self.invulnerable) { heroGraphics.alpha = LK.ticks % 10 < 5 ? 0.5 : 1.0; if (LK.ticks - self.lastHit > 120) { // 2 seconds of invulnerability self.invulnerable = false; heroGraphics.alpha = 1.0; } } }; self.takeDamage = function () { if (!self.invulnerable) { self.lives--; self.lastHit = LK.ticks; self.invulnerable = true; LK.effects.flashObject(self, 0xff0000, 500); if (self.lives <= 0) { LK.showGameOver(); } } }; self.shoot = function () { if (tripleShot && LK.ticks < tripleShootEndTime) { // Triple shot for (var i = -1; i <= 1; i++) { var bullet = new HeroBullet(); bullet.x = self.x + i * 50; bullet.y = self.y - heroGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); } } else { // Normal shot var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - heroGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); } }; return self; }); // HeroBullet class var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('heroBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -15; self.update = function () { self.y += self.speed; if (self.y < -bulletGraphics.height) { self.destroy(); } }; }); // PowerUp class var PowerUp = Container.expand(function () { var self = Container.call(this); var powerUpGraphics = self.attachAsset('powerUp', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 4; self.update = function () { self.y += self.speed; if (self.y > 2732 + powerUpGraphics.height) { self.destroy(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var hero; var heroBullets = []; var enemies = []; var asteroids = []; var powerUps = []; var score = 0; var gameStarted = false; var showingShipSelection = false; var tripleShot = false; var tripleShootEndTime = 0; // UI Elements var scoreTxt; var livesTxt; var startScreen; var shipSelectionScreen; function createStartScreen() { startScreen = new Container(); game.addChild(startScreen); var titleText = new Text2('DEFEND THE GALAXY', { size: 120, fill: 0xFFFFFF }); titleText.anchor.set(0.5, 0.5); titleText.x = 2048 / 2; titleText.y = 2732 / 3; startScreen.addChild(titleText); var startButton = new Container(); var startButtonBg = LK.getAsset('hero', { anchorX: 0.5, anchorY: 0.5, scaleX: 4, scaleY: 2 }); startButton.addChild(startButtonBg); var startButtonText = new Text2('START GAME', { size: 60, fill: 0xFFFFFF }); startButtonText.anchor.set(0.5, 0.5); startButton.addChild(startButtonText); startButton.x = 2048 / 2; startButton.y = 2732 / 2; startScreen.addChild(startButton); startButton.down = function () { startGame(); }; var shipsButton = new Container(); var shipsButtonBg = LK.getAsset('hero2', { anchorX: 0.5, anchorY: 0.5, scaleX: 4, scaleY: 2 }); shipsButton.addChild(shipsButtonBg); var shipsButtonText = new Text2('SHIPS', { size: 60, fill: 0xFFFFFF }); shipsButtonText.anchor.set(0.5, 0.5); shipsButton.addChild(shipsButtonText); shipsButton.x = 2048 / 2; shipsButton.y = 2732 / 2 + 200; startScreen.addChild(shipsButton); shipsButton.down = function () { showShipSelection(); }; } function createShipSelectionScreen() { shipSelectionScreen = new Container(); game.addChild(shipSelectionScreen); var titleText = new Text2('SELECT YOUR SHIP', { size: 100, fill: 0xFFFFFF }); titleText.anchor.set(0.5, 0.5); titleText.x = 2048 / 2; titleText.y = 400; shipSelectionScreen.addChild(titleText); var shipAssets = ['hero', 'hero2', 'hero3']; var shipColors = ['Blue Fighter', 'Red Cruiser', 'Cyan Interceptor']; for (var i = 0; i < 3; i++) { var shipButton = new Container(); var shipGraphics = LK.getAsset(shipAssets[i], { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); shipButton.addChild(shipGraphics); var shipName = new Text2(shipColors[i], { size: 60, fill: 0xFFFFFF }); shipName.anchor.set(0.5, 0.5); shipName.y = 120; shipButton.addChild(shipName); shipButton.x = 2048 / 4 + i * 2048 / 4; shipButton.y = 2732 / 2; shipSelectionScreen.addChild(shipButton); shipButton.shipIndex = i; shipButton.down = function () { storage.selectedShip = this.shipIndex; hideShipSelection(); }; } var backButton = new Container(); var backButtonBg = LK.getAsset('asteroid', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 1 }); backButton.addChild(backButtonBg); var backButtonText = new Text2('BACK', { size: 60, fill: 0xFFFFFF }); backButtonText.anchor.set(0.5, 0.5); backButton.addChild(backButtonText); backButton.x = 2048 / 2; backButton.y = 2732 - 300; shipSelectionScreen.addChild(backButton); backButton.down = function () { hideShipSelection(); }; shipSelectionScreen.visible = false; } function showShipSelection() { showingShipSelection = true; startScreen.visible = false; shipSelectionScreen.visible = true; } function hideShipSelection() { showingShipSelection = false; startScreen.visible = true; shipSelectionScreen.visible = false; } function startGame() { gameStarted = true; startScreen.destroy(); shipSelectionScreen.destroy(); // Initialize UI scoreTxt = new Text2('Score: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); livesTxt = new Text2('Lives: 5', { size: 80, fill: 0xFFFFFF }); livesTxt.anchor.set(0.5, 0); livesTxt.y = 100; LK.gui.top.addChild(livesTxt); // Initialize hero hero = new Hero(); hero.x = 2048 / 2; hero.y = 2732 - 200; game.addChild(hero); } // Create screens createStartScreen(); createShipSelectionScreen(); // Handle game events game.move = function (x, y, obj) { if (gameStarted && hero) { hero.x = x; hero.y = y; } }; // Update game state game.update = function () { if (!gameStarted) return; // Automatic shooting if (LK.ticks % 20 == 0) { // Shoot every 20 ticks hero.shoot(); } // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); if (heroBullets[i].y < -heroBullets[i].height) { heroBullets[i].destroy(); heroBullets.splice(i, 1); } } // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (enemies[i].y > 2732 + enemies[i].height) { enemies[i].destroy(); enemies.splice(i, 1); } } // Update asteroids for (var i = asteroids.length - 1; i >= 0; i--) { asteroids[i].update(); if (asteroids[i].y > 2732 + asteroids[i].height) { asteroids[i].destroy(); asteroids.splice(i, 1); } } // Update power-ups for (var i = powerUps.length - 1; i >= 0; i--) { powerUps[i].update(); if (powerUps[i].y > 2732 + powerUps[i].height) { powerUps[i].destroy(); powerUps.splice(i, 1); } } // Check bullet vs enemy collisions for (var i = heroBullets.length - 1; i >= 0; i--) { for (var j = enemies.length - 1; j >= 0; j--) { if (heroBullets[i] && enemies[j] && heroBullets[i].intersects(enemies[j])) { heroBullets[i].destroy(); enemies[j].destroy(); heroBullets.splice(i, 1); enemies.splice(j, 1); score += 10; scoreTxt.setText('Score: ' + score); break; } } } // Check hero vs enemy collisions for (var i = enemies.length - 1; i >= 0; i--) { if (hero.intersects(enemies[i])) { hero.takeDamage(); enemies[i].destroy(); enemies.splice(i, 1); livesTxt.setText('Lives: ' + hero.lives); } } // Check hero vs asteroid collisions for (var i = asteroids.length - 1; i >= 0; i--) { if (hero.intersects(asteroids[i])) { hero.takeDamage(); asteroids[i].destroy(); asteroids.splice(i, 1); livesTxt.setText('Lives: ' + hero.lives); } } // Check hero vs power-up collisions for (var i = powerUps.length - 1; i >= 0; i--) { if (hero.intersects(powerUps[i])) { tripleShot = true; tripleShootEndTime = LK.ticks + 600; // 10 seconds at 60fps powerUps[i].destroy(); powerUps.splice(i, 1); LK.effects.flashObject(hero, 0x9b59b6, 500); } } // Spawn enemies if (LK.ticks % 60 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -100; game.addChild(enemy); enemies.push(enemy); } // Spawn asteroids if (LK.ticks % 120 == 0) { var asteroid = new Asteroid(); asteroid.x = Math.random() * 2048; asteroid.y = -100; game.addChild(asteroid); asteroids.push(asteroid); } // Spawn power-ups (less frequently) if (LK.ticks % 300 == 0) { // Every 5 seconds var powerUp = new PowerUp(); powerUp.x = Math.random() * 2048; powerUp.y = -100; game.addChild(powerUp); powerUps.push(powerUp); } };
/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1", {
selectedShip: 0
});
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Asteroid class
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;
if (self.y > 2732 + asteroidGraphics.height) {
self.destroy();
}
};
});
// Enemy class
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.y += self.speed;
if (self.y > 2732 + enemyGraphics.height) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
self.lives = 5;
self.lastHit = 0;
self.invulnerable = false;
self.shipType = storage.selectedShip || 0;
var shipAssets = ['hero', 'hero2', 'hero3'];
var heroGraphics = self.attachAsset(shipAssets[self.shipType], {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.changeShip = function (shipIndex) {
self.removeChild(heroGraphics);
self.shipType = shipIndex;
heroGraphics = self.attachAsset(shipAssets[shipIndex], {
anchorX: 0.5,
anchorY: 0.5
});
};
self.update = function () {
// Handle invulnerability flashing
if (self.invulnerable) {
heroGraphics.alpha = LK.ticks % 10 < 5 ? 0.5 : 1.0;
if (LK.ticks - self.lastHit > 120) {
// 2 seconds of invulnerability
self.invulnerable = false;
heroGraphics.alpha = 1.0;
}
}
};
self.takeDamage = function () {
if (!self.invulnerable) {
self.lives--;
self.lastHit = LK.ticks;
self.invulnerable = true;
LK.effects.flashObject(self, 0xff0000, 500);
if (self.lives <= 0) {
LK.showGameOver();
}
}
};
self.shoot = function () {
if (tripleShot && LK.ticks < tripleShootEndTime) {
// Triple shot
for (var i = -1; i <= 1; i++) {
var bullet = new HeroBullet();
bullet.x = self.x + i * 50;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
}
} else {
// Normal shot
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
}
};
return self;
});
// HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('heroBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < -bulletGraphics.height) {
self.destroy();
}
};
});
// PowerUp class
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 4;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + powerUpGraphics.height) {
self.destroy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemies = [];
var asteroids = [];
var powerUps = [];
var score = 0;
var gameStarted = false;
var showingShipSelection = false;
var tripleShot = false;
var tripleShootEndTime = 0;
// UI Elements
var scoreTxt;
var livesTxt;
var startScreen;
var shipSelectionScreen;
function createStartScreen() {
startScreen = new Container();
game.addChild(startScreen);
var titleText = new Text2('DEFEND THE GALAXY', {
size: 120,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 2732 / 3;
startScreen.addChild(titleText);
var startButton = new Container();
var startButtonBg = LK.getAsset('hero', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4,
scaleY: 2
});
startButton.addChild(startButtonBg);
var startButtonText = new Text2('START GAME', {
size: 60,
fill: 0xFFFFFF
});
startButtonText.anchor.set(0.5, 0.5);
startButton.addChild(startButtonText);
startButton.x = 2048 / 2;
startButton.y = 2732 / 2;
startScreen.addChild(startButton);
startButton.down = function () {
startGame();
};
var shipsButton = new Container();
var shipsButtonBg = LK.getAsset('hero2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4,
scaleY: 2
});
shipsButton.addChild(shipsButtonBg);
var shipsButtonText = new Text2('SHIPS', {
size: 60,
fill: 0xFFFFFF
});
shipsButtonText.anchor.set(0.5, 0.5);
shipsButton.addChild(shipsButtonText);
shipsButton.x = 2048 / 2;
shipsButton.y = 2732 / 2 + 200;
startScreen.addChild(shipsButton);
shipsButton.down = function () {
showShipSelection();
};
}
function createShipSelectionScreen() {
shipSelectionScreen = new Container();
game.addChild(shipSelectionScreen);
var titleText = new Text2('SELECT YOUR SHIP', {
size: 100,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 400;
shipSelectionScreen.addChild(titleText);
var shipAssets = ['hero', 'hero2', 'hero3'];
var shipColors = ['Blue Fighter', 'Red Cruiser', 'Cyan Interceptor'];
for (var i = 0; i < 3; i++) {
var shipButton = new Container();
var shipGraphics = LK.getAsset(shipAssets[i], {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
shipButton.addChild(shipGraphics);
var shipName = new Text2(shipColors[i], {
size: 60,
fill: 0xFFFFFF
});
shipName.anchor.set(0.5, 0.5);
shipName.y = 120;
shipButton.addChild(shipName);
shipButton.x = 2048 / 4 + i * 2048 / 4;
shipButton.y = 2732 / 2;
shipSelectionScreen.addChild(shipButton);
shipButton.shipIndex = i;
shipButton.down = function () {
storage.selectedShip = this.shipIndex;
hideShipSelection();
};
}
var backButton = new Container();
var backButtonBg = LK.getAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 1
});
backButton.addChild(backButtonBg);
var backButtonText = new Text2('BACK', {
size: 60,
fill: 0xFFFFFF
});
backButtonText.anchor.set(0.5, 0.5);
backButton.addChild(backButtonText);
backButton.x = 2048 / 2;
backButton.y = 2732 - 300;
shipSelectionScreen.addChild(backButton);
backButton.down = function () {
hideShipSelection();
};
shipSelectionScreen.visible = false;
}
function showShipSelection() {
showingShipSelection = true;
startScreen.visible = false;
shipSelectionScreen.visible = true;
}
function hideShipSelection() {
showingShipSelection = false;
startScreen.visible = true;
shipSelectionScreen.visible = false;
}
function startGame() {
gameStarted = true;
startScreen.destroy();
shipSelectionScreen.destroy();
// Initialize UI
scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
livesTxt = new Text2('Lives: 5', {
size: 80,
fill: 0xFFFFFF
});
livesTxt.anchor.set(0.5, 0);
livesTxt.y = 100;
LK.gui.top.addChild(livesTxt);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
}
// Create screens
createStartScreen();
createShipSelectionScreen();
// Handle game events
game.move = function (x, y, obj) {
if (gameStarted && hero) {
hero.x = x;
hero.y = y;
}
};
// Update game state
game.update = function () {
if (!gameStarted) return;
// Automatic shooting
if (LK.ticks % 20 == 0) {
// Shoot every 20 ticks
hero.shoot();
}
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (heroBullets[i].y < -heroBullets[i].height) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].y > 2732 + enemies[i].height) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].update();
if (asteroids[i].y > 2732 + asteroids[i].height) {
asteroids[i].destroy();
asteroids.splice(i, 1);
}
}
// Update power-ups
for (var i = powerUps.length - 1; i >= 0; i--) {
powerUps[i].update();
if (powerUps[i].y > 2732 + powerUps[i].height) {
powerUps[i].destroy();
powerUps.splice(i, 1);
}
}
// Check bullet vs enemy collisions
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i] && enemies[j] && heroBullets[i].intersects(enemies[j])) {
heroBullets[i].destroy();
enemies[j].destroy();
heroBullets.splice(i, 1);
enemies.splice(j, 1);
score += 10;
scoreTxt.setText('Score: ' + score);
break;
}
}
}
// Check hero vs enemy collisions
for (var i = enemies.length - 1; i >= 0; i--) {
if (hero.intersects(enemies[i])) {
hero.takeDamage();
enemies[i].destroy();
enemies.splice(i, 1);
livesTxt.setText('Lives: ' + hero.lives);
}
}
// Check hero vs asteroid collisions
for (var i = asteroids.length - 1; i >= 0; i--) {
if (hero.intersects(asteroids[i])) {
hero.takeDamage();
asteroids[i].destroy();
asteroids.splice(i, 1);
livesTxt.setText('Lives: ' + hero.lives);
}
}
// Check hero vs power-up collisions
for (var i = powerUps.length - 1; i >= 0; i--) {
if (hero.intersects(powerUps[i])) {
tripleShot = true;
tripleShootEndTime = LK.ticks + 600; // 10 seconds at 60fps
powerUps[i].destroy();
powerUps.splice(i, 1);
LK.effects.flashObject(hero, 0x9b59b6, 500);
}
}
// Spawn enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -100;
game.addChild(enemy);
enemies.push(enemy);
}
// Spawn asteroids
if (LK.ticks % 120 == 0) {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -100;
game.addChild(asteroid);
asteroids.push(asteroid);
}
// Spawn power-ups (less frequently)
if (LK.ticks % 300 == 0) {
// Every 5 seconds
var powerUp = new PowerUp();
powerUp.x = Math.random() * 2048;
powerUp.y = -100;
game.addChild(powerUp);
powerUps.push(powerUp);
}
};