I am programming the game bricks, and i have a wierd problem: the ball only hits bricks above a curtain line.
ball 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:
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:
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()); } }