/**** * Classes ****/ // The assets will be automatically created and loaded by the LK engine // Class for the player's bullet var PlayerBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('playerBullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); } }; }); // Class for the target var Target = Container.expand(function () { var self = Container.call(this); var targetGraphics = self.attachAsset('target', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = 0; self.x = Math.random() * 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize player's bullet and target arrays var playerBullets = []; var targets = []; // Initialize score and timer var score = 0; var timer = 60; // Create score and timer text var scoreText = new Text2('Score: 0', { size: 50, fill: "#ffffff" }); scoreText.x = 20; scoreText.y = 20; game.addChild(scoreText); var timerText = new Text2('Time: 60', { size: 50, fill: "#ffffff" }); timerText.x = 2048 - 20; timerText.y = 20; timerText.anchor.set(1, 0); game.addChild(timerText); // Create targets for (var i = 0; i < 10; i++) { var target = new Target(); target.x = Math.random() * 2048; target.y = Math.random() * 2732; targets.push(target); game.addChild(target); } // Game update function game.update = function () { // Update score and timer text scoreText.setText('Score: ' + score); timerText.setText('Time: ' + timer); // Check for bullet and target collision for (var i = playerBullets.length - 1; i >= 0; i--) { for (var j = targets.length - 1; j >= 0; j--) { if (playerBullets[i].intersects(targets[j])) { playerBullets[i].destroy(); playerBullets.splice(i, 1); targets[j].destroy(); targets.splice(j, 1); score++; break; } } } // End game when timer reaches 0 if (timer <= 0) { LK.showGameOver(); } }; // Game down event function game.down = function (x, y, obj) { var bullet = new PlayerBullet(); bullet.x = x; bullet.y = 2732; playerBullets.push(bullet); game.addChild(bullet); }; // Set interval to decrease timer every second LK.setInterval(function () { timer--; }, 1000);
/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine
// Class for the player's bullet
var PlayerBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('playerBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
self.destroy();
}
};
});
// Class for the target
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
self.x = Math.random() * 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize player's bullet and target arrays
var playerBullets = [];
var targets = [];
// Initialize score and timer
var score = 0;
var timer = 60;
// Create score and timer text
var scoreText = new Text2('Score: 0', {
size: 50,
fill: "#ffffff"
});
scoreText.x = 20;
scoreText.y = 20;
game.addChild(scoreText);
var timerText = new Text2('Time: 60', {
size: 50,
fill: "#ffffff"
});
timerText.x = 2048 - 20;
timerText.y = 20;
timerText.anchor.set(1, 0);
game.addChild(timerText);
// Create targets
for (var i = 0; i < 10; i++) {
var target = new Target();
target.x = Math.random() * 2048;
target.y = Math.random() * 2732;
targets.push(target);
game.addChild(target);
}
// Game update function
game.update = function () {
// Update score and timer text
scoreText.setText('Score: ' + score);
timerText.setText('Time: ' + timer);
// Check for bullet and target collision
for (var i = playerBullets.length - 1; i >= 0; i--) {
for (var j = targets.length - 1; j >= 0; j--) {
if (playerBullets[i].intersects(targets[j])) {
playerBullets[i].destroy();
playerBullets.splice(i, 1);
targets[j].destroy();
targets.splice(j, 1);
score++;
break;
}
}
}
// End game when timer reaches 0
if (timer <= 0) {
LK.showGameOver();
}
};
// Game down event function
game.down = function (x, y, obj) {
var bullet = new PlayerBullet();
bullet.x = x;
bullet.y = 2732;
playerBullets.push(bullet);
game.addChild(bullet);
};
// Set interval to decrease timer every second
LK.setInterval(function () {
timer--;
}, 1000);