December 14, 2017

The Shield : Turbo C++


   The following program displays an animation that forms a protective shield in space. It has been developed using the Turbo C++ 3.0 IDE. It uses BGI Graphics to render all the graphics. 



Screenshots





The Shield - Output

The Shield - Output

The Shield - Output

The Shield - Output





Source Code



#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
#include <math.h>

#define SPEED 5

void *buff;
int num=20,radius=4;
int star_x[500],star_y[500],star=500;

struct Ball{
int x;
int y;
}pBall[20];

void initBall()
{
       int x=20,y=20,r=radius,fcolor,bcol;

       fcolor = random(15)+1;
       bcol=fcolor+1;
       if(bcol>15)
              bcol=1;
      
       setcolor(bcol);
       setfillstyle(SOLID_FILL,fcolor);
       circle(x,y,r);
       floodfill(x,y,bcol);

       buff=malloc(sizeof(imagesize(x-r,y-r,x+r,y+r)));
       getimage(x-r,y-r,x+r,y+r,buff);

       setfillstyle(SOLID_FILL,0);
       bar(x-r,y-r,x+r,y+r);

}


void rotate(int index, int radius)
{
    int x = radius-1;
    int y = 0;
    int dx = 1;
    int dy = 1;
    int err = dx - (radius << 1);
       int x0=pBall[index].x;
       int y0=pBall[index].y;

    while (x >= y)
    {
       delay(SPEED);
       if(kbhit())
       exit(0);
       putimage(x0 + x, y0 + y,buff,XOR_PUT);
       putimage(x0 + y, y0 + x,buff,XOR_PUT);
       putimage(x0 - y, y0 + x,buff,XOR_PUT);
       putimage(x0 - x, y0 + y,buff,XOR_PUT);
       putimage(x0 - x, y0 - y,buff,XOR_PUT);
       putimage(x0 - y, y0 - x,buff,XOR_PUT);
       putimage(x0 + y, y0 - x,buff,XOR_PUT);
       putimage(x0 + x, y0 - y,buff,XOR_PUT);

       if (err <= 0)
       {
           y++;
           err += dy;
           dy += 2;
       }
       if (err > 0)
       {
           x--;
           dx += 2;
           err += (-radius << 1) + dx;
       }
    }
}

void init()
{
int x=getmaxx()/2;
int y=240;


       for(int i=0;i<num;i++)
       {
              pBall[i].x=x;
              pBall[i].y=y;
       }

       for(int j=0;j<star;j++)
       {
              star_x[j]=random(640);
              star_y[j]=random(480);
       }

}

void main()
{
       int gd=DETECT,gm,R_C=0,flag=0;
       initgraph(&gd,&gm,"c://turboc3//bgi");
       initBall();
       init();
       while(!kbhit())
       {

              if(flag==0)
              {
              cleardevice();

              for(int i=0;i<num;i++)
              {
                           for(int m=0;m<star;m++)
                     putpixel(star_x[m],star_y[m],random(16));
                     initBall();
                     rotate(i,10+R_C);
                     delay(SPEED);
                     R_C+=10;

              }
              flag=1;
              }
              if(flag==1)
              {
              cleardevice();

              for(int i=0;i<num;i++)
              {
                     for(int m=0;m<star;m++)
                     putpixel(star_x[m],star_y[m],random(16));
                     initBall();
                     rotate(i,10+R_C);
                     delay(SPEED);
                     R_C-=10;

              }
              flag=0;
              }




       }
}




Download




Click here to view and download the source code of The Shield (21 KB). 


Click here to download the executable file of The Shield (20 KB) (Please note that it is a 16-bit executable and will not run on 64-bit systems).




No comments:

Post a Comment