let player; let aliens = []; let bullets = []; let alienBullets = []; let score = 0; let level = 1; let gameOverState = false; let alienSpeed = 1; let alienFireRate = 0.02; let isMobile = false; let playerImg; let alienImgs = []; let bulletImg; function preload() { playerImg = loadImage("/content/25c3a3abe6f5eeee010463f1bb27f77ae7dab5a3acce312443275281b56bf4c1i0"); alienImgs.push(loadImage("/content/ce5c8b5eab5e553ccff3cdabc316a648ea7db33c219e10748b568b9812c168aci0")); alienImgs.push(loadImage("/content/ddc2d09af0a171032ebf1fbc0d6d194b90d1848cf0dfa74e457b59fddb348924i0")); bulletImg = loadImage("/content/7db3a0b86253f5af2fa50fd2c8ace2605d9cf5328e77ce2d353519a32a7ec3bbi0"); } function setup() { createCanvas(windowWidth, windowHeight); isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); resetGame(); window.addEventListener("keydown", function(e) { if ([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) { e.preventDefault(); } }, false); } function draw() { background(0); if (!gameOverState) { player.show(); player.move(); for (let alien of aliens) { alien.show(); alien.move(); } for (let i = bullets.length - 1; i >= 0; i--) { bullets[i].show(); bullets[i].move(); if (bullets[i].offscreen()) { bullets.splice(i, 1); } else { for (let j = aliens.length - 1; j >= 0; j--) { if (bullets[i].hits(aliens[j])) { aliens.splice(j, 1); bullets.splice(i, 1); score += 10; break; } } } } if (random(1) < alienFireRate && aliens.length > 0) { let randomAlien = random(aliens); alienBullets.push(new Bullet(randomAlien.x, randomAlien.y, 5)); } for (let i = alienBullets.length - 1; i >= 0; i--) { alienBullets[i].show(); alienBullets[i].move(); if (alienBullets[i].offscreen()) { alienBullets.splice(i, 1); } else if (alienBullets[i].hits(player)) { gameOver(); } } if (aliens.length === 0) { levelUp(); } } else { displayGameOver(); } displayScore(); } function keyPressed() { if (key === ' ' && !gameOverState) { fireBullet(); } else if (key === 'r' && gameOverState) { resetGame(); } } function mousePressed() { if (isMobile && !gameOverState) { fireBullet(); } else if (isMobile && gameOverState) { resetGame(); } } function fireBullet() { bullets.push(new Bullet( player.x + player.w / 2 - (width * 0.0225)/2, player.y - (width * 0.0225 * 0.5), -height * 0.01 )); } function createAliens() { let aliensPerRow = min(level + 2, 6); let rows = min(level, 6); let gridWidth = width * 0.8; let gridHeight = height * 0.4; let cellWidth = gridWidth / aliensPerRow; let cellHeight = gridHeight / rows; for (let i = 0; i < aliensPerRow; i++) { for (let j = 0; j < rows; j++) { let baseX = (width - gridWidth) / 2 + i * cellWidth; let baseY = 60 + j * cellHeight; let randomOffsetX = random(-cellWidth * 0.2, cellWidth * 0.2); let randomOffsetY = random(-cellHeight * 0.2, cellHeight * 0.2); let x = baseX + cellWidth / 2 + randomOffsetX; let y = baseY + randomOffsetY; x = constrain(x, 0, width); y = constrain(y, 60, height / 2); let alienImg = random(alienImgs); aliens.push(new Alien(x, y, alienImg)); } } } function displayScore() { fill(255); textSize(16); text(`Score: ${score}`, width - 100, 20); text(`Level: ${level}`, width - 100, 40); } function gameOver() { gameOverState = true; } function displayGameOver() { textAlign(CENTER); textSize(32); fill(255); text("GAME OVER", width / 2, height / 2); textSize(16); text("Press 'R' to Restart", width / 2, height / 2 + 40); } function resetGame() { player = new Player(); aliens = []; bullets = []; alienBullets = []; score = 0; level = 1; gameOverState = false; alienSpeed = 1; alienFireRate = 0.02; createAliens(); } function levelUp() { level++; alienSpeed += 0.5; alienFireRate += 0.005; createAliens(); } class Player { constructor() { this.w = width * 0.1; this.h = this.w; this.x = width / 2 - this.w / 2; this.y = height - this.h - 10; } show() { let aspectRatio = playerImg.width / playerImg.height; let drawWidth = this.w; let drawHeight = this.w / aspectRatio; let yOffset = (this.h - drawHeight) / 2; image(playerImg, this.x, this.y + yOffset, drawWidth, drawHeight); } move() { if (isMobile) { if (touches.length > 0) { this.x = constrain(touches[0].x - this.w / 2, 0, width - this.w); } } else { if (keyIsDown(LEFT_ARROW)) { this.x = max(this.x - width * 0.01, 0); } if (keyIsDown(RIGHT_ARROW)) { this.x = min(this.x + width * 0.01, width - this.w); } } } } class Alien { constructor(x, y, img) { this.x = x; this.y = y; this.w = width * 0.05; this.h = this.w; this.xdir = 1; this.img = img; } show() { image(this.img, this.x, this.y, this.w, this.h); } move() { this.x += this.xdir * alienSpeed; if (this.x > width - this.w || this.x < 0) { this.xdir *= -1; this.y += this.h; } if (this.y > height - this.h) { gameOver(); } } } class Bullet { constructor(x, y, speed) { this.x = x; this.y = y; this.speed = speed; this.size = width * 0.0225; } show() { image(bulletImg, this.x, this.y, this.size, this.size); } move() { this.y += this.speed; } hits(target) { let d = dist(this.x + this.size / 2, this.y + this.size / 2, target.x + target.w / 2, target.y + target.h / 2); return d < (this.size + target.w) / 2; } offscreen() { return (this.y < 0 || this.y > height); } } function windowResized() { resizeCanvas(windowWidth, windowHeight); }