/**** * 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 = 3; self.update = function () { self.y += self.speed; if (self.y > 2732 + asteroidGraphics.height) { self.destroy(); } }; }); // 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 = 5; self.update = function () { self.y += self.speed; if (self.y > 2732 + enemyGraphics.height) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Update logic for hero }; self.shoot = function () { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - heroGraphics.height / 2; game.addChild(bullet); heroBullets.push(bullet); }; }); // HeroBullet 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(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var hero; var heroBullets = []; var enemies = []; 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); // Initialize hero hero = new Hero(); hero.x = 2048 / 2; hero.y = 2732 - 200; game.addChild(hero); // Handle game events game.down = function (x, y, obj) { hero.x = x; hero.y = y; }; game.move = function (x, y, obj) { hero.x = x; hero.y = y; }; game.up = function (x, y, obj) { // No action needed on up event }; // Update game state game.update = function () { // Update hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].update(); if (heroBullets[i].y < -heroBullets[i].height) { heroBullets[i].destroy(); heroBullets.splice(i, 1); } } // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (enemies[i].y > 2732 + enemies[i].height) { enemies[i].destroy(); enemies.splice(i, 1); } } // Update asteroids for (var i = asteroids.length - 1; i >= 0; i--) { asteroids[i].update(); if (asteroids[i].y > 2732 + asteroids[i].height) { asteroids[i].destroy(); asteroids.splice(i, 1); } } // Check for collisions for (var i = heroBullets.length - 1; i >= 0; i--) { for (var j = enemies.length - 1; j >= 0; j--) { if (heroBullets[i].intersects(enemies[j])) { heroBullets[i].destroy(); enemies[j].destroy(); heroBullets.splice(i, 1); enemies.splice(j, 1); score += 10; scoreTxt.setText(score); break; } } } // Spawn enemies if (LK.ticks % 60 == 0) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = -enemy.height; game.addChild(enemy); enemies.push(enemy); } // Spawn asteroids if (LK.ticks % 120 == 0) { var asteroid = new Asteroid(); asteroid.x = Math.random() * 2048; asteroid.y = -asteroid.height; game.addChild(asteroid); asteroids.push(asteroid); } }; // Shoot bullets game.down = function (x, y, obj) { hero.shoot(); }; game.move = function (x, y, obj) { hero.x = x; hero.y = y; }; game.up = function (x, y, obj) { // No action needed on up event };
/****
* 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 = 3;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + asteroidGraphics.height) {
self.destroy();
}
};
});
// 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 = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + enemyGraphics.height) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for hero
};
self.shoot = function () {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - heroGraphics.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
};
});
// HeroBullet 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();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var heroBullets = [];
var enemies = [];
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);
// Initialize hero
hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Handle game events
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.up = function (x, y, obj) {
// No action needed on up event
};
// Update game state
game.update = function () {
// Update hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].update();
if (heroBullets[i].y < -heroBullets[i].height) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].y > 2732 + enemies[i].height) {
enemies[i].destroy();
enemies.splice(i, 1);
}
}
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].update();
if (asteroids[i].y > 2732 + asteroids[i].height) {
asteroids[i].destroy();
asteroids.splice(i, 1);
}
}
// Check for collisions
for (var i = heroBullets.length - 1; i >= 0; i--) {
for (var j = enemies.length - 1; j >= 0; j--) {
if (heroBullets[i].intersects(enemies[j])) {
heroBullets[i].destroy();
enemies[j].destroy();
heroBullets.splice(i, 1);
enemies.splice(j, 1);
score += 10;
scoreTxt.setText(score);
break;
}
}
}
// Spawn enemies
if (LK.ticks % 60 == 0) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = -enemy.height;
game.addChild(enemy);
enemies.push(enemy);
}
// Spawn asteroids
if (LK.ticks % 120 == 0) {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height;
game.addChild(asteroid);
asteroids.push(asteroid);
}
};
// Shoot bullets
game.down = function (x, y, obj) {
hero.shoot();
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.up = function (x, y, obj) {
// No action needed on up event
};