molebouncer.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 32 行

CPP
32
字号
class MoleBouncer : public Bouncer
{
  public:
    MoleBouncer(Shape& s, double angle, double v)
     : Bouncer(s,angle,v)
   {  }
    virtual void update(AnimatedCanvas& ac)
    {
        RandGen rgen;
        Iterator<Shape> it(ac.makeIterator());
        bool collided = false;               // collision or still bouncing?
        Point p = getLocation();
        double angle = getAngle();
        for(it.Init(); it.HasMore(); it.Next())
        {   // check for collision, but not with myself
            if (it.Current().id() != this->id() && it.Current().overlaps(*this))
            {   ac.removeShape(this);
                ac.addShape(
                  new MoleBouncer(
                      CircleShape(Point(rgen.RandReal(0,ac.width()/10),
                                        rgen.RandReal(0,ac.height()/10)),
                      RADIUS, CanvasColor::BLUE), 2*PI - angle, 4));
                collided = true;
                break;
            }
        }
        if (!collided)           // no collision, update
        {   Bouncer::update(ac); 
        }
    }
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?