在C语言中利用随机函数实现动画技术
本程序已在AST 386SX/20(VGA)上,Turb C 2.0编译下通过。
/*利用随机函数实现动画技术实例*/
#include<conioh>
#include<stdioh>
#include<stdibh>
#include<graphicsh>
#include<timeh>
#defineX1260
#defineY1140
#defineX2320
#defineY2180
#defineXY16
int GDriver,GMode,MColors,ECode;
struct palettetype palette;
void Initialize(void);void RBars(void);
int main()
{
Initialize();/*初始化图形系统*//*显示放大字体*/
setcolor(YELLOW);
settextstyle(TRIPLEX-FONT,HORIZ-DIR,4);
settextjustify(CENTER-TEXT,CENTER-TEXT);
outtextxy((getmaxx()/2-17),360,"COMPUTER");
RBars();/*主程序*/
closegraph();/*关闭图形系统*/exit(1);
}
void Initialize(void)
{
GDriver=DETECT;
initgraph(&GDriver,&GMode,"");
ECode=graphresult();
if(ECode!=gr0k){
printf("Graphics Error:%s\n",grapherrormsg(ECode));
exit(1);}
getpalette(&palette);
MColors=getmaxcolor()+1;
}
void RBars(void)
{
int color;/*画“微型计算机”图形*/
setcolor(WHITE);
setfillstyle(1,DARKGRAY);
bar3d(X1-20,Y1-10,X2+56,Y2+70,0,3);
setfillstyle(CLOSE-DOT-FILL,RED);
bar(X1+4,Y1+78,X1+20,Y1+83);
setcolor(MAGENTA);
circle(X2+28,Y2+60,4);
circle(X2+16,Y2+60,4);
circle(X2+4,Y2+60,4);
setcolor(WHITE);
setfillstyle(SOLID-FILL,DARKGRAY);
bar3d(X1-60,Y1+120,X1+154,Y1+170,0,2);
bar3d(X1+20,Y1+126,X1+100,Y1+164,0,2);
line(X1+20,Y1+145,X1+100,Y1+145);
setfillstyle(SOLID-FILL,GREEN);
bar(X1+26,Y1+130,X1+34,Y1+152);
setfillstyle(WIDE-DOT-FILL,RED);
bar(x1-24,y1+128,X1-44,Y1+142);
/*利用随机函实现矩形画面互相覆盖,产生动感*/
while(!dbhit()){
color=random(MColors-1)+1;
setcolor(color);
setfillstyle(random(11)+1,color);
bar3d(X1+random(getmaxx()/XY),Y1+random
(getmaxy()/XY),
X@+random(getmaxx()/XY),Y2+random
(getmaxy()/XY),0,5);}
}