2

I am new to Java and am learning from Stanford lectures from YouTube.

So I was trying out their assignment to make a breakout game and so far so good until now. I have all my bricks, ball and paddle including the mechanics of the game but when i run the game only one brick can be removed when hit by the ball. see this. That brick happens to be the last brick added to the canvas.

The ball just flies past all other bricks with no effect. The relevant code is below.

Am I lacking some important knowledge about getElementAt here? I have a feeling that getCollidingObject is not assigned to collider, which is making the collision detection faulty. I hope someone can enlighten me on this!

private void addBallMotion(){
// y component of starting velocity; pixels per second
  vy = 3.0;

/* x component of starting velocity; pixels per second
 * which ranges according to the random generator
 * can be negative or positive, ball can go left or right with equal chances
 */
  vx = rgen.nextDouble(1.0, 3.0);
  if (rgen.nextBoolean(0.5)){
    vx = -vx;
  }
  while (true){
    ball.move(vx, vy);
    checkCollision();
    pause(FPS);
  }
}

private void checkCollision(){
  checkWallCollision();
  checkBrickAndPaddleCollision();
}

private void checkWallCollision(){
  //checks for left or right collision
  if ( (ball.getX() < leftBounds.getX() ) || (ball.getX() + BALL_RADIUS * 2 > rightBounds.getX()) ){
    vx = -vx;
  }
  //checks for top or bottom collision
  if ( (ball.getY() < topBounds.getY() ) || (ball.getY() + BALL_RADIUS * 2 > bottomBounds.getY()) ){
    vy = -vy;
  }
}

private void checkBrickAndPaddleCollision(){
  GObject collider = getCollidingObject();
  if (collider == brick){
    remove(collider);
    vy = -vy;
  }
  if (collider == paddle){
    vy = -vy;
  }
}

//check for collision at the 4 edges of the ball
//starting with the left and going clockwise
private GObject getCollidingObject(){
  GObject  ballLeft= getElementAt (ball.getX() - 1, ball.getY() + BALL_RADIUS);
  GObject  ballRight= getElementAt (ball.getX() + BALL_RADIUS * 2 + 1, ball.getY() + BALL_RADIUS);
  GObject  ballTop = getElementAt (ball.getX() + BALL_RADIUS * 2, ball.getY() - 1);
  GObject  ballBottom= getElementAt (ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS * 2 + 1);

  if (ballLeft != null){
    return (ballLeft);
  }
  else if (ballTop != null){
    return (ballTop);
  }
  else if (ballRight != null){
    return (ballRight);
  }
  else if (ballBottom != null){
    return (ballBottom);
  }
  return (null);
}

private GRect paddle;  // creates a paddle that only moves linearly according to mouses' x coordinate
private GRect brick;
private GOval ball;
private double vx, vy;  // x and y components of the ball's velocity
//private GObject collider;

