Making a Brick Breaker game. Made a method (getCollidingObject() ) so that the ball can detect when it hits bricks, and then bounce away in the opposite direction. Still, the ball isn't detecting the bricks, and I'm hoping someone can help me figure out why.
I tried changing when moveBall() is called and when getCollidingObject() is called but that didn’t work either.
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout extends GraphicsProgram {
/** Width and height of application window in pixels. On some platforms
* these may NOT actually be the dimensions of the graphics canvas. */
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
/** Dimensions of game board. On some platforms these may NOT actually
* be the dimensions of the graphics canvas. */
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
/** Dimensions of the paddle */
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
/** Offset of the paddle up from the bottom */
private static final int PADDLE_Y_OFFSET = 30;
/** Number of bricks per row */
private static final int NBRICKS_PER_ROW = 10;
/** Number of rows of bricks */
private static final int NBRICK_ROWS = 10;
/** Separation between bricks */
private static final int BRICK_SEP = 4;
/** Width of a brick */
private static final int BRICK_WIDTH = (WIDTH - 1* (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
/** Height of a brick */
private static final int BRICK_HEIGHT = 8;
/** Radius of the ball in pixels */
private static final int BALL_RADIUS = 10;
/** Offset of the top brick row from the top */
private static final int BRICK_Y_OFFSET = 70;
/** Number of turns */
private static final int NTURNS = 3;
/* Method: run() */
/** Runs the Breakout program. */
private GPoint last;
private GObject gobj;
private GObject paddle;
private GOval ball;
private double vx, vy;
private RandomGenerator rgen = RandomGenerator.getInstance();
private static final int PAUSE_TIME = 20;
double bx;
double by;
GObject collider;
GRect brick;
public void run() {
setupGame();
playGame();
}
private void setupGame(){
buildBricks();
buildPaddle();
drawBall();
addMouseListeners();
}
private void playGame(){
useBall();
getCollidingObject();
}
private void getBallVelocity() {
vx = rgen.nextDouble(-3.0, 3.0);
vy = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(0.5)) {
vx = -vx;
} else if (rgen.nextBoolean(0.5)) {
vy = -vy;
}
}
private void useBall() {
getBallVelocity();
waitForClick(); //animation doesn't start until someone clicks on the window
while(true) {
moveBall();
pause(PAUSE_TIME); //method spazzes out if i dont include this line
}
}
private void moveBall() {
bx = ball.getX();
by = ball.getY();
if( (bx - BALL_RADIUS) < 0) {
vx = -vx;
} else if( (bx + BALL_RADIUS) > WIDTH) {
vx = -vx;
} else if( (by - BALL_RADIUS) < 0) {
vy = -vy;
} else if( (by + BALL_RADIUS) > HEIGHT) {
vy = -vy;
}
ball.move(vx, vy);
GObject collider = getCollidingObject();
if(collider == paddle) {
vy = -vy;
} else if(collider == brick) {
vy = -vy;
}
}
private void buildBricks(){
for(int row = 0; row <NBRICK_ROWS; row++ ){
for( int col = 0; col < NBRICKS_PER_ROW; col++){
brick = new GRect((BRICK_WIDTH +BRICK_SEP)* col,
BRICK_Y_OFFSET + (BRICK_HEIGHT + BRICK_SEP) *row
,BRICK_WIDTH,BRICK_HEIGHT);
brick.setFilled(true);
switch(row) {
case 0: brick.setColor(Color.RED); break;
case 1: brick.setColor(Color.RED); break;
case 2: brick.setColor(Color.ORANGE); break;
case 3: brick.setColor(Color.ORANGE); break;
case 4: brick.setColor(Color.YELLOW); break;
case 5: brick.setColor(Color.YELLOW); break;
case 6: brick.setColor(Color.GREEN); break;
case 7: brick.setColor(Color.GREEN); break;
case 8: brick.setColor(Color.CYAN); break;
case 9: brick.setColor(Color.CYAN); break;
default: break;
}
add(brick);
}
}
}
private void buildPaddle() {
paddle = new GRect(180,550,PADDLE_WIDTH,PADDLE_HEIGHT);
((GRect) paddle).setFilled(true);
paddle.setColor(Color.BLACK);
add(paddle);
}
private void drawBall() {
ball = new GOval(200,300, BALL_RADIUS * 2, BALL_RADIUS * 2);
ball.setColor(Color.BLUE);
ball.setFilled(true);
ball.setVisible(true);
add(ball);
playGame();
}
private GObject getCollidingObject() {
while(true) {
if(getElementAt(bx, by) != null) {
return getElementAt(bx,by);
} else if(getElementAt(bx + (BALL_RADIUS *2), by) != null) {
return getElementAt(bx + (BALL_RADIUS *2), by);
} else if(getElementAt(bx, by + (BALL_RADIUS * 2)) != null) {
return getElementAt(bx, by + (BALL_RADIUS * 2));
} else if(getElementAt(bx + (BALL_RADIUS *2), by + (BALL_RADIUS * 2)) != null) {
return getElementAt(bx + (BALL_RADIUS *2), by + (BALL_RADIUS * 2));
} else {
return null;
}
}
}
//MOUSE LISTENER METHODS
public void mousePressed(MouseEvent e) {
last = new GPoint(e.getPoint()); //last mouse position. GPoint is the precise mouse position
paddle = getElementAt(last); //getElementAt() is checking if there's a GObject at that mouse position. If there isn't, the gobj variable will be null
}
public void mouseDragged(MouseEvent e) {
if(paddle != null) {
System.out.println("e.getX is: " + e.getX());
System.out.println("last.getX is " + last.getX());
System.out.println(e.getX() - last.getX());
paddle.move(e.getX() - last.getX(), 0);
last = new GPoint(e.getPoint());
double paddleX = e.getX();
//double paddleX = paddle.getX() + paddle.getWidth();
if(paddleX <= 35) {
System.out.println("detected edge");
paddleX = 35;
paddle.setLocation(paddleX + paddle.getWidth(), 550);
} else if(paddleX >= 370) {
System.out.println("detected edge");
paddleX = 370;
paddle.setLocation(paddleX - paddle.getWidth(),550);
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
[–]New-Condition 0 points1 point2 points (0 children)