Brick Breaker-Develop a game in java.
I have used IDE Eclipse.
This is how the game looks.
1. we have a slider that can move left and right using left and right arrows.
2. we have a red color ball
3. we have a matrix of black bricks, when the ball hits the brick , the brick disappears and the score increases by 5.
4. when the ball hits the slider ,it bounces back towards the bricks(upward).
5. Press ENTER to start a new game.
Source Code:
MAIN CLASS
public class Main {
public static void main(String args[]) {
JFrame obj=new JFrame();
GamePlay gamePlay=new GamePlay();
obj.setBounds(10,10,700,600);
obj.setTitle("BreakOut Ball");
obj.setResizable(false);
obj.setVisible(true);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(gamePlay);
}
}
GAMEPLAY CLASS
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=8;
private int playerX=310;// starting position of the sloder
private int ballposX=120;//starting ball position
private int ballposY=350;
private int ballXdir=-1;//direction of the ball
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.pink);
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(691,0,3,592);
//scores
g.setColor(Color.blue);
g.setFont(new Font("serif",Font.BOLD,20));
g.drawString("Yor Score :"+score,500,30);
//the paddle
g.setColor(Color.black);
g.fillRect(playerX,550,100,10);
//the ball
g.setColor(Color.RED);
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("WELL PLAYED! YOU WON! YAY",150,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("WELL PLAYED! GAME OVER ",100,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 the player is true then check the borders and directions
if(new Rectangle(ballposX,ballposY,20,20).intersects(new Rectangle(playerX,550,100,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) {
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);
totalBricks--;
score+=5;
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();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {//right arrow moves the slider to right
if(playerX >=600) {
playerX=600;
}
else {
moveRight();
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT) {//left arrow moves the slider to left
if(playerX < 10) {
playerX=10;
}
else {
moveLeft();
}
}
if(e.getKeyCode()==KeyEvent.VK_ENTER) {// if we press 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();
}
}
}
public void moveRight() {
play=true;
playerX+=20;
}
public void moveLeft() {
play=true;
playerX-=20;
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
MAP GENERATOR CLASS
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.black);
g.fillRect(j*brickWidth+80,i*brickHeight+50,brickWidth,brickHeight);
g.setStroke(new BasicStroke(3));
g.setColor(Color.white);
g.drawRect(j*brickWidth+80,i*brickHeight+50,brickWidth,brickHeight);
}
}
}
}
public void setBrickValue(int value,int row,int col) {
map[row][col]=value;
}
}
please give me the source code mam
ReplyDelete