/****
* Classes
****/
// AxeAttack class representing the hero's axe attack
var AxeAttack = Container.expand(function () {
var self = Container.call(this);
var axeGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y -= self.speed;
};
});
// Bullet class for projectiles
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;
};
});
// DmgAttack class representing the hero's attack with monsters
var DmgAttack = Container.expand(function () {
var self = Container.call(this);
var dmgGraphics = self.attachAsset('goodMonster', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y -= self.speed;
// Check for collision with enemies
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
if (self.intersects(enemy)) {
enemy.health -= hero.attack;
self.destroy();
if (enemy.health <= 0) {
enemy.destroy();
enemies.splice(i, 1);
}
break;
}
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Hero class representing the player's character
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.attack = 10;
self.update = function () {
// Update logic for hero
};
});
// HeroAttack class representing the hero's attack
var HeroAttack = Container.expand(function () {
var self = Container.call(this);
var attackGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y -= self.speed;
};
});
// Monster class representing enemy monsters
var Monster = Container.expand(function () {
var self = Container.call(this);
var monsterGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 20;
self.speed = 2;
self.update = function () {
self.y += self.speed;
// Check for collision with hero
if (self.intersects(hero)) {
hero.health -= 10;
self.destroy();
}
};
});
// MonsterFollower class representing monsters that follow the hero
var MonsterFollower = Container.expand(function () {
var self = Container.call(this);
var monsterGraphics = self.attachAsset('enemy2', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 20;
self.speed = 2;
self.update = function () {
// Calculate direction towards the hero
var directionX = hero.x - self.x;
var directionY = hero.y - self.y;
var length = Math.sqrt(directionX * directionX + directionY * directionY);
// Normalize direction and move towards the hero
self.x += directionX / length * self.speed;
self.y += directionY / length * self.speed;
// Check for collision with hero
if (self.intersects(hero)) {
hero.health -= 10;
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Add background image to the game
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
background.x = 2048 / 2;
background.y = 2732 / 2;
game.addChild(background);
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize enemies array
var enemies = [];
// Create and add initial monsters to the game
for (var i = 0; i < 3; i++) {
var monster = new Monster();
monster.x = Math.random() * 2048; // Random x position
monster.y = Math.random() * 500; // Random y position within the top 500 pixels
enemies.push(monster);
game.addChild(monster);
}
// Create and add initial MonsterFollowers to the game
for (var i = 0; i < 3; i++) {
var monsterFollower = new MonsterFollower();
monsterFollower.x = Math.random() * 2048; // Random x position
monsterFollower.y = Math.random() * 500; // Random y position within the top 500 pixels
enemies.push(monsterFollower);
game.addChild(monsterFollower);
}
// Infinite monster spawning logic
var spawnInterval = LK.setInterval(function () {
var newMonster = new Monster();
newMonster.x = Math.random() * 2048; // Random x position
newMonster.y = Math.random() * 500; // Random y position within the top 500 pixels
enemies.push(newMonster);
game.addChild(newMonster);
var newMonsterFollower = new MonsterFollower();
newMonsterFollower.x = Math.random() * 2048; // Random x position
newMonsterFollower.y = Math.random() * 500; // Random y position within the top 500 pixels
enemies.push(newMonsterFollower);
game.addChild(newMonsterFollower);
}, 2000); // Spawn a new monster and a new MonsterFollower every 2 seconds
// Create and add three dead monsters to the game
for (var j = 0; j < 3; j++) {
var deadMonster = new Monster();
deadMonster.x = Math.random() * 2048; // Random x position
deadMonster.y = Math.random() * 500 + 500; // Random y position within the next 500 pixels
deadMonster.health = 0; // Set health to 0 to indicate it's dead
enemies.push(deadMonster);
game.addChild(deadMonster);
}
// Initialize hero's attack array
var heroAttacks = [];
// Handle game updates
game.update = function () {
hero.update();
// Update all enemies including MonsterFollowers
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
}
// Update hero's DmgAttack
for (var j = heroAttacks.length - 1; j >= 0; j--) {
var attack = heroAttacks[j];
attack.update();
// Remove off-screen attacks
if (attack.y < -50) {
attack.destroy();
heroAttacks.splice(j, 1);
}
}
// Fire hero's attack
if (LK.ticks % 30 == 0) {
var newAttack = new HeroAttack();
newAttack.x = hero.x;
newAttack.y = hero.y - hero.height / 2;
heroAttacks.push(newAttack);
game.addChild(newAttack);
}
};
// Handle touch events for hero movement
game.down = function (x, y, obj) {
hero.x = x;
hero.y = y;
// Fire hero's DmgAttack on click
var newDmgAttack = new DmgAttack();
newDmgAttack.x = hero.x;
newDmgAttack.y = hero.y - hero.height / 2;
heroAttacks.push(newDmgAttack);
game.addChild(newDmgAttack);
};
game.move = function (x, y, obj) {
hero.x = x;
hero.y = y;
};
game.up = function (x, y, obj) {
// Handle touch release if needed
};
// Display hero health
var healthTxt = new Text2('Health: ' + hero.health, {
size: 100,
fill: 0xFFFFFF
});
healthTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(healthTxt);
// Update health display
game.update = function () {
healthTxt.setText('Health: ' + hero.health);
if (hero.health <= 0) {
LK.showGameOver();
}
};
// Add the health text to the GUI overlay
LK.gui.top.addChild(healthTxt);
MG hero red and blu. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
monster dark red. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
monster blu. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
bullet red and blu. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
background red and blu. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
fire red. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows