/**** * Classes ****/ // Class for the Alien var Alien = Container.expand(function () { var self = Container.call(this); var alienGraphics = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Alien update logic }; return self; }); //<Assets used in the game will automatically appear here> // Class for the Hero var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Hero update logic }; return self; }); // Class for the Shape var Shape = Container.expand(function () { var self = Container.call(this); var shapeGraphics = self.attachAsset('shape', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Shape update logic }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var hero; var aliens = []; var shapes = []; var scoreTxt; var score = 0; // Initialize hero hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Initialize score text scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to spawn an alien function spawnAlien() { var alien = new Alien(); alien.x = Math.random() * 2048; alien.y = -100; aliens.push(alien); game.addChild(alien); } // Function to spawn a shape function spawnShape() { var shape = new Shape(); shape.x = Math.random() * 2048; shape.y = -100; shapes.push(shape); game.addChild(shape); } // Update function for the game game.update = function () { // Update hero hero.update(); // Update aliens for (var i = aliens.length - 1; i >= 0; i--) { aliens[i].update(); if (aliens[i].y > 2732) { aliens[i].destroy(); aliens.splice(i, 1); } } // Update shapes for (var j = shapes.length - 1; j >= 0; j--) { shapes[j].update(); if (shapes[j].y > 2732) { shapes[j].destroy(); shapes.splice(j, 1); } } // Check for collisions between hero and shapes for (var k = shapes.length - 1; k >= 0; k--) { if (hero.intersects(shapes[k])) { score += 1; scoreTxt.setText(score); shapes[k].destroy(); shapes.splice(k, 1); } } // Check for collisions between hero and aliens for (var l = aliens.length - 1; l >= 0; l--) { if (hero.intersects(aliens[l])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn aliens and shapes periodically if (LK.ticks % 60 == 0) { spawnAlien(); } if (LK.ticks % 120 == 0) { spawnShape(); } }; // Event listeners for dragging the hero var dragNode = null; game.down = function (x, y, obj) { dragNode = hero; handleMove(x, y, obj); }; game.move = function (x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } }; game.up = function (x, y, obj) { dragNode = null; }; function handleMove(x, y, obj) { if (dragNode) { dragNode.x = x; dragNode.y = y; } }
/****
* Classes
****/
// Class for the Alien
var Alien = Container.expand(function () {
var self = Container.call(this);
var alienGraphics = self.attachAsset('alien', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Alien update logic
};
return self;
});
//<Assets used in the game will automatically appear here>
// Class for the Hero
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Hero update logic
};
return self;
});
// Class for the Shape
var Shape = Container.expand(function () {
var self = Container.call(this);
var shapeGraphics = self.attachAsset('shape', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Shape update logic
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize arrays and variables
var hero;
var aliens = [];
var shapes = [];
var scoreTxt;
var score = 0;
// Initialize hero
hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn an alien
function spawnAlien() {
var alien = new Alien();
alien.x = Math.random() * 2048;
alien.y = -100;
aliens.push(alien);
game.addChild(alien);
}
// Function to spawn a shape
function spawnShape() {
var shape = new Shape();
shape.x = Math.random() * 2048;
shape.y = -100;
shapes.push(shape);
game.addChild(shape);
}
// Update function for the game
game.update = function () {
// Update hero
hero.update();
// Update aliens
for (var i = aliens.length - 1; i >= 0; i--) {
aliens[i].update();
if (aliens[i].y > 2732) {
aliens[i].destroy();
aliens.splice(i, 1);
}
}
// Update shapes
for (var j = shapes.length - 1; j >= 0; j--) {
shapes[j].update();
if (shapes[j].y > 2732) {
shapes[j].destroy();
shapes.splice(j, 1);
}
}
// Check for collisions between hero and shapes
for (var k = shapes.length - 1; k >= 0; k--) {
if (hero.intersects(shapes[k])) {
score += 1;
scoreTxt.setText(score);
shapes[k].destroy();
shapes.splice(k, 1);
}
}
// Check for collisions between hero and aliens
for (var l = aliens.length - 1; l >= 0; l--) {
if (hero.intersects(aliens[l])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn aliens and shapes periodically
if (LK.ticks % 60 == 0) {
spawnAlien();
}
if (LK.ticks % 120 == 0) {
spawnShape();
}
};
// Event listeners for dragging the hero
var dragNode = null;
game.down = function (x, y, obj) {
dragNode = hero;
handleMove(x, y, obj);
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}