码迷,mamicode.com
首页 > 编程语言 > 详细

ege图形库之动画排序

时间:2017-05-30 12:17:08      阅读:666      评论:0      收藏:0      [点我收藏+]

标签:art   while   double   set   i+1   环境   count   clu   fireworks   

老师布置了一个学习ege图形库来做动画排序的小动画程序,这是我自己做的效果。由于个人水平有限,可能代码有些地方可以改进。不足之处敬请指出。

注:要运行该代码需要正确配置,连接好ege图形库的头文件,做好准备工作。具体教程请看http://jingyan.baidu.com/article/4b07be3c40d35f48b380f3c7.html

编程环境:codeblocks   windows10  corei5 cpu

源代码:


#include<graphics.h>
#include<cmath>
class fireball//烟花火球类
{
private:
double x;
double y;
double vx;
double vy;
int r;
public:
void setfireball(double a,double b,double c,double d){x=a;y=b;vx=c;vy=d;}
void setx(double a){x=a;}
double getx(){return x;}
void sety(double a){y=a;}
double gety(){return y;}
void setvx(double a){vx=a;}
double getvx(){return vx;}
void setvy(double a){vy=a;}
double getvy(){return vy;}
void explode(){}
void setr(int a){r=a;}
int getr(){return r;}
void show()
{
//模拟的是抛体运动
setx(getx()+vx);
sety(gety()+vy);
vy+=0.3f;
fillellipse(x,y,r,r);
}
};
class firework//烟花类
{
private:
fireball f[80];//用80个火球类作为烟花类的子对象
int x0;int y0;
public:
int getx0(){return x0;}
int gety0(){return y0;}
int c=255;
void setfirework(int x,int y,int r)
{
double angle=0;
for(int i=0;i<80;i++)
{
f[i].setvx(8*cos(angle));f[i].setvy(8*sin(angle));f[i].setx(x);f[i].sety(y);f[i].setr(r);
angle+=0.078539;
}
x0=x;y0=y;
}
void showfirework()
{
c--;
setfillcolor(EGERGB(c,c,0));
fillellipse(x0,y0,5,5);
for(int i=0;i<80;i++){if(i%2==0)setfillcolor(EGERGB(255,0,0));else setfillcolor(EGERGB(0,255,0));if(i%3==0)setfillcolor(EGERGB(255,255,0));f[i].show();}
}
};
void fireworkscene(int num)//用若干个烟花组成一个烟花景象
{
while(num--)
{
setbkcolor(EGERGB(0x0,0x0,0x0));
setbkmode(TRANSPARENT);
//firework fw[15];
//for(int i=0;i<15;i++)fw[i].setfirework(40+i*40,100);
firework f;f.setfirework(320,100,3);firework f2;f2.setfirework(200,78,2);firework f3;f3.setfirework(480,148,1);
int count=0;int y2=480,y3=480;int y=480;firework f4;f4.setfirework(150,350-100,3);firework f5;f5.setfirework(400,380-100,1);firework f6;f6.setfirework(500,398-100,2);
int y4=480,y5=480,y6=480;firework f7;f7.setfirework(198,230,1);firework f9;f9.setfirework(322,200,1);firework f8;f8.setfirework(448,269,2);firework f10;f10.setfirework(336,70,3);
int y7=480,y8=480,y9=480;int y10=480;
for(;is_run()&&count<200;delay_fps(80))
{count++;
imagefilter_blurring(NULL, 0x7e, 0xff);
setcolor(EGERGB(255,255,255));
if(y2>f2.gety0())circle(f2.getx0(),y2=y2-4,3);
else f2.showfirework();
if(y3>f3.gety0())circle(f3.getx0(),y3=y3-4,3);
else f3.showfirework();
if(y>f.gety0())circle(f.getx0(),y=y-4,3);
else f.showfirework();
if(y4>f4.gety0())circle(f4.getx0(),y4=y4-4,3);
else f4.showfirework();
if(y5>f5.gety0())circle(f5.getx0(),y5=y5-4,3);
else f5.showfirework();
if(y6>f6.gety0())circle(f6.getx0(),y6=y6-4,3);
else f6.showfirework();
if(y7>f7.gety0())circle(f7.getx0(),y7=y7-4,3);
else f7.showfirework();
if(y8>f8.gety0())circle(f8.getx0(),y8=y8-4,3);
else f8.showfirework();
if(y9>f9.gety0())circle(f9.getx0(),y9=y9-4,3);
else f9.showfirework();
if(y10>f10.gety0())circle(f10.getx0(),y10=y10-4,3);
else f10.showfirework();

setfillcolor(EGERGB(255,255,0));
fillellipse(500,50,20,20);//金黄色的月亮
for(int i=0;i<30;i++)
{
fillellipse((i*i*34+i*8*8*i*i-3)%640,(i*i*i*28+i*8+i*i-232)%240,1,1);//美丽的星星
}
}
}
}
class box//条形柱
{
private:
int x1;int h;
bool highlight;
bool flag;
public:
void setflag(bool n){flag=n;}
bool getflag(){return flag;}
void sethighlight(int b){highlight=b;}
bool gethighlight(){return highlight;}
int getx1(){return x1;}
int geth(){return h;}
void setx1(int a){x1=a;}
void seth(int b){h=b;}
box(int a=0,int b=0):x1(a),h(b){highlight=0;}
void setbox(int x,int h0)
{
x1=x;h=h0;flag=0;
}
void showfall()
{
if(gethighlight())setfillcolor(EGERGB(255,0x0,0x0));
else setfillcolor(EGERGB(0,0,255));
int h0=-400;int i=0;
for(;is_run()&&h0<=0;delay_fps(10+2*i))
{
cleardevice();
for(int i=0;i<30;i++)
{
fillellipse((i*i*34+i*8*8*i*i-3)%640,(i*i*i*28+i*8+i*i-232)%240,1,1);//美丽的星星
}
bar(x1,400-h+h0,x1+20,400+h0);
setfont(18,0,"宋体");
int l=0;
int n=geth();int m;int n0=geth();
while(n>0)
{
m=n%10;
n=n/10;
l++;
}
char ch[l];
for(int i=0;i<l;i++)
{
m=n0%10;n0=n0/10;ch[l-i-1]=‘0‘+m;
}
outtextxy(x1,400-h-25+h0,ch);
i++;
h0=h0+1;
}
for(int i=0;i<10;i++)
{
int l=0;
int n=geth();int m;int n0=geth();
while(n>0)
{
m=n%10;
n=n/10;
l++;
}
char ch[l];
for(int i=0;i<l;i++)
{
m=n0%10;n0=n0/10;ch[l-i-1]=‘0‘+m;
}
delay_fps(60-i*2);cleardevice();

bar(x1,400-h-i,x1+20,400-i);outtextxy(x1,400-h-25-i,ch);
}
for(int i=0;i<10;i++)
{
int l=0;
int n=geth();int m;int n0=geth();
while(n>0)
{
m=n%10;
n=n/10;
l++;
}
char ch[l];
for(int i=0;i<l;i++)
{
m=n0%10;n0=n0/10;ch[l-i-1]=‘0‘+m;
}
delay_fps(40+i);cleardevice();
bar(x1,400-h+i,x1+20,400+i);outtextxy(x1,400-h-25+i,ch);
}
Sleep(2000);
}
void show()
{setcolor(EGERGB(0xFF,0x0,0x0));
if(highlight==0)
{setfillcolor(EGERGB(0x0,0x0,0xff));
bar(x1,400-h,x1+20,400);
setfont(18,0,"宋体");
int l=0;
int n=geth();int m;int n0=geth();
while(n>0)
{
m=n%10;
n=n/10;
l++;
}
char ch[l];
for(int i=0;i<l;i++)
{
m=n0%10;n0=n0/10;ch[l-i-1]=‘0‘+m;
}
outtextxy(x1,400-h-25,ch);
}
else
{
setfillcolor(EGERGB(255,0x0,0x0));
bar(x1,400-h,x1+20,400);
setfont(18,0,"宋体");
int l=0;
int n=geth();int m;int n0=geth();
while(n>0)
{
m=n%10;
n=n/10;
l++;
}
char ch[l];
for(int i=0;i<l;i++)
{
m=n0%10;n0=n0/10;ch[l-i-1]=‘0‘+m;
}
outtextxy(x1,400-h-25,ch);
}
if(flag)
{
setcolor(EGERGB(255,255,0x0));
line(x1+10,400-h,x1+10,350-h);
setfillcolor(EGERGB(255,0,255));
bar(x1-10,350-h,x1+10,355-h);
setfillcolor(EGERGB(0,255,255));
bar(x1-10,355-h,x1+10,360-h);
setfillcolor(EGERGB(255,255,0));
bar(x1-10,360-h,x1+10,365-h);
}
}

};
box b[15];
void showallboxfall()//柱体坠落的效果
{
setfillcolor(EGERGB(0x0,0x0,0xFF));
int h0=-400;int m=0;int x1;int h;
for(;is_run()&&h0<=0;delay_fps(10+2*m))
{
cleardevice();
for(int j=0;j<15;j++)
{x1=b[j].getx1();h=b[j].geth();
bar(x1,400-h+h0,x1+20,400+h0);
setfont(18,0,"宋体");
int l=0;
int n=b[j].geth();int m;int n0=b[j].geth();
while(n>0)
{
m=n%10;
n=n/10;
l++;
}
char ch[l];
for(int k=0;k<l;k++)
{
m=n0%10;n0=n0/10;ch[l-k-1]=‘0‘+m;
}
outtextxy(x1,400-h-25+h0,ch);
}
m++;
h0=h0+1;
}


for(int i=0;i<10;i++)
{
delay_fps(60-i*2);cleardevice();
for(int a=0;a<15;a++)
{x1=b[a].getx1();h=b[a].geth();
bar(x1,400-h-i,x1+20,400-i);
}
}
for(int i=0;i<10;i++)
{
delay_fps(40+i);cleardevice();
for(int a=0;a<15;a++)
{
x1=b[a].getx1();h=b[a].geth();
bar(x1,400-h+i,x1+20,400+i);
}
}

Sleep(1000);
}
void selectsort()//选择排序
{void movebox(box&,box&);void showallbox();
for(int i=0;i<15;i++)
{int k=i;
for(int j=k;j<15;j++)
{
if(b[k].geth()>b[j].geth())k=j;
}
if(k!=i){movebox(b[k],b[i]);box t=b[k];b[k]=b[i];b[i]=t;}
b[i].setflag(1);
}
showallbox();
Sleep(1000);
fireworkscene(2);
showallbox();
Sleep(1000);
}
void bubblesort()//冒泡排序
{void showallbox();
void movebox(box&,box&);
for(int i=0;i<15;i++)
{
for(int j=0;j<15-i-1;j++)
{
if(b[j].geth()>b[j+1].geth()){movebox(b[j],b[j+1]);box t=b[j];b[j]=b[j+1];b[j+1]=t;}
}
b[15-i].setflag(1);
}
b[0].setflag(1);
showallbox();
Sleep(1000);
fireworkscene(2);
showallbox();
Sleep(1000);
}

