www.pudn.com > pb2132.rar > pb.cpp
#include#include #include #include #include #include #include int midx, midy; int sj, bh, co; //随机数 //////////////////////////////类定义 ///图形类 class Tuxing { public: Tuxing(int bianhua,int color); ~Tuxing(); virtual void draw()=0; protected: int change,x,y,color; }; Tuxing::Tuxing(int bianhua,int col) { change=bianhua; color=col; } Tuxing::~Tuxing() { } ///圆类 class Circle:public Tuxing { public: Circle(int bianhua,int col):Tuxing(bianhua,col){} void draw(); private: int radius; }; void Circle::draw() { setcolor(color); switch(change) { case 0: //中央放大 for(radius=0;radius<200;radius++) { setcolor(color); circle(midx, midy, radius); delay(20); cleardevice(); if(bioskey(1) != 0) exit(0); } break; case 1: //左至右 for(x=0,y=midy;x<2*midx;x+=3) { setcolor(color); circle(x,y,x/4); delay(20); cleardevice(); if(bioskey(1) != 0) exit(0); } break; case 2: //右至左 for(x=2*midx,y=midy;x>0;x-=3) { setcolor(color); circle(x,y,x/4); delay(20); cleardevice(); if(bioskey(1) != 0) exit(0); } break; case 3: //上至下 for(x=midx,y=0;y<2*midy;y+=3) { setcolor(color); circle(x,y,y/4); delay(20); cleardevice(); if(bioskey(1) != 0) exit(0); } break; case 4: //下至上 for(x=midx,y=2*midy;y>0;y-=3) { setcolor(color); circle(x,y,y/4); delay(20); cleardevice(); if(bioskey(1) != 0) exit(0); } break; default: x=midx,y=midy; for(double k;k<6.28;k+=0.05) { setcolor(color); circle(x,y,k); delay(20); cleardevice(); if(bioskey(1) != 0) exit(0); } } } ////正方形类 class Square:public Tuxing { public: Square(int bianhua,int col):Tuxing(bianhua,col){} void drawsquare(int xo,int yo,int i,double s); void draw(); protected: }; void Square::drawsquare(int x0,int y0,int a,double s) //画正方形 { int x1=x0-a*sin(3.1416/4-s); int y1=y0-a*cos(3.1416/4-s); int x2=x0+a*cos(3.1416/4-s); int y2=y0-a*sin(3.1416/4-s); int x3=x0+a*sin(3.1416/4-s); int y3=y0+a*cos(3.1416/4-s); int x4=x0-a*cos(3.1416/4-s); int y4=y0+a*sin(3.1416/4-s); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); delay(20); cleardevice(); if(bioskey(1)!=0) exit(0); } void Square::draw() //画动画正方形 { setcolor(color); switch(change) { case 0: //中央放大 x=midx,y=midy; for(int i=0;i 0;x-=3) { drawsquare(x,y,x/4,0); } break; case 2: //左至右 for(x=0,y=midy;x<2*midx;x+=3) { drawsquare(x,y,x/4,0); } break; case 3: //上至下 for(x=midx,y=0;y<2*midy;y+=3) { drawsquare(x,y,y/4,0); } break; case 4: //下至上 for(x=midx,y=2*midy;y>0;y-=3) { drawsquare(x,y,y/4,0); } break; default: //中央旋转 x=midx,y=midy; for(double k=0;k<6.28;k+=0.05) { drawsquare(x,y,150,k); } } } /////三角形类 class Three:public Tuxing { public: Three(int bianhua,int col):Tuxing(bianhua,col){} void draw(); void drawthree(int x0,int y0,int a,double s); //a尺寸 s偏离角度 protected: int a; //尺寸 }; void Three::drawthree(int x0,int y0,int a,double s) //画静态三角形 { int x1=x0+a*sin(s); int y1=y0-a*cos(s); int x2=x0-a*cos(3.1416*2/3-3.1416/2-s); int y2=y0+a*sin(3.1416*2/3-3.1416/2-s); int x3=x0+a*sin(3.1416-s-3.1416*2/3); int y3=y0+a*cos(3.1416-s-3.1416*2/3); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); delay(20); cleardevice(); if(bioskey(1)!=0) exit(0); } void Three::draw() //画运动三角形 { setcolor(color); switch(change) { case 0: //中央放大 for(int i=0;i 0;x-=3) { drawthree(x,y,x/4,0); } break; case 3: //上至下 for(x=midx,y=0;y<2*midy;y+=3) { drawthree(x,y,y/4,0); } break; case 4: //下至上 for(x=midx,y=2*midy;y>0;y-=3) { drawthree(x,y,y/4,0); } break; default: //中央旋转 x=midx,y=midy; for(double k=0;k<6.28;k+=0.05) { drawthree(x,y,150,k); } } } ///////////////////////////////函数部分 void drawing(Tuxing& s) { s.draw(); } void star() { sj=random(3); bh=random(6); co=random(getmaxcolor()-1)+1; Circle d(bh,co); Square e(bh,co); Three f(bh,co); switch(sj) { case 0: drawing(d); break; case 1: drawing(e); break; default: drawing(f); } } void main() { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "f:\\tc\\BGI"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor()); while(1) star(); }