JAVA---------------Brick-breaker

1.Class - MapGenerator --


package Brick_breaker;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics2D;


public class MapGenerator {

public int[][] map;

public int brickwid;

public int brickhei;

public MapGenerator(int row , int col) {

map = new int[row][col];

for(int i = 0 ; i < row ; i++) {

for(int j = 0 ; j < col ; j++) {

map[i][j] = 1;

}

}

brickwid = 540/col;

brickhei = 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*brickwid + 80, i*brickhei + 50, brickwid, brickhei);

g.setStroke(new BasicStroke(3));

g.setColor(Color.black);

g.drawRect(j*brickwid + 80, i*brickhei + 50, brickwid, brickhei);

}

}

}

}

public void setBrickvalue(int value , int row , int col) {

map[row][col] = value;

}

}


2.Class - GamePlay -- 


package Brick_breaker;


import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;


import javax.swing.JPanel;

import javax.swing.Timer;


public class GamePlay extends JPanel implements KeyListener, ActionListener{

private boolean play = false;

private int score = 0;

private int totalbricks = 21;

private Timer timer;

private int delay = 0;

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(3 ,7);

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);

//drawing map

map.draw((Graphics2D) g);

//borders

g.setColor(Color.yellow);

g.fillRect(0, 0, 3, 592);

g.fillRect(0, 0, 692, 3);

g.fillRect(692, 0, 3, 592);

//scores

g.setColor(Color.white);

g.setFont(new Font("serif" , Font.BOLD , 25));

g.drawString(""+score , 592 , 30);

//paddle

g.setColor(Color.green);

g.fillRect(playerx, 550, 100, 8);

//ball

g.setColor(Color.yellow);

g.fillOval(ballposx, ballposy, 20, 20);

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.setFont(new Font("serif" , Font.BOLD , 20));

g.drawString("Press Enter to restart" , 230 , 350);

}

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: " , 190 , 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();

if(play) {

if(new Rectangle(ballposx , ballposy , 20 , 20).intersects(new Rectangle(playerx , 550 , 100 , 8))) {

ballydir = -ballydir;

}

x: 

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.brickwid + 80;

int bricky = i*map.brickhei + 50;

int brickwid = map.brickwid;

int brickhei = map.brickhei;

Rectangle rect = new Rectangle(brickx, bricky , brickwid , brickhei);

Rectangle ballrect = new Rectangle(ballposx , ballposy , 20 , 20);

Rectangle brickrect = rect;

if(ballrect.intersects(brickrect)) {

map.setBrickvalue(0 ,  i ,  j);

totalbricks--;

score += 5;

if(ballposx + 19 <= brickrect.x || ballposx + 1 >= brickrect.x + brickrect.width) {

ballxdir = -ballxdir;

}

else

ballydir = -ballydir;

break x;

}

}

}

}

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) {}

public void keyReleased(KeyEvent e) {}


@Override

public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_ENTER) {

if(!play) {

play = true;

playerx = 310;

ballposx = 120;

ballposy = 350;

ballxdir = -1;

ballydir = -2;

score = 0;

totalbricks = 21;

map = new MapGenerator(3,7);

repaint();

}

}

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();

}

}

public void moveleft() {

play = true;

playerx -= 20;

}

public void moveright() {

play = true;

playerx += 20;

}

}


3.Class - Main --

package Brick_breaker;


import javax.swing.JFrame;


public class Main {


public static void main(String[] args) {

JFrame ob = new JFrame();

GamePlay gamePlay = new GamePlay();

ob.setBounds(10, 10, 700, 600);

ob.setResizable(false);

ob.setVisible(true);

ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ob.add(gamePlay);

}

}

Comments