/**** * Classes ****/ // Define the Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.createAsset('soccerPlayer', 'Hero character', 0.5, 0.5); self.speed = 5; self.shootInterval = 30; self.shootTick = 0; self.moveLeft = function () { self.x = Math.max(self.width / 2, self.x - self.speed * 8); }; self.moveRight = function () { self.x = Math.min(2048 - self.width / 2, self.x + self.speed * 8); }; self.shoot = function () { if (self.shootTick <= 0) { var bullet = new HeroBullet(); bullet.x = self.x; bullet.y = self.y - self.height / 2; game.addChild(bullet); heroBullets.push(bullet); self.shootTick = self.shootInterval; } }; self.update = function () { if (self.shootTick > 0) { self.shootTick--; } }; }); // Define the HeroBullet class var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', 0.5, 1); self.speed = -10; self.move = function () { self.y += self.speed; }; }); // Define the Zombie class var Zombie = Container.expand(function () { var self = Container.call(this); var zombieGraphics = self.createAsset('zombie', 'Zombie enemy', 0.5, 0.5); self.speed = 2; self.move = function () { self.y += self.speed + Math.min(Math.floor(score / 10), 5); }; }); // Define the GoldenZombie class var GoldenZombie = Zombie.expand(function () { var self = Zombie.call(this); self.createAsset('goldenZombie', 'Golden Zombie enemy', 0.5, 0.5); }); // Define the Background class var Background = Container.expand(function () { var self = Container.call(this); var backgroundGraphics = self.createAsset('apocalypseBackground', 'Apocalyptic background', 0, 0); backgroundGraphics.width = 2048; backgroundGraphics.height = 2732; }); /**** * Initialize Game ****/ // Create and add the background to the game var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Create and add the background to the game var background = game.addChild(new Background()); var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 100; var heroBullets = []; var zombies = []; var score = 0; var scoreTxt = new Text2(score.toString(), { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(.5, 0); LK.gui.top.addChild(scoreTxt); // Handle touch and keyboard events for hero movement and shooting game.on('down', function (obj) { var touchPos = obj.event.getLocalPosition(game); if (touchPos.x < 2048 / 2) { hero.moveLeft(); } else { hero.moveRight(); } hero.shoot(); }); game.on('keydown', function (obj) { var key = obj.event.key; if (key === 'a' || key === 'A') { hero.moveLeft(); } }); // Main game loop LK.on('tick', function () { // Update hero hero.update(); // Move hero bullets for (var i = heroBullets.length - 1; i >= 0; i--) { heroBullets[i].move(); if (heroBullets[i].y < -50) { heroBullets[i].destroy(); heroBullets.splice(i, 1); } } // Spawn zombies if (LK.ticks % 120 == 0) { var zombie; if (score >= 30) { zombie = new GoldenZombie(); } else { zombie = new Zombie(); } zombie.x = Math.random() * (2048 - zombie.width) + zombie.width / 2; zombie.y = -zombie.height / 2; zombies.push(zombie); game.addChild(zombie); } // Move zombies and check for collision with hero for (var j = zombies.length - 1; j >= 0; j--) { zombies[j].move(); if (zombies[j].intersects(hero)) { // End the game if a zombie hits the hero LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(score); return; } if (zombies[j].y > 2732 + 50) { zombies[j].destroy(); zombies.splice(j, 1); } } // Check for bullet-zombie collisions for (var k = heroBullets.length - 1; k >= 0; k--) { for (var l = zombies.length - 1; l >= 0; l--) { if (heroBullets[k].intersects(zombies[l])) { heroBullets[k].destroy(); heroBullets.splice(k, 1); zombies[l].destroy(); score++; scoreTxt.setText(score.toString()); zombies.splice(l, 1); break; } } } });
/****
* Classes
****/
// Define the Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('soccerPlayer', 'Hero character', 0.5, 0.5);
self.speed = 5;
self.shootInterval = 30;
self.shootTick = 0;
self.moveLeft = function () {
self.x = Math.max(self.width / 2, self.x - self.speed * 8);
};
self.moveRight = function () {
self.x = Math.min(2048 - self.width / 2, self.x + self.speed * 8);
};
self.shoot = function () {
if (self.shootTick <= 0) {
var bullet = new HeroBullet();
bullet.x = self.x;
bullet.y = self.y - self.height / 2;
game.addChild(bullet);
heroBullets.push(bullet);
self.shootTick = self.shootInterval;
}
};
self.update = function () {
if (self.shootTick > 0) {
self.shootTick--;
}
};
});
// Define the HeroBullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', 0.5, 1);
self.speed = -10;
self.move = function () {
self.y += self.speed;
};
});
// Define the Zombie class
var Zombie = Container.expand(function () {
var self = Container.call(this);
var zombieGraphics = self.createAsset('zombie', 'Zombie enemy', 0.5, 0.5);
self.speed = 2;
self.move = function () {
self.y += self.speed + Math.min(Math.floor(score / 10), 5);
};
});
// Define the GoldenZombie class
var GoldenZombie = Zombie.expand(function () {
var self = Zombie.call(this);
self.createAsset('goldenZombie', 'Golden Zombie enemy', 0.5, 0.5);
});
// Define the Background class
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.createAsset('apocalypseBackground', 'Apocalyptic background', 0, 0);
backgroundGraphics.width = 2048;
backgroundGraphics.height = 2732;
});
/****
* Initialize Game
****/
// Create and add the background to the game
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Create and add the background to the game
var background = game.addChild(new Background());
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 100;
var heroBullets = [];
var zombies = [];
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle touch and keyboard events for hero movement and shooting
game.on('down', function (obj) {
var touchPos = obj.event.getLocalPosition(game);
if (touchPos.x < 2048 / 2) {
hero.moveLeft();
} else {
hero.moveRight();
}
hero.shoot();
});
game.on('keydown', function (obj) {
var key = obj.event.key;
if (key === 'a' || key === 'A') {
hero.moveLeft();
}
});
// Main game loop
LK.on('tick', function () {
// Update hero
hero.update();
// Move hero bullets
for (var i = heroBullets.length - 1; i >= 0; i--) {
heroBullets[i].move();
if (heroBullets[i].y < -50) {
heroBullets[i].destroy();
heroBullets.splice(i, 1);
}
}
// Spawn zombies
if (LK.ticks % 120 == 0) {
var zombie;
if (score >= 30) {
zombie = new GoldenZombie();
} else {
zombie = new Zombie();
}
zombie.x = Math.random() * (2048 - zombie.width) + zombie.width / 2;
zombie.y = -zombie.height / 2;
zombies.push(zombie);
game.addChild(zombie);
}
// Move zombies and check for collision with hero
for (var j = zombies.length - 1; j >= 0; j--) {
zombies[j].move();
if (zombies[j].intersects(hero)) {
// End the game if a zombie hits the hero
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver(score);
return;
}
if (zombies[j].y > 2732 + 50) {
zombies[j].destroy();
zombies.splice(j, 1);
}
}
// Check for bullet-zombie collisions
for (var k = heroBullets.length - 1; k >= 0; k--) {
for (var l = zombies.length - 1; l >= 0; l--) {
if (heroBullets[k].intersects(zombies[l])) {
heroBullets[k].destroy();
heroBullets.splice(k, 1);
zombies[l].destroy();
score++;
scoreTxt.setText(score.toString());
zombies.splice(l, 1);
break;
}
}
}
});
zombie from top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
foot ball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a soccer player top down running with a ball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
soccer field from the top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.