/**** * 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.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(); } }; }); //<Assets used in the game will automatically appear here> // 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 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; // Initialize bullets array var bullets = []; // Function to spawn bullets from random directions function spawnBullet() { var bullet = new Bullet(); 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(); if (bullets[i].intersects(player)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn bullets periodically if (LK.ticks % 60 == 0) { spawnBullet(); } };
/****
* 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.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();
}
};
});
//<Assets used in the game will automatically appear here>
// 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 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
// Initialize bullets array
var bullets = [];
// Function to spawn bullets from random directions
function spawnBullet() {
var bullet = new Bullet();
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();
if (bullets[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn bullets periodically
if (LK.ticks % 60 == 0) {
spawnBullet();
}
};