/**** * 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(); } }; }); // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.health = 50; self.attackPower = 5; self.update = function () { // Enemy update logic }; self.attack = function (target) { target.takeDamage(self.attackPower); }; self.takeDamage = function (damage) { self.health -= damage; if (self.health <= 0) { 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.health = 100; self.attackPower = 10; self.update = function () { // Hero update logic }; self.attack = function (target) { target.takeDamage(self.attackPower); }; self.takeDamage = function (damage) { self.health -= damage; if (self.health <= 0) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize hero var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Initialize enemies var enemies = []; for (var i = 0; i < 5; i++) { var enemy = game.addChild(new Enemy()); enemy.x = 2048 / 2 + (i - 2) * 150; enemy.y = 200; enemies.push(enemy); } // Initialize bullets var bullets = []; // Handle touch events for hero movement game.down = function (x, y, obj) { hero.x = x; hero.y = y; }; // Handle game update game.update = function () { // Update hero hero.update(); // Update enemies for (var i = 0; i < enemies.length; i++) { enemies[i].update(); if (enemies[i].intersects(hero)) { enemies[i].attack(hero); } } // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); for (var j = 0; j < enemies.length; j++) { if (bullets[i].intersects(enemies[j])) { bullets[i].destroy(); bullets.splice(i, 1); enemies[j].takeDamage(hero.attackPower); break; } } } // Fire bullet if (LK.ticks % 30 == 0) { var bullet = new Bullet(); bullet.x = hero.x; bullet.y = hero.y - 50; bullets.push(bullet); game.addChild(bullet); } };
/****
* 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();
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 50;
self.attackPower = 5;
self.update = function () {
// Enemy update logic
};
self.attack = function (target) {
target.takeDamage(self.attackPower);
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
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.health = 100;
self.attackPower = 10;
self.update = function () {
// Hero update logic
};
self.attack = function (target) {
target.takeDamage(self.attackPower);
};
self.takeDamage = function (damage) {
self.health -= damage;
if (self.health <= 0) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize enemies
var enemies = [];
for (var i = 0; i < 5; i++) {
var enemy = game.addChild(new Enemy());
enemy.x = 2048 / 2 + (i - 2) * 150;
enemy.y = 200;
enemies.push(enemy);
}
// Initialize bullets
var bullets = [];
// Handle touch events for hero movement
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
// Handle game update
game.update = function () {
// Update hero
hero.update();
// Update enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
enemies[i].attack(hero);
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
for (var j = 0; j < enemies.length; j++) {
if (bullets[i].intersects(enemies[j])) {
bullets[i].destroy();
bullets.splice(i, 1);
enemies[j].takeDamage(hero.attackPower);
break;
}
}
}
// Fire bullet
if (LK.ticks % 30 == 0) {
var bullet = new Bullet();
bullet.x = hero.x;
bullet.y = hero.y - 50;
bullets.push(bullet);
game.addChild(bullet);
}
};