Skip to content

Instantly share code, notes, and snippets.

@NatashaTheRobot
Created November 9, 2011 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NatashaTheRobot/1350610 to your computer and use it in GitHub Desktop.
Save NatashaTheRobot/1350610 to your computer and use it in GitHub Desktop.
This is the solution to step 1 of the Stanford CS106A Breakout game assignment, setting up the bricks.
//adding an individual brick object
private GRect brick;
//drawing all the bricks necessary for the game
private void drawBricks(double cx, double cy) {
/*need to have several columns in each row
* so there need to be two for loops,
* one for loop for the rows and one for loop for the columns.
*/
for( int row = 0; row < NBRICK_ROWS; row++ ) {
for (int column = 0; column < NBRICKS_PER_ROW; column++) {
/* To get the x coordinate for the starting width:
* start at the center width,
* subtract half of the bricks (width) in the row,
* subtract half of the separations (width) between the bricks in the row,
* now you're at where the first brick should be,
* so for the starting point of the next bricks in the column, you need to:
* add a brick width
* add a separation width
*/
double x = cx - (NBRICKS_PER_ROW*BRICK_WIDTH)/2 - ((NBRICKS_PER_ROW-1)*BRICK_SEP)/2 + column*BRICK_WIDTH + column*BRICK_SEP;
/* To get the y coordinate of the starting height:
* start at the given length from the top for the first row,
* then add a brick height and a brick separation for each of the following rows
*/
double y = cy + row*BRICK_HEIGHT + row*BRICK_SEP;
brick = new GRect( x , y , BRICK_WIDTH , BRICK_HEIGHT );
add (brick);
brick.setFilled(true);
//Setting colors depending on which row the bricks are in
if (row < 2) {
brick.setColor(Color.RED);
}
if (row == 2 || row == 3) {
brick.setColor(Color.ORANGE);
}
if (row == 4 || row == 5) {
brick.setColor(Color.YELLOW);
}
if (row == 6 || row == 7) {
brick.setColor(Color.GREEN);
}
if (row == 8 || row == 9) {
brick.setColor(Color.CYAN);
}
}
}
@NatashaTheRobot
Copy link
Author

Can you add a lot more comments to your code? I did this a while ago, so I'm not sure what your code is for...

@NatashaTheRobot
Copy link
Author

Now, it's much better :) Make sure to add good comments to your code moving forward so anyone reading it will understand what you're doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment