精細(xì)繪圖及動(dòng)畫
更新時(shí)間: 2007-06-01 13:06:05來源: 粵嵌教育瀏覽量:607
這個(gè)程序是本人自己編寫,所以這里能后做出詳細(xì)解釋。程序運(yùn)行后會(huì)在屏幕上用triplex_font顯示“press any key for set off”,下方是用基本的線條畫出的發(fā)射架(有點(diǎn)粗糙),即程序中的DrawGun()函數(shù),當(dāng)按下任意鍵后,將在發(fā)射架上方出現(xiàn)一個(gè)用橫線填充的飛機(jī),同時(shí)背景被繁星填充,此時(shí)必須按住任意鍵不放,否則飛機(jī)將不會(huì)發(fā)射。發(fā)射完成后,即飛機(jī)坐標(biāo)小于0,將出現(xiàn)一句話:"successfully!!!,press ESC to quit",若中途按ESC退出,則飛機(jī)Y坐標(biāo)不小于0,將出現(xiàn)"failue,..."這句話。
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<alloc.h>
#define ESC 0x1b/*ESC的ASCII碼,十六進(jìn)制*/
void InstallGraph(void);/*圖形程序加載函數(shù)的說名語句*/
void DrawGun(void); /*畫發(fā)射架函數(shù)的說明語句*/
void DrawPlan(void); /*畫飛機(jī)函數(shù)的說明語句*/
void DrawStar(void); /*畫星函數(shù)的說明語句*/
void *buf; /*全局變量,用于存儲(chǔ)飛機(jī)的圖像*/
int x0=300; /*以下是飛機(jī)的位置初始化參數(shù)*/
int y0=340;
int width1=5;
int width2=20;
int height=25;
int y1=8;
int y2=15;
int y3=23;
int y4=38;
/*主函數(shù)開始*/
main()
{
int size;
int i=0;
int key;
int station;
InstallGraph();
setbkcolor(BLACK);
cleardevice();/*圖形驅(qū)動(dòng)程序和屏幕初始化工作完成*/
setcolor(BLUE);/*設(shè)置字的顏色*/
settextstyle(TRIPLEX_FONT,0,4);/*設(shè)置字體*/
outtextxy(80,100,"Ready!...Any key to SET OFF !!!");
DrawGun(); /*畫出發(fā)射架*/
getch(); /*if press key,clear the words and start*/
cleardevice();
DrawGun();
DrawPlan();
DrawStar();
setcolor(LIGHTRED);/*define result words color*/
do{ /*程序主循環(huán)開始,用于運(yùn)動(dòng)飛機(jī),方法是用異或的方式在屏幕上連續(xù)畫出飛機(jī) 的圖像,每畫一次,新圖像和來的位置相差兩個(gè)像素點(diǎn)。這個(gè)值是可調(diào)的,值越大,飛機(jī)飛行的速度越快*/
putimage(x0-width2,y0-height-3*width1-i,buf,XOR_PUT);
i+=2;
putimage(x0-width2,y0-height-3*width1-i,buf,XOR_PUT);
key=getch();
if(y0-height-3*width1-i==0)
outtextxy(60,100,"Successfully!!! Press ESC to quit");
if(key==ESC){
if(y0-height-3*width1-i>0){
cleardevice();
outtextxy(100,100,"Failue. What did you do?");
outtextxy(130,300,"Press any key to quit.");
getch();
}
}
}while(key!=ESC);
free(buf);
closegraph();
return 0;
}
/*****************Install the graphics library***********************/
void InstallGraph(void)
{
int grdriver=DETECT;
int grmode;
int errorcode;
char *errormsg;
registerbgidriver(EGAVGA_driver);
registerbgifont(triplex_font);
initgraph(&grdriver,&grmode,"");
errorcode=graphresult();
errormsg=grapherrormsg(errorcode);
if(errorcode!=grOk){
printf("Graphics error: %s\n",errormsg);
printf("Press any key to exit.\n");
getch();
exit(1);
}
}
/************************Draw the star ********************************/
void DrawStar(void)
{
int seed=2000;
int i,dotx,doty,height,width,color,maxcolor;
maxcolor=getmaxcolor();
width=getmaxx();
height=getmaxy();
srand(seed);
for(i=0;i<250;i++){
dotx=i+random(width-1);
doty=i+random(height-1);
color=random(maxcolor);
setcolor(color);
putpixel(dotx,doty,color);
circle(dotx+1,doty+1,1);
}
srand(seed);
}
/****************Draw the bottom gun********************/
void DrawGun(void)
{
int x0=300;
int y0=430;
int height=45;
int rx=20;
int ry=5;
int rightx,righty,leftx,lefty;
int centery1=30;
rightx=righty=leftx=lefty=12;
setcolor(LIGHTGREEN);
ellipse(x0,y0,180,360,rx,ry);
ellipse(x0,y0-height,0,360,rx,ry);
line(x0+rx,y0,x0+rx,y0-height);
line(x0-rx,y0,x0-rx,y0-height);
moveto(x0+rx,y0);
lineto(x0+rx+rightx,y0+righty);
moveto(x0+rx+rightx,y0+righty);
lineto(x0+rx+rightx+10,y0+righty+10);
circle(x0+rx+rightx+10,y0+righty+10,4);
moveto(x0,y0+ry);
lineto(x0,y0+centery1);
moveto(x0,y0+centery1);
lineto(x0,y0+centery1+10);
circle(x0,y0+centery1+10,4);
moveto(x0-rx,y0);
lineto(x0-rx-leftx,y0+lefty);
moveto(x0-rx-leftx,y0+lefty);
lineto(x0-rx-leftx-10,y0+lefty+10);
circle(x0-rx-leftx-10,y0+lefty+10,4);
line(x0-rx-leftx,y0+lefty,x0,y0+centery1);
line(x0,y0+centery1,x0+rx+rightx,y0+righty);
}
/*****************Draw the plan fly to the sky***********************/
void DrawPlan(void)
{
int size;
setcolor(LIGHTRED);
setfillstyle(1,BLUE);
ellipse(x0,y0-height,0,180,width1,3*width1);
moveto(x0+width1,y0); /*moveto center right side*/
lineto(x0+width1,y0-height);
moveto(x0+width1,y0);
lineto(x0+width2,y0+y2);
moveto(x0-width1,y0); /*moveto center left side*/
lineto(x0-width1,y0-height);
moveto(x0-width1,y0);
lineto(x0-width2,y0+y2);
moveto(x0+width2,y0+y3);/*moveto right bottom side*/
lineto(x0+width2,y0+y1);
moveto(x0+width2,y0+y3);
lineto(x0+width1,y0+y2);
moveto(x0-width2,y0+y3);/*moveto left bottom side*/
lineto(x0-width2,y0+y1);
moveto(x0-width2,y0+y3);
lineto(x0-width1,y0+y2);
moveto(x0,y0+y4); /*moveto the bottomest*/
lineto(x0+width1,y0+y2);
moveto(x0,y0+y4);
lineto(x0-width1,y0+y2);
setfillstyle(2,LIGHTRED); /*fill the plan with a style */
floodfill(x0,y0,LIGHTRED);
size=imagesize(x0-width2-1,y0-height-3*width1,
x0+width2+1,y0+y4);
buf=malloc(size);
if(!buf){
printf("No enough memory!");
exit(0);
}
getimage(x0-width2,y0-height-3*width1,x0+width2,y0+y4,buf);
}
粵嵌動(dòng)態(tài)
推薦閱讀
- ·Linux字符設(shè)備驅(qū)動(dòng)框架解析:file_operations的核心作用與實(shí)現(xiàn)
- ·廣東朝歌數(shù)碼科技股份有限公司專場(chǎng)招聘會(huì)
- ·深化產(chǎn)教融合,共筑技能人才培養(yǎng)新生態(tài) —— 廣州華立學(xué)院到訪粵嵌從化校區(qū)為深化產(chǎn)教
- ·校企合作新突破 | 粵嵌科技與三亞學(xué)院共探產(chǎn)教融合新路徑
- ·粵嵌科技入選國(guó)家級(jí)職業(yè)數(shù)字展館聯(lián)合建設(shè)單位,賦能計(jì)算機(jī)程序設(shè)計(jì)員高技能人才培養(yǎng)
- ·嵌入式實(shí)時(shí)操作系統(tǒng)的性能優(yōu)化與實(shí)現(xiàn)路徑
- ·校企攜手賦能教育!粵嵌科技助力海南科技職業(yè)大學(xué)探索 AGI 時(shí)代教學(xué)新范式
- ·嵌入式系統(tǒng)中的低功耗設(shè)計(jì)策略與實(shí)現(xiàn)路徑
- ·深圳市軒宇軟件開發(fā)有限公司專場(chǎng)招聘會(huì)
- ·嵌入式系統(tǒng)中的代碼空間優(yōu)化:策略與實(shí)踐