Results 1 to 6 of 6
  1. #1
    dage is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Breakout game in the assignment 3 cs106


    Here is my code:

    Java Code:
    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;
    
    /* Method: run() */
    /** Runs the Breakout program. */
    	public void run() {
    			setUpBricks();
    			setUpPaddle();
    			
    			 while( LivesRemainning != 0) {
    				setUpBall();  
    				while ( ball.getY() < (getHeight() + APPLICATION_HEIGHT  )/2   -2*BALL_RADIUS  ) {
    						moveBall();
    						checkForCollision();
    						if ( NumBricksRemainning == 0)  break;
    						pause(  10 );
    					}
    					LivesRemainning --;
    					remove(ball);
    			}	 
    	}
    	private void setUpBricks()  {
    		
    		// the first step:  draw  application window and bricks
    			add (new GRect ((getWidth()- APPLICATION_WIDTH )/2,(getHeight()- APPLICATION_HEIGHT  )/2,APPLICATION_WIDTH  , APPLICATION_HEIGHT  ));
    			for (int j = 0; j < NBRICK_ROWS; j++ ) {
    				for (int i = 0; i < NBRICKS_PER_ROW ;i++ ) {
    					Color color = Color.BLACK;
    					switch( j ) {
    					case 0: 
    					case 1: color = Color.RED;break;
    					case 2:  
    					case 3: color = Color.ORANGE;break;
    					case 4:  
    					case 5: color = Color.YELLOW;break;
    					case 6:  
    					case 7: color = Color.GREEN;break;
    					case 8: 
    					case 9: color = Color.CYAN;break;
    					default: break;
    			
    					}
    					add(MakeRect(  (getWidth()- APPLICATION_WIDTH )/2 + i*(BRICK_WIDTH + BRICK_SEP ),
    					       (getHeight()- APPLICATION_HEIGHT  )/2 + BRICK_Y_OFFSET + j*(BRICK_HEIGHT + BRICK_SEP ),
    					        BRICK_WIDTH,   BRICK_HEIGHT  ,color ));
    					
    				}
    			}
    	
    		
    	}
    	private void setUpPaddle() {
    		// the second step: draw a movable paddle.
    			paddle =  MakeRect ( 0 , 0 , PADDLE_WIDTH , PADDLE_HEIGHT ,Color.BLACK );
    			add ( paddle ); 
    			addMouseListeners();
    	}
    	private GRect MakeRect (double x, double y,double m, double n,Color color) {
    			GRect rect = new GRect ( x, y, m, n);
    			rect.setFilled(true);
    			rect.setFillColor(color);
    			return rect;
    	}
    	public void mouseMoved (MouseEvent e) {
    			double x  = e.getX();
    			if ( x < (getWidth()- APPLICATION_WIDTH )/2 ) {
    				x = (getWidth()- APPLICATION_WIDTH )/2;
    			}else if ( x > (getWidth()+ APPLICATION_WIDTH )/2 - PADDLE_WIDTH ) {
    				x = (getWidth()+ APPLICATION_WIDTH )/2 - PADDLE_WIDTH ;
    			}else {
    				x = e.getX();
    			}
    			paddle.setLocation( x , (getHeight() + APPLICATION_HEIGHT  )/2 -  PADDLE_Y_OFFSET  );
    	}
    	private void setUpBall() {
    			ball = new GOval ( getWidth()/2 - BALL_RADIUS, getHeight()/2 - BALL_RADIUS, 2*BALL_RADIUS, 2*BALL_RADIUS );
    			ball.setFilled(true);
    			ball.setColor(Color.BLACK);
    			add ( ball );
    			xVel = rgen.nextDouble(1, 3);
    			if (rgen.nextBoolean(0.5)) xVel = -xVel;
    		
    	}
    	private void moveBall () {
    		
    		 
    		ball.move( xVel, yVel );
    	}
    	private void checkForCollision() {
    			if ( ball.getX() >  (getWidth() + WIDTH )/2 - 2*BALL_RADIUS   ) {
    				xVel = -xVel;
    				ball.move(-(ball.getX()- getWidth()/2 -WIDTH/2 +2*BALL_RADIUS),0);
    			}else if (ball.getX() < ( getWidth() - WIDTH )/2) {
    				xVel = -xVel;
    				ball.move(-(ball.getX()-getWidth()/2+WIDTH/2), 0);
    			}else if (ball.getY() < (getHeight()-HEIGHT)/2 ) {
    				yVel = -yVel;
    				ball.move(0, -(ball.getY()-(getHeight()-HEIGHT)/2));
    			}
    			
    			collider = getCollidingObject();
    			if ( collider != null )  {
    					if ( collider == paddle ) {
    						yVel = -yVel;
    					}else   {
    						remove( collider );
    						NumBricksRemainning --;
    						yVel = -yVel;
    					}
    			}
    				 
    	}
    	private GObject getCollidingObject() {
    			GObject gobj;
    			double x = ball.getX();
    			double y = ball.getY();
    			gobj =  getElementAt(x, y);
    			if (gobj == null )      gobj =  getElementAt(x+2*BALL_RADIUS, y  );
    			if (gobj == null )      gobj =  getElementAt(x,y+2*BALL_RADIUS);
    			if (gobj == null )      gobj =  getElementAt(x+2*BALL_RADIUS,y+2*BALL_RADIUS);
    			
    			return gobj;
    	}
    	private GRect paddle;
    	private GOval ball;
    	private RandomGenerator rgen = RandomGenerator.getInstance();
    	private double xVel  ;
    	private double yVel = 3.0;
    	private int LivesRemainning = NTURNS;
    	private int NumBricksRemainning =  NBRICKS_PER_ROW * NBRICK_ROWS ;
    	private GObject collider;
    I want to break the inner while loop in the run method when the number of the bricks remainning in the application is zero. There must be something wrong with my code. when I run my program using Eclipse, there is an unexpected result.
    when the remainning number of the bricks is one,the program is over and the
    the external while loop in the run method is over.
    where am I wrong?:confused:

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    while(LivesRemainning != 0) {
    It would be good practice to change this so that the game doesn't continue if somehow a negative value is passed for LivesRemainning: (Btw the correct spelling is "Remaining")
    Java Code:
    while (LivesRemaining > 0) {
    Now, your inner-While loop is broken when there are no bricks remaining:

    Java Code:
    if (NumBricksRemainning == 0)  break;
    However, the first while loops contines because RemainingLives != 0. In fact, you set RemainingLives to NTURNS = 3. After the inner-while is broken, these statements are made:

    Java Code:
    LivesRemainning --;
    remove(ball);
    Then the ball is set-up again because RemainningLives now equals 3 - 1 = 2, so the outer while still runs.

    Java Code:
    while(LivesRemainning != 0) {
    setUpBall();
    If you want to break the outer while from the inner while you need to use a label, e.g.

    Java Code:
    OuterLoop:
    while (true) {
      while (true) {
        break Outerloop;
      }
    }

  3. #3
    dage is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default


    thanks ,ozzyman. very grateful for your reply.
    Maybe what I say in my thread is not very clear,
    what you write in your post is not the answer to
    my question.

    I run my program using Eclipse,then the program
    terminates at the time when there is still one brick
    remaining in the application window (NumBricksRemaining = 1) .
    Java Code:
     		if ( collider != null )  {
    				    if ( collider == paddle ) {
    				 	yVel = -yVel;
    				    }else   {
    					remove( collider );
    				            NumBricksRemainning --;
    					yVel = -yVel;
    				    }
    			              }
    Moreover ,the
    program straightly break out of the external while
    loop in the run method ,even if LivesRemaining are not
    equal to zero. These two wrong results are not the results I want .
    Java Code:
    while( LivesRemainning != 0) {
    		setUpBall();  
    		while ( ball.getY() < (getHeight() + APPLICATION_HEIGHT  )/2   -2*BALL_RADIUS  ) {
    					moveBall();
    					checkForCollision();
    					if ( NumBricksRemainning == 0)  break;
    					pause(  10 );
    					}
    					LivesRemainning --;

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    What is
    ball.getY() < (getHeight() + APPLICATION_HEIGHT )/2 -2*BALL_RADIUS
    meant to do?
    Last edited by ozzyman; 03-16-2011 at 11:41 AM.

  5. #5
    dage is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default



    Thanks.



    As you know,ball.getY() gets the y coordinate of the ball.
    when
    ball.getY() = (getHeight() + APPLICATION_HEIGHT )/2 -2*BALL_RADIUS

    ,the ball fall down to the bottom of the application window.
    At that time, the inner while loop is over.
    so,that code is one of termination conditions.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Oh i see, so:
    Java Code:
    while (ball is in the air) {
       moveBall();
       checkForCollision();
       if (NumBricksRemainning == 0) break;
       pause(10);
    }
    LiveRemainning--;
    remove(ball);

    Makes perfect sense now. But you don't have a problem until the User has won the level, or removed all the bricks. So when you clear all the bricks and you didn't waste any lifes, this happens:

    LivesRemainning=3
    Java Code:
    if (NumBricksRemainning == 0) break;
    [program returns to external while]
    while (LivesRemainning != 0) { //returns true
       setUpBall(); //program continues

    So I would suggest you change your external while to:
    Java Code:
    while (LivesRemainning > 0 && NumBricksRemainning > 0) {

    And see if that works?

Similar Threads

  1. Making a breakout Game
    By JavaIsSoEasy?? in forum Java Gaming
    Replies: 0
    Last Post: 03-05-2011, 10:52 AM
  2. Need help with Breakout game
    By tfitz666 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 05:26 AM
  3. Breakout Game code help.
    By Ceasar in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:30 AM
  4. Problem with an assignment: Backgammon game
    By Poddy in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:32 AM
  5. Game of Life assignment
    By javan00b in forum New To Java
    Replies: 4
    Last Post: 04-28-2008, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •