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 ?
Printable View
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.
I wrote this class.. can you give me more leads about what to do ?
Bricks:
Code :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.
I need to paint the breaks in the top of the screen (4-5 lines of breaks).
What problems are you having?
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.
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
Code :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.
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:
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); } }
2. Matka:
Code :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):
Code :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
Code :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?Quote:
I need to paint 4-5 lines of bricks in the top of the screen.
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.
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?
I thought about a matrix [20][4] 4 rows 20 each row
Ok, that could work. Usually its row,column: [4][20]
But how I suppost to do that ? Can you give me some leads ?
And thank you for all your help :D
Use nested loops to assign values to the two dim array.