Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

Brick Breaker Game in Java

 Brick Breaker Game in Java

In this blog tutorial, we will create a simple game clone using  Java 2D and event handling.

In this game, we have one paddle, one ball, and 18 bricks.  I have used a timer to create a game cycle, and simply changing directions Top, bottom, left, and right we will be able to create a clone.

The objective of the ball is to destroy all bricks.  After destroying each brick score of 5 is given and if destroy all balls your score will be printed along with the greeting message.  If you fail to do so lost message will be printed and you can restart the game.



The game consists of 3 files: Main.javaBricks.javaGameplay.java

//Main.java

import javax.swing.JFrame;

 

public class Main {

    public static void main(String[] args) {

       JFrame f=new JFrame();

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

       f.setResizable(false);

       f.setTitle("Brick Breaker Game!");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       Gameplay gp=new Gameplay();

       f.add(gp);

       f.setVisible(true);

      

    }

}

//Bricks.java

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics2D;

 

public class Bricks {

    int brick[][];

    int width,height;

   

    public Bricks(int r,int c) {

       brick=new int[r][c];

       for(int i=0;i<r;i++)

       {

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

               brick[i][j]=1;

           }

       }

       width=540/c;

       height=150/r;

    }

    public void draw(Graphics2D g)

    {

       for(int i=0;i<brick.length;i++)

       {

           for(int j=0;j<brick[0].length;j++) {

               if(brick[i][j]>0)

               {

                 

                  g.setStroke(new BasicStroke(4));

                  g.setColor(Color.red);                 

                  g.drawRect(j*width+80, i*height+50, width, height);

                 

                  g.setColor(Color.white);

                  g.fillRect(j*width+80, i*height+50, width, height);

               }

           }

       }

   

    }

    public void setBrick(int v,int r,int c) {

       brick[r][c]=v;

    }

}

//Gameplay.java

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{

    int ballX=120;

    int ballY=350;

    int balldirX=-1;

    int balldirY=-2;         

    int score=0;

    Timer timer;

    int delay=10;

    int paddle=295;

    boolean play;

    Bricks br;

    int total=21;

    public Gameplay() {

       addKeyListener(this);

       setFocusable(true);

       setFocusTraversalKeysEnabled(false);

       timer=new Timer(delay, this);

       timer.start();

       //create bricks

       br=new Bricks(3,7);

      

    }

    public void paint(Graphics g) {

      

       //background

       g.setColor(Color.BLACK);

       g.fillRect(1, 1, 692, 592);

      

       //borders

       g.setColor(Color.red);

       g.fillRect(0, 0, 692, 3);//top border

       g.fillRect(0, 0,3, 592);//left border

       g.fillRect(681, 0,3, 581);//right border

      

       //paddle

       g.setColor(Color.yellow);

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

      

       //ball

       g.setColor(Color.green);

       g.fillOval(ballX, ballY, 20, 20);

      

       //bricks

       br.draw((Graphics2D)g);

      

       //score

       g.setColor(Color.yellow);

       g.setFont(new Font("Arial",Font.BOLD,20));

       g.drawString("Your score is:"+score, 500, 30);

      

       //gameover

       if( total<=0) {

           play=false;

           balldirX=0;

           balldirY=0;

           g.setColor(Color.red);

           g.setFont(new Font("Arial",Font.BOLD,30));   

           g.drawString("You WON!! Your Score is "+score, 150, 350);

           g.setFont(new Font("Arial",Font.BOLD,20));   

           g.drawString("Press ENTER to restart", 230, 400);

       }

       if(ballY>=550 ) {

           play=false;

           balldirX=0;

           balldirY=0;

           g.setColor(Color.red);

           g.setFont(new Font("Arial",Font.BOLD,30));   

           g.drawString("GAME OVER!! Your Score is "+score, 150, 350);

           g.setFont(new Font("Arial",Font.BOLD,20));   

           g.drawString("Press ENTER to restart", 230, 400);

       }

    }

    @Override

    public void actionPerformed(ActionEvent e) {

       if(play)

       {

           if(ballX<=0)

               balldirX=-balldirX;

           if(ballX>=681)

               balldirX=-balldirX;

           if(ballY<=0)

               balldirY=-balldirY;

           Rectangle ballRect=new Rectangle(ballX,ballY,20,20);

           Rectangle paddleRect=new Rectangle(paddle,550,100,8);

          

           if(ballRect.intersects(paddleRect)) {

               balldirY=-balldirY;

              

           }

          

           A:

               for(int i=0;i<br.brick.length;i++) {

               for(int j=0;j<br.brick[0].length;j++) {

                  if(br.brick[i][j]>0) {

                      int w=br.width;

                      int h=br.height;

                      int xPos=80+j*w;

                      int yPos=50+i*h;

                      Rectangle brickRect=new Rectangle(xPos,yPos,w,h);

                      if(ballRect.intersects(brickRect)) {

                          br.setBrick(0,i,j);

                          total--;

                          score+=5;

                          if(ballX<=xPos || ballX+1>xPos+w)//left & right

                              balldirX=-balldirX;

                          else

                              balldirY=-balldirY;

                          break A;

                      }

                  }

                     

               }

           }

              

           ballX+=-balldirX;

           ballY+=-balldirY;

       }

       repaint();

    }

   

    @Override

    public void keyPressed(KeyEvent e) {

       int code=e.getKeyCode();

       if(code==KeyEvent.VK_RIGHT)

       {

           if(paddle>=580)

               paddle=580;

           else

           {

               play=true;

               paddle+=20;

           }

      

           repaint();

       }

       if(code==KeyEvent.VK_LEFT)

       {

           if(paddle<=10)

               paddle=10;

           else

           {

               play=true;

               paddle-=20;

           }

           repaint();

      

       }

       if(code==KeyEvent.VK_ENTER)

       {

           ballX=120;

           ballY=350;

           balldirX=-1;

           balldirY=-2;         

           score=0;

           paddle=295;

           total=21;

           br=new Bricks(3,7);

       }

      

    }

    @Override

    public void keyTyped(KeyEvent e) {

    }

    @Override

    public void keyReleased(KeyEvent e) {  

    }

}

 

Output:




 

 

Brick Breaker Game in Java  Brick Breaker Game in Java Reviewed by Asst. Prof. Sunita Rai on July 03, 2022 Rating: 5

No comments:

Powered by Blogger.