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

Thread: Brick Breaker

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

    Default Brick Breaker

    Hey, I'm making a brick breaker game and I need some help.
    I need to know how to do the brick class..
    any help ?


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

    Default Re: Brick Breaker

    A starting point: define what data the brick needs to keep and what methods it needs to maintain that data.
    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

    I wrote this class.. can you give me more leads about what to do ?
    Bricks:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Brick {
    	private int x;
    	private int y;
    	protected Color color;
     
    	public Brick (int x, int y){
    		this.x=x;
    		this.y=y;
    	}
     
    	public void setColor (Color newColor)
    	{ color = newColor;	}
     
    	public void Paint (Graphics g)
    	{
    		g.fillRect(x, y, 35, 45);
    		g.setColor(color); 
     
    	}
     
    }

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

    Default Re: Brick Breaker

    What is a Brick object supposed to do? Does it need setter methods?
    Does it have a state?

    BTW Java naming standards says methods should start with lowercase letters.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    I need to paint the breaks in the top of the screen (4-5 lines of breaks).

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

    Default Re: Brick Breaker

    What problems are you having?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    jan4185 (February 7th, 2013)

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

    Default Re: Brick Breaker

    I cant find a way to do that and for some reason I cant change the color of the bricks..

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

    Default Re: Brick Breaker

    You'll have to post some code that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    I don't know what to do, I tried to paint it with matrix but I cant..
    look what I tried:
    I wrote this in the main page

    public void paintBricks(){
    		Graphics g;
    		int i=80,j=50,k,l;
    		Brick[][] brickArr = new Brick[20][3];
    		for(k=0;k<=20;k++){
    			for(l=0;l<=3;l++){
    				brickArr[k][l]=aBrick.Paint(g, i, j);
    			}
    		}
    	}

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

    Default Re: Brick Breaker

    Sorry, without a complete program I can't compile, execute and test the code to see what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    Ok , this is what I've done, I need to paint 4-5 lines of bricks in the top of the screen.
    My classes so far are:

    1. 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);
    	}
    }

    2. Matka:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
    public class matka extends Thread {
    	private int x;
    	private int y;
    	protected Color color;
     
    	public matka (int x, int y)
    	{
    		this.x=x;
    		this.y=y;
    	}
     
    	public void setColor (Color newColor)
    	{ color = newColor;	}
     
    	public void Paint (Graphics g)
    	{
    		//Rectangle r=new Rectangle();
    		g.fillRect(x, y, 200, 20);
    		g.setColor(color); 
     
    	}
    	public void moveL(int dx){
    		this.x+=dx;
     
    	}
    	public void moveR(int dx){
    		this.x-=dx;
     
    	}
     
    }

    3. BallWorld1 (main page):

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
     
    public class BallWorld1 extends Frame {
     
    	private Ball aBall;
    	private matka aMatka;
    	private Brick aBrick;
     
    	public static void main (String [ ] args)	{
    		BallWorld1 world = new BallWorld1 (Color.red);
    		world.show ();
    	}
     
     
    	private BallWorld1 (Color ballColor) {
     
    		// constructor for new ball world
    			// resize our frame
    		setSize (getHeight(),getWidth());
    		setTitle ("Ball World");
    		addMouseListener(new BallListener());
    		addKeyListener(new MatkaListener());
    		aMatka = new matka(570,680);
    		//aBrick = new Brick(80,50);
    			// initialize object data field
    		aBall = new Ball (10, 15, 5);
    		aMatka.setColor(ballColor);
    		aBall.setColor (ballColor);
    		aBall.setMotion (6.0, 10.0);
     
    		addWindowListener( 
    			new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
     
     
     
    	public void paint (Graphics g) {
    			//aBrick.Paint(g);
    			aMatka.Paint(g);
    			aBall.paint (g);
    			aBall.move();
    			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());
    	try { Thread.sleep(100); }
    			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)
    		 {
    			 switch (e.getKeyCode())
    			 {
    			 case KeyEvent.VK_RIGHT: aMatka.moveR(-10);
    				 break;
    			 case KeyEvent.VK_LEFT: aMatka.moveL(-10);
    				 break;
    			 }
    		 }
    		@Override
    		public void keyReleased(KeyEvent arg0) {
    			// TODO Auto-generated method stub
     
    		}
    		@Override
    		public void keyTyped(KeyEvent arg0) {
    			// TODO Auto-generated method stub
     
    		}
     
    	}
     
    }

    4. Brick

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Brick {
    	private int x;
    	private int y;
     
    	public Brick (int x, int y){
    		this.x=x;
    		this.y=y;
    	}
     
     
    	public void Paint (Graphics g, int dx,int dy)
    	{
    		this.x=dx;
    		this.y=dy;
    		g.fillRect(x, y, 50, 60);
    		g.setColor(Color.RED); 
     
    	}
     
    }

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

    Default Re: Brick Breaker

    I need to paint 4-5 lines of bricks in the top of the screen.
    Which class and method is supposed to do that?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    the class Brick and the method paint.

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

    Default Re: Brick Breaker

    I'm not sure how the Brick class would create instances of itself and put them at the top of the screen in rows.
    The paint() method defintely should NOT create the bricks. It should call each of them so they can paint themselves.

    Some method needs to create a list of brick objects that the paint() method can use for painting them. Where should that be done?

    This code is using the OLD style GUI: AWT not Swing. There will be some minor problems with that.

    The program should use a Timer for controlling the calls to repaint(). It should NOT use sleep() in the paint() method or call repaint() in the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    I need to paint them in the Ballworld1

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

    Default Re: Brick Breaker

    You can't paint them until they exist. Where will they be created and saved so they can be painted?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    I thought about a matrix [20][4] 4 rows 20 each row

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

    Default Re: Brick Breaker

    Ok, that could work. Usually its row,column: [4][20]
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Brick Breaker

    But how I suppost to do that ? Can you give me some leads ?
    And thank you for all your help

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

    Default Re: Brick Breaker

    Use nested loops to assign values to the two dim array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to remove a brick (Java)
    By usherlad in forum Object Oriented Programming
    Replies: 33
    Last Post: August 30th, 2012, 02:45 PM
  2. 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
  3. Destroy Brick
    By Ceasar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 10th, 2009, 04:36 AM