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 } } }