0

I'm working on Breakout and making good progress. I'm at the point where I'd like to add color to the bricks. I've implemented an array of colors that the 'if' loop iterates through to add color. The bricks color perfectly, however the script seems to mess up the paddle initialization.

If I do not add color to the bricks, then the paddle initializes. If I try and add color to the bricks, the paddle does not initialize.

Any idea what might be happening?

Regards!

Alex

// Initializes window with a grid of bricks.
void initBricks(GWindow window)
{
    for (int r = 0; r < ROWS; r++)
    {
        for (int c = 0; c < COLS; c++)
        {
        char *colors[] = {"00FFFF", "FF00FF","FFFF00", "000000"};

        GRect initBricks = newGRect((2 + (c * 40)), (50 + (r * 15)), 36, 10);
        setColor(initBricks, colors[r]);
        setFilled(initBricks, true);

        add(window, initBricks);
        }
    }     
}

// Instantiates paddle in bottom-middle of window.
GRect initPaddle(GWindow window)
{
    GRect initPaddle = newGRect(170, 540, 60, 12);

    setColor(initPaddle, "000000");
    setFilled(initPaddle, true);

    add(window, initPaddle);

    while (true)
    {
        GEvent event = getNextEvent(MOUSE_EVENT);

        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED) 
            {
                double x = getX(event) - 30;
                setLocation(initPaddle, x, 540);
            }
        }
    }

    return NULL;
}
2
  • Could you post the code where you make calls to these functions? Also, did the paddle work before you added code to instantiate the bricks? Your mouse event code should be within your main(). The initPaddle will be called only once simply to draw the paddle. Commented Aug 2, 2015 at 18:28
  • Hello! Yes, the paddle works when I instantiate the bricks. The paddle does not show when i try to add color to the bricks. I will update the page with the full code.
    – Alex C
    Commented Aug 3, 2015 at 0:38

1 Answer 1

1

Without seeing the value of ROWS, I suspect that the problem is an interaction between the number of rows and the number of colors in your code. If you have 5 or more rows of bricks, it is choking when it hits the 5th row because your color array only has 4 colors or elements. When r in your outer loop hits 4, it's trying to access colors[4], which doesn't exist. Instead of throwing an error, it just locks up. When there are only 4 rows of bricks, it works fine. If ROWS is 5, just add another color.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Thank you! Yes, it appears I was only accounting for four rows when ROWS was taking into account five rows!
    – Alex C
    Commented Aug 3, 2015 at 0:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .