Results 1 to 5 of 5

Thread: C++ Breakout clone problem

  1. #1
    it's not fun, don't do it Moon Rabbits's Avatar
    Join Date
    Mar 2005
    Posts
    5,582

    Default C++ Breakout clone problem

    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?

  2. #2

    Default

    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
    Last edited by robfinalfantasy; 02-03-2007 at 05:48 PM.

  3. #3
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Quote Originally Posted by Moon Rabbits View Post
    However, when I run the program only 9 columns are drawn when I call the function to draw my bricks. Why is this?
    Code:
    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.
    Figaro Castle

  4. #4

    Default

    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:
    Code:
    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.

  5. #5
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Quote Originally Posted by robfinalfantasy View Post
    NUM_COLUMNS is set to 10 not 9.
    Doh, I missed the "respectively".
    Figaro Castle

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •