/**** * Classes ****/ // Enemy ship class var EnemyShip = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemyShip', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.y += self.speed; if (self.y > 2732) { // Reset enemy position if it goes off screen self.y = -50; self.x = Math.random() * 2048; } }; }); // Player bullet class 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; }; }); // Player spaceship class var PlayerShip = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.attachAsset('playerShip', { anchorX: 0.5, anchorY: 0.5 }); self.move = function (x, y) { self.x = x; self.y = y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize player spaceship asset // Initialize enemy ship asset // Initialize bullet asset for the player var playerShip = game.addChild(new PlayerShip()); playerShip.x = 1024; // Center horizontally playerShip.y = 2400; // Position towards the bottom var enemies = []; var bullets = []; var score = 0; // Create enemies for (var i = 0; i < 5; i++) { var enemy = new EnemyShip(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * -500; // Start off-screen enemies.push(enemy); game.addChild(enemy); } // Touch event to move player ship game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); playerShip.move(pos.x, pos.y); }); // Shoot bullet on touch game.on('up', function () { var bullet = new PlayerBullet(); bullet.x = playerShip.x; bullet.y = playerShip.y - 50; bullets.push(bullet); game.addChild(bullet); }); // Game tick event LK.on('tick', function () { // Update bullets for (var b = bullets.length - 1; b >= 0; b--) { bullets[b].update(); if (bullets[b].y < 0) { bullets[b].destroy(); bullets.splice(b, 1); } } // Update enemies for (var e = enemies.length - 1; e >= 0; e--) { enemies[e].update(); // Check collision with bullets for (var b = bullets.length - 1; b >= 0; b--) { if (enemies[e].intersects(bullets[b])) { enemies[e].destroy(); enemies.splice(e, 1); bullets[b].destroy(); bullets.splice(b, 1); score += 1; break; } } } // Add more enemies if needed if (enemies.length < 5) { var enemy = new EnemyShip(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * -500; // Start off-screen enemies.push(enemy); game.addChild(enemy); } });
/****
* Classes
****/
// Enemy ship class
var EnemyShip = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemyShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
// Reset enemy position if it goes off screen
self.y = -50;
self.x = Math.random() * 2048;
}
};
});
// Player bullet class
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;
};
});
// Player spaceship class
var PlayerShip = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('playerShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize player spaceship asset
// Initialize enemy ship asset
// Initialize bullet asset for the player
var playerShip = game.addChild(new PlayerShip());
playerShip.x = 1024; // Center horizontally
playerShip.y = 2400; // Position towards the bottom
var enemies = [];
var bullets = [];
var score = 0;
// Create enemies
for (var i = 0; i < 5; i++) {
var enemy = new EnemyShip();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * -500; // Start off-screen
enemies.push(enemy);
game.addChild(enemy);
}
// Touch event to move player ship
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
playerShip.move(pos.x, pos.y);
});
// Shoot bullet on touch
game.on('up', function () {
var bullet = new PlayerBullet();
bullet.x = playerShip.x;
bullet.y = playerShip.y - 50;
bullets.push(bullet);
game.addChild(bullet);
});
// Game tick event
LK.on('tick', function () {
// Update bullets
for (var b = bullets.length - 1; b >= 0; b--) {
bullets[b].update();
if (bullets[b].y < 0) {
bullets[b].destroy();
bullets.splice(b, 1);
}
}
// Update enemies
for (var e = enemies.length - 1; e >= 0; e--) {
enemies[e].update();
// Check collision with bullets
for (var b = bullets.length - 1; b >= 0; b--) {
if (enemies[e].intersects(bullets[b])) {
enemies[e].destroy();
enemies.splice(e, 1);
bullets[b].destroy();
bullets.splice(b, 1);
score += 1;
break;
}
}
}
// Add more enemies if needed
if (enemies.length < 5) {
var enemy = new EnemyShip();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * -500; // Start off-screen
enemies.push(enemy);
game.addChild(enemy);
}
});