Created
May 21, 2023 09:51
-
-
Save R3DHULK/4b8828a87c48b9300b0f4c30575966b3 to your computer and use it in GitHub Desktop.
Break Out Game In Java Swing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class BreakoutGame extends JFrame { | |
private static final int WIDTH = 480; | |
private static final int HEIGHT = 320; | |
private Ball ball; | |
private Paddle paddle; | |
private Brick[] bricks; | |
private Timer timer; | |
public BreakoutGame() { | |
setTitle("Breakout Game"); | |
setSize(WIDTH, HEIGHT); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setResizable(false); | |
setLocationRelativeTo(null); | |
ball = new Ball(); | |
paddle = new Paddle(); | |
bricks = new Brick[30]; | |
timer = new Timer(5, new GameLoop()); | |
addKeyListener(new PaddleMovement()); | |
initializeBricks(); | |
setVisible(true); | |
timer.start(); | |
} | |
private void initializeBricks() { | |
int brickX = 70; | |
int brickY = 50; | |
for (int i = 0; i < bricks.length; i++) { | |
bricks[i] = new Brick(brickX, brickY); | |
brickX += Brick.WIDTH + 10; | |
if ((i + 1) % 10 == 0) { | |
brickX = 70; | |
brickY += Brick.HEIGHT + 10; | |
} | |
} | |
} | |
@Override | |
public void paint(Graphics g) { | |
super.paint(g); | |
Graphics2D g2d = (Graphics2D) g; | |
ball.draw(g2d); | |
paddle.draw(g2d); | |
for (Brick brick : bricks) { | |
brick.draw(g2d); | |
} | |
} | |
private class GameLoop implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
ball.update(); | |
paddle.update(); | |
checkCollision(); | |
repaint(); | |
} | |
} | |
private void checkCollision() { | |
Rectangle ballRect = ball.getBounds(); | |
Rectangle paddleRect = paddle.getBounds(); | |
if (ballRect.intersects(paddleRect)) { | |
ball.reverseY(); | |
} | |
for (Brick brick : bricks) { | |
if (brick.isVisible()) { | |
Rectangle brickRect = brick.getBounds(); | |
if (ballRect.intersects(brickRect)) { | |
ball.reverseY(); | |
brick.setVisible(false); | |
} | |
} | |
} | |
} | |
private class PaddleMovement extends KeyAdapter { | |
@Override | |
public void keyPressed(KeyEvent e) { | |
paddle.keyPressed(e); | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
paddle.keyReleased(e); | |
} | |
} | |
private class Ball { | |
private static final int SIZE = 20; | |
private static final int SPEED = 2; | |
private int x; | |
private int y; | |
private int dx; | |
private int dy; | |
public Ball() { | |
x = WIDTH / 2 - SIZE / 2; | |
y = HEIGHT / 2 - SIZE / 2; | |
dx = SPEED; | |
dy = -SPEED; | |
} | |
public void update() { | |
x += dx; | |
y += dy; | |
if (x <= 0 || x >= WIDTH - SIZE) { | |
reverseX(); | |
} | |
if (y <= 0 || y >= HEIGHT - SIZE) { | |
reverseY(); | |
} | |
} | |
public void draw(Graphics2D g2d) { | |
g2d.setColor(Color.RED); | |
g2d.fillOval(x, y, SIZE, SIZE); | |
} | |
public Rectangle getBounds() { | |
return new Rectangle(x, y, SIZE, SIZE); | |
} | |
public void reverseX() { | |
dx = -dx; | |
} | |
public void reverseY() { | |
dy = -dy; | |
} | |
} | |
private class Paddle { | |
private static final int WIDTH = 100; | |
private static final int HEIGHT = 20; | |
private static final int SPEED = 4; | |
private int x; | |
private int y; | |
private int dx; | |
public Paddle() { | |
x = WIDTH / 2 - WIDTH / 2; | |
y = HEIGHT + 50; | |
dx = 0; | |
} | |
public void update() { | |
x += dx; | |
if (x <= 0) { | |
x = 0; | |
} else if (x >= WIDTH - Paddle.WIDTH) { | |
x = WIDTH - Paddle.WIDTH; | |
} | |
} | |
public void draw(Graphics2D g2d) { | |
g2d.setColor(Color.BLUE); | |
g2d.fillRect(x, y, WIDTH, HEIGHT); | |
} | |
public Rectangle getBounds() { | |
return new Rectangle(x, y, WIDTH, HEIGHT); | |
} | |
public void keyPressed(KeyEvent e) { | |
int key = e.getKeyCode(); | |
if (key == KeyEvent.VK_LEFT) { | |
dx = -SPEED; | |
} else if (key == KeyEvent.VK_RIGHT) { | |
dx = SPEED; | |
} | |
} | |
public void keyReleased(KeyEvent e) { | |
int key = e.getKeyCode(); | |
if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT) { | |
dx = 0; | |
} | |
} | |
} | |
private class Brick { | |
private static final int WIDTH = 40; | |
private static final int HEIGHT = 20; | |
private int x; | |
private int y; | |
private boolean visible; | |
public Brick(int x, int y) { | |
this.x = x; | |
this.y = y; | |
visible = true; | |
} | |
public void draw(Graphics2D g2d) { | |
if (visible) { | |
g2d.setColor(Color.GREEN); | |
g2d.fillRect(x, y, WIDTH, HEIGHT); | |
} | |
} | |
public Rectangle getBounds() { | |
return new Rectangle(x, y, WIDTH, HEIGHT); | |
} | |
public boolean isVisible() { | |
return visible; | |
} | |
public void setVisible(boolean visible) { | |
this.visible = visible; | |
} | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(BreakoutGame::new); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment