Results 1 to 1 of 1
Thread: Stanford Breakout Exercise
- 07-27-2012, 02:08 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 33
- Rep Power
- 0
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 02:24 PM. Reason: added code tags
Similar Threads
-
Stanford CS106a GraphicsHeirarchy
By Newbieprogrammer in forum New To JavaReplies: 0Last Post: 07-15-2012, 07:37 PM -
Stanford cs106a
By D.good in forum IntroductionsReplies: 1Last Post: 02-04-2012, 06:18 PM -
BufferedReader error (Stanford tutorial)
By Jossos in forum New To JavaReplies: 4Last Post: 11-27-2011, 12:01 AM -
CS106A Stanford University
By Learning Java in forum New To JavaReplies: 116Last Post: 07-09-2011, 04:43 PM -
Class exercise CS106A (Stanford university)
By ccie007 in forum New To JavaReplies: 2Last Post: 09-11-2010, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks