Using the swing library in Java, I created the classic block breaker game. In the game the user controls a paddle using the arrow keys and attempts to break all of the blocks using a ball. The game is over when either all the blocks are broken, meaning the user has won, or if the user missed the ball, meaning the user has lost.
package Breaker;
import Breaker.Gameplay;
import javax.swing.*;
public class main {
public static void main(String[] args) {
// creates the window
JFrame obj = new JFrame();
//gameplay object
Gameplay gameplay = new Gameplay();
//setting properties of window
obj.setBounds(10,10,710,600); //size
obj.setTitle("Breaker");
obj.setResizable(false);
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gameplay);
}
}
package Breaker;
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 = 3;
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 Block map;
public Gameplay(){
map= new Block(3,7);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g){
// background
g.setColor(Color.BLUE);
g.fillRect(1,1,692,592);
//drawing blocks
map.draw((Graphics2D)g);
//borders
g.setColor(Color.MAGENTA);
g.fillRect(0,0,3,592);
g.fillRect(0,0,692,3);
g.fillRect(691,0,3,592);
//score
g.setColor(Color.red);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("Score: "+score, 590, 30);
//paddle
g.setColor(Color.red);
g.fillRect(playerX, 550, 100, 8);
//ball
g.setColor(Color.black);
g.fillOval(ballposX, ballposY, 20, 20);
//wining game
if(totalBricks < -17){
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("You beat the game! Score: " + score, 200, 300);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press Enter to Restart", 230, 350);
}
//game over
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, Score: " + score, 195, 300);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press Enter to Restart", 230, 350);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
//ball movement
if(play){
//collison with paddle
if(new Rectangle(ballposX, ballposY, 20,20).intersects(new Rectangle(playerX,550, 100, 8))){
ballYdir =-ballYdir;
}
// breaking blocks, collsion detection
i: 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){
int brickX = j*map.brickWidth + 80;
int brickY = i * map.brickHeight + 50;
int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight;
//rectangle around break
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);
totalBricks --;
score += 5;
//left and right intersect
if(ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width){
ballXdir =-ballXdir;
}else{
ballYdir =- ballYdir;
}
break i;
}
}
}
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX <0){
ballXdir =- ballXdir;
}
if(ballposY <0){
ballYdir =- ballYdir;
}
if(ballposX >670){
ballXdir =- ballXdir;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
//user input
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
//check if in bounds
if(playerX >=600){
playerX = 600;
}else{
moveRight();
}
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
if(playerX < 10){
playerX = 10;
}else{
moveLeft();
}
}
//restart game
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 Block(3, 7);
repaint();
}
}
}
private void moveLeft() {
play = true;
playerX -=20;
}
private void moveRight() {
play = true;
playerX+= 20;
}
@Override
public void keyReleased(KeyEvent e) {
}
}
public class Block {
public int[][] map;
public int brickWidth;
public int brickHeight;
public Block(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.GREEN);
g.fillRect(j*brickWidth + 80, i*brickHeight + 50, brickWidth, brickHeight);
g.setStroke(new BasicStroke(3));
g.setColor(Color.BLUE);
g.drawRect(j*brickWidth + 80, i*brickHeight + 50, brickWidth, brickHeight);
}
}
}
}
//breaking brick
public void setBrickValue(int value, int row, int col){
map[row][col] = value;
}
}