Brick Breaker!

This commit is contained in:
2021-11-22 20:10:26 -05:00
commit a9f1bb3f6c
15 changed files with 825 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package tech.nevets.brickbreaker;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame();
Gameplay play = new Gameplay();
frame.setBounds(10, 10, 700, 600);
frame.setTitle("Brick Breaker");
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(play);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,212 @@
package tech.nevets.brickbreaker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Gameplay extends JPanel implements KeyListener, ActionListener {
private boolean play = false;
private int score = 0;
private int totalBricks = 48;
private Timer timer;
private int delay = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private MapGenerator map;
public Gameplay() {
map = new MapGenerator(4, 12);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g) {
//Background
g.setColor(Color.BLACK);
g.fillRect(1, 1, 692, 592);
//Draw Map
map.draw((Graphics2D) g);
//Borders
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 592);
//Scores
g.setColor(Color.WHITE);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("" + score, 590,30);
//Paddle
g.setColor(Color.GREEN);
g.fillRect(playerX, 550, 100, 8);
//Ball
g.setColor(Color.YELLOW);
g.fillOval(ballposX, ballposY, 20, 20);
//Win Condition
if (totalBricks <= 0) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("You Won!", 260, 300);
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press (Enter) to Restart", 230, 350);
}
//Loss Condition
if (ballposY > 570) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("Game Over, Scores: " + score, 190, 300);
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press (Enter) to Restart", 230, 350);
}
g.dispose();
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) {
playerX = 600;
} else {
moveRight();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if(playerX < 10) {
playerX = 10;
} else {
moveLeft();
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!play) {
play = true;
ballposX = 120;
ballposY = 350;
ballXdir = -1;
ballYdir = -2;
playerX = 310;
score = 0;
totalBricks = 21;
map = new MapGenerator(3, 7);
repaint();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
//Do nothing
}
@Override
public void keyTyped(KeyEvent e) {
//Do nothing
}
public void moveRight() {
play = true;
playerX += 20;
}
public void moveLeft() {
play = true;
playerX -= 20;
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
if (play) {
if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550 , 30, 8))) {
ballYdir = -ballYdir;
ballXdir = -2;
} else if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX + 70, 550, 30, 8))) {
ballYdir = -ballYdir;
ballXdir = ballXdir + 1;
} else if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX + 30, 550, 40, 8))) {
ballYdir = -ballYdir;
}
A: for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j < map.map[0].length; j++) {
if (map.map[i][j] > 0) {
//Add to score on brick break
int brickX = j * map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j);
score +=5;
totalBricks--;
//Bounce mechanics
if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
ballXdir = -ballXdir;
} else {
ballYdir = -ballYdir;
}
break A;
}
}
}
}
ballposX += ballXdir;
ballposY += ballYdir;
if (ballposX < 0) {
ballXdir = -ballXdir;
}
if(ballposY < 0) {
ballYdir = -ballYdir;
}
if(ballposX > 670) {
ballXdir = -ballXdir;
}
repaint();
}
}
}

View File

@@ -0,0 +1,42 @@
package tech.nevets.brickbreaker;
import java.awt.*;
public class MapGenerator {
public int map[][];
public int brickWidth;
public int brickHeight;
public MapGenerator(int row, int col) {
map = new int[row][col];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
map[i][j] = 1;
}
}
brickWidth = 540 / col;
brickHeight = 150 / row;
}
public void draw(Graphics2D g) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (map[i][j] > 0) {
g.setColor(Color.WHITE);
g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
//Show separate bricks
g.setStroke(new BasicStroke(3));
g.setColor(Color.BLACK);
g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
}
}
}
}
public void setBrickValue(int value, int row, int col) {
map[row][col] = value;
}
}