Animation sorting of Ege graphics library

Source: Internet
Author: User
Tags gety

The teacher arranged a learning Ege graphics library to do animation sort of small animation program, this is my own effect. Because of the limited personal level, there may be some areas where the code can be improved. Please point out the deficiencies.

Note: To run this code needs to be properly configured, connect the Ege graphics library header file, ready to work. For specific tutorials, see http://jingyan.baidu.com/article/4b07be3c40d35f48b380f3c7.html

Programming Environment: Codeblocks WINDOWS10 COREI5 CPU

Source:


#include <graphics.h>
#include <cmath>
Class fireball//Fireworks 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 ()
{
The simulation is the projectile motion
Setx (Getx () +VX);
Sety (Gety () +vy);
vy+=0.3f;
FillEllipse (X,Y,R,R);
}
};
Class firework//Fireworks
{
Private
Fireball f[80];//with 80 fire ball as sub-object of fireworks class
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)//Use several fireworks to form a firework scene
{
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);//Golden Moon
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);//Beautiful stars
}
}
}
}
Class box//Bar Column
{
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);//Beautiful stars
}
Bar (X1,400-h+h0,x1+20,400+h0);
SetFont (18,0, "Song Body");
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, "Song Body");
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, "Song Body");
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 ()//column Fall effect
{
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, "Song-Body");
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 ()//select sort
{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 ()//bubble sort
{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 ()//insert sort
{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 ();//Because this is a small to large row, so bubble sort is equivalent to inserting sort
Sleep (+);
}
void menu ()//select menu
{
//cleardevice ();
//setbkcolor (Egergb (0x0,0x0,0xff));
SetFont (25,0, "Arial");
SetColor (Egergb (0xff,0x0,0x00));
Outtextxy (250,100, "1. Select Sort");
Outtextxy (250,200, "2. Bubble sort");
Outtextxy (250,300, "3. Insert Sort");
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;//simulates the height of each cylinder randomly initialized
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)//This is the function of the cylinder movement
{//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);
}

Display effect:

Animation sorting of Ege graphics library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.