I am trying to build the bricks for the breakout assignment and I have a weird bug. I was trying to work out the spacing between the bricks and the it's fine EXCEPT when there are 10 columns. 9 columns is fine, 11 columns is fine, but 10 columns creates a larger than normal space at the right side of the window. Is this a bug in software or am I coding something weird?
Thanks!
Here is my code and screenshots
#include<spl/gwindow.h>
#include<spl/gobjects.h>
// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400
// 1st brick location
#define BRICK_X 3
#define BRICK_Y 100
// height and width of brick
#define BRICK_W ((WIDTH - (SPACE * (COLS + 1))) / COLS)
#define BRICK_H ((HEIGHT / 10) / ROWS)
// space size
#define SPACE 3
// number of rows of bricks
#define ROWS 5
// number of columns of bricks
#define COLS 11
int main(void)
{
GWindow window = newGWindow(WIDTH, HEIGHT);
for(int j = 0; j < ROWS; j++)
{
for(int i = 0; i < COLS; i++)
{
GRect brick = newGRect(BRICK_X + (i * BRICK_W) + (SPACE * i), (BRICK_Y + (j * BRICK_H) + (SPACE * j)), BRICK_W, BRICK_H);
setColor(brick, "Red");
add(window, brick);
}
}
return 0;
}
update:
So I took your advice and added 1/2 spaces in the beginning and end of the row so that # of spaces equal # of columns. I think the whole number thing is resolved but I'm still getting unequal spacing at the ends.
#include<spl/gwindow.h>
#include<spl/gobjects.h>
#include<spl/gevents.h>
// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400
// 1st brick location
#define BRICK_X (SPACE / 2)
#define BRICK_Y 100
// height and width of brick
#define BRICK_W ((WIDTH - (SPACE * (COLS))) / COLS)
#define BRICK_H ((HEIGHT / 10) / ROWS)
// space size
#define SPACE 6
// number of rows of bricks
#define ROWS 5
// number of columns of bricks
#define COLS 9
int main(void)
{
GWindow window = newGWindow(WIDTH, HEIGHT);
for(int j = 0; j < ROWS; j++)
{
for(int i = 0; i < COLS; i++)
{
GRect brick = newGRect(BRICK_X + (BRICK_W * i) + (SPACE * i), (BRICK_Y + (BRICK_H * j) + (SPACE * j)), BRICK_W, BRICK_H);
setColor(brick, "Red");
add(window, brick);
}
}
return 0;
}