/**** * Classes ****/ // 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 = 2; self.update = function () { self.y += self.speed; }; }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.shoot = function () { var bullet = new PlayerBullet(); bullet.x = self.x; bullet.y = self.y - playerGraphics.height / 2; game.addChild(bullet); playerBullets.push(bullet); }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Bullet class for player bullets var PlayerBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('playerBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = new Player(); player.x = 2048 / 2; player.y = 2732 - 150; game.addChild(player); // Arrays to keep track of bullets and enemies var playerBullets = []; var enemies = []; // Score display var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Game update function game.update = function () { // Update player bullets for (var i = playerBullets.length - 1; i >= 0; i--) { playerBullets[i].update(); if (playerBullets[i].y < -50) { playerBullets[i].destroy(); playerBullets.splice(i, 1); } } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); if (enemies[j].y > 2732 + 50) { enemies[j].destroy(); enemies.splice(j, 1); } } // Check for collisions for (var k = playerBullets.length - 1; k >= 0; k--) { for (var l = enemies.length - 1; l >= 0; l--) { if (playerBullets[k].intersects(enemies[l])) { playerBullets[k].destroy(); enemies[l].destroy(); playerBullets.splice(k, 1); enemies.splice(l, 1); LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); break; } } } // Spawn new enemies if (LK.ticks % 60 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -50; game.addChild(enemy); enemies.push(enemy); } }; // Player shooting game.down = function (x, y, obj) { player.shoot(); };
/****
* Classes
****/
// 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 = 2;
self.update = function () {
self.y += self.speed;
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.shoot = function () {
var bullet = new PlayerBullet();
bullet.x = self.x;
bullet.y = self.y - playerGraphics.height / 2;
game.addChild(bullet);
playerBullets.push(bullet);
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Bullet class for player bullets
var PlayerBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('playerBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
player.x = 2048 / 2;
player.y = 2732 - 150;
game.addChild(player);
// Arrays to keep track of bullets and enemies
var playerBullets = [];
var enemies = [];
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game update function
game.update = function () {
// Update player bullets
for (var i = playerBullets.length - 1; i >= 0; i--) {
playerBullets[i].update();
if (playerBullets[i].y < -50) {
playerBullets[i].destroy();
playerBullets.splice(i, 1);
}
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
if (enemies[j].y > 2732 + 50) {
enemies[j].destroy();
enemies.splice(j, 1);
}
}
// Check for collisions
for (var k = playerBullets.length - 1; k >= 0; k--) {
for (var l = enemies.length - 1; l >= 0; l--) {
if (playerBullets[k].intersects(enemies[l])) {
playerBullets[k].destroy();
enemies[l].destroy();
playerBullets.splice(k, 1);
enemies.splice(l, 1);
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
break;
}
}
}
// Spawn new enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -50;
game.addChild(enemy);
enemies.push(enemy);
}
};
// Player shooting
game.down = function (x, y, obj) {
player.shoot();
};