# Need a loop for rows and columns

• October 8th, 2009, 11:22 PM
Ceasar
Need a loop for rows and columns
hey guys i need help making a loop to create bricks in my breakout game.

here is the code to make one brick:

//creates a brick in breakoutWourld.
brick = new Brick(1,"red");

any ideas on how i can loop it to make 2 rows that have 11 bricks.
• October 8th, 2009, 11:56 PM
helloworld922
Re: Need a loop for rows and columns
for loops are perfectly suited for this problem.

Code :

for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 11; j++)
{
brick = new Brick(1,"red");
}
}

• October 9th, 2009, 12:05 AM
Ceasar
Re: Need a loop for rows and columns
Umm it creates the blocks but they seem to be stacked on each other.

ummis it possible to use the brick height and width in the code so they dont stack on each other?
• October 9th, 2009, 12:06 AM
Ceasar
Re: Need a loop for rows and columns
private static final int BRICK_HEIGHT = 8;
private static final int BRICK_WIDTH = 16;
• October 9th, 2009, 12:24 AM
helloworld922
Re: Need a loop for rows and columns
just multiply j by BRICK_WIDTH and i by BRICK_HEIGHT
• October 9th, 2009, 12:50 AM
Ceasar
Re: Need a loop for rows and columns
yup that work and i have assigned points to the colors.
• October 9th, 2009, 01:01 AM
Ceasar
Re: Need a loop for rows and columns
Ok here is another question.

If i used the code to make all the bricks of the game how can i change the colours between them?

for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 11; j++)
{
brick = new Brick(1,"red");
}
}

at the moment its making all the bricks red.

i have four colors red, blue, green, yellow.
• October 9th, 2009, 03:55 AM
Freaky Chris
Re: Need a loop for rows and columns
Code :

if( (i*11 + j) % 4 == 0) //red
else if( (i*11 + j) % 3 == 0) //blue
else if( (i*11 + j) % 2 == 0) //green
else //yellow

Or you could use the inlining method, but it would get messy lol

Chris
• October 9th, 2009, 05:47 PM
Ceasar
Re: Need a loop for rows and columns
That doesnt seem to be working for me...not sure what i did wrong