/**** * 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 < -self.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 = 2; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -self.height; } }; }); // 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 = 5; self.update = function () { // Hero update logic self.vy = (self.vy || 0) + 0.5; // gravity self.y += self.vy; for (var i = 0; i < platforms.length; i++) { if (self.intersects(platforms[i]) && self.vy > 0) { self.y = platforms[i].y - self.height / 2; self.vy = 0; } } }; self.down = function (x, y, obj) { self.vy = -10; // jump }; self.move = function (x, y, obj) { // Add movement logic here }; }); //<Assets used in the game will automatically appear here> // Platform class var Platform = Container.expand(function () { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 0.5 }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize platforms var platforms = []; for (var i = 0; i < 5; i++) { var platform = new Platform(); platform.x = Math.random() * 2048; platform.y = Math.random() * 2732; platforms.push(platform); game.addChild(platform); } // Initialize hero var hero = new Hero(); hero.x = 2048 / 2; hero.y = 2732 - 200; game.addChild(hero); // Initialize enemies var enemies = []; for (var i = 0; i < 5; i++) { var enemy = new Enemy(); enemy.x = Math.random() * 2048; enemy.y = Math.random() * -2732; enemies.push(enemy); game.addChild(enemy); } // Initialize bullets array var bullets = []; // Handle game move event game.move = function (x, y, obj) { hero.move(x, y, obj); }; // Handle game down event game.down = function (x, y, obj) { var bullet = new Bullet(); bullet.x = hero.x; bullet.y = hero.y; bullets.push(bullet); game.addChild(bullet); }; // Game update function game.update = function () { // Update hero hero.update(); // Update enemies for (var i = 0; i < enemies.length; i++) { enemies[i].update(); if (hero.intersects(enemies[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update bullets for (var j = bullets.length - 1; j >= 0; j--) { bullets[j].update(); for (var k = enemies.length - 1; k >= 0; k--) { if (bullets[j].intersects(enemies[k])) { bullets[j].destroy(); bullets.splice(j, 1); enemies[k].destroy(); enemies.splice(k, 1); break; } } } };
/****
* 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 < -self.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 = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -self.height;
}
};
});
// 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 = 5;
self.update = function () {
// Hero update logic
self.vy = (self.vy || 0) + 0.5; // gravity
self.y += self.vy;
for (var i = 0; i < platforms.length; i++) {
if (self.intersects(platforms[i]) && self.vy > 0) {
self.y = platforms[i].y - self.height / 2;
self.vy = 0;
}
}
};
self.down = function (x, y, obj) {
self.vy = -10; // jump
};
self.move = function (x, y, obj) {
// Add movement logic here
};
});
//<Assets used in the game will automatically appear here>
// Platform class
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize platforms
var platforms = [];
for (var i = 0; i < 5; i++) {
var platform = new Platform();
platform.x = Math.random() * 2048;
platform.y = Math.random() * 2732;
platforms.push(platform);
game.addChild(platform);
}
// Initialize hero
var hero = new Hero();
hero.x = 2048 / 2;
hero.y = 2732 - 200;
game.addChild(hero);
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
var enemy = new Enemy();
enemy.x = Math.random() * 2048;
enemy.y = Math.random() * -2732;
enemies.push(enemy);
game.addChild(enemy);
}
// Initialize bullets array
var bullets = [];
// Handle game move event
game.move = function (x, y, obj) {
hero.move(x, y, obj);
};
// Handle game down event
game.down = function (x, y, obj) {
var bullet = new Bullet();
bullet.x = hero.x;
bullet.y = hero.y;
bullets.push(bullet);
game.addChild(bullet);
};
// Game update function
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
if (hero.intersects(enemies[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
for (var k = enemies.length - 1; k >= 0; k--) {
if (bullets[j].intersects(enemies[k])) {
bullets[j].destroy();
bullets.splice(j, 1);
enemies[k].destroy();
enemies.splice(k, 1);
break;
}
}
}
};