# Brick Breaker HELP!!!!

• February 9th, 2013, 01:58 PM
jan4185
Brick Breaker HELP!!!!
Hi, I am building now a Brick Breaker game and for some reason the bricks are not disapear.. moreover, the matka is not reacte to the ball..
my classes:

BALL:
Code :

```// // general purpose reusable bouncing ball abstraction // Described in Chapter 4 of // Understanding Object-Oriented Programming with Java // by Timothy A Budd // Published by Addison-Wesley // // see ftp://ftp.cs.orst.edu/pub/budd/java/ReadMe.html // for further information //   import java.awt.*;   public class Ball { protected Rectangle location; protected double dx; protected double dy; protected Color color;   public Ball (int x, int y, int r) { location = new Rectangle(x-r, y-r, 2*r, 2*r); dx = 0; dy = 0; color = Color.blue; }   // functions that set attributes public void setColor (Color newColor) { color = newColor; }   public void setMotion (double ndx, double ndy) { dx = ndx; dy = ndy; }   // functions that access attributes of ball public int radius () { return location.width / 2; }   public int x () { return location.x + radius(); }   public int y () { return location.y + radius(); }   public double xMotion () { return dx; }   public double yMotion () { return dy; }   public Rectangle region () { return location; }   // functions that change attributes of ball public void moveTo (int x, int y) { location.setLocation(x, y); }   public void move () { location.translate ((int) dx, (int) dy); }   public void paint (Graphics g) { g.setColor (color); g.fillOval (location.x, location.y, location.width, location.height); }   public void setx(int i) { // TODO Auto-generated method stub   }   public void sety(int i) { // TODO Auto-generated method stub   } }```

MATKA:
Code :

```import java.awt.*;   public class matka { private int x; private int y;   public matka (int x, int y) { this.x=x; this.y=y; } public void Paint (Graphics g) { g.fillRect(x, y, 200, 20); } public void moveL(int dx){ this.x-=dx;   } public void moveR(int dx){ this.x+=dx;   } public int x () { return this.x; }   public int y () { return this.y; }   }```

BRICK:
Code :

```import java.awt.Graphics;     public class Brick { private int x; private int y; private boolean ifHit; public Brick(int x, int y) { this.x=x; this.y=y; this.ifHit = false; } public void Paint (Graphics g) { g.fillRect(x, y, 80, 30); } public int x () { return this.x; }   public int y () { return this.y; }   public boolean IfHit() {return this.ifHit;}   public void setIfHit(boolean ifHit) { this.ifHit = ifHit; }         }```

BALLWORLD1 (MAIN):
Code :

