/**** * Classes ****/ // Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); } }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -100; self.x = Math.random() * 2048; } }; }); // PowerUp class var PowerUp = Container.expand(function () { var self = Container.call(this); var powerUpGraphics = self.attachAsset('powerUp', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { self.y += 3; if (self.y > 2732) { self.y = -100; self.x = Math.random() * 2048; } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Spaceship class var Spaceship = Container.expand(function () { var self = Container.call(this); var spaceshipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Spaceship update logic }; self.move = function (x, y) { self.x = x; self.y = y; }; self.shoot = function () { var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y; game.addChild(bullet); }; }); // Star class var Star = Container.expand(function () { var self = Container.call(this); var starGraphics = self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5, scaleX: Math.random() * 0.5 + 0.5, scaleY: Math.random() * 0.5 + 0.5 }); self.speed = Math.random() * 3 + 1; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -100; self.x = Math.random() * 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize game elements if (!spaceship) { var spaceship = game.addChild(new Spaceship()); spaceship.x = 1024; spaceship.y = 2400; } var stars = []; for (var i = 0; i < 200; i++) { var star = new Star(); star.x = Math.random() * 2048; star.y = Math.random() * -2732; stars.push(star); game.addChild(star); } var obstacles = []; for (var i = 0; i < 5; i++) { var obstacle = new Obstacle(); obstacle.x = Math.random() * 2048; obstacle.y = Math.random() * -2732; obstacles.push(obstacle); game.addChild(obstacle); } var powerUps = []; for (var i = 0; i < 3; i++) { var powerUp = new PowerUp(); powerUp.x = Math.random() * 2048; powerUp.y = Math.random() * -2732; powerUps.push(powerUp); game.addChild(powerUp); } // Handle game events game.down = function (x, y, obj) { spaceship.move(x, y); spaceship.shoot(); }; game.update = function () { for (var i = 0; i < obstacles.length; i++) { obstacles[i].update(); if (spaceship.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } for (var j = 0; j < game.children.length; j++) { if (game.children[j] instanceof Bullet && game.children[j].intersects(obstacles[i])) { obstacles[i].destroy(); obstacles.splice(i, 1); game.children[j].destroy(); break; } } } for (var i = 0; i < stars.length; i++) { stars[i].update(); } for (var i = 0; i < powerUps.length; i++) { powerUps[i].update(); if (spaceship.intersects(powerUps[i])) { LK.setScore(LK.getScore() + 10); powerUps[i].destroy(); powerUps.splice(i, 1); } } };
/****
* Classes
****/
// Bullet class
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -100;
self.x = Math.random() * 2048;
}
};
});
// PowerUp class
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 3;
if (self.y > 2732) {
self.y = -100;
self.x = Math.random() * 2048;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Spaceship class
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var spaceshipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Spaceship update logic
};
self.move = function (x, y) {
self.x = x;
self.y = y;
};
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y;
game.addChild(bullet);
};
});
// Star class
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: Math.random() * 0.5 + 0.5,
scaleY: Math.random() * 0.5 + 0.5
});
self.speed = Math.random() * 3 + 1;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -100;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game elements
if (!spaceship) {
var spaceship = game.addChild(new Spaceship());
spaceship.x = 1024;
spaceship.y = 2400;
}
var stars = [];
for (var i = 0; i < 200; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * -2732;
stars.push(star);
game.addChild(star);
}
var obstacles = [];
for (var i = 0; i < 5; i++) {
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * -2732;
obstacles.push(obstacle);
game.addChild(obstacle);
}
var powerUps = [];
for (var i = 0; i < 3; i++) {
var powerUp = new PowerUp();
powerUp.x = Math.random() * 2048;
powerUp.y = Math.random() * -2732;
powerUps.push(powerUp);
game.addChild(powerUp);
}
// Handle game events
game.down = function (x, y, obj) {
spaceship.move(x, y);
spaceship.shoot();
};
game.update = function () {
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
if (spaceship.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
for (var j = 0; j < game.children.length; j++) {
if (game.children[j] instanceof Bullet && game.children[j].intersects(obstacles[i])) {
obstacles[i].destroy();
obstacles.splice(i, 1);
game.children[j].destroy();
break;
}
}
}
for (var i = 0; i < stars.length; i++) {
stars[i].update();
}
for (var i = 0; i < powerUps.length; i++) {
powerUps[i].update();
if (spaceship.intersects(powerUps[i])) {
LK.setScore(LK.getScore() + 10);
powerUps[i].destroy();
powerUps.splice(i, 1);
}
}
};