/****
* Classes
****/
// Alien class representing the enemies
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Bullet class for player's bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Player class representing the elite operative
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2500;
// Array to hold bullets
var bullets = [];
// Array to hold aliens
var aliens = [];
// Function to spawn aliens
function spawnAlien() {
var alien = new Alien();
alien.x = Math.random() * 2048;
alien.y = 0;
aliens.push(alien);
game.addChild(alien);
}
// Function to handle shooting
function shoot() {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
}
// Game update loop
game.update = function () {
// Update player
player.update();
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].destroyed) {
bullets.splice(i, 1);
}
}
// Update aliens
for (var j = aliens.length - 1; j >= 0; j--) {
aliens[j].update();
if (aliens[j].destroyed) {
aliens.splice(j, 1);
}
}
// Check for collisions
for (var k = bullets.length - 1; k >= 0; k--) {
for (var l = aliens.length - 1; l >= 0; l--) {
if (bullets[k].intersects(aliens[l])) {
bullets[k].destroy();
aliens[l].destroy();
bullets.splice(k, 1);
aliens.splice(l, 1);
break;
}
}
}
// Spawn aliens periodically
if (LK.ticks % 60 == 0) {
spawnAlien();
}
};
// Handle player movement
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
// Handle shooting
game.down = function (x, y, obj) {
shoot();
}; /****
* Classes
****/
// Alien class representing the enemies
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Bullet class for player's bullets
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Player class representing the elite operative
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2500;
// Array to hold bullets
var bullets = [];
// Array to hold aliens
var aliens = [];
// Function to spawn aliens
function spawnAlien() {
var alien = new Alien();
alien.x = Math.random() * 2048;
alien.y = 0;
aliens.push(alien);
game.addChild(alien);
}
// Function to handle shooting
function shoot() {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
bullets.push(bullet);
game.addChild(bullet);
}
// Game update loop
game.update = function () {
// Update player
player.update();
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].destroyed) {
bullets.splice(i, 1);
}
}
// Update aliens
for (var j = aliens.length - 1; j >= 0; j--) {
aliens[j].update();
if (aliens[j].destroyed) {
aliens.splice(j, 1);
}
}
// Check for collisions
for (var k = bullets.length - 1; k >= 0; k--) {
for (var l = aliens.length - 1; l >= 0; l--) {
if (bullets[k].intersects(aliens[l])) {
bullets[k].destroy();
aliens[l].destroy();
bullets.splice(k, 1);
aliens.splice(l, 1);
break;
}
}
}
// Spawn aliens periodically
if (LK.ticks % 60 == 0) {
spawnAlien();
}
};
// Handle player movement
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
// Handle shooting
game.down = function (x, y, obj) {
shoot();
};