Selasa, 05 Mei 2026

JAVA Netbeans Menu Launcher Game 2 (With Player Name)

 





/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package multipanelmenu;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


public class MultiPanelMenu extends JFrame {


    private CardLayout cardLayout;

    private JPanel mainPanel;

    private JTextField nameField;

    private JLabel displayLabel;


    public MultiPanelMenu() {

        setTitle("Menu Example");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(400, 200);

        setLocationRelativeTo(null);


        // CardLayout to switch between panels

        cardLayout = new CardLayout();

        mainPanel = new JPanel(cardLayout);


        // Create panels

        JPanel panel1 = createMenuPanel();

        JPanel panel2 = createLevel1Panel();

        JPanel panel3 = createDisplayPanel();


        // Add panels to mainPanel

        mainPanel.add(panel1, "Menu");

        mainPanel.add(panel2, "Level1");

        mainPanel.add(panel3, "Display");


        add(mainPanel);

        cardLayout.show(mainPanel, "Menu");

    }


    // Panel 1: Main Menu

    private JPanel createMenuPanel() {

        JPanel panel = new JPanel(new GridLayout(4, 1, 10, 10));


        JButton btnLevel1 = new JButton("Level 1");

        JButton btnLevel2 = new JButton("Level 2");

        JButton btnLevel3 = new JButton("Level 3");

        JButton btnClose = new JButton("Close");


        btnLevel1.addActionListener(e -> cardLayout.show(mainPanel, "Level1"));

        btnLevel2.addActionListener(e -> JOptionPane.showMessageDialog(this, "Level 2 not implemented yet."));

        btnLevel3.addActionListener(e -> JOptionPane.showMessageDialog(this, "Level 3 not implemented yet."));

        btnClose.addActionListener(e -> System.exit(0));


        panel.add(btnLevel1);

        panel.add(btnLevel2);

        panel.add(btnLevel3);

        panel.add(btnClose);


        return panel;

    }


    // Panel 2: Level 1 Input

    private JPanel createLevel1Panel() {

        JPanel panel = new JPanel(new FlowLayout());


        JLabel label = new JLabel("Name Player:");

        nameField = new JTextField(15);

        JButton btnOk = new JButton("OK");


        btnOk.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                String name = nameField.getText().trim();

                if (name.isEmpty()) {

                    JOptionPane.showMessageDialog(MultiPanelMenu.this, "Please enter a name.");

                } else {

                    displayLabel.setText("Player Name: " + name);

                    cardLayout.show(mainPanel, "Display");

                }

            }

        });


        panel.add(label);

        panel.add(nameField);

        panel.add(btnOk);


        return panel;

    }


    // Panel 3: Display Player Name

    private JPanel createDisplayPanel() {

        JPanel panel = new JPanel(new BorderLayout());


        displayLabel = new JLabel("", SwingConstants.CENTER);

        displayLabel.setFont(new Font("Arial", Font.BOLD, 16));


        JButton btnBack = new JButton("Back to Menu");

        btnBack.addActionListener(e -> cardLayout.show(mainPanel, "Menu"));


        panel.add(displayLabel, BorderLayout.CENTER);

        panel.add(btnBack, BorderLayout.SOUTH);


        return panel;

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {

            new MultiPanelMenu().setVisible(true);

        });

    }

}

JAVA Netbeans Menu Launcher Game

 





/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package gamemenu1;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.Iterator;


public class GameMenu1 extends JFrame {


    private CardLayout cardLayout;

    private JPanel mainPanel;


    public GameMenu1() {

        setTitle("Game Menu");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(800, 600);

        setLocationRelativeTo(null);


        cardLayout = new CardLayout();

        mainPanel = new JPanel(cardLayout);


        // Menu Panel

        JPanel menuPanel = new JPanel();

        menuPanel.setLayout(new GridLayout(4, 1, 10, 10));

        JButton btnLevel1 = new JButton("Level 1");

        JButton btnLevel2 = new JButton("Level 2");

        JButton btnLevel3 = new JButton("Level 3");

        JButton btnClose = new JButton("Close");


        menuPanel.add(btnLevel1);

        menuPanel.add(btnLevel2);

        menuPanel.add(btnLevel3);

        menuPanel.add(btnClose);


        // Game Panel (Level 1)

        GamePanel level1Panel = new GamePanel(this);


        // Add panels to CardLayout

        mainPanel.add(menuPanel, "Menu");

        mainPanel.add(level1Panel, "Level1");


        // Button actions

        btnLevel1.addActionListener(e -> {

            cardLayout.show(mainPanel, "Level1");

            level1Panel.requestFocusInWindow();

        });


        btnLevel2.addActionListener(e -> JOptionPane.showMessageDialog(this, "Level 2 not implemented yet."));

        btnLevel3.addActionListener(e -> JOptionPane.showMessageDialog(this, "Level 3 not implemented yet."));

        btnClose.addActionListener(e -> System.exit(0));


        add(mainPanel);

        setVisible(true);

    }


