/****
* Classes
****/
// 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 = 5;
self.direction = {
x: 0,
y: 0
};
self.scored = false; // Flag to track if score has been counted for this bullet
self.update = function () {
self.x += self.speed * self.direction.x;
self.y += self.speed * self.direction.y;
if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
self.destroy();
}
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Temporary background color
});
/****
* Game Code
****/
var background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
// Add instructional text at the beginning to inform the player how to score
var instructionText = new Text2('Stay close to bullets to score!', {
size: 80,
fill: "#ffffff"
});
instructionText.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionText);
// Play background music
LK.playMusic('TechnoMusic1', {
loop: true
});
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
// Initialize and display proximity score text
var proximityScoreTxt = new Text2('0', {
size: 100,
fill: "#ffffff"
});
proximityScoreTxt.anchor.set(0.5, 0);
proximityScoreTxt.y = instructionText.height; // Position below the instructional text
LK.gui.top.addChild(proximityScoreTxt);
// Initialize bullets array
var bullets = [];
var bulletSpeed = 5;
// Function to spawn bullets from random directions
function spawnBullet() {
var bullet = new Bullet();
bullet.speed = bulletSpeed;
var edge = Math.floor(Math.random() * 4);
switch (edge) {
case 0:
// Top
bullet.x = Math.random() * 2048;
bullet.y = 0;
bullet.direction = {
x: 0,
y: 1
};
break;
case 1:
// Bottom
bullet.x = Math.random() * 2048;
bullet.y = 2732;
bullet.direction = {
x: 0,
y: -1
};
break;
case 2:
// Left
bullet.x = 0;
bullet.y = Math.random() * 2732;
bullet.direction = {
x: 1,
y: 0
};
break;
case 3:
// Right
bullet.x = 2048;
bullet.y = Math.random() * 2732;
bullet.direction = {
x: -1,
y: 0
};
break;
}
bullets.push(bullet);
game.addChild(bullet);
}
// Function to handle player movement
function handleMove(x, y, obj) {
player.x = x;
player.y = y;
}
// Event listeners for player movement
game.move = handleMove;
game.down = handleMove;
game.up = function (x, y, obj) {
// Stop player movement
};
// Game update function
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
// Check proximity to player
var dx = bullets[i].x - player.x;
var dy = bullets[i].y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 150 && !bullets[i].scored) {
bullets[i].scored = true; // Mark bullet as scored
// Arbitrary proximity threshold
LK.setScore(LK.getScore() + 1);
proximityScoreTxt.setText(LK.getScore());
LK.getSound('scoreSound').play();
}
if (bullets[i].intersects(player)) {
if (bullets[i].scored) {
// Remove the last point if the player scores as they die
LK.setScore(LK.getScore() - 1);
proximityScoreTxt.setText(LK.getScore());
}
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Increase bullet speed and spawn frequency over time
if (LK.ticks % 600 == 0) {
// Every 10 seconds
bulletSpeed += 1;
}
// Spawn bullets periodically
if (LK.ticks % Math.max(5, 15 - Math.floor(LK.ticks / 600)) == 0) {
// Increase spawn rate over time
spawnBullet();
}
// Spawn missiles periodically
};