📄 missiletoplane.cpp
字号:
#include < windows.h >
#include < strstrea.h > //ostrstream用
#include < stdio.h > //wsprintf用
#include < math.h > //绘正弦曲线用
#include < time.h > //时间延迟函数用
void MissleToPlane( HDC hdc)
{
char szBuffer[50] ;
RECT rc ;
LOGFONT LogFont ;//逻辑汉字
LogFont.lfHeight = 5 ;//设置字体高
MoveToEx( hdc , 100 , 400 , NULL) ; //绘y坐标轴:
LineTo( hdc , 100 , 100 ) ;
rc.top = 100 ; //设置字符"y"的位置
rc.left = 90 ;
rc.right = 1000 ;
rc.bottom = 1000 ;
ostrstream( szBuffer , 50 )<<"Y"<<ends ; //输出坐标y
DrawText( hdc , szBuffer , strlen(szBuffer) , &rc , DT_EXPANDTABS ) ;
MoveToEx(hdc , 100 , 400 , NULL ) ; //绘x坐标轴:
LineTo( hdc , 600 , 400 ) ;
rc.top = 410 ; //设置字符"x"的位置
rc.left = 600 ;
rc.right = 1000 ;
rc.bottom = 1000 ;
ostrstream( szBuffer , 50 )<<"X"<<ends ;
DrawText( hdc , szBuffer , strlen(szBuffer) , &rc , DT_EXPANDTABS ) ;
double radius = 200 ; //半径
double xm = radius ; //导弹初始坐标
double ym = radius ;
MoveToEx( hdc , xm + 100 , 400 - ym ,NULL ) ;
rc.top = long( 400 - ym - 35 ) ; //设置"导弹"字符的位置和颜色
rc.left = long( xm + 100 );
rc.right = 2000 ;
rc.bottom = 1000 ;
ostrstream( szBuffer , 50 )<<"导弹"<<ends ;
SetTextColor(hdc , RGB( 0 , 210 , 255 ) ) ;
DrawText( hdc , szBuffer , strlen( szBuffer ) , &rc ,DT_EXPANDTABS ) ;
double xa = 2 * radius ;
double ya = radius ; //飞机初始坐标
MoveToEx( hdc , xa + 100 , 400 - ya ,NULL ) ;
rc.top = long( 400 - ya - 35 ) ;//设置"飞机"字符的位置和颜色
rc.left = long( xa + 100 ) ;
rc.right = 2000 ;
rc.bottom = 1000 ;
ostrstream( szBuffer , 50 )<<"飞机"<<ends ;
SetTextColor( hdc , RGB( 0 , 220 , 0 ) ) ;
DrawText( hdc , szBuffer , strlen( szBuffer ) , &rc , DT_EXPANDTABS ) ;
double cosj , sinj ; //导弹与飞机直线与水平坐标夹角的正弦,余弦
double l = 0 ; //飞机飞行的距离
double dt = 0.0005 ; //时间步长
double s0 = 0.1 ; //循环退出的条件
double s ; //飞机与导弹的距离
double va = 10 , vm = 11 ; //分别是飞机和导弹的速度
double t = 0 ; //总的时间
HPEN hPen1 , hPen2 ; //画笔
s = radius ;
hPen2 = CreatePen( 1 , 1 , RGB( 0 , 210 , 255 ) ) ; //创建画笔2
hPen1 = CreatePen( 1 , 1 , RGB( 0 , 220 , 0 ) ) ; //创建画笔1
while( s > s0 )
{
l += va * dt ;
xa = radius + cos( l / radius ) * radius ; //飞机坐标
ya = radius - sin( l / radius ) * radius ;
SelectObject( hdc , hPen2 ) ;
LineTo( hdc , xa + 100 , 400 - ya) ;
MoveToEx( hdc , xm + 100 ,400 - ym , NULL ) ;
SelectObject( hdc , hPen1 ) ;//使用绿色画笔画图
cosj = ( xa - xm ) / s ;//导弹与飞机直线与水平坐标夹角的正弦,余弦
sinj = ( ya - ym ) / s ;
xm = xm + vm * dt * cosj ;//导弹坐标
ym = ym + vm * dt * sinj ;
LineTo( hdc , xm + 100 , 400 - ym ) ;
MoveToEx( hdc , xa + 100, 400 - ya , NULL );
s=sqrt( ( xa - xm ) * ( xa - xm ) + ( ya - ym ) * ( ya - ym ) ) ;//飞机与导弹的距离
t += dt ;//总的时间
}
MessageBeep ( MB_OK );
rc.top = long( 400 - ym - 35 ) ;
rc.left = long( xm + 100 - 5 );
rc.right = 2000 ;
rc.bottom = 1000 ;
ostrstream( szBuffer , 50 )<<"砰!"<<ends ;
SetTextColor( hdc , RGB( 255 , 0 , 255 ) ) ;
DrawText( hdc , szBuffer , strlen( szBuffer ) , &rc , DT_EXPANDTABS) ;
DeleteObject( hPen1 ) ;//删除画笔
DeleteObject( hPen2 ) ;//删除画笔
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -