⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wrap.cpp

📁 AI-CODE坦克机器人 《C++语言学习利器 —AI-CODE坦克机器人》-杜飞雪-源代码
💻 CPP
字号:
#include <airobot/cpp/SimpleRobot.hpp>

/**
 * 围绕对手作来回的移动
 * @author xiemin
 */
class Wrap : public SimpleRobot
{
	private:
		//在一个方向上最大的移动时间
		static const long MAX_TIME;
		//移动的方向,这个变量的值只能取1和-1,1表示前进,-1表示后退
		int direction;
		//当前在一个方向上的移动时间
		long time;
		
	public:
		Wrap(void)
		{
			direction = 1;
			time = 0;
		}
		
		void onTick(TickAction* action)
		{
			setDirection();
			doMove();
			doTurn();
		}
		
	private:
		
		/**
		 * 设置移动的方向
		 */
		void setDirection(void)
		{
			time++;
			if(time>MAX_TIME)
			{
				direction *= -1; //变换移动方向
				time = 0;
			}
		}
		
		/**
		 * 执行移动
		 */
		void doMove(void)
		{
			move(10*direction);
		}
		
		/**
		 * 执行转动
		 */
		void doTurn(void)
		{
			Point2D center = getCenter();
			double lineHeading = Math::heading(getLocation(), center);
			double headingTo = lineHeading + Math::PI/2;
			double bearing = Math::bearing(headingTo, getHeading());
			turn(bearing);
		}
		
		/**
		 * 得到圆心位置
		 */
		Point2D getCenter(void)
		{
			Bot* bot = getFirstOpponent();
			if(bot!=NULL) return bot->getLocation();
			else return Point2D(getCourtWidth()/2, getCourtHeight()/2);
		}
};

const long Wrap::MAX_TIME = 35;
		

//启动机器人程序
int main(int argC, char* argV[])
{
	Robot* robot = new Wrap();
	return startup(argC, argV, robot);
}

⌨️ 快捷键说明

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