    public void showMenu() {

        cardLayout.show(mainPanel, "Menu");

    }


    // Game Panel Class

    static class GamePanel extends JPanel implements ActionListener {

        private int playerX = 100, playerY = 100;

        private final int playerSize = 30;

        private final int speed = 5;


        private java.util.List<Rectangle> missiles = new ArrayList<>();

        private Timer timer;

        private GameMenu1 parent;


        public GamePanel(GameMenu1 parent) {

            this.parent = parent;

            setBackground(Color.BLACK);

            setFocusable(true);


            // Key Bindings for movement

            bindKey("W", () -> playerY -= speed);

            bindKey("S", () -> playerY += speed);

            bindKey("A", () -> playerX -= speed);

            bindKey("D", () -> playerX += speed);

            bindKey("SPACE", this::fireMissile);

            bindKey("ESCAPE", () -> parent.showMenu());


            timer = new Timer(16, this); // ~60 FPS

            timer.start();

        }


        private void bindKey(String key, Runnable action) {

            getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key);

            getActionMap().put(key, new AbstractAction() {

                @Override

                public void actionPerformed(ActionEvent e) {

                    action.run();

                }

            });

        }


        private void fireMissile() {

            missiles.add(new Rectangle(playerX + playerSize / 2 - 2, playerY - 10, 4, 10));

        }


        @Override

        public void actionPerformed(ActionEvent e) {

            // Move missiles upward

            Iterator<Rectangle> it = missiles.iterator();

            while (it.hasNext()) {

                Rectangle m = it.next();

                m.y -= 10;

                if (m.y < 0) it.remove();

            }

            repaint();

        }


        @Override

        protected void paintComponent(Graphics g) {

            super.paintComponent(g);


            // Draw player (circle)

            g.setColor(Color.YELLOW);

            g.fillOval(playerX, playerY, playerSize, playerSize);


            // Draw missiles

            g.setColor(Color.RED);

            for (Rectangle m : missiles) {

                g.fillRect(m.x, m.y, m.width+2, m.height+2);

            }

        }

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(GameMenu1::new);

    }

}

Minggu, 03 Mei 2026

Sabtu, 02 Mei 2026

JAVA - Create Game Launcher (Gemini AI & Copilot)

 

prompt :
java netbeans create opening game with jpanel 1 with button play, close if button play clicked then jpanel 2 is opened text="game begin"

Creating a simple transition between two screens (panels) in NetBeans is most efficiently handled using a CardLayout. This allows you to stack multiple JPanel components and "flip" between them.

Here is a clean, step-by-step guide to setting this up.


1. The Main Frame Setup

The JFrame acts as the container. We will use a JPanel with a CardLayout to hold both your Menu and the Game screen.

Java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameLauncher extends JFrame {

    // CardLayout helps us switch between panels
    private CardLayout cardLayout = new CardLayout();
    private JPanel mainContainer = new JPanel(cardLayout);

    public GameLauncher() {
        setTitle("Simple Game Launcher");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // Create the two panels
        JPanel menuPanel = createMenuPanel();
        JPanel gamePanel = createGamePanel();

        // Add panels to the container with unique names
        mainContainer.add(menuPanel, "Menu");
        mainContainer.add(gamePanel, "Game");

        add(mainContainer);
        setVisible(true);
    }

    // --- Panel 1: The Opening Menu ---
    private JPanel createMenuPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.DARK_GRAY);
        panel.setLayout(new GridBagLayout()); // Centers the button

        JButton playButton = new JButton("PLAY");
        playButton.setPreferredSize(new Dimension(100, 40));

        // Action: Switch to the "Game" panel when clicked
        playButton.addActionListener(e -> cardLayout.show(mainContainer, "Game"));

        panel.add(playButton);
        return panel;
    }

    // --- Panel 2: The Game Screen ---
    private JPanel createGamePanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.BLACK);
        panel.setLayout(new GridBagLayout());

        JLabel text = new JLabel("GAME BEGIN");
        text.setForeground(Color.WHITE);
        text.setFont(new Font("Arial", Font.BOLD, 24));

        panel.add(text);
        return panel;
    }

    public static void main(String[] args) {
        // Run the GUI on the Event Dispatch Thread
        SwingUtilities.invokeLater(GameLauncher::new);
    }
}

