机器人足球代码1_第1页
机器人足球代码1_第2页
机器人足球代码1_第3页
机器人足球代码1_第4页
机器人足球代码1_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、足球机器人程序代码 /应用程序模板 #include <airobot/c/ActionListeningRobot.h>/判断自身与球的方位是否危险bool AzimuthDanger()    double ballX = getBallX();    double myX = getX();    double radius = getBallRadius()+getRadius();        if(getAttack()

2、>0 && myX+radius*2>ballX) return true;    if(getAttack()<0 && myX-radius*2<ballX) return true;            if( (getAttack()>0 && getBallX()<getCourtWidth()/2) | (getAttack()<0 && getBallX(

3、)>getCourtWidth()/2) )            if(getBallY()>getCourtHeight()/2)&&(getY()>getBallY()return true;        if(getBallY()<getCourtHeight()/2)&&(getY()<getBallY()return true;  

4、          return false;/判断球的运动方向 返回true 危险bool JudgeDanger()double ballX = getBallX();double ballY = getBallY();double goalMin = getCourtHeight()/2-getGoalWidth()/2;double goalMax = getCourtHeight()/2+getGoalWidth()/2;double b = ballY - tan(getBallHeading()*ballX;i

5、f(getAttack()>0 && getBallHeading()>PI/2 && getBallHeading()<PI*3/2)    if(goalMin<50*tan(getBallHeading() + b && goalMax>50*tan(getBallHeading() + b)return true;    else return false;else if(getAttack()<=0 && (getBallHe

6、ading()>=0 && getBallHeading()<PI/2          | getBallHeading()<2*PI && getBallHeading()>3*PI/2)          if(goalMin<tan(getBallHeading()*(getCourtWidth() - 50) + b && tan(getB

7、allHeading()*(getCourtWidth() - 50) + b<goalMax)      return true; else return false;else return false;/预测足球在times个单位时间后的位置void Next(int times, double* nextX, double* nextY)double velocity = getBallVelocity();double heading = getBallHeading();double acc = -getBallNegative

8、Acceleration();*nextX = getBallX();*nextY = getBallY();for (int i = 0; i < times; i+)   if(velocity=0) break;      nextPoint(*nextX, *nextY, heading, velocity, nextX, nextY);   / 摩擦减速   if (velocity > 0) velocity = fmax(0, velocity+acc);&#

9、160;  else if (velocity < 0) velocity = fmin(0, velocity -acc);     / 撞墙检测   if (*nextX) < 0 | (*nextX) > getCourtWidth()       heading = PI - heading;    modifyInCourt(nextX, nextY, getBallRadius();   

10、0;  if (*nextY) < 0 | (*nextY) > getCourtHeight()       heading = -heading;    modifyInCourt(nextX, nextY, getBallRadius();   / 跌代计算球的运行线路,计算未来的击球点void OptimumPosition(double* hitedX ,double* hitedY )    *hitedX = getBallX()

11、;    *hitedY = getBallY();    for (int i = 0; i < 200; i+)            double nextX, nextY;        Next(i, &nextX, &nextY);        if (dista

12、nce(nextX, nextY, getX(), getY() < i * getMaxMoveVelocity()                    hitedX = &nextX;            hitedY = &nextY;   

13、60;        break;            /1v1得到敌机坐标double getBotX()    int id = getAttack()>0? 1:0;    struct Bot bot;    getBotById(id, &bot);    return bot.x;

14、double getBotY()    int id = getAttack()>0? 1:0;    struct Bot bot;    getBotById(id, &bot);    return bot.y;double getBotVelocity()    int id = getAttack()>0? 1:0;    struct Bot bot;   

15、; getBotById(id, &bot);    return bot.moveVelocity;bool BotBallBotDistance()double MDistance = distance(getX(), getY(), getBallX(), getBallY();double DDistance = distance(getBotX(), getBotY(), getBallX(), getBallY();if(MDistance<DDistance)return true;return false;/将球踢向目标void Sh

16、oot(double targetX, double targetY)double ballX, ballY;OptimumPosition(&ballX,&ballY);double Rn=0.93;Rn= (AzimuthDanger() && getBallVelocity()>getMaxMoveVelocity()/2) ? Rn=1.1 : Rn=0.93;        double MDDistance = distance(getX(), getY(), getBotX

17、(), getBotY(); /争球        if(getBallVelocity() < getMaxMoveVelocity()/2           && MDDistance<=getBallRadius()*2+getRadius()*2            &&

18、(getX()=getBotX() | getY()=getBotY() Rn=0.1;                if(getBotVelocity()=0) Rn=0.93;/R越小,抢劫能力就越强,R越大,防守能力越强double R =(getBallRadius() + getRadius()*Rn);double tangentX1, tangentX2, tangentY1, tangentY2; double bestHitX, bes

19、tHitY; double distanceToTarget = distance(targetX, targetY, getX(), getY();if(distanceToTarget<R) setMoveToward(targetX, targetY, getMaxMoveVelocity();return;double tangentBearing = acos(R/distanceToTarget);double ballToMeHeading = heading(ballX, ballY, getX(), getY();nextPoint(ballX, ballY, ball

20、ToMeHeading + tangentBearing,R,&tangentX1, &tangentY1);nextPoint(ballX, ballY,ballToMeHeading - tangentBearing,R,&tangentX2, &tangentY2);nextPoint(ballX, ballY,heading(targetX, targetY, ballX, ballY),R,&bestHitX, &bestHitY);modifyInCourt(&tangentX1, &tangentY1, getRad

21、ius();modifyInCourt(&tangentX2, &tangentY2, getRadius();modifyInCourt(&bestHitX, &bestHitY, getRadius();double xx=0;double yy=0;if (distance(getX(), getY(), bestHitX, bestHitY) < distance(getX(), getY(), tangentX1, tangentY1)      xx=bestHitX; yy=bestHitY;

22、else if (distance(bestHitX, bestHitY, tangentX1, tangentY1) < distance(bestHitX, bestHitY, tangentX2, tangentY2)      xx=tangentX1; yy=tangentY1;else       xx=tangentX2; yy=tangentY2;        /防止争边球  

23、60;     if(getX()>(getBallRadius()+getRadius()*3 | getX()<getCourtWidth()-(getBallRadius()+getRadius()*3)                    double jj= getBallRadius()-getRadius()>0 ? getBallRadius

24、()-getRadius() : getRadius()-getBallRadius();            yy = MIN(getCourtHeight()-jj, yy);            yy = MAX(jj, yy);            

25、60;          /抄近路拦截                   double MDistance = distance(getX(), getY(), getBallX(), getBallY();            dou

26、ble DDistance = distance(getBotX(), getBotY(), getBallX(), getBallY();                        if(DDistance<MDistance)            

27、60;               if(getBallVelocity()>getMoveVelocity()/2)                             

28、;        int fx;                     if(getBallY()>getCourtHeight()/2)fx=1;              

29、60;      else fx=-1;                                      yy+=fx*getBallRadius()*2;  

30、                                        /沿对门边路滚动时的防范措施       if(getBallHeading()=PI/2 | ge

31、tBallHeading()=3*PI/2)                  double bx=getAttack()>0 ? getCourtWidth()-getBallRadius() : getBallRadius();           if(getBallX()=bx && getBallVeloc

32、ity()>getMaxMoveVelocity()*2/3)                          double jj=getBallRadius();               xx=getAtt

33、ack()>0? xx-jj : xx+jj;                         setMoveToward(xx, yy, getMaxMoveVelocity();void ShootGoal() /射门    double targetX = getOpponentGoalCenterX();   &#

34、160; double targetY = getOpponentGoalCenterY();     Shoot(targetX, targetY);   void ShootAttack() /带球射门 波浪式进攻    double targetX= getAttack()>0 ? getBallX()*2 : getBallX()/2;    double targetY=0;    if( (getAttack()>0 &&a

35、mp; getBallX()<getCourtWidth()*3/5) | (getAttack()<0 && getBallX()>getCourtWidth()*2/5) )            if(getY()<getBallY()targetY=getCourtHeight();        if(getBallY()<getCourtHeight()/4)targe

36、tY=getCourtHeight();        targetY = MIN(getCourtHeight() / 2 + getGoalWidth() / 2 - getBallRadius(), targetY);targetY = MAX(getCourtHeight() / 2 - getGoalWidth() / 2 + getBallRadius(), targetY);        Shoot(targetX, targetY);&#

37、160;   else ShootGoal();/防止乌龙的策略void Owngoal()if(JudgeDanger()    double Radius = getBallRadius() + getRadius();    double Distance = distance(getX(), getY(), getBallX(), getBallY();    double ballX = getBallX();    double myX = g

38、etX();    double targetX=ballX;     double targetY=0;         if ( (getAttack()>0 && myX>ballX+Radius) | (getAttack()<0 && myX<ballX-Radius) )          if(getY()>getBall

39、Y() targetY=0;      else targetY=getCourtHeight();      Shoot(targetX, targetY);    /预测防守void ForecastGuard()double x = getAttack() > 0 ? getBallX()/2 : getCourtWidth()-(getCourtWidth()-getBallX()/2;double y = getBallY();for(int i=0

40、; i<50; i+)   double nextX, nextY;   Next(i, &nextX, &nextY);   if(getAttack()>0&&nextX<=x) | (getAttack()<0&&nextX>=x)       y = nextY;    break;   /将y坐标限制在球门的范围内y = MIN(getCourtHe

41、ight() / 2 + getGoalWidth() / 2, y);y = MAX(getCourtHeight() / 2 - getGoalWidth() / 2, y);setMoveTo(x, y);void myGuard()    double x = getAttack()>0 ? getBallX()/2 : getCourtWidth()-(getCourtWidth()-getBallX()/2;    double y = getBallY();    y = MIN(ge

42、tCourtHeight()/2+getGoalWidth()/2, y);    y = MAX(getCourtHeight()/2-getGoalWidth()/2, y);    setMoveTo(x, y);/全力防守void GoAllOutGuard()double x = getAttack()>0 ? getBallX()/2 : getCourtWidth()-(getCourtWidth()-getBallX()/2;double y = getBallY();y = MIN(getCourtHeight

43、()/2+getGoalWidth()/2+getRadius()*2*2, y);y = MAX(getCourtHeight()/2-getGoalWidth()/2-getRadius()*2*2, y);setMoveTo(x, y);/防守反击void Guard()double Radius = getBallRadius()*2 + getRadius()*2;double Distance = distance(getX(), getY(), getBallX(), getBallY();if(Distance>Radius*10) myGuard(); else

44、0;     GoAllOutGuard();if(!AzimuthDanger()ShootAttack();return; /方位安全时攻击if(isHitBall() && !AzimuthDanger() && getBallVelocity()>getMoveVelocity()/4) ShootAttack();return;if(BotBallBotDistance()ShootGoal(); /如果自己离球的距离小于敌人则进攻/判断进攻还是防守void AttackOrGuard() 

45、60; if(AzimuthDanger() && getBallVelocity()>0)Guard();return;   if(JudgeDanger() && getBallVelocity()>getMaxMoveVelocity()/2)Guard();return;   ShootAttack();/进攻门前争球void GuardConfrontEachOther()    double Radius = getRadius()+getBallRadius()/2;

46、    double xx=getAttack()>0 ? Radius : getCourtWidth()-Radius;        double MaxY = getCourtHeight() / 2 + getGoalWidth() / 2 + getRadius()*4;    double MinY = getCourtHeight() / 2 - getGoalWidth() / 2 - getRadius()*4;   

47、    if(getBallVelocity()>getMaxMoveVelocity()/2)            if (getAttack()>0 && getX()<xx && (getY()>MaxY | getY()<MinY)               

48、60;     setMoveToward(getBallX(), getBallY(), getMaxMoveVelocity();                if(getAttack()<0 && getX()>getCourtWidth()-xx && (getY()>MaxY | getY()<MinY)    

49、;                setMoveToward(getBallX(), getBallY(), getMaxMoveVelocity();            void Text(char* tt,int color)struct TextView text;text.showTime=1; text.color=color;text.tex

50、t=tt;text.size=40;double len = text.size*3.2;text.x=getX()-len;text.y=getY()-getRadius()*2;addTextView(&text);void drawLine(double dx,double dy,double endx,double endy)    struct LineView view;view.beginX=dx;view.beginY=dy;view.endX=endx;view.endY=endy;view.color=-256;view.showTim

51、e=1;    addLineView(&view);void line()      double MDistance = distance(getX(), getY(), getBallX(), getBallY();      double DDistance = distance(getBotX(), getBotY(), getBallX(), getBallY();      double MDD

52、istance = distance(getBotX(), getBotY(), getX(), getY();            if(MDistance>DDistance)drawLine(getBotX(),getBotY(),getBallX(),getBallY();      else drawLine(getX(),getY(),getBallX(),getBallY();    

53、              if(MDistance=DDistance) drawLine(getX(),getY(),getBotX(),getBotY();/组合策略void startGame()    double starttime=getTime();        double bgj=0; /被进攻次数    double gj=0;

54、60;  /进攻次数    char* tt=""    int color=-1;        while(1)            double timejg=getTime()-starttime;        if(gj-bgj>10) /对方属于防守型机器人

55、 攻击数大于被攻击数超过10次                       tt="ShootGoal"color=-256;              double bx=getBallX();   

56、0;          /球在对方场地 进攻              if ( (getAttack()>0 && bx>getCourtWidth()/2) | (getAttack()<0 && bx<getCourtWidth()/2) ) ShootGoal();tt="ShootGoal"c

57、olor=-256;              /球在自己场地 同时在滚动 防守              else if(getBallVelocity()>getMaxMoveVelocity()/5) Guard(); tt="Guard"color=-1;    &

58、#160;         if(getBallVelocity()<getMaxMoveVelocity()/5) ShootAttack(); tt="ShootAttack"color=-256;                       /球被反弹出去后进攻  &

59、#160;    if( (getAttack()>0 && getBallX()>getCourtWidth()*2/5) | (getAttack()<0 && getBallX()<getCourtWidth()*3/5) )                  if(!JudgeDanger()ShootGoal();tt="Shoot

60、Goal"color=-256;                      /足球临近球门 全力防守       if(getAttack()>0 && getBallX()<getCourtWidth()*2/5) | (getAttack()<0 && getBallX()>ge

61、tCourtWidth()*3/5)                  GoAllOutGuard();            tt="GoAllOutGuard"color=-1;           

62、60;         /球在对方场地 全力进攻       if(getAttack()>0 && getBallX()>getCourtWidth()*3/5) ShootGoal();tt="ShootGoal"color=-256;       else if(getAttack()<0 && getBallX()<getC

63、ourtWidth()*2/5) ShootGoal();tt="ShootGoal"color=-256;                     /强力防守 危险时刻       if( (getAttack()>0 && getBallX()<getBallRadius()*4) | (getAttack()<0 && getBallX()>getCourtWidth()-getBallRadius()*4) )           double x = getAttack() > 0 ? getRadius() : getCourtWidth()-getRadius();    setMoveTo(x, g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论