Here's the whole program:

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 */
  public static final int APPLICATION_WIDTH = 400;
  public static final int APPLICATION_HEIGHT = 600;

    /** Dimensions of game board (usually the same) */
  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 - (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;

    /** Offset of the side bricks from the sides of game window */
  private static final int BRICK_X_OFFSET = ((WIDTH - NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP) + BRICK_SEP) / 2);

    /** Number of turns */
  private static final int NTURNS = 3;

    /**  Number of frames per second */
  private static final int FPS = 1;

    /* Method: run() */
    /** Runs the Breakout program. */
  public void run() {
    addMouseListeners();
    addWorld();
    //  runGame();
  }
  private void addWorld(){
    setSize (APPLICATION_WIDTH, APPLICATION_HEIGHT);
    addPlayingBox();
    addBricks();
    addPaddle();
    addBall();
    //  addCounter();
  }

  //adds the bound area onto screen
  private void addPlayingBox(){
    topBounds = new GLine (0, 0, WIDTH, 0);
    bottomBounds = new GLine (0, HEIGHT, WIDTH, HEIGHT);
    leftBounds = new GLine (0, 0, 0, HEIGHT);
    rightBounds = new GLine (WIDTH, 0, WIDTH, HEIGHT);
    add (topBounds);
    add (bottomBounds);
    add (leftBounds);
    add (rightBounds);
  }

  private void addBricks(){
    for (int i = 0; i < NBRICK_ROWS; i++){
      int y = BRICK_Y_OFFSET + (i * (BRICK_HEIGHT + BRICK_SEP));

      for (int j = 0; j < NBRICKS_PER_ROW; j++){
        int x = (BRICK_X_OFFSET) + (j * (BRICK_WIDTH + BRICK_SEP));
        brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT );
        colorBrick(brick, i);
        add (brick);
      }
    }
  }

  // every consecutive 2 rows are colored the same
  private void colorBrick(GRect brick, int rowNumber){
    brick.setFilled (true);
    switch (rowNumber + 1) {
      case 1: case 2: brick.setColor(Color.red);
        break;
      case 3: case 4: brick.setColor(Color.orange);
        break;
      case 5: case 6: brick.setColor(Color.yellow);
        break;
      case 7: case 8: brick.setColor(Color.green);
        break;
      case 9: case 10:brick.setColor(Color.cyan);
        break;
    }
  }

  //adds paddle to screen
  private void addPaddle(){
    paddle = new GRect (PADDLE_WIDTH, PADDLE_HEIGHT);
    paddle.setFilled(true);
    paddle.setColor (Color.BLACK);
    add (paddle);
  }

  //creates motion for the paddle according to mouse movement
  public void mouseMoved(MouseEvent e){
    paddle.setLocation ((e.getX() - PADDLE_WIDTH / 2), (double) (HEIGHT - PADDLE_Y_OFFSET));

  /* checks if the paddle within the playing area
   * if not the paddles will stay at the extremities*/
    if ( paddle.getX() > (WIDTH - PADDLE_WIDTH)){
      paddle.setLocation((double) (WIDTH - PADDLE_WIDTH), (double) (HEIGHT - PADDLE_Y_OFFSET));
    }
    if ( paddle.getX() < 0){
      paddle.setLocation((double) 0, (double) (APPLICATION_HEIGHT - PADDLE_Y_OFFSET));
    }
  }

  private void addBall(){
    ball = new GOval (((WIDTH - BALL_RADIUS * 2) / 2), ((HEIGHT - BALL_RADIUS * 2) / 2),
              BALL_RADIUS * 2, BALL_RADIUS * 2);
    ball.setFilled(true);
    ball.setColor(Color.BLACK);
    add (ball);
    addBallMotion();
  }

  private void addBallMotion(){
  // y component of starting velocity; pixels per second
    vy = 3.0;

  /* x component of starting velocity; pixels per second
   * which ranges according to the random generator
   * can be negative or positive, ball can go left or right with equal chances
   */
    vx = rgen.nextDouble(1.0, 3.0);
    if (rgen.nextBoolean(0.5)){
      vx = -vx;
    }
    while (true){
      ball.move(vx, vy);
      checkCollision();
      pause(FPS);
    }
  }

  private void checkCollision(){
    checkWallCollision();
    checkBrickAndPaddleCollision();
  }

  private void checkWallCollision(){
    //checks for left or right collision
    if ( (ball.getX() < leftBounds.getX() ) || (ball.getX() + BALL_RADIUS * 2 > rightBounds.getX()) ){
      vx = -vx;
    }
    //checks for top or bottom collision
    if ( (ball.getY() < topBounds.getY() ) || (ball.getY() + BALL_RADIUS * 2 > bottomBounds.getY()) ){
      vy = -vy;
    }
  }

  private void checkBrickAndPaddleCollision(){
    GObject collider = getCollidingObject();
    if (collider == brick){
      remove(collider);
      vy = -vy;
    }
    if (collider == paddle){
      vy = -vy;
    }
  }

  //check for collision at the 4 edges of the ball
  //starting with the left and going clockwise
  private GObject getCollidingObject(){
    GObject  ballLeft= getElementAt (ball.getX() - 1, ball.getY() + BALL_RADIUS);
    GObject  ballRight= getElementAt (ball.getX() + BALL_RADIUS * 2 + 1, ball.getY() + BALL_RADIUS);
    GObject  ballTop = getElementAt (ball.getX() + BALL_RADIUS * 2, ball.getY() - 1);
    GObject  ballBottom= getElementAt (ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS * 2 + 1);

    if (ballLeft != null){
      return (ballLeft);
    }
    else if (ballTop != null){
      return (ballTop);
    }
    else if (ballRight != null){
      return (ballRight);
    }
    else if (ballBottom != null){
      return (ballBottom);
    }
    return (null);
  }

  private GRect paddle;  // creates a paddle that only moves linearly according to mouses' x coordinate
  private GRect brick;
  private GOval ball;
  private double vx, vy;  // x and y components of the ball's velocity
  private RandomGenerator rgen = RandomGenerator.getInstance();
  //private GObject collider;
  private GLine topBounds;  // creates a bounding box that is the playing area
  private GLine bottomBounds;
  private GLine leftBounds;
  private GLine rightBounds;
}
Kevin
  • 53,822
  • 15
  • 101
  • 132
