📄 fireline.cpp
字号:
#include <airobot/cpp/SimpleRobot.hpp>
/**
* 这个机器人演示了如何用迭代的方法打击走直线的机器人
* @author xiemin
*/
class FireLine : public SimpleRobot
{
private:
//开火时的炮弹能量
static const double POWER;
public:
void onTick(TickAction* action)
{
Bot* bot = getFirstOpponent();
if(bot==NULL) return;
long time = 1;
while(true)
{
//计算在time时间后对手的位置
Point2D next= Math::nextPoint(bot->getLocation(), bot->getHeading(), bot->getVelocity()*time);
//判断子弹在time时间后能否打到在next位置的对手
double distance = Math::distance(next, getLocation());
if(distance/getBulletVelocity(POWER)<=time)
{
//能打到,迭代结束
fire(next, POWER); //开火
return;
}
else time++; //进入下一次迭代
}
}
};
//开火时的炮弹能量
const double FireLine::POWER = 2;
//启动机器人程序
int main(int argC, char* argV[])
{
Robot* robot = new FireLine();
return startup(argC, argV, robot);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -