let startButton, settingsButton, creditsButton, backButton;
let playerX, playerY, playerSpeed;
textAlign(CENTER, CENTER);
startButton = createButton('Start');
startButton.position(width / 2 - 40, height / 2 + 140);
startButton.style('font-size', '20px');
startButton.mousePressed(() => {
settingsButton = createButton('Settings');
settingsButton.position(width / 2 - 40, height / 2 + 180);
settingsButton.style('font-size', '20px');
settingsButton.mousePressed(() => {
creditsButton = createButton('Credits');
creditsButton.position(width / 2 - 40, height / 2 + 220);
creditsButton.style('font-size', '20px');
creditsButton.mousePressed(() => {
backButton = createButton('Back');
backButton.position(20, 20);
backButton.style('font-size', '20px');
backButton.mousePressed(() => {
if (gameState === 'menu') {
for (let i = 0; i < 5; i++) {
x: random(50, width - 50),
if (gameState === 'menu') {
} else if (gameState === 'playing') {
} else if (gameState === 'settings') {
} else if (gameState === 'credits') {
} else if (gameState === 'gameOver') {
text('Nebula Quest', width / 2, height / 2 - 200);
text('An interstellar adventure awaits...', width / 2, height / 2 + 100);
drawSpaceship(width / 2, height / 2 - 50, 3);
function drawSettingsUI() {
text('Settings', width / 2, height / 2);
function drawCreditsUI() {
text('Credits', width / 2, height / 2);
text('Developed by Boray', width / 2, height / 2 + 40);
drawSpaceship(playerX, playerY, 2);
function drawSpaceship(x, y, s) {
let flameSize = 15 + sin(frameCount * 0.2) * 5;
triangle(-5, 20, 5, 20, 0, 20 + flameSize);
if (keys[LEFT_ARROW]) playerX = max(30, playerX - playerSpeed);
if (keys[RIGHT_ARROW]) playerX = min(width - 30, playerX + playerSpeed);
if (keys[UP_ARROW]) playerY = max(30, playerY - playerSpeed);
if (keys[DOWN_ARROW]) playerY = min(height - 30, playerY + playerSpeed);
function handleAsteroids() {
for (let rock of spaceRocks) {
rect(rock.x, rock.y, rock.size, rock.size);
rock.y = random(-500, -50);
rock.x = random(50, width - 50);
function checkCollisions() {
for (let rock of spaceRocks) {
if (dist(playerX, playerY, rock.x, rock.y) < rock.size / 2 + 15) {
function drawExplosion() {
if (explosionFrames > 0) {
fill(255, random(100, 255), 0);
ellipse(explosionX, explosionY, 50 + random(-10, 10), 50 + random(-10, 10));
for (let i = 0; i < 50; i++) {
ellipse(random(width), random(height), 2, 2);