2. Key Components Explained

FeatureDescription
CardLayoutThink of this like a deck of cards. Only one "card" (panel) is visible at a time.
cardLayout.show()This method takes the container and the String name of the panel you want to display.
GridBagLayoutA simple way to ensure your buttons and text stay perfectly centered in the middle of the window.
Lambda ExpressionThe e -> ... syntax is a shorthand way to write the ActionListener for your button.

3. How to do this in NetBeans GUI Builder (Design Mode)

If you prefer using the Drag-and-Drop interface in NetBeans:

  1. Create a New JFrame Form: Right-click package > New > JFrame Form.

  2. Add a Panel: Drag a JPanel onto the Frame. Right-click it > Set Layout > Card Layout. (Let's call this mainPanel).

  3. Add Sub-Panels: Drag two more JPanel components inside mainPanel.

    • In the Navigator (bottom left), give them variable names like menuPanel and gamePanel.

  4. Design:

    • Select menuPanel and drag a JButton into it.

    • Select gamePanel and drag a JLabel into it.

  5. The Code: Double-click the "Play" button to generate the action event and paste this inside:

    Java
    CardLayout cl = (CardLayout) mainPanel.getLayout();
    cl.show(mainPanel, "gamePanel"); 
    

    (Note: NetBeans usually names the cards based on the variable name of the panel added).






Copilot AI

Got it ✅ — you want a Java Swing program (can be built in NetBeans) with:

  • JPanel 1: Contains Play and Close buttons.
  • If Play is clicked → Switch to JPanel 2 with text "Game Begin".
  • If Close is clicked → Exit the program.

Here’s a complete, runnable Java program that does exactly that.
It uses a CardLayout to switch between panels.

Java
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class OpeningGame extends JFrame { private CardLayout cardLayout; private JPanel mainPanel; public OpeningGame() { setTitle("Opening Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); setLocationRelativeTo(null); // Center window // CardLayout to switch between panels cardLayout = new CardLayout(); mainPanel = new JPanel(cardLayout); // Create panels JPanel panel1 = createOpeningPanel(); JPanel panel2 = createGamePanel(); // Add panels to CardLayout mainPanel.add(panel1, "Opening"); mainPanel.add(panel2, "Game"); add(mainPanel); setVisible(true); } // Panel 1: Opening screen with Play and Close buttons private JPanel createOpeningPanel() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); JButton playButton = new JButton("Play"); JButton closeButton = new JButton("Close"); // Play button action playButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cardLayout.show(mainPanel, "Game"); } }); // Close button action closeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); panel.add(playButton); panel.add(closeButton); return panel; } // Panel 2: Game screen private JPanel createGamePanel() { JPanel panel = new JPanel(); JLabel label = new JLabel("Game Begin"); label.setFont(new Font("Arial", Font.BOLD, 20)); panel.add(label); return panel; } public static void main(String[] args) { // Run GUI in Event Dispatch Thread SwingUtilities.invokeLater(() -> new OpeningGame()); } }

How it works:

  1. CardLayout is used to switch between JPanel 1 and JPanel 2.
  2. Play button → cardLayout.show(mainPanel, "Game") switches to the game panel.
  3. Close button → System.exit(0) closes the app.
  4. NetBeans:
    • Create a new Java Application project.
    • Add this code to a .java file.
    • Run it — you’ll see the opening screen with two buttons.

JAVA - Quadcopter Game - Shape Circle

 





/*

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

 */

package quadcopteranimation;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.AffineTransform;


public class QuadcopterSim extends JPanel implements ActionListener, KeyListener {

    private int x = 250, y = 250; // Body position

    private double angle = 0;      // Rotor rotation angle

    private final int speed = 5;   // Movement speed

    private Timer timer;


    public QuadcopterSim() {

        setFocusable(true);

        addKeyListener(this);

        // Timer fires every 16ms (~60 FPS)

        timer = new Timer(16, this);

        timer.start();

    }


    @Override

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


        int bodyWidth = 35;

        int bodyHeight = 35;

        int spokeLen = 30;


        // 1. Draw the Body (Rectangle)

        g2d.setColor(Color.BLACK);

        g2d.drawOval(x - bodyWidth/2, y - bodyHeight/2, bodyWidth, bodyHeight);


        // 2. Draw Spokes and Rotors

        // Relative positions for the 4 corners

        int[][] offsets = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};

        

        for (int[] pos : offsets) {

            int endX = x + (pos[0] * spokeLen);

            int endY = y + (pos[1] * spokeLen);


            // Draw Spoke

            g2d.setColor(Color.GRAY);

            g2d.drawLine(x, y, endX, endY);


            // Draw Rotating Rotor

            drawRotor(g2d, endX, endY, angle);

        }

    }


    private void drawRotor(Graphics2D g2d, int cx, int cy, double angle) {

        AffineTransform old = g2d.getTransform();

        g2d.translate(cx, cy);

        g2d.rotate(angle);

        

        g2d.setColor(Color.RED);

        // Draw the rotor circle

        g2d.drawOval(-15, -15, 30, 30);

        // Draw a line across the circle to show rotation

        g2d.drawLine(-15, 0, 15, 0);

        

        g2d.setTransform(old);

    }


    @Override

    public void actionPerformed(ActionEvent e) {

        angle += 0.3; // Spin the rotors

        repaint();

    }


    @Override

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_W) y -= speed;

        if (key == KeyEvent.VK_S) y += speed;

        if (key == KeyEvent.VK_A) x -= speed;

        if (key == KeyEvent.VK_D) x += speed;

    }


    @Override public void keyReleased(KeyEvent e) {}

    @Override public void keyTyped(KeyEvent e) {}


    public static void main(String[] args) {

        JFrame frame = new JFrame("Quadcopter GUI Control");

        frame.add(new QuadcopterSim());

        frame.setSize(600, 600);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

}

JAVA - Quadcopter Game - Bonus Code

 



/*

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

 */

package quadcopteranimation4;

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.util.Random;


public class QuadcopterAnimation4 extends JPanel {

    // Quadcopter position and movement

    private double x = 100, y = 100;

    private double dx = 2, dy = 2; // Velocity

    private double rotationAngle = 0; // Rotor rotation

    

    private final int BODY_WIDTH = 60;

    private final int BODY_HEIGHT = 50;

    private final int ROTOR_SIZE = 30;

    private final Random rand = new Random();


    public QuadcopterAnimation4() {

        // Animation Timer: ~60 FPS (16ms delay)

        Timer timer = new Timer(16, e -> {

            updatePosition();

            rotationAngle += 0.5; // Speed of rotor spin

            repaint();

        });

        timer.start();

    }


    private void updatePosition() {

        x += dx;

        y += dy;


        // Bounce off walls and pick a random new direction

        if (x < 0 || x > getWidth() - BODY_WIDTH - ROTOR_SIZE) {

            dx = -dx + (rand.nextDouble() - 0.5); 

            x = Math.max(0, Math.min(x, getWidth() - BODY_WIDTH));

        }

        if (y < 0 || y > getHeight() - BODY_HEIGHT - ROTOR_SIZE) {

            dy = -dy + (rand.nextDouble() - 0.5);

            y = Math.max(0, Math.min(y, getHeight() - BODY_HEIGHT));

        }

    }


    @Override

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


        // 1. Draw Body

        g2d.setColor(Color.ORANGE);

        g2d.fillRect((int)x, (int)y, BODY_WIDTH, BODY_HEIGHT);


        // 2. Draw 4 Rotors (Corners)

        drawRotor(g2d, x, y); // Top Left

        drawRotor(g2d, x + BODY_WIDTH, y); // Top Right

        drawRotor(g2d, x, y + BODY_HEIGHT); // Bottom Left

        drawRotor(g2d, x + BODY_WIDTH, y + BODY_HEIGHT); // Bottom Right

    }


    private void drawRotor(Graphics2D g2d, double cx, double cy) {

        g2d.setColor(Color.BLACK);

        // Draw the outer circle

        Ellipse2D circle = new Ellipse2D.Double(cx - ROTOR_SIZE/2.0, cy - ROTOR_SIZE/2.0, ROTOR_SIZE, ROTOR_SIZE);

        g2d.draw(circle);


        // Draw 4 Rotating Spokes

        for (int i = 0; i < 4; i++) {

            double angle = rotationAngle + (i * Math.PI / 2);

            int x2 = (int) (cx + Math.cos(angle) * (ROTOR_SIZE / 2.0));

            int y2 = (int) (cy + Math.sin(angle) * (ROTOR_SIZE / 2.0));

            g2d.drawLine((int)cx, (int)cy, x2, y2);

        }

    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("Quadcopter Random Flight");

        frame.add(new QuadcopterAnimation4());

        frame.setSize(800, 600);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

    }

}