void insertsort()//插入排序
{void showallbox();
cleardevice();
for(int i=1;i<15;i++)b[i].setbox(b[i].getx1(),20*i+30);
box a(30,230);a.sethighlight(1);
b[0]=a;b[0].sethighlight(1);a.sethighlight(1);
a.showfall();
for(int i=1;i<15;i++)b[i].setbox(b[i].getx1(),20*i+30);
bubblesort();//因为此处已经从小到大排好了,所以用冒泡排序就相当于插入排序了
Sleep(1000);
}
void menu()//选择菜单
{
//cleardevice();
//SetBkColor(EGERGB(0x0,0x0,0xFF));
setfont(25,0,"宋体");
setcolor(EGERGB(0xFF,0x0,0x00));
outtextxy(250,100,"1.选择排序");
outtextxy(250,200,"2.冒泡排序");
outtextxy(250,300,"3.插入排序");
char n;
n=getch();
if(n==‘2‘)bubblesort();
if(n==‘1‘)selectsort();
if(n==‘3‘)insertsort();
}

int main()
{int h;
INITGRAPH(640,480);
//setbkcolor(EGERGB(0,255,0));
setfillcolor(EGERGB(0xFF,0x0,0x0));
void showallbox();
//showallbox();
void movebox(box& ,box&);
while(1)
{
for(int i=0;i<15;i++)
{
h=(20*i+3*i*i+14*i*i*i)%400;//模拟随机初始化各个柱体的高度
if(h==0)h=10*i+100;
b[i].setbox(30+i*40,h);
}
showallboxfall();
menu();
}
getch();
closegraph();
return 0;
}
void showallbox()
{
for(int i=0;i<15;i++)b[i].show();
}
void movebox(box &a,box &b)//这是柱体移动的函数
{//Sleep(500);

a.sethighlight(1);b.sethighlight(1);

int x2=b.getx1();
for(;is_run();)
{

if(a.getx1()<b.getx1())
{
while(a.getx1()<=x2)
{delay_fps(90);
int m=a.getx1();
a.setx1(m+1);
int n=b.getx1();
b.setx1(n-1);
cleardevice();
showallbox();
}
break;
}
else movebox(b,a);
}
a.sethighlight(0);b.sethighlight(0);
}

显示效果:

技术分享

技术分享

技术分享

 

 

ege图形库之动画排序

标签:art   while   double   set   i+1   环境   count   clu   fireworks   

原文地址:http://www.cnblogs.com/linruier/p/6919717.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!