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 ?
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.
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 ?
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.
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); } }
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.
I need to paint the breaks in the top of the screen (4-5 lines of breaks).
What problems are you having?
If you don't understand my answer, don't ignore it, ask a question.
jan4185 (February 7th, 2013)
I cant find a way to do that and for some reason I cant change the color of the bricks..
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.
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); } } }
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.
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); } }
Which class and method is supposed to do that?I need to paint 4-5 lines of bricks in the top of the screen.
If you don't understand my answer, don't ignore it, ask a question.
the class Brick and the method paint.
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.
I need to paint them in the Ballworld1
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.
I thought about a matrix [20][4] 4 rows 20 each row
Ok, that could work. Usually its row,column: [4][20]
If you don't understand my answer, don't ignore it, ask a question.
But how I suppost to do that ? Can you give me some leads ?
And thank you for all your help
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.