我无法让我的移动点程序正常工作。我有一个程序,可以让点向右移动,但是我如何让它们循环回来,让它们看起来像是在连续移动,而不是离开页面呢?我需要编辑的代码块如下所示。我希望有人能帮助我,并再次感谢提前!
void advanceDots(XPoint dots[], unsigned num_dots, XWindowAttributes &w){
int s = num_dots;
for(s = 0; s < num_dots; s++){
if(s < num_dots){
dots[s].x++;
}
}
}
发布于 2017-01-28 02:45:18
如果您当前正在“左移”vs“右移”,则需要另一个变量来跟踪。然后当你到达一个边界时交换。
如下所示:
bool isMovingLeft = false;
void advanceDots(XPoint dots[], unsigned num_dots, XWindowAttributes &w)
{
if (isMovingLeft && (dots[0].x <= 0))
{
isMovingLeft = false;
}
else if (!isMovingLeft && dots[num_dots-1] >= w.width)
{
isMovingLeft = true;
}
int increment = isMovingLeft ? -1 : 1;
int s = num_dots;
for(s = 0; s < num_dots; s++)
{
dots[s].x += increment;
}
}
发布于 2017-01-28 02:40:44
通常情况下,位置是有某种边界的。我假设它是用XWindowAttributes编写的,只是为了给你举个例子:
要让这些点“回到”开头,而不是“移出页面”,你可以像这样将它们放回原处:
//I dont know the structure of XWindowAttributes so i will define one just for the example!
struct XWindowAttributes
{
int x_size; // the maximal size of the window in the x direction
};
void advanceDots(XPoint dots[], unsigned num_dots, XWindowAttributes &w)
{
for(int s = 0; s < num_dots; s++)
{
XPoint& dot = dots[s];
dots.x++;
if(dots.x >= w.x_size)
{
dots.x = 0;
}
}
}
https://stackoverflow.com/questions/41905774
复制相似问题