Gerald
  • 567
  • 1
  • 10
  • 17
  • Madsonic, did you try to debug your application? To put breakpoints in some relevant - in your opinion - places and check what happens? – Piotr Chojnacki Sep 30 '13 at 06:00
  • 1
    I tend to suspect, from your description of the problem, that you are doing something wrong while adding bricks to your field. As a side note, do not compare objects with `==`! Use the `.equals` method. – Aurand Sep 30 '13 at 06:01
  • @Piotr yes i have tried to debug my program for a few days already but to no avail. Im sorry what are breakpoints? still a noob here! – Gerald Sep 30 '13 at 06:02
  • @madsonic Also I have a question, what is `brick` inside `checkBrickAndPaddleCollision()`? Isn't it maybe the last `brick` added to the canvas which you talked about? This could be the reason then. Just show me a place where you're initializing your `brick` variable. – Piotr Chojnacki Sep 30 '13 at 06:03
  • @madsonic Well, to explain what breakpoints are and how to use them, it's a bit complex thing for this place - the question is not related to this, so I'll just give you this article to read: http://www.vogella.com/articles/EclipseDebugging/article.html It's pretty simple, but a bit complex. – Piotr Chojnacki Sep 30 '13 at 06:05
  • @Piotr thanks for the link. the bricks are initialized at `addBricks()`. The `brick` in `checkBrickAndPaddleCollision()` are all the brick objects i have added to the canvas. Is this the right way to refer to them? – Gerald Sep 30 '13 at 06:15
  • @madsonic Tell me if you understand what I wrote in my answer. If not, I'll try to suggest some resolution for your issue. – Piotr Chojnacki Sep 30 '13 at 06:19

1 Answers1

4

Exactly as I thought. Look - You have a field in your class called brick. It's of GRect type. At the beginning you're calling addWorld() method, which calls addBricks(). Now check what you wrote:

private void addBricks(){
    for (int i = 0; i < NBRICK_ROWS; i++){
        int y = BRICK_Y_OFFSET + (i * (BRICK_HEIGHT + BRICK_SEP));

        for (int j = 0; j < NBRICKS_PER_ROW; j++){
            int x = (BRICK_X_OFFSET) + (j * (BRICK_WIDTH + BRICK_SEP));
            brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT );
            colorBrick(brick, i);
            add (brick);
        }
    }
}

What happens in there? You're having a loop in which brick is overriden NBRICK_ROWS * NBRICKS_PER_ROW times, which causes brick field to be the last created brick on your screen.

Basically, you're doing something similar to this:

int x;
x = 5;
x = 6;
x = 8;
// x is 8 now 

And in checkBrickAndPaddleCollision() you're checking only if collider is brick. Which in other words means, you're checking if it's the last brick - if it is, then you remove it.

First of all you should create an array of bricks, not just one field.

ArrayList<GRect> bricks;

instead of:

GRect brick;

And then in addBricks() method, you should have:

bricks.add(new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT));

instead of:

brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT );

After this, in checkBrickAndPaddleCollision(), you should not check:

if (collider == brick) {
     remove(collider);
     vy = -vy;
}

but rather all bricks:

for(GRect brick : bricks) {
    if (collider.equals(brick)) {  // Note that you should rather use .equals, instead of == as Aurand stated in his comment
         remove(collider);
         vy = -vy;
    }
}
Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
  • I see! so if i wish to remove the other bricks how should i refer to them? what would be the parameter inside remove()? – Gerald Sep 30 '13 at 06:33
  • i thank you for your effort. However after further research, i realized my codes are viable meaning that I do not have to employ new methods i.e. array and equal. I am reluctant to use your answer due to the fact that if i were to follow the stanford course, they have not taught us these tricks yet (array and equal) and the whole breakout game could be done. I found another student with similar codes and his game [click here for his codes](https://sites.google.com/site/qasimisonline/my-stuff/breakout-game-code-java-) is working perfectly fine. It seems like we are missing out something here. – Gerald Sep 30 '13 at 10:19
  • @madsonic Ok, you have killed me. You're creating a game and you're calling arrays a trick? I think you should just spend few minutes on figuring out why his code works. Instead of this `for` loop, you can do this: `if(collider == null) return; else if(collider != paddle) { // do your stuff here }` – Piotr Chojnacki Oct 01 '13 at 05:54
  • I am sorry if i sounded offensive. I was just curious to find out why with the same set of tools he could get the game working but I couldn't. Thank you. Your suggestions do work fine. – Gerald Oct 01 '13 at 12:11
  • @madsonic What wrong happened again that you unliked my answer? :-) – Piotr Chojnacki Oct 05 '13 at 07:09