使用Opengl实现模拟飞机追击问题

#include
#include
#include
#include
#include

#define pi 3.14

int dlong(int jx,int jy,int hx,int hy)
{
int d;
d = sqrt((jx - hx) * (jx - hx) + (jy - hy) * (jy - hy));
return d;
}

void coordinate()
{
int i = 0,dir,jx = 0,jy = 50,hx = 80,hy = 0,hx0 = 80,hy0 =0,jx0 = 0,jy0 = 50,d;
srand((unsigned)time(NULL));
for(i = 0;i < 12;i ++)
{
d = sqrt((jx0 - hx0)*(jx0 - hx0)+(jy0 - hy0)*(jy0 -hy0));
if(d <= 10)
{
printf("第%d分钟,歼击机追击到轰炸机!\n",i+1);
break;
}
jx = jx0 + (int)((hx0 - jx0)/(float)d*20);
jy = jy0 + (int)((hy0 - jy0)/(float)d*20);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINE_STRIP);
glVertex2f((float)jx0,(float)jy0);
glVertex2f((float)jx,(float)jy);
jx0 = jx;jy0 = jy;
glEnd();
dir = rand()1; //角度
if(dir < 90)
{
hx = hx - (int)(cos(dir*pi/180)*15);
hy = hy - (int)(sin(dir*pi/180)*15);
}
if(dir == 90)
{
hy = hy - 15;
}
if(dir > 90&&dir <= 180)
{
hx = hx + (int)(cos((dir-90)*pi/180)*15);
hy = hy - (int)(sin((dir-90)*pi/180)*15);
}
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_STRIP);
glVertex2f((float)hx0,(float)hy0);
glVertex2f((float)hx,(float)hy);
hx0 = hx;hy0 = hy;
glEnd();
if(d > 10&&i == 11)
{
printf("歼击机未能追击到轰炸机!\n");
}
}
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
coordinate();
glFlush();
}

int main(int argc, char* argv)
{
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(400,400); //设置窗口大小
glutInitWindowPosition(800,200); //设置窗口显示位置
glutCreateWindow("The Way");//设置窗口标题
glutDisplayFunc(display);
gluOrtho2D(-200.0f,200.0f,-200.0f,200.0f);
glutMainLoop(); return 0;
}
使用Opengl实现模拟飞机追击问题o

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值