Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Breakout Game

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Breakout Game

    I am developing a breakout game for a university assignment, its in for monday haha so not much time, i am confused on how to make this 'simple' game as i prefer visual basic, i have produced coding that has a bat and a ball that shoots randomly.
    the preferences are:

    -ball disappears when not hit
    -ball changes direction when hit by bat
    -bat moves under mouse control
    -cannot play without money
    -ball sets off randomly each time
    -brick is destroyed when hit by ball

    -deployment of 5 bricks, destroyed when hit
    -5 goes for £1, displayed and updated as played

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    import javax.swing.*;
     
     
    public class BreakOut extends JFrame implements ActionListener,MouseListener, MouseMotionListener {
     
     private JPanel paper;
     private JButton btnInsert£, btnGo;
     private JTextField txtHowManyGoes;
     private int x, y, xChange, yChange, diameter, batX, batY, ballX, ballY;
     private int preX=0, preY=0 ;
     private int counter;
     private Boolean firstTime=true, brk1;
     private JLabel lblDisplay;
     private Timer timer1;
     private Random RandNum;
     
     
     public static void main(String[] args){
     
      BreakOut breakOut = new BreakOut();
      breakOut.setSize(500, 500);
      breakOut.setBackground(Color.green);
      breakOut.setVisible(true);
     
     }
     
     public BreakOut(){
      setLayout(new FlowLayout());
     
      btnInsert£ = new JButton("Insert £");
      txtHowManyGoes = new JTextField(2);
      add(btnInsert£); add(txtHowManyGoes);
     
      btnGo = new JButton("Go!..");
      btnGo.setEnabled(false);
      add(btnGo);
      btnInsert£.addActionListener(this);
      btnGo.addActionListener(this);
     
      lblDisplay = new JLabel("....X Y Coordinates......");
      paper = new JPanel();
      paper.setPreferredSize(new Dimension(400, 400));
      paper.setBackground(Color.green);
      add(paper);
      add(lblDisplay);
      addMouseListener(this);
      addMouseMotionListener(this);
     
      x =0; y =0; xChange = 2; yChange =7; diameter = 11;
      counter=0;
     
      timer1 =new Timer (50, this);
     
     }
     
     public void moveBall(){
      Graphics myPen =  paper.getGraphics();
     
      myPen.setColor(Color.GREEN);
      myPen.fillOval(x, y, diameter, diameter);
     
      x = x + xChange ;
      y = y + yChange ;
     
      if (x <= 0 || x >= paper.getWidth())
       xChange = -xChange;
     
      if (y <= 0 || y >= paper.getHeight()){
     
       timer1.stop();
       counter--;
       txtHowManyGoes.setText(counter+"");
     
      }
      myPen.setColor(Color.blue);
      myPen.fillOval(x, y, diameter, diameter);
      ballX =x; ballY = y;
     
      }
     
     
     public void actionPerformed(ActionEvent event){
      //moveBall();
      if (event.getSource()==btnInsert£){
       btnGo.setEnabled(true);
       counter = counter + 5;
       txtHowManyGoes.setText(counter +"");
      }
      if (event.getSource()==timer1){
       moveBall();
      }  
      if (event.getSource()==btnGo){
       if (counter >0){
        Random randNum;  randNum = new Random();
        x= randNum.nextInt((paper.getWidth()));
        y=0;
        timer1 = new Timer(10, this)
        ;timer1.start();
       }
       else
        btnGo.setEnabled(false);
      }
     
     } 
     
     public void drawBat(){
     
      int batY;
      Graphics myPen =  paper.getGraphics();
      //paint(myPen); // stops flickering!
      batY = paper.getHeight()-20;  // bottom of the screen!  
      myPen.clearRect(0, 0, paper.getWidth(),
      paper.getHeight());
     // myPen.fillRect(batX-60,batY , 60, 10);
      myPen.setColor(Color.BLACK);
      batX = batX - 70;//co-ordinates of the batY position
      batY = batY - 30;
      //batY = paper.getHeight()-40;
      myPen.fillRect(batX,batY , 70, 10); //adjusts bat size
      preX = batX; preY = batY;
     
     }
     private void checkCollision(){
     
    		Graphics myPen =  paper.getGraphics();
     
    		// ball hitting bat!
    		if ((ballY + ballX) >= 30 && ballY >= batX && ballY <= (batX + 60))
    		//if((ballX >= batX)&& (ballX <= batX +30 && (ballY >= batY )))	
    		yChange = -5;
    }
     
     public void mouseMoved(MouseEvent event){
     
     
         lblDisplay.setText("X is " +x + " and Y is " + y );
      batX = event.getX();
      //batY = event.getY();
      drawBat();
      checkCollision();
     
      batX = batX - 70;
      batY = batY - 40;
      //batY = paper.getHeight()-40;
      //getMousePosition();
     }
     
     public void drawBrks(){
    		Graphics myPen =  paper.getGraphics();
    		myPen.setColor(Color.BLUE);
    		if (brk1== true)
    		 myPen.fillRect(80, 10, 40, 10);
     
     }
     public void mouseClicked(MouseEvent e){    
     /* x=1;y=1;
      Graphics myPen =  paper.getGraphics();
      myPen.setColor(Color.GREEN);
      myPen.fillRect(0, 0, paper.getWidth(),
          paper.getHeight());
      timer1 = new Timer(10, this);
      timer1.start(); 
     }*/
     }
     
     
     
     
     public void mouseEntered(MouseEvent e){
     }
     public void mouseExited(MouseEvent e){
     }
     public void mouseReleased(MouseEvent e){
     }
     public void mousePressed(MouseEvent e){
     }
     
     public void mouseDragged(MouseEvent e){
     }
     
    }// end of BouncingBall


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Breakout Game

    So, what do you want us to do with it?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Breakout Game

    sorry i was not specific, i need help on the ball hitting the bat code what i have so far in the code is :

    PHP Code:
     private void checkCollision(){

            
    Graphics myPen =  paper.getGraphics();
                

            if ((
    ballY ballX) >= 30 && ballY >= batX && ballY <= (batX 60))

                
    yChange = -yChange

    this seems to work but it only allows me to hit it once then the game will not continue properly

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Breakout Game

    You should better flush it and keep it iterating everytime by applying specific function provided to you by given API.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Breakout Game

    How does the game "not continue properly"? Do you get an Exception? Does it freeze up? Something else? Have you stepped through the program with a debugger?

    PS- Mentioning your deadlines will make you seem impatient, which will actually decrease your chances of getting help here. There are hundreds of posts here, each with its own deadline and urgency.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Breakout Game

    Let me start fresh,
    below is my coding for a breakout game, i have become stuck on the code that allows the ball to hit the bat and go back up, the ball will hit where the mouse is, not on the bat this bit of coding is:
    PHP Code:
             private void checkCollision(){
                 
    Graphics myPen =  paper.getGraphics();
                 
    // ball hitting bat!
                 
    if ((ballY ballX) >= 30 && ballY >= batX && ballY <= (batX 60)){
                     
    yChange = -yChange;
                 }
             } 
    once this collision has taken place you press the button go to let another ball come down, the ball will stop at the start of the screen, if you continue to press the 'go' button it will stay at the top of the screen, the program is a single class program which is why i do not have a main class, critism and comments are all welcome, i am looking for help on the above coding and if anyone can find the problem to why the program will not continue sending more balls down.
    with the program it once it has hit the bat(mouse) it dissappears at the top of the screen wen it should bounce back not sure on this which is why i am looking for help from people who understand java a great deal more than i do
    thank you

    :

    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    import javax.swing.*;


    public class 
    BreakOut extends JFrame implements ActionListener,MouseListenerMouseMotionListener {

         private 
    JPanel paper;
         private 
    JButton btnInsert£btnGo;
         private 
    JTextField txtHowManyGoes;
         private 
    int xyxChangeyChangediameterbatXbatYballXballY;
         private 
    int preX=0preY=;
         private 
    int counter;
         private 
    Boolean firstTime=truebrk1;
         private 
    JLabel lblDisplay;
         private 
    Timer timer1;
         private 
    Random RandNum;

      
         public static 
    void main(String[] args){
          
          
    BreakOut breakOut = new BreakOut();
          
    breakOut.setSize(500500);
          
    breakOut.setBackground(Color.green);
          
    breakOut.setVisible(true);

     }
     
             public 
    BreakOut(){
                 
    setLayout(new FlowLayout());  
                 
    btnInsert£ = new JButton("Insert £");
                 
    txtHowManyGoes = new JTextField(2);
                 
    add(btnInsert£); add(txtHowManyGoes);
                 
    btnGo = new JButton("Go!..");
                 
    btnGo.setEnabled(false);
                 
    add(btnGo);
                 
    btnInsert£.addActionListener(this);
                 
    btnGo.addActionListener(this); 
                 
    lblDisplay = new JLabel("....X Y Coordinates......");
                 
    paper = new JPanel();
                 
    paper.setPreferredSize(new Dimension(400400));
                 
    paper.setBackground(Color.green);
                 
    add(paper);
                 
    add(lblDisplay);
                 
    addMouseListener(this);
                 
    addMouseMotionListener(this);  
                 
    =0=0xChange 2yChange =7diameter 11;
                 
    counter=0;
                 
    timer1 =new Timer (50this);
             }
         
      
             public 
    void moveBall(){
                 
    Graphics myPen =  paper.getGraphics();  
                 
    myPen.setColor(Color.GREEN);
                 
    myPen.fillOval(xydiameterdiameter);
                 
    xChange ;
                 
    yChange ;
                 if (
    <= || >= paper.getWidth()){
                     
    xChange = -xChange;  
                 }
                 if (
    <= || >= paper.getHeight()){
                     
    timer1.stop();
                     
    counter--;
                     
    txtHowManyGoes.setText(counter+"");  
                 }
                 
    myPen.setColor(Color.blue);
                 
    myPen.fillOval(xydiameterdiameter);
                 
    ballX =xballY y;
             }

             public 
    void actionPerformed(ActionEvent event){
                 
    //moveBall();
                 
    if (event.getSource()==btnInsert£){
                     
    btnGo.setEnabled(true);
                     
    counter counter 5;
                     
    txtHowManyGoes.setText(counter +"");
                 }
                 if (
    event.getSource()==timer1){
                     
    moveBall();
                 }
                 if (
    event.getSource()==btnGo){
                     if (
    counter >0){
                         
    Random randNum;  randNum = new Random();
                         
    xrandNum.nextInt((paper.getWidth()));
                         
    y=0;
                         
    timer1 = new Timer(10this);
                         
    timer1.start();
                     }
                     else 
    btnGo.setEnabled(false);
                 }
             }
     
             public 
    void drawBat(){
                 
    int batY;
                 
    Graphics myPen =  paper.getGraphics();
                 
    //paint(myPen); // stops flickering!
                 
    batY paper.getHeight()-20;  // bottom of the screen!
                 
    myPen.clearRect(00paper.getWidth(),paper.getHeight());
                 
    // myPen.fillRect(batX-60,batY , 60, 10);
                 
    myPen.setColor(Color.BLACK);
                 
    batX batX 70batY batY 30;//Co-ordinates of the batY position
                 //batY = paper.getHeight()-40;
                 
    myPen.fillRect(batX,batY 7010); //adjusts bat size
                 
    preX batXpreY batY;
             }

             private 
    void checkCollision(){
                 
    Graphics myPen =  paper.getGraphics();
                 
    // ball hitting bat!
                 
    if ((ballY ballX) >= 30 && ballY >= batX && ballY <= (batX 60)){
                     
    //if((ballX >= batX)&& (ballX <= batX +30 && (ballY >= batY )))
                     //yChange = -5;
                     
    yChange = -yChange;
                 }
             }
      
             public 
    void mouseMoved(MouseEvent event){
                 
    lblDisplay.setText("X is " +" and Y is " );
                 
    batX event.getX();
                 
    //batY = event.getY();
                 
    drawBat();
                 
    checkCollision();
                 
    batX batX 70;
                 
    batY batY 40;
                 
    //batY = paper.getHeight()-40;
                 //getMousePosition();
             
    }
     
             public 
    void drawBrks(){
                 
    Graphics myPen =  paper.getGraphics();
                 
    myPen.setColor(Color.BLUE);
                 if (
    brk1== true){
                     
    myPen.fillRect(80104010);
                 }
             }
             
            public 
    void mouseClicked(MouseEvent e){
            }

            public 
    void mouseEntered(MouseEvent e){
            }

            public 
    void mouseExited(MouseEvent e){
            }

            public 
    void mouseReleased(MouseEvent e){
            }

            public 
    void mousePressed(MouseEvent e){
            }
            
            public 
    void mouseDragged(MouseEvent e){
            }

    }
    // end of BouncingBall 

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Breakout Game

    Well here's your first problem- you should never use getGraphics() on a component. Extend the JPanel and override paintComponent() instead.

    Recommended reading: Painting in AWT and Swing
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Breakout Game

    if ((ballY + ballX) >= 30 && ballY >= batX && ballY <= (batX + 60)){
    yChange = -yChange;
    this code is the problem i need it so... when the bat on x axis, and the ball comes to wher the bat is, the ball will change direction,


    p.s. does anyone know a piece of coding that allows the mouse curser to stay in one point i have:
    batX = batX - 70;
    which makes it stay in line at the y axis of the bat but using the same code with 'x' does not work

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Breakout Game

    That line might be one of your problems, but your painting is definitely also incorrect. That could possibly lead to a bunch of other problems (or cover up problems that are already there), so it's the first thing you should fix.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Game !
    By Ahmed.Ibrahem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 08:01 PM
  2. Breakout Game- program help
    By strength.honor in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2010, 02:44 PM
  3. breakout paddle algorithm
    By Brain_Child in forum Algorithms & Recursion
    Replies: 0
    Last Post: December 30th, 2009, 05:24 AM
  4. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  5. Breakout Game
    By Ceasar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2009, 12:30 AM