int playerX; int playerY; int playerSize = 40; int bulletSize = 10; boolean shooting = false; int bulletX, bulletY; ArrayList aliens; boolean gameOver = false; void setup() { size(600, 400); playerX = width / 2; playerY = height - 50; aliens = new ArrayList(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { aliens.add(new Alien(80 + i * 80, 50 + j * 50)); } } } void draw() { background(0); if (!gameOver) { // Draw player fill(0, 255, 0); drawPlayer(playerX, playerY); // Draw and move aliens for (Alien a : aliens) { a.display(); a.move(); } // Draw and move bullet if (shooting) { fill(255, 0, 0); ellipse(bulletX, bulletY, bulletSize, bulletSize); bulletY -= 5; if (bulletY < 0) shooting = false; // Check for collisions for (int i = aliens.size() - 1; i >= 0; i--) { if (aliens.get(i).hit(bulletX, bulletY)) { aliens.remove(i); shooting = false; break; } } } // Check if all aliens are destroyed if (aliens.isEmpty()) { gameOver = true; } } else { fill(255); textSize(32); textAlign(CENTER, CENTER); text("You Win!", width / 2, height / 2); } } void keyPressed() { if (keyCode == LEFT) { playerX -= 20; } else if (keyCode == RIGHT) { playerX += 20; } else if (key == ' ' && !shooting) { bulletX = playerX + playerSize / 2; bulletY = playerY; shooting = true; } } class Alien { int x, y; int size = 30; int speed = 2; Alien(int x, int y) { this.x = x; this.y = y; } void display() { fill(255, 0, 0); // RED aliens drawAlien(x, y); } void move() { x += speed; if (x > width - size || x < 0) { speed *= -1; y += 20; } } boolean hit(int bx, int by) { return bx > x && bx < x + size && by > y && by < y + size; } } void drawAlien(int x, int y) { fill(255, 0, 0); // RED aliens rect(x + 5, y, 20, 5); // Top bar rect(x, y + 5, 30, 5); // Second row rect(x, y + 10, 30, 5); // Third row rect(x + 5, y + 15, 20, 5); // Fourth row rect(x, y + 20, 10, 5); // Left foot rect(x + 20, y + 20, 10, 5); // Right foot } void drawPlayer(int x, int y) { fill(0, 255, 0); rect(x + 10, y, 20, 5); // Top bar rect(x, y + 5, 40, 5); // Second row rect(x + 5, y + 10, 30, 5); // Third row rect(x + 15, y + 15, 10, 5); // Cannon barrel }