C中的断砖器-增加砖的行数
我正在尝试使用C和斯坦福便携式库SPL重新创建brickbreaker。我的initBricks函数的目标是添加5行砖,每行10列,总共50块砖。当我运行此代码时,我的窗口只有一行10块砖。由于某些原因,它不会输出其他4行。我看不出我的代码哪里出错了。我正在增加y坐标0,在创建每一行后,0是窗口左上角的40C中的断砖器-增加砖的行数,c,function,for-loop,computer-science,C,Function,For Loop,Computer Science,我正在尝试使用C和斯坦福便携式库SPL重新创建brickbreaker。我的initBricks函数的目标是添加5行砖,每行10列,总共50块砖。当我运行此代码时,我的窗口只有一行10块砖。由于某些原因,它不会输出其他4行。我看不出我的代码哪里出错了。我正在增加y坐标0,在创建每一行后,0是窗口左上角的40 // number of rows of bricks #define ROWS 5 // number of columns of bricks #define COLS 10 //
// number of rows of bricks
#define ROWS 5
// number of columns of bricks
#define COLS 10
// height and width of bricks
#define BRICK_H 7.5
#define BRICK_W 35
// initializes window with grid of bricks
void initBricks(GWindow window)
{
// set initial brick coordinates
int x_coord = 2;
int y_coord = 10;
for (int i = 0; i < ROWS; i++)
{
// Create 10 columns of bricks
for (int j = 0; j < COLS; j++)
{
// create a brick
GRect brick = newGRect(x_coord, y_coord, BRICK_W, BRICK_H);
setFilled(brick, true);
setColor(brick, "RED");
add(window, brick);
// increment x coordinate for column spacing
x_coord += 40;
}
// increment y coordinate for row spacing
y_coord += 40;
}
}
任何帮助都将不胜感激 您需要为每行重置X坐标
for (int i = 0; i < ROWS; i++)
{
// Create 10 columns of bricks
int x_coord = 2; // <---- move line down to here
for (int j = 0; j < COLS; j++)
{
...
您需要重置每行的X坐标
for (int i = 0; i < ROWS; i++)
{
// Create 10 columns of bricks
int x_coord = 2; // <---- move line down to here
for (int j = 0; j < COLS; j++)
{
...
您不会在外部for循环中重置x_coord变量,因此在第一行的末尾,x_coord为202,这就是它开始写入下一行的位置,该行可能在您的窗口之外。简单地加上x_坐标=2;在y_坐标+=40之后;你应该把它修好
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
// create a brick
GRect brick = newGRect(x_coord, y_coord, BRICK_W, BRICK_H);
setFilled(brick, true);
setColor(brick, "RED");
add(window, brick);
// increment x coordinate for column spacing
x_coord += 40;
}
// increment y coordinate for row spacing
y_coord += 40;
//reset c coordinate
x_coord = 2;
}
您不会在外部for循环中重置x_coord变量,因此在第一行的末尾,x_coord为202,这就是它开始写入下一行的位置,该行可能在您的窗口之外。简单地加上x_坐标=2;在y_坐标+=40之后;你应该把它修好
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
// create a brick
GRect brick = newGRect(x_coord, y_coord, BRICK_W, BRICK_H);
setFilled(brick, true);
setColor(brick, "RED");
add(window, brick);
// increment x coordinate for column spacing
x_coord += 40;
}
// increment y coordinate for row spacing
y_coord += 40;
//reset c coordinate
x_coord = 2;
}
如前所述,问题是X坐标。您可以通过将X、Y计算移动到for循环中来修复它
如前所述,问题是X坐标。您可以通过将X、Y计算移动到for循环中来修复它
您应该在外循环体中初始化y坐标。 因为每次到达下一行y坐标时,都要返回其初始位置
// number of rows of bricks
#define ROWS 5
// number of columns of bricks
#define COLS 10
// height and width of bricks
#define BRICK_H 7.5
#define BRICK_W 35
// initializes window with grid of bricks
void initBricks(GWindow window)
{
// set initial brick coordinates
int x_coord = 2;
for (int i = 0; i < ROWS; i++)
{
int y_coord = 10;
// Create 10 columns of bricks
for (int j = 0; j < COLS; j++)
{
// create a brick
GRect brick = newGRect(x_coord, y_coord, BRICK_W, BRICK_H);
setFilled(brick, true);
setColor(brick, "RED");
add(window, brick);
// increment x coordinate for column spacing
x_coord += 40;
}
// increment y coordinate for row spacing
y_coord += 40;
}
}
您应该在外循环体中初始化y坐标。 因为每次到达下一行y坐标时,都要返回其初始位置
// number of rows of bricks
#define ROWS 5
// number of columns of bricks
#define COLS 10
// height and width of bricks
#define BRICK_H 7.5
#define BRICK_W 35
// initializes window with grid of bricks
void initBricks(GWindow window)
{
// set initial brick coordinates
int x_coord = 2;
for (int i = 0; i < ROWS; i++)
{
int y_coord = 10;
// Create 10 columns of bricks
for (int j = 0; j < COLS; j++)
{
// create a brick
GRect brick = newGRect(x_coord, y_coord, BRICK_W, BRICK_H);
setFilled(brick, true);
setColor(brick, "RED");
add(window, brick);
// increment x coordinate for column spacing
x_coord += 40;
}
// increment y coordinate for row spacing
y_coord += 40;
}
}
我能理解x_coord+=40;理想情况下x_coord+=砖+砂浆;但不是y_coord+=40;。如果您从未重置xcord,我怀疑剩余层上的所有砖块都被放置在您的视图之外port@RyanS抱歉,我同时回答了。谢谢各位,我重新编译了x坐标,它是在第一个for循环中启动的,工作起来就像一个charmI可以理解x_坐标+=40;理想情况下x_coord+=砖+砂浆;但不是y_coord+=40;。如果您从未重置xcord,我怀疑剩余层上的所有砖块都被放置在您的视图之外port@RyanS抱歉,我同时回答了。谢谢各位,我重新编译了x coord,它是在第一个for循环中启动的,工作起来像个charm,这是一个小错误,但我看不到它!休息被低估了,哈哈。谢谢@Dom非常欣赏这样一个小错误,但我看不出来!休息被低估了lol。非常感谢@Dom。事实上,情况正好相反。第一个for循环内的init x_坐标和第二个for循环外的init y_坐标实际上是另一种方式。在第一个for循环内部初始化x_坐标,在两个for循环外部初始化y_坐标