Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break breaker game #4

Open
Faizan6016 opened this issue Mar 22, 2024 · 2 comments
Open

Break breaker game #4

Faizan6016 opened this issue Mar 22, 2024 · 2 comments

Comments

@Faizan6016
Copy link

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
Copy link

Gokilp commented May 9, 2024

please assign to me #gssoc24

@swatimaurya19
Copy link

could you please assign this to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants