Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 324605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:08:22+00:00 2026-05-12T09:08:22+00:00

I am taking an intro to programming online. However, I am stuck on one

  • 0

I am taking an intro to programming online. However, I am stuck on one assignment.

The assignment is to write a breakout game. I have successfully written 97% of the game. However, the game stops before all the bricks are removed. Sometimes there are 4 bricks remaining, some times 11. The program is designed to stop when the score counter reaches the point when all the bricks are gone, so it must be reaching that point early.

What am I doing wrong?

Edit: Inlined code. and rephrased question

/*
 * File: Breakout.java
 * -------------------
 * Name:Alex Godin
 * 
 * This file will eventually implement the game of Breakout.
 */

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;

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

/**pause time*/
   private static final int PAUSE_TIME = 3;

/**THE VALUE OF EACH BRICK*/
   private static final int BRICKVAL = 10;

/** ivar holding the ball*/
   private GOval ball;

/**The current row(for setup)*/
   private static int rownum = 0;

/**The paddle*/
   private static GRect paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);

/**The velocity*/
   private static double vx, vy;

/**the random generator*/
   private RandomGenerator rgen = RandomGenerator.getInstance();

/**bricks remaining*/
   private static int bricks = NBRICKS_PER_ROW * NBRICK_ROWS;

/**the score int*/
   private static int scoreINT = 0;

/**livesRemaining*/
   private static int livesINT = NTURNS;

/**score label*/
   private static GLabel score = new GLabel("Score:" + scoreINT,0,0);

/**lives label*/
   GLabel lives = new GLabel("lives :" + livesINT,0,0);

/* Method: run() */
/** Runs the Breakout program */
   public void run() {
      scoreAndLives();
      setUpBricks();
      paddle();
      addMouseListeners();
      addKeyListeners();
      vx = rgen.nextDouble(1.0, 3.0);
      ball();
      move();
   }

/**adds a score and life counter*/
   private void scoreAndLives(){
      score();
      lives();
   }

/**adds a score counter*/
   private void score(){
      score.setLocation(7,7 + score.getHeight());
      score.setColor(Color.RED);
      score.setFont(new Font("Serif", Font.BOLD, 24));
      add(score);
   }

/**adds a life counter*/
   private void lives(){
      lives.setLocation(WIDTH - lives.getWidth()*2 + 7,7 + lives.getHeight());
      lives.setColor(Color.RED);
      lives.setFont(new Font("Serif", Font.BOLD, 24));
      add(lives);
   }

/**designs the brick */
   private GRect brickDesign() {
      GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
      brick.setFilled(true);
      switch (rownum + 1){
         case 1: brick.setColor(Color.RED); break;
         case 2: brick.setColor(Color.RED); break;
         case 3: brick.setColor(Color.ORANGE); break;
         case 4: brick.setColor(Color.ORANGE); break;
         case 5: brick.setColor(Color.YELLOW); break;
         case 6: brick.setColor(Color.YELLOW); break;
         case 7: brick.setColor(Color.GREEN); break;
         case 8: brick.setColor(Color.GREEN); break;
         case 9: brick.setColor(Color.CYAN); break;
         case 10: brick.setColor(Color.CYAN); break;
      }
      return brick; 
   }

/**sets up the bricks*/
   private void setUpBricks(){
      int x=0;
      int y=0;
      for(int i=0; i<NBRICK_ROWS; i++){
         x=0;
         y=rownum * BRICK_HEIGHT + BRICK_SEP * i + BRICK_Y_OFFSET;
         for(int j=0; j<NBRICKS_PER_ROW + 1; j++){
            add(brickDesign(), x, y);
            x=(j * BRICK_WIDTH) + (BRICK_SEP * j);
         }                                                                                                                                                                                                                                    
         rownum+=1;
      }
   }

/**initializes the paddle*/
   private void paddle(){
      int xCenter = WIDTH/2 - PADDLE_WIDTH/2;
      paddle.setFilled(true);
      add(paddle, xCenter, HEIGHT-PADDLE_Y_OFFSET);
   }

/**moves the paddle*/
   public void mouseMoved(MouseEvent e){
      int x = e.getX();
      if(x < WIDTH-PADDLE_WIDTH){
         paddle.setLocation(x, APPLICATION_HEIGHT - PADDLE_Y_OFFSET);
      }
   }

/**sets up the ball*/
   private void ball(){
      ball = new GOval( WIDTH/2 - BALL_RADIUS, HEIGHT/2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2);
      ball.setFilled(true);
      add(ball);
      vy = 3.0;
   }

/**the animation*/
   private void move(){
      if (rgen.nextBoolean(0.5)) vx = -vx;
         while(true){
            ball.move(vx, vy);
            checkWallColisions();
            checkCollisions();
            pause(PAUSE_TIME);
            if(scoreINT == bricks * BRICKVAL){
               break;
            }
         }
   }

