#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Q about a breakout game that I am designing


    I'm designing a breakout game and I have a serious problem

    Code:
    /*  * File: Breakout.java  * -------------------  * Name:  * Section Leader:  *   
    * 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 {  	// Name:  	// Section Leader:  	
     /** 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; 	 
    /** Declares the delay between every step of the movement of the ball */ 	
    private static final int DELAY = 20;  
    /** Runs the Breakout program. */ 	
    public void run() { 		
    setup(); 		
    waitForClick(); 		
    remove(startLabel); 		
    addMouseListeners(); 		play(); 		 		 		 		 	} 	
    /** This Method creates the setup of the game in the beginning */ 	
    private void setup(){ 		setSize(WIDTH,HEIGHT); 		finalBrickSetup(); 		
    createBound(); 		
    createPaddle(); 		starter(); 		 		 		 		 	} 	 	
    /** This method starts the program */ 	
    private void starter(){ 		
    startLabel = new GLabel ("Click anywhere to start !!!"); 		
    startLabel.setColor (Color.GREEN); 		
    add(startLabel, WIDTH / 2 - startLabel.getWidth() / 1.5, HEIGHT /2 ); 		 	}  	
    /** This Method Changes the color to the right ones */ 	private void finalBrickSetup(){ 		createBrickSetup(); 		 		 	} 	
    /** This Method create The setup of bricks */ 
    	private void createBrickSetup(){ 		
    for (int i = 1; i <= NBRICK_ROWS; i++){ 			
    for (int j = 1; j <= NBRICKS_PER_ROW; j++){ 				
    Color brickColor1 = Color.RED; 				
    brick = createBrick(brickColor1); 				
    if (i <= 2){ 				brick.setColor(Color.RED); 				} 				 				
    else if (i <=4 ){ 					brick.setColor(Color.ORANGE); 				} 				 				
    else if (i <= 6){ 					brick.setColor(Color.YELLOW); 				} 				 				
    else if (i <= 8){ 					brick.setColor(Color.GREEN); 				} 				
    else brick.setColor(Color.CYAN); 					 				 				 				
    brick.setLocation(BRICK_SEP * (j+1) + BRICK_WIDTH * (j-1),  								BRICK_Y_OFFSET + (i-1) * BRICK_HEIGHT + BRICK_SEP * (i-1) ); 				add(brick); 					 				 				 					 				 			 			} 		} 		 	}  	
    /** This Method create one brick */ 
    private GRect createBrick(Color brickColor){ 			brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT); 			brick.setColor(brickColor); 			brick.setFilled(true); 			 			
    return Brick; 			 			 				 	} 			 	 
    /** This Method create the Bound to the game */ 	
    private void createBound() { 		
    GRect innerBound = new GRect(BRICK_SEP, BRICK_SEP, APPLICATION_WIDTH + BRICK_SEP, APPLICATION_HEIGHT); 		innerBound.setColor(Color.BLACK); 		
    add(innerBound); 	} 	 	
    /** This Method creates the Paddle */ 	
    private void createPaddle() { 		
    paddle = new GRect (WIDTH / 2 - PADDLE_WIDTH / 2,HEIGHT - PADDLE_Y_OFFSET, PADDLE_WIDTH, PADDLE_HEIGHT); 		paddle.setColor(Color.BLACK); 		paddle.setFilled(true); 		add (paddle); 	}  	
    /** This Methods Starts the game */ 	
    public void mouseMoved(MouseEvent e){ 		remove(startLabel); 		
    paddlePosition = paddle.getX(); 		
    if (e.getX() > 5){ 			
    if (e.getX() < WIDTH - PADDLE_WIDTH /1.15 ){ 			paddle.move(e.getX() - paddlePosition, 0); 			 			} 		 			
    else { 				paddle.setLocation(WIDTH - PADDLE_WIDTH/1.15,HEIGHT - PADDLE_Y_OFFSET); 				
    paddlePosition = paddle.getX(); 			} 			 		} 	} 		 	
    private void play(){ 		
    createBall(); 		
    vx = initializingVx(); 		
    while (ball.getY() <= HEIGHT - BALL_RADIUS * 2){ 			
    moveBall(); 			checkForCollisionWalls(); 			checkForCollisionsItems(); 			pause(DELAY); 		} 		 		 		 	} 	
    /** This method creates the ball */  	
    private void createBall(){ 		
    ball = new GOval (WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2); 		ball.setFilled(true); 		
    add(ball); 	} 	 	
    /** This method defines the movement of the ball */ 	 	private void moveBall(){ 		 		
    ball.move(vx, vy); 		 	} 	 	
    /** This method check for Collision */ 	
    private void checkForCollisionWalls(){ 		
    if (ball.getY() > HEIGHT - BALL_RADIUS * 2){ 			
    vy = -vy; 			
    ball.move(vx, vy);			 		} 		 		
    else if (ball.getY() < 0){ 			
    vy = -vy; 			
    ball.move(vx, vy); 		} 		 		
    else if (ball.getX() < 0){ 			
    vx = -vx; 			
    ball.move(vx,vy); 		} 		
    else if (ball.getX() > WIDTH - BALL_RADIUS /2){ 			
    vx = -vx; 			
    ball.move(vx,vy); 		} 	} 	 	
    /** Initializing vx */ 	
    private double initializingVx(){ 		
    vx = rgen.nextDouble(1.0, 3.0); 		
    if (rgen.nextBoolean(0.5)) vx = -vx; 		
    return vx; 		 	} 	 	 	
    /** This Method checks for Collision with items */ 	 	private void checkForCollisionsItems(){ 		
    GObject collider = getCollidingObject(); 		
    if (collider == paddle){ 			
    vy = - vy; 			 		} 		 		
    if (collider == brick){ 			
    vy = -vy; 			 			
    remove (brick); 		} 		 	} 	 	
    /** This Method checks if the ball collided */ 	
    private GObject getCollidingObject(){ 		
    GObject temp = getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS * 2);; 		
    GObject item = null;  		
    if (temp != null){ 			
    item = temp; 			
    return item; 		} 			 		
    temp = getElementAt(ball.getX(), ball.getY()); 		
    if (temp != null){ 			
    item = temp ; 			
    return item; 		} 		
    temp = getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()); 		
    if (temp != null){ 			
    item = temp; 			
    return item; 		} 		
    temp = getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2); 		
    if (temp != null){ 			
    item = temp; 			
    return item; 		} 			 		
    return item; 		 		 			 	} 		 			 		 		 		 		 		 	 	 	 	
    /** These are the instance variables */ 	
    private GLabel startLabel; 	
    private GRect paddle; 	
    private double paddlePosition; 	
    private GOval ball; 	
    private double vy = 3.0; 	
    private double vx; 	
    private RandomGenerator rgen = new RandomGenerator(); 	private GRect brick; 	 	 	 	 	 	 	 		 }
    I don't know how to make the program recognize the collision with the bricks.
    My first thought that I didn't defined them as I should
    but I don't know how I should have done it better.
  2. #2
  3. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    Let's try making your code actually readable. You know, with tabbing and that sort of thing.

    The simplest way to see if two objects collide is to see if their distances are less than a certain value. When you consider two circles, that value is the average of the two radii. When you consider two rectangles, that value is the vertical distance being less than the height of the smaller one, or the horizontal distance being less than the width of the smaller one (distances for squares are measured from top left). A circle-rectangle collision is similar, and I leave that up to you to figure out.

    (Obviously this simple way fails to work for more intricate shapes, though you can always approximate the shapes as rectangles or circles.)
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0
    Thank you for the answer, but my problem is different.
    I know how to define the collision,
    but I can't reach the specific brick that collides with the ball.

    If you'll look I defined the brick in two for loops one vertical and one horizontal and in every phase I added a brick and then I got the formation of the bricks.

    but now I don't know how to access the specific brick the the ball collide with.

    Maybe I should have defined the setup of the bricks different...
    but I don't know how... Maybe you have a suggestion???

    Would it be more comftable if I send the code different I didn't know how?
    Maybe as javadoc?
  6. #4
  7. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    Here's a suggestion: use java the way it's meant to be used: classes.

    If you had defined each individual component as its own class, this would be a lot easier to figure out. Block, Paddle, Ball can all extend a superclass or implement an interface. Each has its own methods for detecting collisions with the others.

    Here's what you need to do. You need to read up on OOP design, this is incredibly important. You need to read up on java code conventions to make your code readable. You need to redo the project from scratch because, frankly, what you've done is a good first learning effort but nothing more than that; it will either never be functional, or it will be functional but not something you'll actually ever want to look at. This may sound harsh but please believe me when I say your program is really not salvageable.

    java Code:
    public interface BreakoutObject {
         public boolean collidesWith(BreakoutObject other);
         public void move();
         ...
    }

    java Code:
    public class Block implements BreakoutObject {
         private int x, y, width, height;
         ...
    }

    java Code:
    public class Paddle implements BreakoutObject {
         private int x, y, width, height;
         private double speed; // pixels per second, negative is left
         ...
    }


    You get the point.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents

IMN logo majestic logo threadwatch logo seochat tools logo