Prompt Gemini AI :
java netbeans swing gui canvas jpanel create menu game launcher button "level 1", "level 2", "level 3", if button "level 1" clicked then launch jpanel with 4 small circles rotating a circle and can move by key WASD and press space key to launch missile, create 20 rectangle enemies moving random and launch missile every second, when hit by missile then dissapear, score system add, and when the player hit by enemy then game over
To create this game in Java using Swing, the best approach is to use a CardLayout to switch between your Menu and the Game Panel.
Below is a complete, simplified implementation. You can copy this into a single file named GameLauncher.java in your NetBeans project.
Java Game Launcher Implementation
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class GameLauncher extends JFrame {
private CardLayout cardLayout = new CardLayout();
private JPanel mainPanel = new JPanel(cardLayout);
public GameLauncher() {
setTitle("Java Circle Strike");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Menu Panel
JPanel menuPanel = new JPanel(new GridLayout(4, 1, 10, 10));
JButton btnL1 = new JButton("Level 1");
JButton btnL2 = new JButton("Level 2");
JButton btnL3 = new JButton("Level 3");
btnL1.addActionListener(e -> startGame());
menuPanel.add(new JLabel("GAME LAUNCHER", SwingConstants.CENTER));
menuPanel.add(btnL1);
menuPanel.add(btnL2);
menuPanel.add(btnL3);
mainPanel.add(menuPanel, "Menu");
add(mainPanel);
cardLayout.show(mainPanel, "Menu");
}
private void startGame() {
GamePanel game = new GamePanel(this);
mainPanel.add(game, "Game");
cardLayout.show(mainPanel, "Game");
game.requestFocusInWindow();
}
public void showMenu() {
cardLayout.show(mainPanel, "Menu");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GameLauncher().setVisible(true));
}
}
class GamePanel extends JPanel implements ActionListener, KeyListener {
private Timer timer;
private int playerX = 400, playerY = 300;
private double rotation = 0;
private int score = 0;
private boolean gameOver = false;
private final int PLAYER_SPEED = 5;
private boolean[] keys = new boolean[256];
private ArrayList<GameObject> enemies = new ArrayList<>();
private ArrayList<Projectile> playerMissiles = new ArrayList<>();
private ArrayList<Projectile> enemyMissiles = new ArrayList<>();
private long lastEnemyShootTime = 0;
public GamePanel(GameLauncher parent) {
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
// Initialize 20 enemies
Random rand = new Random();
for (int i = 0; i < 20; i++) {
enemies.add(new GameObject(rand.nextInt(700), rand.nextInt(500), 30, 30));
}
timer = new Timer(16, this); // ~60 FPS
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (gameOver) {
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 50));
g.drawString("GAME OVER", 250, 300);
g.setFont(new Font("Arial", Font.PLAIN, 20));
g.drawString("Score: " + score, 350, 350);
return;
}
// Draw Score
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 20, 20);
// Draw Player (Rotating circles)
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.CYAN);
for (int i = 0; i < 4; i++) {
double angle = rotation + (i * Math.PI / 2);
int cx = playerX + (int) (30 * Math.cos(angle));
int cy = playerY + (int) (30 * Math.sin(angle));
g2d.fillOval(cx - 5, cy - 5, 10, 10);
}
g2d.drawOval(playerX - 5, playerY - 5, 10, 10); // Center point
// Draw Enemies
g.setColor(Color.RED);
for (GameObject e : enemies) {
g.fillRect(e.x, e.y, e.w, e.h);
}
// Draw Player Missiles
g.setColor(Color.YELLOW);
for (Projectile p : playerMissiles) g.fillOval(p.x, p.y, 5, 5);
// Draw Enemy Missiles
g.setColor(Color.ORANGE);
for (Projectile p : enemyMissiles) g.fillOval(p.x, p.y, 5, 5);
}
@Override
public void actionPerformed(ActionEvent e) {
if (gameOver) return;
// Player Movement
if (keys[KeyEvent.VK_W]) playerY -= PLAYER_SPEED;
if (keys[KeyEvent.VK_S]) playerY += PLAYER_SPEED;
if (keys[KeyEvent.VK_A]) playerX -= PLAYER_SPEED;
if (keys[KeyEvent.VK_D]) playerX += PLAYER_SPEED;
rotation += 0.05;
// Enemy Logic
Random rand = new Random();
long currentTime = System.currentTimeMillis();
boolean shouldShoot = (currentTime - lastEnemyShootTime > 1000);
for (GameObject enemy : enemies) {
// Random movement
enemy.x += rand.nextInt(5) - 2;
enemy.y += rand.nextInt(5) - 2;
// Enemy shooting every second
if (shouldShoot) {
enemyMissiles.add(new Projectile(enemy.x + 15, enemy.y + 30, 0, 4));
}
// Player-Enemy Collision
if (new Rectangle(playerX - 15, playerY - 15, 30, 30).intersects(enemy.getBounds())) {
gameOver = true;
}
}
if (shouldShoot) lastEnemyShootTime = currentTime;
// Update Missiles
playerMissiles.forEach(p -> p.y -= 7);
enemyMissiles.forEach(p -> p.y += 4);
// Collision Detection: Player Missile hits Enemy
for (int i = playerMissiles.size() - 1; i >= 0; i--) {
Projectile p = playerMissiles.get(i);
for (int j = enemies.size() - 1; j >= 0; j--) {
if (enemies.get(j).getBounds().contains(p.x, p.y)) {
enemies.remove(j);
playerMissiles.remove(i);
score += 100;
break;
}
}
}
// Collision Detection: Enemy Missile hits Player
for (Projectile p : enemyMissiles) {
if (new Rectangle(playerX - 15, playerY - 15, 30, 30).contains(p.x, p.y)) {
gameOver = true;
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
playerMissiles.add(new Projectile(playerX, playerY - 20, 0, -7));
}
}
@Override public void keyReleased(KeyEvent e) { keys[e.getKeyCode()] = false; }
@Override public void keyTyped(KeyEvent e) {}
}
class GameObject {
int x, y, w, h;
GameObject(int x, int y, int w, int h) { this.x = x; this.y = y; this.w = w; this.h = h; }
Rectangle getBounds() { return new Rectangle(x, y, w, h); }
}
class Projectile {
int x, y, vx, vy;
Projectile(int x, int y, int vx, int vy) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; }
}
Key Features Included:
Menu System: Uses
CardLayoutto switch from the "Level" buttons to the game screen.The Rotating Player: Instead of a static image, the player is drawn as 4 circles orbiting a center point using basic trigonometry:
$x = \text{centerX} + \text{radius} \cdot \cos(\theta)$
$y = \text{centerY} + \text{radius} \cdot \sin(\theta)$
Movement: Smooth movement using a boolean array for the
WASDkeys to prevent the "stutter" often found in basic KeyListeners.Enemy AI:
20 rectangle enemies are spawned.
They move randomly each frame.
They fire a projectile downward exactly every 1,000 milliseconds (1 second).
Game Logic:
Score: Increases when you hit an enemy.
Collision: Uses
Rectangle.intersects()andRectangle.contains().Game Over: Triggers if a player touches an enemy or an enemy's missile.

Tidak ada komentar:
Posting Komentar