/**Checks for colisions with the wall*/
   private void checkWallColisions(){
      if(xWallCollision() == true){
         xColide();
      }
      if(yWallCollision() == true){

         yColide();
      }           

   }

/**what to do in case of a x collision*/
   private void xColide(){
      if(vx>0){
         vx = -1 * rgen.nextDouble(1.0, 3.0);
      }else{
         vx = rgen.nextDouble(1.0, 3.0);
      }
   }

/**what to do in case of a y collision*/
   private void yColide(){
      if(vx>0){
         vx = rgen.nextDouble(1.0, 3.0);
      }else{
         vx = -1 * rgen.nextDouble(1.0, 3.0);
      }
      vy=-vy;     
   }

/**checks for an x wall colision*/
   private boolean xWallCollision(){
      if(ball.getX() + BALL_RADIUS*2 > WIDTH){
         double bally=ball.getY();
         ball.setLocation(WIDTH-BALL_RADIUS*2, bally);
         return true;
      }else if(ball.getX() < 0){
         double bally=ball.getY();
         ball.setLocation(0, bally);
         return true;
      }else{
         return false;
      }
   }

/**checks for a y wall colision*/
   private boolean yWallCollision(){
      if(ball.getY() > HEIGHT - BALL_RADIUS*2){
         return true;
      }if(ball.getY() < 0){
         return true;
      }else{
         return false;
      }
   }

/**gets coliders*/
   private GObject getColidingObject(){
      if(getElementAt(ball.getX(), ball.getY()) != null){
         return getElementAt(ball.getX(), ball.getY());
      }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()) != null){
         return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY());
      }else if(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2) != null){
         return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2);
      }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2) != null){
         return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2);
      }else{
         return null;
      }
   }

/**checks for brick and paddle colisions*/
   private void checkCollisions(){
      GObject colider = getColidingObject();
      if(colider == paddle){
         yColide();
      }else if(colider == lives || colider == score){

      }else if(colider != null){
         yColide();
         remove(colider);
         scoreINT+=BRICKVAL;
         score.setLabel("Score:" + scoreINT);
      }
   }
}

I can get the ball to bounce around however the loop escapes before all the bricks have been removed and the ball stops bouncing. The loop is set to escape when the score reaches the point at which all the bricks will be gone. However it’s reaching that point too early.

/**the animation*/
private void move(){
        if (rgen.nextBoolean(0.5)) vx = -vx;
                while(true){
                        checkCollisions();
                        ball.move(vx, vy);
                        checkWallColisions();
                        pause(PAUSE_TIME);
                        //where i'm having issues - the loop is set to escape when the score reaches the point at which all the bricks will be gone but the score is reaching that point too early 
                        if(scoreINT == bricks * BRICKVAL){
                                break;
                        }
                }
}

/**gets coliders*/
private GObject getColidingObject(){
        if(getElementAt(ball.getX(), ball.getY()) != null){
                return getElementAt(ball.getX(), ball.getY());
        }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()) != null){
                return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY());
        }else if(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2) != null){
                return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2);
        }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2) != null){
                return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2);
        }else{
                return null;
        }
}

/**checks for brick and paddle colisions*/
private void checkCollisions(){
        GObject colider = getColidingObject();
        if(colider == paddle){
                yColide();
        }else if(colider == lives || colider == score){}else if(colider != null){
                remove(colider);
                yColide();
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:08:22+00:00Added an answer on May 12, 2026 at 9:08 am

    Are you using an IDE such as Netbeans or Eclipse to write this game? If so, you could set some breakpoints in your code and run it in debug mode to try and find out what is happening.

    I presume there is a method that checks if the score is zero and aborts the program? Put a breakpoint in that method and run the application – once the breakpoint is reached you can use Watches observe the state of your program when it exits.

    Have you checked that the program is exiting cleanly? Is it definitely a zero score that shuts it down? An exception could be getting thrown that is aborting your app.

    Also, if you are new to programming then debugging is a very good skill to learn!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 239k
  • Answers 239k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you use random padding such as OAEP in PKCS#1,… May 13, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer I used RubyAMF in production before and it worked great… May 13, 2026 at 7:05 am
  • Editorial Team
    Editorial Team added an answer You can use a union query: SELECT "" As Sort,… May 13, 2026 at 7:05 am

Related Questions

Alright here's the deal, I'm taking an intro to C++ class at my university
Is there an easy way to manually (ie. not through code) find the size
I am taking my first foray into PHP programming and need to configure the
I've been a software developer for over twenty years, programming in C, Perl, SQL,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.