SpaceInvadersGame.java
                        
                             · 4.7 KiB · Java
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SpaceInvadersGame extends JPanel implements KeyListener, ActionListener {
    private int playerX, playerY;
    private List<Alien> aliens;
    private List<Bullet> bullets;
    private boolean isGameOver;
    private int score;
    private SpaceInvadersGame() {
        playerX = 300;
        playerY = 480;
        isGameOver = false;
        score = 0;
        aliens = new ArrayList<>();
        bullets = new ArrayList();
        // Initialize aliens
        for (int i = 0; i < 6; i++) {
            aliens.add(new Alien(i * 100, 0));
        }
        Timer timer = new Timer(15, this);
        timer.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Draw background
        g.setColor(Color.black);
        g.fillRect(0, 0, 800, 600);
        // Draw player
        g.setColor(Color.blue);
        g.fillRect(playerX, playerY, 50, 50);
        // Draw aliens
        for (Alien alien : aliens) {
            g.setColor(Color.green);
            g.fillRect(alien.x, alien.y, 50, 50);
        }
        // Draw bullets
        for (Bullet bullet : bullets) {
            g.setColor(Color.red);
            g.fillRect(bullet.x, bullet.y, 10, 20);
        }
        // Display score
        g.setColor(Color.white);
        g.setFont(new Font("Arial", Font.PLAIN, 20));
        g.drawString("Score: " + score, 10, 30);
        if (isGameOver) {
            g.setFont(new Font("Arial", Font.PLAIN, 50));
            g.drawString("Game Over", 300, 300);
        }
    }
    public void actionPerformed(ActionEvent e) {
        if (!isGameOver) {
            // Move aliens
            for (Alien alien : aliens) {
                alien.y += 2;
            }
            // Move bullets
            for (Bullet bullet : bullets) {
                bullet.y -= 5;
            }
            // Check for collisions
            checkCollisions();
            // Remove off-screen aliens and bullets
            aliens.removeIf(alien -> alien.y > 600);
            bullets.removeIf(bullet -> bullet.y < 0);
            // Spawn new aliens
            if (aliens.isEmpty()) {
                for (int i = 0; i < 6; i++) {
                    aliens.add(new Alien(i * 100, 0));
                }
            }
            repaint();
        }
    }
    private void checkCollisions() {
        for (Alien alien : new ArrayList<>(aliens)) {
            for (Bullet bullet : new ArrayList<>(bullets)) {
                if (alien.getBounds().intersects(bullet.getBounds())) {
                    aliens.remove(alien);
                    bullets.remove(bullet);
                    score += 10;
                }
            }
            if (alien.getBounds().intersects(new Rectangle(playerX, playerY, 50, 50))) {
                isGameOver = true;
            }
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT && playerX > 0) {
            playerX -= 20;
        }
        if (key == KeyEvent.VK_RIGHT && playerX < 750) {
            playerX += 20;
        }
        if (key == KeyEvent.VK_SPACE) {
            if (!isGameOver) {
                bullets.add(new Bullet(playerX + 20, playerY));
            } else {
                // Restart the game
                isGameOver = false;
                playerX = 300;
                score = 0;
                aliens.clear();
                bullets.clear();
                for (int i = 0; i < 6; i++) {
                    aliens.add(new Alien(i * 100, 0));
                }
            }
        }
    }
    public void keyReleased(KeyEvent e) {}
    public static void main(String[] args) {
        JFrame frame = new JFrame("Space Invaders");
        SpaceInvadersGame game = new SpaceInvadersGame();
        frame.add(game);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    private class Alien {
        int x, y;
        Alien(int x, int y) {
            this.x = x;
            this.y = y;
        }
        Rectangle getBounds() {
            return new Rectangle(x, y, 50, 50);
        }
    }
    private class Bullet {
        int x, y;
        Bullet(int x, int y) {
            this.x = x;
            this.y = y;
        }
        Rectangle getBounds() {
            return new Rectangle(x, y, 10, 20);
        }
    }
}
                | 1 | import javax.swing.*; | 
| 2 | import java.awt.*; | 
| 3 | import java.awt.event.ActionEvent; | 
| 4 | import java.awt.event.ActionListener; | 
| 5 | import java.awt.event.KeyEvent; | 
| 6 | import java.awt.event.KeyListener; | 
| 7 | import java.util.ArrayList; | 
| 8 | import java.util.List; | 
| 9 | import java.util.Random; | 
| 10 | |
| 11 | public class SpaceInvadersGame extends JPanel implements KeyListener, ActionListener { | 
| 12 | private int playerX, playerY; | 
| 13 | private List<Alien> aliens; | 
| 14 | private List<Bullet> bullets; | 
| 15 | private boolean isGameOver; | 
| 16 | private int score; | 
| 17 | |
| 18 | private SpaceInvadersGame() { | 
| 19 | playerX = 300; | 
| 20 | playerY = 480; | 
| 21 | isGameOver = false; | 
| 22 | score = 0; | 
| 23 | |
| 24 | aliens = new ArrayList<>(); | 
| 25 | bullets = new ArrayList(); | 
| 26 | |
| 27 | // Initialize aliens | 
| 28 | for (int i = 0; i < 6; i++) { | 
| 29 | aliens.add(new Alien(i * 100, 0)); | 
| 30 | } | 
| 31 | |
| 32 | Timer timer = new Timer(15, this); | 
| 33 | timer.start(); | 
| 34 | |
| 35 | addKeyListener(this); | 
| 36 | setFocusable(true); | 
| 37 | setFocusTraversalKeysEnabled(false); | 
| 38 | } | 
| 39 | |
| 40 | public void paintComponent(Graphics g) { | 
| 41 | super.paintComponent(g); | 
| 42 | // Draw background | 
| 43 | g.setColor(Color.black); | 
| 44 | g.fillRect(0, 0, 800, 600); | 
| 45 | |
| 46 | // Draw player | 
| 47 | g.setColor(Color.blue); | 
| 48 | g.fillRect(playerX, playerY, 50, 50); | 
| 49 | |
| 50 | // Draw aliens | 
| 51 | for (Alien alien : aliens) { | 
| 52 | g.setColor(Color.green); | 
| 53 | g.fillRect(alien.x, alien.y, 50, 50); | 
| 54 | } | 
| 55 | |
| 56 | // Draw bullets | 
| 57 | for (Bullet bullet : bullets) { | 
| 58 | g.setColor(Color.red); | 
| 59 | g.fillRect(bullet.x, bullet.y, 10, 20); | 
| 60 | } | 
| 61 | |
| 62 | // Display score | 
| 63 | g.setColor(Color.white); | 
| 64 | g.setFont(new Font("Arial", Font.PLAIN, 20)); | 
| 65 | g.drawString("Score: " + score, 10, 30); | 
| 66 | |
| 67 | if (isGameOver) { | 
| 68 | g.setFont(new Font("Arial", Font.PLAIN, 50)); | 
| 69 | g.drawString("Game Over", 300, 300); | 
| 70 | } | 
| 71 | } | 
| 72 | |
| 73 | public void actionPerformed(ActionEvent e) { | 
| 74 | if (!isGameOver) { | 
| 75 | // Move aliens | 
| 76 | for (Alien alien : aliens) { | 
| 77 | alien.y += 2; | 
| 78 | } | 
| 79 | |
| 80 | // Move bullets | 
| 81 | for (Bullet bullet : bullets) { | 
| 82 | bullet.y -= 5; | 
| 83 | } | 
| 84 | |
| 85 | // Check for collisions | 
| 86 | checkCollisions(); | 
| 87 | |
| 88 | // Remove off-screen aliens and bullets | 
| 89 | aliens.removeIf(alien -> alien.y > 600); | 
| 90 | bullets.removeIf(bullet -> bullet.y < 0); | 
| 91 | |
| 92 | // Spawn new aliens | 
| 93 | if (aliens.isEmpty()) { | 
| 94 | for (int i = 0; i < 6; i++) { | 
| 95 | aliens.add(new Alien(i * 100, 0)); | 
| 96 | } | 
| 97 | } | 
| 98 | |
| 99 | repaint(); | 
| 100 | } | 
| 101 | } | 
| 102 | |
| 103 | private void checkCollisions() { | 
| 104 | for (Alien alien : new ArrayList<>(aliens)) { | 
| 105 | for (Bullet bullet : new ArrayList<>(bullets)) { | 
| 106 | if (alien.getBounds().intersects(bullet.getBounds())) { | 
| 107 | aliens.remove(alien); | 
| 108 | bullets.remove(bullet); | 
| 109 | score += 10; | 
| 110 | } | 
| 111 | } | 
| 112 | |
| 113 | if (alien.getBounds().intersects(new Rectangle(playerX, playerY, 50, 50))) { | 
| 114 | isGameOver = true; | 
| 115 | } | 
| 116 | } | 
| 117 | } | 
| 118 | |
| 119 | public void keyTyped(KeyEvent e) {} | 
| 120 | |
| 121 | public void keyPressed(KeyEvent e) { | 
| 122 | int key = e.getKeyCode(); | 
| 123 | |
| 124 | if (key == KeyEvent.VK_LEFT && playerX > 0) { | 
| 125 | playerX -= 20; | 
| 126 | } | 
| 127 | |
| 128 | if (key == KeyEvent.VK_RIGHT && playerX < 750) { | 
| 129 | playerX += 20; | 
| 130 | } | 
| 131 | |
| 132 | if (key == KeyEvent.VK_SPACE) { | 
| 133 | if (!isGameOver) { | 
| 134 | bullets.add(new Bullet(playerX + 20, playerY)); | 
| 135 | } else { | 
| 136 | // Restart the game | 
| 137 | isGameOver = false; | 
| 138 | playerX = 300; | 
| 139 | score = 0; | 
| 140 | aliens.clear(); | 
| 141 | bullets.clear(); | 
| 142 | for (int i = 0; i < 6; i++) { | 
| 143 | aliens.add(new Alien(i * 100, 0)); | 
| 144 | } | 
| 145 | } | 
| 146 | } | 
| 147 | } | 
| 148 | |
| 149 | public void keyReleased(KeyEvent e) {} | 
| 150 | |
| 151 | public static void main(String[] args) { | 
| 152 | JFrame frame = new JFrame("Space Invaders"); | 
| 153 | SpaceInvadersGame game = new SpaceInvadersGame(); | 
| 154 | frame.add(game); | 
| 155 | frame.setSize(800, 600); | 
| 156 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 
| 157 | frame.setVisible(true); | 
| 158 | } | 
| 159 | |
| 160 | private class Alien { | 
| 161 | int x, y; | 
| 162 | |
| 163 | Alien(int x, int y) { | 
| 164 | this.x = x; | 
| 165 | this.y = y; | 
| 166 | } | 
| 167 | |
| 168 | Rectangle getBounds() { | 
| 169 | return new Rectangle(x, y, 50, 50); | 
| 170 | } | 
| 171 | } | 
| 172 | |
| 173 | private class Bullet { | 
| 174 | int x, y; | 
| 175 | |
| 176 | Bullet(int x, int y) { | 
| 177 | this.x = x; | 
| 178 | this.y = y; | 
| 179 | } | 
| 180 | |
| 181 | Rectangle getBounds() { | 
| 182 | return new Rectangle(x, y, 10, 20); | 
| 183 | } | 
| 184 | } | 
| 185 | } |