/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8; self.update = function () { self.y += self.speed; }; return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Star = Container.expand(function () { var self = Container.call(this); var starGraphics = self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000011 }); /**** * Game Code ****/ // Game state management var gameState = 'menu'; // 'menu' or 'playing' // Create stars for background var stars = []; for (var i = 0; i < 150; i++) { var star = new Star(); star.x = Math.random() * 2048; star.y = Math.random() * 2732; stars.push(star); game.addChild(star); } // Main Menu Elements var titleText = new Text2('SPACE SHOOTER', { size: 120, fill: 0xFFFFFF }); titleText.anchor.set(0.5, 0.5); titleText.x = 1024; titleText.y = 800; game.addChild(titleText); var startButton = new Text2('TAP TO START', { size: 80, fill: 0x00FF00 }); startButton.anchor.set(0.5, 0.5); startButton.x = 1024; startButton.y = 1400; game.addChild(startButton); // Instructions text var instructionsText = new Text2('Move left and right to avoid bullets', { size: 50, fill: 0xCCCCCC }); instructionsText.anchor.set(0.5, 0.5); instructionsText.x = 1024; instructionsText.y = 1600; game.addChild(instructionsText); function startGame() { // Hide menu elements titleText.visible = false; startButton.visible = false; instructionsText.visible = false; // Change game state gameState = 'playing'; // Reset game start time gameStartTime = Date.now(); } // Game variables (initialized when game starts) var player = null; var bullets = []; var bulletSpawnTimer = 0; var bulletSpawnDelay = 60; // Start spawning every 60 ticks (1 second) var minSpawnDelay = 20; // Minimum spawn delay (maximum difficulty) var difficultyTimer = 0; var speedMultiplier = 1; var gameStartTime = Date.now(); var dragNode = null; var scoreTxt = null; var timeTxt = null; function initializeGameplay() { // Create player player = game.addChild(new Player()); player.x = 1024; // Center horizontally player.y = 2200; // Near bottom of screen // Reset game variables bullets = []; bulletSpawnTimer = 0; speedMultiplier = 1; // Score display scoreTxt = new Text2('0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(1, 0); LK.gui.topRight.addChild(scoreTxt); // Time display timeTxt = new Text2('Time: 0s', { size: 60, fill: 0xFFFFFF }); timeTxt.anchor.set(0, 0); timeTxt.x = 120; timeTxt.y = 20; LK.gui.topLeft.addChild(timeTxt); } function updateScore() { var timeAlive = Math.floor((Date.now() - gameStartTime) / 1000); LK.setScore(timeAlive); scoreTxt.setText(timeAlive); timeTxt.setText('Time: ' + timeAlive + 's'); } function spawnBullet() { var bullet = new Bullet(); bullet.x = Math.random() * (2048 - 100) + 50; // Random x position with margin bullet.y = -30; // Start above screen bullet.speed = (6 + Math.random() * 4) * speedMultiplier; // Apply speed multiplier bullets.push(bullet); game.addChild(bullet); } function increaseDifficulty() { var currentScore = LK.getScore(); // Check if we've reached a new 5-point milestone if (currentScore > 0 && currentScore % 5 === 0) { // Only increase difficulty if we haven't already increased it for this score if (currentScore / 5 > speedMultiplier - 1) { speedMultiplier = Math.floor(currentScore / 5) + 1; // Decrease spawn delay (more bullets spawn) bulletSpawnDelay = Math.max(minSpawnDelay, Math.floor(60 / speedMultiplier)); } } } function handleMove(x, y, obj) { if (dragNode) { // Keep player within screen bounds horizontally only dragNode.x = Math.max(40, Math.min(2008, x)); // Keep player locked to bottom area - no vertical movement dragNode.y = 2200; } } game.move = handleMove; game.down = function (x, y, obj) { if (gameState === 'menu') { // Start the game when menu is tapped startGame(); initializeGameplay(); } else if (gameState === 'playing' && player) { // Set drag node to player for any touch on screen dragNode = player; handleMove(x, y, obj); } }; game.up = function (x, y, obj) { dragNode = null; }; game.update = function () { if (gameState === 'menu') { // Add blinking effect to start button if (LK.ticks % 60 < 30) { startButton.alpha = 1; } else { startButton.alpha = 0.5; } return; } if (gameState === 'playing' && player) { updateScore(); increaseDifficulty(); // Spawn bullets bulletSpawnTimer++; if (bulletSpawnTimer >= bulletSpawnDelay) { spawnBullet(); bulletSpawnTimer = 0; } // Update and check bullets for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; // Initialize tracking variables if (bullet.lastY === undefined) bullet.lastY = bullet.y; if (bullet.lastIntersecting === undefined) bullet.lastIntersecting = false; // Check if bullet went off screen if (bullet.lastY <= 2800 && bullet.y > 2800) { bullet.destroy(); bullets.splice(i, 1); continue; } // Check collision with player var currentIntersecting = bullet.intersects(player); if (!bullet.lastIntersecting && currentIntersecting) { // Collision detected - game over LK.getSound('hit').play(); LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } // Update tracking variables bullet.lastY = bullet.y; bullet.lastIntersecting = currentIntersecting; } } };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000011
});
/****
* Game Code
****/
// Game state management
var gameState = 'menu'; // 'menu' or 'playing'
// Create stars for background
var stars = [];
for (var i = 0; i < 150; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
// Main Menu Elements
var titleText = new Text2('SPACE SHOOTER', {
size: 120,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 800;
game.addChild(titleText);
var startButton = new Text2('TAP TO START', {
size: 80,
fill: 0x00FF00
});
startButton.anchor.set(0.5, 0.5);
startButton.x = 1024;
startButton.y = 1400;
game.addChild(startButton);
// Instructions text
var instructionsText = new Text2('Move left and right to avoid bullets', {
size: 50,
fill: 0xCCCCCC
});
instructionsText.anchor.set(0.5, 0.5);
instructionsText.x = 1024;
instructionsText.y = 1600;
game.addChild(instructionsText);
function startGame() {
// Hide menu elements
titleText.visible = false;
startButton.visible = false;
instructionsText.visible = false;
// Change game state
gameState = 'playing';
// Reset game start time
gameStartTime = Date.now();
}
// Game variables (initialized when game starts)
var player = null;
var bullets = [];
var bulletSpawnTimer = 0;
var bulletSpawnDelay = 60; // Start spawning every 60 ticks (1 second)
var minSpawnDelay = 20; // Minimum spawn delay (maximum difficulty)
var difficultyTimer = 0;
var speedMultiplier = 1;
var gameStartTime = Date.now();
var dragNode = null;
var scoreTxt = null;
var timeTxt = null;
function initializeGameplay() {
// Create player
player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2200; // Near bottom of screen
// Reset game variables
bullets = [];
bulletSpawnTimer = 0;
speedMultiplier = 1;
// Score display
scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
// Time display
timeTxt = new Text2('Time: 0s', {
size: 60,
fill: 0xFFFFFF
});
timeTxt.anchor.set(0, 0);
timeTxt.x = 120;
timeTxt.y = 20;
LK.gui.topLeft.addChild(timeTxt);
}
function updateScore() {
var timeAlive = Math.floor((Date.now() - gameStartTime) / 1000);
LK.setScore(timeAlive);
scoreTxt.setText(timeAlive);
timeTxt.setText('Time: ' + timeAlive + 's');
}
function spawnBullet() {
var bullet = new Bullet();
bullet.x = Math.random() * (2048 - 100) + 50; // Random x position with margin
bullet.y = -30; // Start above screen
bullet.speed = (6 + Math.random() * 4) * speedMultiplier; // Apply speed multiplier
bullets.push(bullet);
game.addChild(bullet);
}
function increaseDifficulty() {
var currentScore = LK.getScore();
// Check if we've reached a new 5-point milestone
if (currentScore > 0 && currentScore % 5 === 0) {
// Only increase difficulty if we haven't already increased it for this score
if (currentScore / 5 > speedMultiplier - 1) {
speedMultiplier = Math.floor(currentScore / 5) + 1;
// Decrease spawn delay (more bullets spawn)
bulletSpawnDelay = Math.max(minSpawnDelay, Math.floor(60 / speedMultiplier));
}
}
}
function handleMove(x, y, obj) {
if (dragNode) {
// Keep player within screen bounds horizontally only
dragNode.x = Math.max(40, Math.min(2008, x));
// Keep player locked to bottom area - no vertical movement
dragNode.y = 2200;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
if (gameState === 'menu') {
// Start the game when menu is tapped
startGame();
initializeGameplay();
} else if (gameState === 'playing' && player) {
// Set drag node to player for any touch on screen
dragNode = player;
handleMove(x, y, obj);
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.update = function () {
if (gameState === 'menu') {
// Add blinking effect to start button
if (LK.ticks % 60 < 30) {
startButton.alpha = 1;
} else {
startButton.alpha = 0.5;
}
return;
}
if (gameState === 'playing' && player) {
updateScore();
increaseDifficulty();
// Spawn bullets
bulletSpawnTimer++;
if (bulletSpawnTimer >= bulletSpawnDelay) {
spawnBullet();
bulletSpawnTimer = 0;
}
// Update and check bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Initialize tracking variables
if (bullet.lastY === undefined) bullet.lastY = bullet.y;
if (bullet.lastIntersecting === undefined) bullet.lastIntersecting = false;
// Check if bullet went off screen
if (bullet.lastY <= 2800 && bullet.y > 2800) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
// Check collision with player
var currentIntersecting = bullet.intersects(player);
if (!bullet.lastIntersecting && currentIntersecting) {
// Collision detected - game over
LK.getSound('hit').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Update tracking variables
bullet.lastY = bullet.y;
bullet.lastIntersecting = currentIntersecting;
}
}
};