PDA

View Full Version : C++ Breakout clone problem



Moon Rabbits
02-03-2007, 04:47 PM
I'm programming a breakout clone in C++ using Allegro to help me framiliarize myself with the Allegro graphics library, but I'm having a few problems.

The bricks are initialized like this:


for(int i = 0; i < NUM_COLUMNS; i++)
{
for(int j = 0; j < NUM_ROWS; j++)
{
bricks[i][j].x = BRICK_WIDTH + (i*BRICK_WIDTH) + (i*3);
bricks[i][j].y = BRICK_HEIGHT + (j*BRICK_HEIGHT) + (j*5);

bricks[i][j].col[0] = 255;
if(j < (COL_CHANGE_INTERVAL*2))
{bricks[i][j].col[0]=0; bricks[i][j].col[1]=255;}
if(j < COL_CHANGE_INTERVAL)
{bricks[i][j].col[0]=0; bricks[i][j].col[1]=0; bricks[i][j].col[2]=255;}
}
}

(Ignore the bolded stuff) Where NUM_ROWS and NUM_COLUMNS are defined as 9 and 10 respectively. BRICK_WIDTH and BRICK_HEIGHT are defined as 50 and 15 respectively.

However, when I run the program only 9 columns are drawn when I call the function to draw my bricks. Why is this?

robfinalfantasy
02-03-2007, 05:35 PM
How did you define the vector 'bricks'? I noticed that you are using the variable i in the for loop to run through the columns. The problem is that the first set of brackets is used for rows not columns. I get the feeling you mixed them up.


The vector should have been defined:

brick bricks[NUM_ROWS][NUM_COLUMNS];

not

brick bricks[NUM_COLUMNS][NUM_ROWS];

Of course this does not matter if you intentionally decided to use the rows constant to represent columns and the columns constant to represent rows, but somehow I doubt that was the case.

If you're not sure about what I said try experimenting with the following matrix

int matrix[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
//Think of the array as the following matrix
// 1 2 3
// 4 5 6
// 7 8 9
cout < < matrix[0][1]; //prints 2
cout < < matrix[1][2]; //prints 6
cout < < matrix[2][2]; //prints 9
//Thus matrix[i][j] refers to the integer element found at row i and column j

Flying Mullet
02-06-2007, 12:42 PM
However, when I run the program only 9 columns are drawn when I call the function to draw my bricks. Why is this?


for(int i = 0; i < NUM_COLUMNS; i++)
If NUM_COLUMNS is set to 9, then 9 columns is all you're going to get. Remember, it's while i is <i>less than</i> 9. Thus the loop will be processed while i is equal to 0, 1, 2, 3, 4, 5, 6, 7 and 8, a total of 9 times.

Also, if you want to show code on the page, wrapping it in [c<i></i>ode] tags rather than [qu<i></i>ote] tags works rather nicely.

robfinalfantasy
02-06-2007, 04:58 PM
NUM_COLUMNS is set to 10 not 9. I'm still convinced that she mixed up the variables i & j.

The loops should be the following:

for(int i = 0; i < NUM_ROWS; i++)
for(int j = 0; j < NUM_COLUMNS; i++)
more code here

As long as you plan on using i in the first set of brackets you need to remember that it refers to the rows therefore in the for statement i needs to be compared with NUM_ROWS not NUM_COLUMNS.

Flying Mullet
02-06-2007, 05:05 PM
NUM_COLUMNS is set to 10 not 9.
Doh, I missed the "respectively". :bigsmile: