/**** * Classes ****/ // Bullet class for the cop's bullets var Bullet = 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; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Cop class representing the player character var Cop = Container.expand(function () { var self = Container.call(this); var copGraphics = self.attachAsset('cop', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Update logic for the cop }; self.move = function (x, y) { self.x = x; self.y = y; }; }); // Thief class representing the enemy var Thief = Container.expand(function () { var self = Container.call(this); var thiefGraphics = self.attachAsset('thief', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -self.height; self.x = Math.random() * 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize game variables var cop = new Cop(); var thieves = []; var bullets = []; var score = 0; // Add cop to the game game.addChild(cop); cop.x = 1024; cop.y = 2400; // Create thieves for (var i = 0; i < 5; i++) { var thief = new Thief(); thief.x = Math.random() * 2048; thief.y = Math.random() * -2732; thieves.push(thief); game.addChild(thief); } // Handle game updates game.update = function () { // Update cop cop.update(); // Update thieves for (var i = 0; i < thieves.length; i++) { thieves[i].update(); if (cop.intersects(thieves[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update bullets for (var j = bullets.length - 1; j >= 0; j--) { bullets[j].update(); if (bullets[j].y < -50) { bullets[j].destroy(); bullets.splice(j, 1); } else { for (var k = thieves.length - 1; k >= 0; k--) { if (bullets[j].intersects(thieves[k])) { score++; bullets[j].destroy(); bullets.splice(j, 1); thieves[k].destroy(); thieves.splice(k, 1); break; } } } } // Fire bullets if (LK.ticks % 20 == 0) { var bullet = new Bullet(); bullet.x = cop.x; bullet.y = cop.y; bullets.push(bullet); game.addChild(bullet); } }; // Handle touch events game.down = function (x, y, obj) { cop.move(x, y); }; game.move = function (x, y, obj) { cop.move(x, y); }; game.up = function (x, y, obj) { // No action needed on touch up };
/****
* Classes
****/
// Bullet class for the cop's bullets
var Bullet = 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;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Cop class representing the player character
var Cop = Container.expand(function () {
var self = Container.call(this);
var copGraphics = self.attachAsset('cop', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update logic for the cop
};
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
// Thief class representing the enemy
var Thief = Container.expand(function () {
var self = Container.call(this);
var thiefGraphics = self.attachAsset('thief', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = -self.height;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var cop = new Cop();
var thieves = [];
var bullets = [];
var score = 0;
// Add cop to the game
game.addChild(cop);
cop.x = 1024;
cop.y = 2400;
// Create thieves
for (var i = 0; i < 5; i++) {
var thief = new Thief();
thief.x = Math.random() * 2048;
thief.y = Math.random() * -2732;
thieves.push(thief);
game.addChild(thief);
}
// Handle game updates
game.update = function () {
// Update cop
cop.update();
// Update thieves
for (var i = 0; i < thieves.length; i++) {
thieves[i].update();
if (cop.intersects(thieves[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
if (bullets[j].y < -50) {
bullets[j].destroy();
bullets.splice(j, 1);
} else {
for (var k = thieves.length - 1; k >= 0; k--) {
if (bullets[j].intersects(thieves[k])) {
score++;
bullets[j].destroy();
bullets.splice(j, 1);
thieves[k].destroy();
thieves.splice(k, 1);
break;
}
}
}
}
// Fire bullets
if (LK.ticks % 20 == 0) {
var bullet = new Bullet();
bullet.x = cop.x;
bullet.y = cop.y;
bullets.push(bullet);
game.addChild(bullet);
}
};
// Handle touch events
game.down = function (x, y, obj) {
cop.move(x, y);
};
game.move = function (x, y, obj) {
cop.move(x, y);
};
game.up = function (x, y, obj) {
// No action needed on touch up
};