JAVA - Quadcopter Game 4

 



/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */
package quadcoptergame5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;

public class QuadcopterGame5 extends JPanel implements ActionListener, KeyListener {
    Timer timer;
    int quadX = 400, quadY = 300;
    int score = 0;
    boolean gameOver = false;
    double angle = 0; // untuk rotasi baling-baling

    ArrayList<Rectangle> enemies = new ArrayList<>();
    ArrayList<Rectangle> missiles = new ArrayList<>();
    ArrayList<Rectangle> enemyMissiles = new ArrayList<>();
    Random rand = new Random();

    public QuadcopterGame5() {
        setPreferredSize(new Dimension(800, 600));
        setBackground(Color.BLACK);
        timer = new Timer(30, this);
        timer.start();
        addKeyListener(this);
        setFocusable(true);

        // Buat 20 musuh acak
        for (int i = 0; i < 20; i++) {
            enemies.add(new Rectangle(rand.nextInt(700), rand.nextInt(500), 20, 20));
        }

        // Timer untuk musuh menembak setiap 1 detik
        new Timer(1000, e -> {
            if (!enemies.isEmpty()) {
                Rectangle enemy = enemies.get(rand.nextInt(enemies.size()));
                enemyMissiles.add(new Rectangle(enemy.x, enemy.y, 5, 10));
            }
        }).start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (gameOver) {
            g.setColor(Color.RED);
            g.setFont(new Font("Arial", Font.BOLD, 40));
            g.drawString("GAME OVER", 300, 300);
            return;
        }

        // Gambar quadcopter (badan + 4 baling-baling berputar)
        g.setColor(Color.CYAN);
        g.drawOval(quadX, quadY, 30, 30);

        int cx = quadX + 15, cy = quadY + 15;
        int radius = 40;
        for (int i = 0; i < 4; i++) {
            double theta = angle + i * Math.PI / 2;
            int x = (int)(cx + radius * Math.cos(theta));
            int y = (int)(cy + radius * Math.sin(theta));
            //g.drawLine(cx, cy, x, y);
            g.drawOval(x-10, y-10, 20, 20);
        }

        // Gambar musuh
        g.setColor(Color.GREEN);
        for (Rectangle enemy : enemies) {
            g.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
        }

        // Gambar misil quadcopter
        g.setColor(Color.YELLOW);
        for (Rectangle m : missiles) {
            g.fillRect(m.x, m.y, m.width, m.height);
        }

        // Gambar misil musuh
        g.setColor(Color.RED);
        for (Rectangle em : enemyMissiles) {
            g.fillRect(em.x, em.y, em.width, em.height);
        }

        // Skor
        g.setColor(Color.WHITE);
        g.drawString("Score: " + score, 10, 20);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        angle += 0.1; // rotasi baling-baling

        // Update posisi misil quadcopter
        for (Rectangle m : missiles) {
            m.y -= 10;
        }
        missiles.removeIf(m -> m.y < 0);

        // Update posisi musuh (bergerak acak halus)
        for (Rectangle enemy : enemies) {
            enemy.x += rand.nextInt(3) - 1;
            enemy.y += rand.nextInt(3) - 1;
            if (enemy.x < 0) enemy.x = 0;
            if (enemy.y < 0) enemy.y = 0;
            if (enemy.x > 780) enemy.x = 780;
            if (enemy.y > 580) enemy.y = 580;
        }

        // Update misil musuh
        for (Rectangle em : enemyMissiles) {
            em.y += 5;
            if (em.intersects(new Rectangle(quadX, quadY, 30, 30))) {
                gameOver = true;
            }
        }

        // Cek tabrakan misil dengan musuh
        Iterator<Rectangle> itM = missiles.iterator();
        while (itM.hasNext()) {
            Rectangle m = itM.next();
            Iterator<Rectangle> itE = enemies.iterator();
            while (itE.hasNext()) {
                Rectangle enemy = itE.next();
                if (m.intersects(enemy)) {
                    itM.remove();
                    itE.remove();
                    score++;
                    break;
                }
            }
        }

        repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (gameOver) return;
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_W) quadY -= 10;
        if (key == KeyEvent.VK_S) quadY += 10;
        if (key == KeyEvent.VK_A) quadX -= 10;
        if (key == KeyEvent.VK_D) quadX += 10;
        if (key == KeyEvent.VK_SPACE) {
            missiles.add(new Rectangle(quadX+10, quadY, 5, 10));
        }
    }

    @Override public void keyReleased(KeyEvent e) {}
    @Override public void keyTyped(KeyEvent e) {}

    public static void main(String[] args) {
        JFrame frame = new JFrame("Quadcopter Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new QuadcopterGame5());
        frame.pack();
        frame.setVisible(true);
    }
}