首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个循环不能工作?(BGI形状)

为什么这个循环不能工作?(BGI形状)
EN

Stack Overflow用户
提问于 2013-11-28 18:24:26
回答 1查看 49关注 0票数 0

我试图创造形状,使他们反弹的边缘,但我的if循环似乎不工作,我不知道为什么。这条线移动形状:

代码语言:javascript
复制
line1.x  += line1.xvelocity;
line1.y  += line1.yvelocity;
line1.x2 += line1.xvelocity;
line1.y2 += line1.yvelocity;

我想保持640x480的形状,所以我写了

代码语言:javascript
复制
if (line1.x >> 640) line1.xvelocity *= (-1);
if (line1.x << 0) line1.xvelocity *= (-1);
if (line1.y >> 480) line1.yvelocity *= (-1);
if (line1.y << 0) line1.yvelocity *= (-1);
if (line1.x2 >> 640) line1.xvelocity *= (-1);
if (line1.x2 << 0) line1.xvelocity *= (-1);
if (line1.y2 >> 480) line1.yvelocity *= (-1);
if (line1.y2 << 0) line1.yvelocity *= (-1);

我尝试使用||,或者仅仅使用x,yx2,y2坐标。有什么帮助吗?谢谢。

代码语言:javascript
复制
class Line: public GenericShape
{
public:
    int x2, y2;
    Line();
    Line(int x_in, int y_in, int color_in, int xvel, int yvel, int x2_in, int y2_in)
        : GenericShape(x_in, y_in, color_in, xvel, yvel),
          x2(x2_in),
          y2(y2_in)
    {}
    void draw() const;
};

Line line1(50, 150, 4, 2, -3, 180, 60); // xvelocity=2 yvelocity =-3
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-28 18:34:50

条件在

代码语言:javascript
复制
if (line1.x >> 640)

总是为零,因为'>>‘是按位向右移位的操作,而不是比较。它相当于第1.x行除以2^640。

把它改成

代码语言:javascript
复制
if(line1.x >= 640)

在其他条件下,“<<”改为“<”,“>>”也改为“>=”。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20272691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档