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 4 of 4

Thread: Brick Breaker HELP!!!!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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..
    please help ...
    my classes:

    BALL:
    //
    //	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:
    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:
    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):
    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
     
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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:
              //  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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Brick Breaker
    By jan4185 in forum Object Oriented Programming
    Replies: 19
    Last Post: February 8th, 2013, 02:32 PM
  2. How to remove a brick (Java)
    By usherlad in forum Object Oriented Programming
    Replies: 33
    Last Post: August 30th, 2012, 02:45 PM
  3. Make brick wall using BlueJ
    By boumasmoud in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 3rd, 2011, 04:32 PM
  4. Destroy Brick
    By Ceasar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 10th, 2009, 04:36 AM