```import java.awt.*; import java.util.Vector; import java.awt.event.*;   public class BallWorld1 extends Frame {   private Ball aBall; private matka aMatka; private Vector<Brick> bricks; private int indicat=0;     public static void main (String [ ] args) { BallWorld1 world = new BallWorld1 (Color.red); world.show(); }     public BallWorld1 (Color ballColor) { // constructor for new ball world // resize our frame setSize (getHeight(),getWidth()); setTitle ("shover"); addMouseListener(new BallListener()); addKeyListener(new MatkaListener()); aMatka = new matka(570,700 ); // initialize object data field   aBall = new Ball (aMatka.x()+100, aMatka.y()-5, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, -6.0); int bx=100,by=50,counter=0; bricks = new Vector<Brick>(); for(int i = 0;i<40;i++) { if(counter%10==0) { by+=50; bx=300; } bricks.add(new Brick(bx,by)); bx+=90; counter++; } counter=0; addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }     public void paint (Graphics g) { g.clearRect(0, 0, getWidth(), getHeight()); aMatka.Paint(g); aBall.paint (g); if(indicat >0) aBall.move(); if(indicat==0){ aBall.setx(aMatka.x()+100); aBall.sety(aMatka.y()-5); } for(int i=0;i<40;i++) { if(bricks.elementAt(i).IfHit()==false) bricks.elementAt(i).Paint(g); } if ((aBall.x() < 0) || (aBall.x() > getSize().width)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() < 0) || (aBall.y() > getSize().height)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); if ((aBall.x() >= aMatka.x())&& (aBall.x() <= (aMatka.x())+200 ) && (aBall.y()==aMatka.y())) { aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); if (((aBall.x() >= aMatka.x()) && (aBall.x() <= aMatka.x()+70) && (aBall.y() == aMatka.y()) && (aBall.dx>0))) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); else if (((aBall.x() <= aMatka.x()+200) && (aBall.x() >= aMatka.x()+130) && (aBall.y() == aMatka.y() && (aBall.dx<0)))) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); }   for(int i=0;i<40 && bricks.elementAt(i).IfHit() == false;i++) {     if((aBall.x() >=bricks.elementAt(i).x()) && (aBall.x() <= bricks.elementAt(i).x()+80) && (aBall.y() >=bricks.elementAt(i).y())&&(aBall.y() <=bricks.elementAt(i).y()+5)) { bricks.elementAt(i).setIfHit(true); aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); }   if((aBall.x() >=bricks.elementAt(i).x()) && (aBall.x() <= bricks.elementAt(i).x()+80) && (aBall.y() <=bricks.elementAt(i).y()+30)&&(aBall.y() >=bricks.elementAt(i).y()+25)) { bricks.elementAt(i).setIfHit(true); aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); }   if((aBall.x() ==bricks.elementAt(i).x()) && (aBall.y() >bricks.elementAt(i).y())&&(aBall.y() <bricks.elementAt(i).y()+30)) { bricks.elementAt(i).setIfHit(true); aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); }   if( (aBall.x() == bricks.elementAt(i).x()+80) && (aBall.y() >bricks.elementAt(i).y())&&(aBall.y() <bricks.elementAt(i).y()+30)) { bricks.elementAt(i).setIfHit(true); aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); }   }   try { Thread.sleep(35); } catch (InterruptedException e) {} repaint(); }   class BallListener extends MouseAdapter { public void mousePressed(MouseEvent e) { aBall.moveTo(e.getX(), e.getY()); } }   class MatkaListener implements KeyListener {   public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch( keyCode ) { case KeyEvent.VK_LEFT: if (aMatka.x() > 25) aMatka.moveL(30); break; case KeyEvent.VK_RIGHT : if (aMatka.x() < getWidth()-230) aMatka.moveR(30); break; case KeyEvent.VK_UP: indicat=1; break; } repaint(); } public void KeyTyped(KeyEvent e){   } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub   } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub   }   }   }```
• February 9th, 2013, 02:08 PM
Norm
Re: Brick Breaker HELP!!!!
Time to do some debugging by adding some println() statements to display the ball's position and the postions of bricks to see why the collisions are not being detected.

NOTE: The code has too many magic numbers: 300, 100, 40, 200, 30, 170, 80, 90
Most of these should be defined as variables with descriptive names to allow easy changing of the game to run on PCs with different size screens and to make the code more readable and easier to understand.
• February 9th, 2013, 02:18 PM
jan4185
Re: Brick Breaker HELP!!!!
I can't find the problem.. I checked it many times. Please be my saver.. I need to show that to my teacher tomorrow.
• February 9th, 2013, 03:22 PM
Norm
Re: Brick Breaker HELP!!!!
NOTE: The code has too many magic numbers: 300, 100, 40, 200, 30, 170, 80, 90, ...
Most of these should be defined as variables with descriptive names to allow easy changing of the game to run on PCs with different size screens and to make the code more readable and easier to understand.

For testing make a smaller, simpler starting position. Say 2 rows of 3 bricks in a 200x200 window.
That will make for fewer things to keep track of.

If the code was written with variables as described in the above NOTE, then this change would be very easily done by changing a few of the variables' values.

Some ideas to help debugging:
Add a toString() method to the Ball class and the matka class that returns the x and y values and the dx,dy values. That makes for easier printing.

Instead of printing all the values for the ball and matka as they move around, only print them when they are close:
Code :

``` // Print values when ball close to matka if(Math.abs(aBall.y() - aMatka.y()) < 8) { //<<<<<<<<<<<< System.out.println("ball:"+aBall + ", matka="+aMatka); }```

Print out the value of matka whenever a collision is detected.
Print out the value of the ball whenever its moved by the mousePress.