# bricks wierd problem

• August 6th, 2011, 06:38 AM
bardd
bricks wierd problem
I am programming the game bricks, and i have a wierd problem: the ball only hits bricks above a curtain line.
ball code:
Code :

```import java.awt.Graphics;   public class Ball { private int r; private int centerX; private int centerY; private boolean isLeft; private boolean isUp;   public Ball(int r, int centerX, int centerY) { this.r = r; this.centerX = centerX; this.centerY = centerY; isLeft = false; isUp = true; }   public void check(Brick[][] b) { for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[i][j] != null) { for (int k = 0; k <= b[i][j].getWidth(); k++) { if (contains(b[i][j].getX() + k, b[i][j].getY()) || contains(b[i][j].getX() + k, b[i][j].getY() - b[i][j].getHeight())) { b[i][j] = null; isUp = !isUp; return; } } for (int k = 0; k <= b[i][j].getHeight(); k++) { if (contains(b[i][j].getX(), b[i][j].getY() - k) || contains( b[i][j].getX() + b[i][j].getWidth(), b[i][j].getY() - k)) { b[i][j] = null; isLeft = !isLeft; return; } } } } } }   public void draw(Graphics g) { g.fillOval(centerX - r, centerY - r, 2 * r, 2 * r); }   public int getR() { return r; }   public void setR(int r) { this.r = r; }   public int getCenterX() { return centerX; }   public void setCenterX(int centerX) { this.centerX = centerX; }   public int getCenterY() { return centerY; }   public void setCenterY(int centerY) { this.centerY = centerY; }   public boolean isLeft() { return isLeft; }   public void setLeft(boolean isLeft) { this.isLeft = isLeft; }   public boolean isUp() { return isUp; }   public void setUp(boolean isUp) { this.isUp = isUp; }   public void move() { if (isLeft) centerX += 5; else centerX -= 5; if (isUp) centerY -= 5; else centerY += 5; }   public boolean contains(int x, int y) { double dis = Math.sqrt(Math.pow((centerX - x), 2) + Math.pow((centerY - y), 2)); if (dis <= r) return true; return false; }   public void move(int x, int y) { centerX = x; centerY = y; } }```

brick code:
Code :

```import java.awt.Graphics; public class Brick { private int x; private int y; private int width; private int height;   public Brick(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height;   }   public int getX() { return x; }   public void setX(int x) { this.x = x; }   public int getY() { return y; }   public void setY(int y) { this.y = y; }   public int getWidth() { return width; }   public void setWidth(int width) { this.width = width; }   public int getHeight() { return height; }   public void setHeight(int height) { this.height = height; }   public void draw(Graphics g) { g.fillRect(x, y, width, height); }   public boolean contains(int x, int y) { if (x >= this.x && x <= this.x + width && y >= this.y && y <= this.x + height) return true; return false; }```

main code:
Code :

```package bricks;   import java.awt.Graphics; import java.util.Random; import javax.swing.*;   public class bounce extends JPanel { private Ball ball; private Brick[][] bricks; private int width; private int height; public bounce() { width=900; height=700; Random r = new Random(); ball = new Ball(10, 400, 500); bricks = new Brick[5][8]; for (int i = 0; i < bricks.length; i++) { for (int j = 0; j < bricks[i].length; j++) { if (r.nextBoolean()) { bricks[i][j] = new Brick(10 + i * 60, 10 + j * 35, 50, 25); } } }   Thread t = new Thread(new Runnable() {   @Override public void run() { while (true) { repaint(); ball.check(bricks); try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); t.start(); }   public void paintComponent(Graphics g) { super.paintComponent(g); g.clearRect(0, 0, width, height); g.drawRect(0, 0, width, height); for (int i = 0; i < bricks.length; i++) for (int j = 0; j < bricks[i].length; j++) if (bricks[i][j] != null) bricks[i][j].draw(g); ball.draw(g); ball.move(); if (ball.contains(0, ball.getCenterY()) || ball.contains(width, ball.getCenterY())) { ball.setLeft(!ball.isLeft()); } if (ball.contains(ball.getCenterX(), 0) || ball.contains(ball.getCenterX(), height)) { ball.setUp(!ball.isUp()); } }   public static void main(String[] args) { JFrame f = new JFrame(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setBounds(0, 0, 1000, 1000); f.add(new bounce()); }   }```
• August 6th, 2011, 08:14 AM
Norm
Re: bricks wierd problem
Quote:

the ball only hits bricks above a curtain line.
Sounds like a logic problem.
Have you tried debugging the code by adding printlns to show the values of the variables that control what is happening? Print them out every time they change and when they are used to decide when the ball hits the curtain.

If you want anyone to test your code you need to post code that compiles.
Your posted code does not compile.
• August 6th, 2011, 11:20 AM
bardd
Re: bricks wierd problem
i forgot the imports. it should work now.
• August 6th, 2011, 11:40 AM
Norm
Re: bricks wierd problem
Thanks.
Have you tried debugging the code by adding printlns to show the values of the variables that control what is happening? Print them out every time they change and when they are used to decide when the ball hits the curtain.

Also the code has too many hardcoded/magic numbers: 900, 680, etc.

These should all be changed to use one or two variables that define the width and height of the box etc.
For testing I wanted to reduce the size of the box for quicker bounces but am having a hard time because if I reduce the size of the frame, it doesn't change the hardcoded values you have in the code.
As it currently is coded, it is NOT possible to make changes for easy debugging.
• August 7th, 2011, 11:24 PM
Junky
Re: bricks wierd problem
Code java:

```public void check(Brick[][] b) { for (int i = 0; i < b.length; i++) { for (int j = 0; j < b.length; j++) { if (b[i][j] != null) { for (int k = 0; k <= b[i][j].getWidth(); k++) { if (contains(b[i][j].getX() + k, b[i][j].getY())```
Arrrrrrrrrrgggg!

Kill the ugly code, kill it now.
• August 8th, 2011, 08:20 AM
bardd
Re: bricks wierd problem
i changed it. you shouldn't have a poblem editing now. both width and height are defined in the constructor.
• August 8th, 2011, 08:21 AM
bardd
Re: bricks wierd problem
Quote:

Originally Posted by Junky
Arrrrrrrrrrgggg!

Kill the ugly code, kill it now.

what other options do i have?
• August 8th, 2011, 08:26 AM
Norm
Re: bricks wierd problem
Quote:

the ball only hits bricks above a curtain line.
Where is this line that divides where a brick is hit?
Does it relate to the array that holds the bricks?
• August 8th, 2011, 08:35 AM
bardd
Re: bricks wierd problem
Quote:

Originally Posted by Norm
Where is this line that divides where a brick is hit?
Does it relate to the array that holds the bricks?

what i ment was for some reason the ball hits the bricks on the upper rows of bricks but goes through the lower ones.
• August 8th, 2011, 09:28 AM
Norm
Re: bricks wierd problem
Quote:

the ball hits the bricks on the upper rows of bricks but goes through the lower ones.
Which rows? Then look at the array that holds the bricks and see if there is a correlation.
• August 8th, 2011, 10:40 AM
bardd
Re: bricks wierd problem
Quote:

Originally Posted by Norm
Which rows? Then look at the array that holds the bricks and see if there is a correlation.

the ball doesn't hit the 3 bottom rows.
what do you mean by correlation?
• August 8th, 2011, 10:44 AM
Norm
Re: bricks wierd problem
What are the dimensions of the brick array? Is there a relationship of 3 in the dimensions?
How do your loops go thru the brick array looking for collisions?