/**** * 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 = 5; self.update = function () { self.y += self.speed; if (self.y > 2732 + asteroidGraphics.height) { self.destroy(); asteroids.splice(asteroids.indexOf(self), 1); } }; }); // Hero Bullet 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(); heroBullets.splice(heroBullets.indexOf(self), 1); } }; }); //<Assets used in the game will automatically appear here> // Space Shuttle class var SpaceShuttle = Container.expand(function () { var self = Container.call(this); var shuttleGraphics = self.attachAsset('shuttle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Update logic for the space shuttle }; self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - shuttleGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var heroBullets = []; var asteroids = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create space shuttle var spaceShuttle = new SpaceShuttle(); spaceShuttle.x = 2048 / 2; spaceShuttle.y = 2732 - 200; game.addChild(spaceShuttle); // Handle touch events for shooting game.down = function (x, y, obj) { spaceShuttle.shoot(); }; // Update game logic game.update = function () { // Update space shuttle spaceShuttle.update(); // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); } // Update asteroids for (var j = asteroids.length - 1; j >= 0; j--) { asteroids[j].update(); } // Check for collisions for (var k = heroBullets.length - 1; k >= 0; k--) { for (var l = asteroids.length - 1; l >= 0; l--) { if (heroBullets[k].intersects(asteroids[l])) { heroBullets[k].destroy(); asteroids[l].destroy(); heroBullets.splice(k, 1); asteroids.splice(l, 1); score++; scoreTxt.setText(score); break; } } } // Spawn new asteroids if (LK.ticks % 60 == 0) { var asteroid = new Asteroid(); asteroid.x = Math.random() * 2048; asteroid.y = -asteroid.height; game.addChild(asteroid); asteroids.push(asteroid); } };
/****
* 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 = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + asteroidGraphics.height) {
self.destroy();
asteroids.splice(asteroids.indexOf(self), 1);
}
};
});
// Hero Bullet 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();
heroBullets.splice(heroBullets.indexOf(self), 1);
}
};
});
//<Assets used in the game will automatically appear here>
// Space Shuttle class
var SpaceShuttle = Container.expand(function () {
var self = Container.call(this);
var shuttleGraphics = self.attachAsset('shuttle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for the space shuttle
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - shuttleGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var heroBullets = [];
var asteroids = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create space shuttle
var spaceShuttle = new SpaceShuttle();
spaceShuttle.x = 2048 / 2;
spaceShuttle.y = 2732 - 200;
game.addChild(spaceShuttle);
// Handle touch events for shooting
game.down = function (x, y, obj) {
spaceShuttle.shoot();
};
// Update game logic
game.update = function () {
// Update space shuttle
spaceShuttle.update();
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
}
// Update asteroids
for (var j = asteroids.length - 1; j >= 0; j--) {
asteroids[j].update();
}
// Check for collisions
for (var k = heroBullets.length - 1; k >= 0; k--) {
for (var l = asteroids.length - 1; l >= 0; l--) {
if (heroBullets[k].intersects(asteroids[l])) {
heroBullets[k].destroy();
asteroids[l].destroy();
heroBullets.splice(k, 1);
asteroids.splice(l, 1);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Spawn new asteroids
if (LK.ticks % 60 == 0) {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height;
game.addChild(asteroid);
asteroids.push(asteroid);
}
};