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");
addToGame(brick, 20, 10);
any ideas on how i can loop it to make 2 rows that have 11 bricks.
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");
addToGame(brick,20+j,10+i);
}
}
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?
Re: Need a loop for rows and columns
private static final int BRICK_HEIGHT = 8;
private static final int BRICK_WIDTH = 16;
Re: Need a loop for rows and columns
just multiply j by BRICK_WIDTH and i by BRICK_HEIGHT
Re: Need a loop for rows and columns
yup that work and i have assigned points to the colors.
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");
addToGame(brick,20+j*BRICK_WIDTH,10+i*BRICK_HEIGHT );
}
}
at the moment its making all the bricks red.
i have four colors red, blue, green, yellow.
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
Re: Need a loop for rows and columns
That doesnt seem to be working for me...not sure what i did wrong