/**** * Classes ****/ // Enemy class representing alien ships var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('alienShip', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; }; }); // EnemyBullet class for bullets fired by enemies var EnemyBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('enemyBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Hero class representing the player's spaceship var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('heroShip', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - heroGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); }; }); // HeroBullet class for bullets fired by the hero 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; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var hero = new Hero(); var enemies = []; var heroBullets = []; var enemyBullets = []; var score = 0; // Position the hero at the bottom center of the screen hero.x = 2048 / 2; hero.y = 2732 - 150; game.addChild(hero); // Function to spawn enemies function spawnEnemy() { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -50; game.addChild(enemy); enemies.push(enemy); } // Function to handle game updates game.update = function () { // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { var bullet = heroBullets[i]; bullet.update(); if (bullet.y < -50) { bullet.destroy(); heroBullets.splice(i, 1); } } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { var enemy = enemies[j]; enemy.update(); if (enemy.y > 2732 + 50) { enemy.destroy(); enemies.splice(j, 1); } } // Check for collisions between hero bullets and enemies for (var k = heroBullets.length - 1; k >= 0; k--) { var heroBullet = heroBullets[k]; for (var l = enemies.length - 1; l >= 0; l--) { var enemy = enemies[l]; if (heroBullet.intersects(enemy)) { heroBullet.destroy(); enemy.destroy(); heroBullets.splice(k, 1); enemies.splice(l, 1); score += 10; break; } } } }; // Handle touch events for shooting game.down = function (x, y, obj) { hero.shoot(); }; // Spawn enemies at intervals LK.setInterval(spawnEnemy, 2000);
/****
* Classes
****/
// Enemy class representing alien ships
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('alienShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
});
// EnemyBullet class for bullets fired by enemies
var EnemyBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('enemyBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Hero class representing the player's spaceship
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('heroShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet class for bullets fired by the hero
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;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero = new Hero();
var enemies = [];
var heroBullets = [];
var enemyBullets = [];
var score = 0;
// Position the hero at the bottom center of the screen
hero.x = 2048 / 2;
hero.y = 2732 - 150;
game.addChild(hero);
// Function to spawn enemies
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -50;
game.addChild(enemy);
enemies.push(enemy);
}
// Function to handle game updates
game.update = function () {
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
var bullet = heroBullets[i];
bullet.update();
if (bullet.y < -50) {
bullet.destroy();
heroBullets.splice(i, 1);
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
enemy.update();
if (enemy.y > 2732 + 50) {
enemy.destroy();
enemies.splice(j, 1);
}
}
// Check for collisions between hero bullets and enemies
for (var k = heroBullets.length - 1; k >= 0; k--) {
var heroBullet = heroBullets[k];
for (var l = enemies.length - 1; l >= 0; l--) {
var enemy = enemies[l];
if (heroBullet.intersects(enemy)) {
heroBullet.destroy();
enemy.destroy();
heroBullets.splice(k, 1);
enemies.splice(l, 1);
score += 10;
break;
}
}
}
};
// Handle touch events for shooting
game.down = function (x, y, obj) {
hero.shoot();
};
// Spawn enemies at intervals
LK.setInterval(spawnEnemy, 2000);