📄 精细绘图及动画.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0044)http://myweb.yzu.edu.cn/toby88/c/c1/jcht.htm -->
<!-- saved from url=(0045)http://beckenglish.51.net/cbook/arti/plan.htm --><HTML><HEAD><TITLE>精细绘图及动画</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2800.1106" name=GENERATOR><LINK
href="../%BE%AB%CF%B8%BB%E6%CD%BC%BC%B0%B6%AF%BB%AD.files/moy.css" type=text/css
rel=stylesheet></HEAD>
<BODY onload="">
<P align=center><STRONG class=orang_text>精细绘图及动画</STRONG></P>
<P>制个程序是本人自己编写,所以这里能后做出详细解释。程序运行后会在屏幕上用triplex_font显示“press any key for set
off”,下方是用基本的线条画出的发射架(有点粗糙),即程序中的DrawGun()函数,当按下任意键后,将在发射架上方出现一个用横线填充的飞机,同时背景被繁星填充,此时必须按住任意键不放,否则飞机将不会发射。发射完成后,即飞机坐标小于0,将出现一句话:"successfully!!!,press
ESC to quit",若中途按ESC退出,则飞机Y坐标不小于0,将出现"failue,..."这句话。</P>
<P>#include<graphics.h><BR>#include<conio.h><BR>#include<stdio.h><BR>#include<stdlib.h><BR>#include<alloc.h><BR><BR>#define
ESC
0x1b/*ESC的ASCII码,十六进制*/<BR><BR>void InstallGraph(void);/*图形程序加载函数的说名语句*/<BR>void
DrawGun(void); /*画发射架函数的说明语句*/<BR>void
DrawPlan(void); /*画飞机函数的说明语句*/<BR>void DrawStar(void);
/*画星函数的说明语句*/<BR><BR>void *buf; /*全局变量,用于存储飞机的图像*/<BR>int x0=300;
/*以下是飞机的位置初始化参数*/<BR>int y0=340;<BR>int width1=5;<BR>int width2=20;<BR>int
height=25;<BR>int y1=8;<BR>int y2=15;<BR>int y3=23;<BR>int
y4=38;<BR><BR>/*主函数开始*/<BR>main()<BR>{<BR> int
size;<BR> int i=0;<BR> int
key;<BR> int station;<BR><BR><BR>
InstallGraph();<BR> setbkcolor(BLACK);<BR>
cleardevice();/*图形驱动程序和屏幕初始化工作完成*/<BR><BR>
setcolor(BLUE);/*设置字的颜色*/<BR>
settextstyle(TRIPLEX_FONT,0,4);/*设置字体*/<BR>
outtextxy(80,100,"Ready!...Any key to SET OFF !!!");<BR>
DrawGun(); /*画出发射架*/<BR> getch(); /*if press key,clear
the words and start*/<BR><BR>
cleardevice();<BR> DrawGun();<BR>
DrawPlan();<BR> DrawStar();<BR>
setcolor(LIGHTRED);/*define result words color*/<BR><BR> do{
/*程序主循环开始,用于运动飞机,方法是用异或的方式在屏幕上连续画出飞机
的图像,每画一次,新图像和来的位置相差两个像素点。这个值是可调的,值越大,飞机飞行的速度越快*/<BR><BR>
putimage(x0-width2,y0-height-3*width1-i,buf,XOR_PUT);<BR>
i+=2;<BR>
putimage(x0-width2,y0-height-3*width1-i,buf,XOR_PUT);<BR><BR>
key=getch();<BR>
if(y0-height-3*width1-i==0)<BR>
outtextxy(60,100,"Successfully!!! Press ESC to
quit");<BR>
if(key==ESC){<BR>
if(y0-height-3*width1-i>0){<BR>
cleardevice();<BR>
outtextxy(100,100,"Failue. What did you
do?");<BR>
outtextxy(130,300,"Press any key to
quit.");<BR>
getch();<BR>
}<BR>
}<BR><BR> }while(key!=ESC);<BR><BR>
free(buf);<BR> closegraph();<BR> return
0;<BR><BR>}<BR>/*****************Install the graphics
library***********************/<BR><BR>void
InstallGraph(void)<BR>{<BR> int
grdriver=DETECT;<BR> int grmode;<BR> int
errorcode;<BR> char *errormsg;<BR><BR>
registerbgidriver(EGAVGA_driver);<BR>
registerbgifont(triplex_font);<BR>
initgraph(&grdriver,&grmode,"");<BR><BR>
errorcode=graphresult();<BR>
errormsg=grapherrormsg(errorcode);<BR>
if(errorcode!=grOk){<BR>
printf("Graphics error:
%s\n",errormsg);<BR> printf("Press any
key to exit.\n");<BR>
getch();<BR>
exit(1);<BR>
}<BR>}<BR><BR>/************************Draw the star
********************************/<BR><BR>void
DrawStar(void)<BR>{<BR> int seed=2000;<BR>
int i,dotx,doty,height,width,color,maxcolor;<BR><BR>
maxcolor=getmaxcolor();<BR>
width=getmaxx();<BR>
height=getmaxy();<BR><BR> srand(seed);<BR>
for(i=0;i<250;i++){<BR>
dotx=i+random(width-1);<BR>
doty=i+random(height-1);<BR>
color=random(maxcolor);<BR><BR>
setcolor(color);<BR>
putpixel(dotx,doty,color);<BR>
circle(dotx+1,doty+1,1);<BR><BR>
}<BR> srand(seed);<BR>}<BR>/****************Draw the bottom
gun********************/<BR>void DrawGun(void)<BR>{<BR> int
x0=300;<BR> int y0=430;<BR> int
height=45;<BR> int rx=20;<BR> int
ry=5;<BR> int rightx,righty,leftx,lefty;<BR>
int centery1=30;<BR>
rightx=righty=leftx=lefty=12;<BR><BR><BR>
setcolor(LIGHTGREEN);<BR>
ellipse(x0,y0,180,360,rx,ry);<BR>
ellipse(x0,y0-height,0,360,rx,ry);<BR>
line(x0+rx,y0,x0+rx,y0-height);<BR>
line(x0-rx,y0,x0-rx,y0-height);<BR><BR>
moveto(x0+rx,y0);<BR>
lineto(x0+rx+rightx,y0+righty);<BR>
moveto(x0+rx+rightx,y0+righty);<BR>
lineto(x0+rx+rightx+10,y0+righty+10);<BR>
circle(x0+rx+rightx+10,y0+righty+10,4);<BR><BR><BR>
moveto(x0,y0+ry);<BR>
lineto(x0,y0+centery1);<BR>
moveto(x0,y0+centery1);<BR>
lineto(x0,y0+centery1+10);<BR>
circle(x0,y0+centery1+10,4);<BR><BR><BR>
moveto(x0-rx,y0);<BR>
lineto(x0-rx-leftx,y0+lefty);<BR>
moveto(x0-rx-leftx,y0+lefty);<BR>
lineto(x0-rx-leftx-10,y0+lefty+10);<BR>
circle(x0-rx-leftx-10,y0+lefty+10,4);<BR><BR>
line(x0-rx-leftx,y0+lefty,x0,y0+centery1);<BR>
line(x0,y0+centery1,x0+rx+rightx,y0+righty);<BR>}<BR>/*****************Draw the
plan fly to the sky***********************/<BR>void
DrawPlan(void)<BR>{<BR><BR><BR> int
size;<BR><BR> setcolor(LIGHTRED);<BR>
setfillstyle(1,BLUE);<BR>
ellipse(x0,y0-height,0,180,width1,3*width1);<BR><BR>
moveto(x0+width1,y0); /*moveto center right side*/<BR>
lineto(x0+width1,y0-height);<BR>
moveto(x0+width1,y0);<BR>
lineto(x0+width2,y0+y2);<BR><BR> moveto(x0-width1,y0);
/*moveto center left side*/<BR>
lineto(x0-width1,y0-height);<BR>
moveto(x0-width1,y0);<BR>
lineto(x0-width2,y0+y2);<BR><BR>
moveto(x0+width2,y0+y3);/*moveto right bottom side*/<BR>
lineto(x0+width2,y0+y1);<BR>
moveto(x0+width2,y0+y3);<BR>
lineto(x0+width1,y0+y2);<BR><BR>
moveto(x0-width2,y0+y3);/*moveto left bottom side*/<BR>
lineto(x0-width2,y0+y1);<BR>
moveto(x0-width2,y0+y3);<BR>
lineto(x0-width1,y0+y2);<BR><BR> moveto(x0,y0+y4); /*moveto
the bottomest*/<BR>
lineto(x0+width1,y0+y2);<BR>
moveto(x0,y0+y4);<BR>
lineto(x0-width1,y0+y2);<BR><BR> setfillstyle(2,LIGHTRED);
/*fill the plan with a style */<BR>
floodfill(x0,y0,LIGHTRED);<BR><BR>
size=imagesize(x0-width2-1,y0-height-3*width1,<BR>
x0+width2+1,y0+y4);<BR>
buf=malloc(size);<BR>
if(!buf){<BR> printf("No enough
memory!");<BR>
exit(0);<BR> }<BR>
getimage(x0-width2,y0-height-3*width1,x0+width2,y0+y4,buf);<BR><BR>}</P>
<P align=center><FONT class=select12 color=#000000
size=2>辽宁省铁岭师范高等专科学校微机室版权所有©2002</FONT></P>
<P align=center><A href="javascript:window.close()">关闭窗口</A> </P>
<SCRIPT language=vbscript>
</SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -