Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2012
    Posts
    33
    Rep Power
    0

    Default Stanford Breakout Exercise

    Hiya

    I'm doing the Stanford cs106a course online and am doing an exercise where you code a game of breakout. I'm not stuck- what I've done so far satisfies the first part, which is just to set up the board and the rows of bricks. But what I've done seems very clumsy. I've used a while loop to repeat code several times (most of the following code is the repeated loop), once for each differently colored row of bricks. My first thought was to get the createBrick method (at the bottom) to count the bricks and return different colors. But there doesn't seem to be any way to set the call parameters to accept color. So in the end I did this. Is this as inelegant as I think it is? Thanks! Robbie
    Java Code:
    /** Runs the Breakout program. */
    	public void run() {
    		setUpBreakout();
    	}
    	
    	private void setUpBreakout() {
    		setUpCanvas(APPLICATION_WIDTH / 2, APPLICATION_HEIGHT / 2);
    		setUpBricks();
    	}
    	private void setUpCanvas(double cx, double cy) {
    			double x = cx - WIDTH / 2;
    			double y = cy - HEIGHT /2;
    			GRect canvas = new GRect(x, y, WIDTH, HEIGHT);
    			canvas.setFilled(false);
    			add(canvas);
    	}
    	
    	private void setUpBricks() {
    		double y = BRICK_Y_OFFSET;
    		int numOfRows = NBRICK_ROWS;
    		int numBricks = NBRICKS_PER_ROW;
    		while (numOfRows > 8) {
    			double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
    			for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    			add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.RED));
    			x += BRICK_WIDTH + BRICK_SEP;
    			}
    			numBricks--;
    			numOfRows--;
    			y += BRICK_HEIGHT + BRICK_SEP;
    		}
    		while (numOfRows > 6) {
    			double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
    			for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    			add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.ORANGE));
    			x += BRICK_WIDTH + BRICK_SEP;
    			}
    			numBricks--;
    			numOfRows--;
    			y += BRICK_HEIGHT + BRICK_SEP;
    		}
    		while (numOfRows > 4) {
    			double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
    			for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    			add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.YELLOW));
    			x += BRICK_WIDTH + BRICK_SEP;
    			}
    			numBricks--;
    			numOfRows--;
    			y += BRICK_HEIGHT + BRICK_SEP;
    		}
    		while (numOfRows > 2) {
    			double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
    			for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    			add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.GREEN));
    			x += BRICK_WIDTH + BRICK_SEP;
    			}
    			numBricks--;
    			numOfRows--;
    			y += BRICK_HEIGHT + BRICK_SEP;
    		}
    		while (numOfRows > 0) {
    			double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
    			for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    			add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.CYAN));
    			x += BRICK_WIDTH + BRICK_SEP;
    			}
    			numBricks--;
    			numOfRows--;
    			y += BRICK_HEIGHT + BRICK_SEP;
    		}
    	}
    	
    	private GRect createBrick(double x, double y, int width, int height, Color color) {
    		GRect brick = new GRect(x, y, width, height);
    		brick.setColor(color);
    		brick.setFilled(true);
    		return brick;
    	}
    }
    Last edited by Norm; 07-27-2012 at 03:24 PM. Reason: added code tags

Similar Threads

  1. Stanford CS106a GraphicsHeirarchy
    By Newbieprogrammer in forum New To Java
    Replies: 0
    Last Post: 07-15-2012, 08:37 PM
  2. Stanford cs106a
    By D.good in forum Introductions
    Replies: 1
    Last Post: 02-04-2012, 07:18 PM
  3. BufferedReader error (Stanford tutorial)
    By Jossos in forum New To Java
    Replies: 4
    Last Post: 11-27-2011, 01:01 AM
  4. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 05:43 PM
  5. Class exercise CS106A (Stanford university)
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 09-11-2010, 02:47 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
  •