awaismirza / Java-Game-Brick-Breaker

BrickBreaker Game Developed in Java using swing
133 stars 141 forks source link

Break breaker game #4

Open Faizan6016 opened 7 months ago

Faizan6016 commented 7 months ago

import javax.swing.; import java.awt.; import java.awt.event.*;

public class BrickBreaker extends JFrame {

private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int PADDLE_WIDTH = 100;
private static final int PADDLE_HEIGHT = 20;
private static final int BALL_SIZE = 20;

private int paddleX;
private int ballX, ballY;
private int ballXSpeed = 1, ballYSpeed = -1;

public BrickBreaker() {
    setTitle("Brick Breaker");
    setSize(WIDTH, HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_LEFT) {
                paddleX -= 20;
            } else if (keyCode == KeyEvent.VK_RIGHT) {
                paddleX += 20;
            }
        }
    });
    paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;
    ballX = WIDTH / 2 - BALL_SIZE / 2;
    ballY = HEIGHT / 2 - BALL_SIZE / 2;
    gameLoop();
}

private void gameLoop() {
    while (true) {
        ballX += ballXSpeed;
        ballY += ballYSpeed;

        if (ballX <= 0 || ballX >= WIDTH - BALL_SIZE) {
            ballXSpeed *= -1;
        }
        if (ballY <= 0 || ballY >= HEIGHT - BALL_SIZE) {
            ballYSpeed *= -1;
        }

        if (ballY + BALL_SIZE >= HEIGHT - PADDLE_HEIGHT && ballX + BALL_SIZE >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {
            ballYSpeed *= -1;
        }

        repaint();
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.BLACK);
Gokilp commented 6 months ago

please assign to me #gssoc24

swatimaurya19 commented 6 months ago

could you please assign this to me