/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Hero class representing the player character
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 () {
// Update logic for hero
};
});
// Bullet class for hero's bullets
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
};
});
// Zombie class representing enemy characters
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.attachAsset('zombie', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 1;
self.speed = 5;
self.update = function () {
// Update logic for zombie
self.y += self.speed;
self.x = Math.max(0 + self.width / 2, Math.min(2048 - self.width / 2, self.x));
// Check if zombie is hit by a bullet
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.intersects(self)) {
// Destroy bullet
bullet.destroy();
bullets.splice(i, 1);
// Increase score by 5
LK.setScore(LK.getScore() + 5);
// Update score display
scoreText.setText(LK.getScore());
// Destroy zombie
self.destroy();
zombies.splice(zombies.indexOf(self), 1);
break;
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Play background music
LK.playMusic('Zombi');
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 100;
// Array to keep track of zombies
var zombies = [];
// Array to keep track of bullets
var bullets = [];
// Create score text and add it to the top right of the screen
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreText.anchor.set(1, 0); // Anchor to the top right
LK.gui.topRight.addChild(scoreText);
// Function to spawn a new zombie
function spawnZombie() {
var zombie = new Zombie();
do {
zombie.x = Math.random() * 2048;
} while (zombies.some(function (otherZombie) {
return Math.abs(zombie.x - otherZombie.x) < zombie.width;
}));
zombie.y = -50;
zombies.push(zombie);
game.addChild(zombie);
// Set zombie health based on score
if (LK.getScore() >= 200) {
zombie.health = 3;
} else if (LK.getScore() >= 100) {
zombie.health = 2;
} else {
zombie.health = 1;
}
}
// Function to handle shooting
var lastShotTime = 0;
var shootInterval = 30; // Adjust this value to control shooting frequency
function shootBullet() {
if (LK.ticks - lastShotTime >= shootInterval) {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
bullets.push(bullet);
game.addChild(bullet);
lastShotTime = LK.ticks;
// Play shooting sound
LK.getSound('Mermi').play();
}
}
// Game update loop
game.update = function () {
// Update hero
hero.update();
// Update zombies
for (var i = zombies.length - 1; i >= 0; i--) {
var zombie = zombies[i];
// Increase zombie speed every 100 points
if (LK.getScore() >= 300) {
zombie.speed = 10; // Increase speed
} else if (LK.getScore() >= 200) {
zombie.speed = 8; // Increase speed
} else if (LK.getScore() >= 100) {
zombie.speed = 6; // Increase speed
} else {
zombie.speed = 5; // Default speed
}
zombie.update();
if (zombie.y > 2732) {
// End the game if a zombie goes off the screen
LK.showGameOver();
break;
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
var bullet = bullets[j];
bullet.update();
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(j, 1);
}
}
// Check for collisions between bullets and zombies
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
for (var l = zombies.length - 1; l >= 0; l--) {
var zombie = zombies[l];
if (bullet.intersects(zombie)) {
bullet.destroy();
bullets.splice(k, 1);
zombie.destroy();
zombies.splice(l, 1);
break;
}
}
}
// Check for collisions between hero and zombies
for (var m = zombies.length - 1; m >= 0; m--) {
var zombie = zombies[m];
if (hero.intersects(zombie)) {
// End the game if the hero collides with a zombie
LK.showGameOver();
break;
}
}
// Spawn new zombies periodically
if (LK.ticks % 60 === 0) {
var zombieCount = 1;
if (LK.getScore() >= 300) {
zombieCount = 4;
} else if (LK.getScore() >= 200) {
zombieCount = 3;
} else if (LK.getScore() >= 100) {
zombieCount = 2;
}
for (var i = 0; i < zombieCount; i++) {
spawnZombie();
}
}
};
// Handle touch events for moving the hero
game.down = function (x, y, obj) {
hero.x = x;
};
game.move = function (x, y, obj) {
hero.x = Math.max(0 + hero.width / 2, Math.min(2048 - hero.width / 2, x));
};
// Handle shooting on touch release
game.up = function (x, y, obj) {
shootBullet();
}; /****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Hero class representing the player character
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 () {
// Update logic for hero
};
});
// Bullet class for hero's bullets
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -15;
self.update = function () {
self.y += self.speed;
};
});
// Zombie class representing enemy characters
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.attachAsset('zombie', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 1;
self.speed = 5;
self.update = function () {
// Update logic for zombie
self.y += self.speed;
self.x = Math.max(0 + self.width / 2, Math.min(2048 - self.width / 2, self.x));
// Check if zombie is hit by a bullet
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.intersects(self)) {
// Destroy bullet
bullet.destroy();
bullets.splice(i, 1);
// Increase score by 5
LK.setScore(LK.getScore() + 5);
// Update score display
scoreText.setText(LK.getScore());
// Destroy zombie
self.destroy();
zombies.splice(zombies.indexOf(self), 1);
break;
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Play background music
LK.playMusic('Zombi');
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 100;
// Array to keep track of zombies
var zombies = [];
// Array to keep track of bullets
var bullets = [];
// Create score text and add it to the top right of the screen
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreText.anchor.set(1, 0); // Anchor to the top right
LK.gui.topRight.addChild(scoreText);
// Function to spawn a new zombie
function spawnZombie() {
var zombie = new Zombie();
do {
zombie.x = Math.random() * 2048;
} while (zombies.some(function (otherZombie) {
return Math.abs(zombie.x - otherZombie.x) < zombie.width;
}));
zombie.y = -50;
zombies.push(zombie);
game.addChild(zombie);
// Set zombie health based on score
if (LK.getScore() >= 200) {
zombie.health = 3;
} else if (LK.getScore() >= 100) {
zombie.health = 2;
} else {
zombie.health = 1;
}
}
// Function to handle shooting
var lastShotTime = 0;
var shootInterval = 30; // Adjust this value to control shooting frequency
function shootBullet() {
if (LK.ticks - lastShotTime >= shootInterval) {
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
bullets.push(bullet);
game.addChild(bullet);
lastShotTime = LK.ticks;
// Play shooting sound
LK.getSound('Mermi').play();
}
}
// Game update loop
game.update = function () {
// Update hero
hero.update();
// Update zombies
for (var i = zombies.length - 1; i >= 0; i--) {
var zombie = zombies[i];
// Increase zombie speed every 100 points
if (LK.getScore() >= 300) {
zombie.speed = 10; // Increase speed
} else if (LK.getScore() >= 200) {
zombie.speed = 8; // Increase speed
} else if (LK.getScore() >= 100) {
zombie.speed = 6; // Increase speed
} else {
zombie.speed = 5; // Default speed
}
zombie.update();
if (zombie.y > 2732) {
// End the game if a zombie goes off the screen
LK.showGameOver();
break;
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
var bullet = bullets[j];
bullet.update();
if (bullet.y < 0) {
bullet.destroy();
bullets.splice(j, 1);
}
}
// Check for collisions between bullets and zombies
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
for (var l = zombies.length - 1; l >= 0; l--) {
var zombie = zombies[l];
if (bullet.intersects(zombie)) {
bullet.destroy();
bullets.splice(k, 1);
zombie.destroy();
zombies.splice(l, 1);
break;
}
}
}
// Check for collisions between hero and zombies
for (var m = zombies.length - 1; m >= 0; m--) {
var zombie = zombies[m];
if (hero.intersects(zombie)) {
// End the game if the hero collides with a zombie
LK.showGameOver();
break;
}
}
// Spawn new zombies periodically
if (LK.ticks % 60 === 0) {
var zombieCount = 1;
if (LK.getScore() >= 300) {
zombieCount = 4;
} else if (LK.getScore() >= 200) {
zombieCount = 3;
} else if (LK.getScore() >= 100) {
zombieCount = 2;
}
for (var i = 0; i < zombieCount; i++) {
spawnZombie();
}
}
};
// Handle touch events for moving the hero
game.down = function (x, y, obj) {
hero.x = x;
};
game.move = function (x, y, obj) {
hero.x = Math.max(0 + hero.width / 2, Math.min(2048 - hero.width / 2, x));
};
// Handle shooting on touch release
game.up = function (x, y, obj) {
shootBullet();
};