/**** * Classes ****/ // Block class var Block = Container.expand(function () { var self = Container.call(this); var blockGraphics = self.attachAsset('block', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.horizontalSpeed = Math.random() < 0.5 ? -2 : 2; // Random initial horizontal direction self.update = function () { self.y += self.speed; self.x += self.horizontalSpeed; if (self.x < 0 || self.x > 2048) { self.horizontalSpeed *= -1; // Reverse direction when hitting screen edges } if (self.y > 2732 + 50) { self.destroy(); blocks.splice(blocks.indexOf(self), 1); } }; }); // Bullet class 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; if (self.y < -50) { self.destroy(); bullets.splice(bullets.indexOf(self), 1); } }; }); //<Assets used in the game will automatically appear here> // Plane class var Plane = Container.expand(function () { var self = Container.call(this); var planeGraphics = self.attachAsset('plane', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.5, scaleY: 1.5 }); self.shoot = function () { var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y - planeGraphics.height / 2; game.addChild(bullet); bullets.push(bullet); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var backgroundPeople = LK.getAsset('backgroundPeople', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(backgroundPeople); var plane = new Plane(); plane.x = 2048 / 2; plane.y = 2732 - 200; game.addChild(plane); var bullets = []; var blocks = []; var hearts = 3; var score = 0; var scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var heartsTxt = new Text2('Hearts: 3', { size: 100, fill: "#ffffff" }); heartsTxt.anchor.set(0.5, 0); LK.gui.topRight.addChild(heartsTxt); game.down = function (x, y, obj) { plane.x = x; plane.y = y; }; game.move = function (x, y, obj) { plane.x = x; plane.y = y; }; game.update = function () { if (LK.ticks % 60 === 0) { plane.shoot(); } if (LK.ticks % 120 === 0) { var block = new Block(); block.x = Math.random() * 2048; block.y = -50; game.addChild(block); blocks.push(block); } bullets.forEach(function (bullet) { blocks.forEach(function (block) { if (bullet.intersects(block)) { bullet.destroy(); block.destroy(); bullets.splice(bullets.indexOf(bullet), 1); blocks.splice(blocks.indexOf(block), 1); score += 10; scoreTxt.setText('Score: ' + score); } }); }); blocks.forEach(function (block) { if (plane.intersects(block)) { block.destroy(); blocks.splice(blocks.indexOf(block), 1); hearts -= 1; heartsTxt.setText('Hearts: ' + hearts); if (hearts <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); console.log("Skor has died."); // Log Skor's death var highScore = Math.max(score, highScore || 0); var highScoreTxt = new Text2('High Score: ' + highScore, { size: 100, fill: "#ffffff" }); highScoreTxt.anchor.set(0.5, 0); LK.gui.center.addChild(highScoreTxt); } } }); };
/****
* Classes
****/
// Block class
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.horizontalSpeed = Math.random() < 0.5 ? -2 : 2; // Random initial horizontal direction
self.update = function () {
self.y += self.speed;
self.x += self.horizontalSpeed;
if (self.x < 0 || self.x > 2048) {
self.horizontalSpeed *= -1; // Reverse direction when hitting screen edges
}
if (self.y > 2732 + 50) {
self.destroy();
blocks.splice(blocks.indexOf(self), 1);
}
};
});
// Bullet class
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;
if (self.y < -50) {
self.destroy();
bullets.splice(bullets.indexOf(self), 1);
}
};
});
//<Assets used in the game will automatically appear here>
// Plane class
var Plane = Container.expand(function () {
var self = Container.call(this);
var planeGraphics = self.attachAsset('plane', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.5,
scaleY: 1.5
});
self.shoot = function () {
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y - planeGraphics.height / 2;
game.addChild(bullet);
bullets.push(bullet);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var backgroundPeople = LK.getAsset('backgroundPeople', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(backgroundPeople);
var plane = new Plane();
plane.x = 2048 / 2;
plane.y = 2732 - 200;
game.addChild(plane);
var bullets = [];
var blocks = [];
var hearts = 3;
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var heartsTxt = new Text2('Hearts: 3', {
size: 100,
fill: "#ffffff"
});
heartsTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(heartsTxt);
game.down = function (x, y, obj) {
plane.x = x;
plane.y = y;
};
game.move = function (x, y, obj) {
plane.x = x;
plane.y = y;
};
game.update = function () {
if (LK.ticks % 60 === 0) {
plane.shoot();
}
if (LK.ticks % 120 === 0) {
var block = new Block();
block.x = Math.random() * 2048;
block.y = -50;
game.addChild(block);
blocks.push(block);
}
bullets.forEach(function (bullet) {
blocks.forEach(function (block) {
if (bullet.intersects(block)) {
bullet.destroy();
block.destroy();
bullets.splice(bullets.indexOf(bullet), 1);
blocks.splice(blocks.indexOf(block), 1);
score += 10;
scoreTxt.setText('Score: ' + score);
}
});
});
blocks.forEach(function (block) {
if (plane.intersects(block)) {
block.destroy();
blocks.splice(blocks.indexOf(block), 1);
hearts -= 1;
heartsTxt.setText('Hearts: ' + hearts);
if (hearts <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
console.log("Skor has died."); // Log Skor's death
var highScore = Math.max(score, highScore || 0);
var highScoreTxt = new Text2('High Score: ' + highScore, {
size: 100,
fill: "#ffffff"
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.center.addChild(highScoreTxt);
}
}
});
};