Saturday 5 February 2011

HOW TO CREATE ANIMATION OF SOLAR SYSTEM IN C(UPDATED)

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include"dos.h"
#include<stdlib.h>
union REGS i,o;
static int
bkcol=1,coll=4,scor,balls,posx1,posy1,posx2,posy2,col[8][8]={{2,2,2,2,2,2,
2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,
2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
void main() /*MAIN*/
{
 int gd=DETECT,gm,cont;
 int menu();
 void draw();
 void play();
 void drawcont();
 initgraph(&gd,&gm,"");
 title();
 if(initmouse()==0)
 {
  closegraph();
  exit(1);
 }
 cleardevice();
 drawcont();
 while(1)
 {
  cont=menu();
  hidemouseptr();
  if(cont==1)
 {
 drawcont();
 }
  draw();
  play();
 }
}
/************************MENU********************************/
int menu()
{
 int button=0,a,x,y;
 cleardevice();
 x=getmaxx();
 y=getmaxy();
 setfillstyle(1,3);
 floodfill(15,15,15);
 setcolor(1);
 rectangle(200,120,400,160);
 rectangle(200,230,400,270);
 rectangle(200,340,400,380);
 rectangle(201,121,399,159);
 rectangle(201,231,399,269);
 rectangle(201,341,399,379);
 setfillstyle(1,3);
 floodfill(260,130,1);
 floodfill(260,240,1);
 floodfill(260,350,1);
 settextstyle(1,0,4);
 setcolor(1);
 outtextxy(230,120,"1.NEW");
 outtextxy(210,230,"2.CONTINUE");
 outtextxy(230,340,"3.EXIT");
 setcolor(15);
 line(201,121,398,121);
 line(201,121,201,158);
 line(201,231,398,231);
 line(201,231,201,268);
 line(201,341,398,341);
 line(201,341,201,378);
 setfillstyle(1,0);
 showmouseptr();
 delay(500);
 settextstyle(8,0,1);
 outtextxy(39,441,"HELP");
 setcolor(7);
// rectangle(490,447,100,464);
    rectangle(491,447,100,464);
 do
 {
 hidemouseptr();
  bar(490,448,101,463);
  settextstyle(11,0,9);
  outtextxy(106,450,"MENU");
  showmouseptr();
  do
  {
  getmousepos(&button,&x,&y);
  }while(button!=1);
     if(button & 1==1)
   {
    if((x<400 && x>200)&&(y>120&&y<160))
    {
     return 1;
    }
    else if((x<400 && x>200)&&(y>340&&y<380))
    {
     exit(0);
    }
    else if((x<400 && x>200)&&(y>230&&y<270))
    {
    return 0;
    }
    else
    {
     printf("a");
   //  gotoxy(14,29);
   //printf("                                                ");
  // gotoxy(14,29);
  // printf("SORRY");
   hidemouseptr();
   bar(490,448,102,463);
    outtextxy(102,450,"SORRY");
   showmouseptr();

   delay(500);
     continue;

    }
   }
 }while(1);
}


 initmouse()
 {
  i.x.ax=0;
  int86(0x33,&i,&o);
  return(o.x.ax);
 }

 showmouseptr()
 {
  i.x.ax=1;
  int86(0x33,&i,&o);
  }

  getmousepos(int *button,int *x,int *y)
  {
  i.x.ax=3;
  int86(0x33,&i,&o);
  *button=o.x.bx;
  *x=o.x.cx;
  *y=o.x.dx;
  }
  hidemouseptr()
 {
  i.x.ax=2;
  int86(0x33,&i,&o);
 }
 void drawcont()
 {
   int i,j;
   balls=32;
  scor=0;
   for(j=1;j<=7;j++)
   {
      for(i=1;i<=7;i++)
      {
      if((j<3&&i<3)||(j<3&&i>5)||(j>5&&i<3)||(j>5&&i>5))
          col[j][i]=2;
      else
        if(j!=4||i!=4)
         col[j][i]=1;
        else
           col[j][i]=0;
      }

   }
 }

/********************************DRAW************************************/
  void draw()
 {
  int j,i,x,y,d,a,button,uyc,uxc,cxc,cyc,b,ex;
  setcolor(15);
  x=getmaxx();
  y=getmaxy();
  d=x/22;
  a=d/2;
  uyc=2*d+a;
  cyc=6*d+a;
  uxc=8*d+a;
  cxc=4*d+a;
  cleardevice();
  setfillstyle(1,bkcol);
  floodfill(5,5,15);
  setfillstyle(1,coll);
  bar(10,48,120,67);
  settextstyle(11,0,9);
  outtextxy(25,55,"ENVIRONMENT");
  rectangle(9,47,121,68);
  settextstyle(8,0,1);
  outtextxy(10,8,"BALLS");
  outtextxy(475,8,"SCORE");
  outtextxy(39,441,"HELP");

    setfillstyle(1,0);
     hidemouseptr();
    bar(490,448,102,463);
     showmouseptr();
    gotoxy(11,2);
    printf("   ");
    gotoxy(69,2);
    printf("   ");
    rectangle(542,15,568,32);
     rectangle(79,15,103,32);
    rectangle(491,447,100,464);
    gotoxy(11,2);
    printf("%2d",balls);
    gotoxy(69,2);
    printf("%d",scor);
   for(i=1;i<=2;i++)
   {
    for(j=3;j<=5;j++)
    {
     circle(uxc,uyc,a);
     if(col[j][i]==1)
      setfillstyle(1,coll);
     else
      setfillstyle(1,bkcol);
     floodfill(uxc,uyc,15);
     uxc=uxc+2*d;
    }
    uxc=uxc-6*d;
    uyc=uyc+2*d;
   }
   for(i=3;i<=5;i++)
   {
    for(j=1;j<=7;j++)
    {
     circle(cxc,cyc,a);
     if(col[j][i]==1)
      setfillstyle(1,coll);
     else
      setfillstyle(1,bkcol);
     floodfill(cxc,cyc,15);
     cxc=cxc+2*d;
    }
    cxc=cxc-14*d;
    cyc=cyc+2*d;
   }

    uyc=uyc+6*d;


 for(i=6;i<=7;i++)
   {
    for(j=3;j<=5;j++)
    {
     circle(uxc,uyc,a);
     if(col[j][i]==1)
      setfillstyle(1,coll);
     else
      setfillstyle(1,bkcol);
     floodfill(uxc,uyc,15);
     uxc=uxc+2*d;
    }
     uxc=uxc-6*d;
     uyc=uyc+2*d;
   }
   rectangle(4,40,x-6,y-4);
   setcolor(15);
   settextstyle(8,0,1);
   outtextxy(x/2-60,15,"BRAINVITA");
   setcolor(0);
   rectangle(500,430,570,460);
   setfillstyle(1,coll);
   line(501,431,501,460);
   line(501,459,569,459);
   floodfill(520,440,0);
   setcolor(15);
   settextstyle(6,0,3);
   outtextxy(510,425,"MENU");
   }
/***************************PLAY*************************************/
 void play()
 {
   int
button,x,y,i,help=0,tmp=0,j,a=0,poscodex1,poscodex2,poscodey1,poscodey2,ce
nterx,centery,cencodex=0,cencodey=0,d;
   d=getmaxx()/22;
   showmouseptr();
   delay(500);
   gotoxy(11,2);
   printf("%2d",balls);
   gotoxy(69,2);
   printf("%d",scor);
   setcolor(7);
   settextstyle(11,0,9);
   while(1)
   {
    for(i=0;i<8;i++)
   {
    for(j=0;j<8;j++)
    {
     if(col[i][j]==1)
     {
      if((col[i][j+1]==1 && col[i][j+2]==0) || (col[i][j-1]==1 &&
col[i][j-2]==0) || (col[i+1][j]==1 && col[i+2][j]==0) ||
(col[i-1][j]==1
&& col[i-2][j]==0))
       {
    a=1;
       }
      }
     }
   }
   if(a!=1)
   {
     hidemouseptr();
     setfillstyle(1,0);
    bar(490,448,102,463);
    if(balls==1)
    outtextxy(102,450,"YOU WON");
    else
    outtextxy(102,450,"TRY AGAIN");
  showmouseptr();
    delay(1000);
      hidemouseptr();
     bar(490,448,102,463);
    outtextxy(102,450,"PRESS ANY KEY");
     showmouseptr();
score();
    hidemouseptr();
    break;
   }
    hidemouseptr();
     setfillstyle(1,0);
    bar(490,448,102,463);
    settextstyle(11,0,9);
    outtextxy(102,450,"CLICK A BALL TO BE TAKEN");
    showmouseptr();
    do
    {
     getmousepos(&button,&posx1,&posy1);
     if(posx1>500&&posx1<570&&posy1>430&&posy1<460)
     {
    if(help!=1)
    {
    bar(490,448,102,463);
    outtextxy(102,450,"TO OPEN MENU");
    help=1;
    }
     }
     else if(posx1>9&&posx1<121&&posy1>47&&posy1<68)
     {
       if(help!=2)
       {
    bar(490,448,102,463);
    outtextxy(102,450,"TO CHANGE ENVIRONMENT COLOUR");
    help=2;
       }
     }
     else
     {
       if(help!=3)
       {
    bar(490,448,102,463);
    outtextxy(102,450,"CLICK A BALL TO BE TAKEN");
    help=3;
    }
     }
    }while(button!=1);

   if(posx1>500&&posx1<570&&posy1>430&&posy1<460)
   {
    cleardevice();
    hidemouseptr();
   return;
   }
    if(posx1>9&&posx1<121&&posy1>47&&posy1<68)
   {
     if(tmp==4)
         tmp=0;
     else
        tmp++;
     switch(tmp)
     {
     case 0:
       bkcol=1;
       coll=4;
       break;
     case 4:
       bkcol=2;
       coll=6;
       break;
     case 1:
       bkcol=9;
       coll=7;
       break;
       case 2:
       bkcol=12;
       coll=7;
       break;
       case 3:
       bkcol=1;
       coll=9;
       break;
     }
     hidemouseptr();
     draw();
     showmouseptr();
     continue;
   }


   delay(500);
   poscodex1=getxcode(1,d);
   poscodey1=getycode(1,d);
   if(col[poscodex1][poscodey1]==0 || poscodex1==0 || poscodey1==0)
   {
   printf("a");
  hidemouseptr();
       bar(490,448,102,463);
    outtextxy(102,450,"SORRY THIS MOVE IS NOT ALLOWED");
   showmouseptr();
 delay(500);
   continue;
   }
   hidemouseptr();
     bar(490,448,102,463);
    outtextxy(102,450,"L-CLICK TO MOVE OR R-CLICK TO UNSELECT");
   showmouseptr();
   help=0;
   do
    {
     getmousepos(&button,&posx2,&posy2);
    if(posx2>500&&posx2<570&&posy2>430&&posy2<460)
     {
    if(help!=1)
    {
    bar(490,448,102,463);
    outtextxy(102,450,"TO OPEN MENU");
    help=1;
    }
     }
     else if(posx2>9&&posx2<121&&posy2>47&&posy2<68)
     {
       if(help!=2)
       {
    bar(490,448,102,463);
    outtextxy(102,450,"TO CHANGE ENVIRONMENT COLOUR");
    help=2;
       }
     }
     else
     {
       if(help!=3)
       {
    bar(490,448,102,463);
    outtextxy(102,450,"L-CLICK TO MOVE OR R-CLICK TO UNSELECT");
    help=3;
    }
     }
    }while(button!=1&&button!=2);
    if(button==2)
    continue;
   if(posx2>500&&posx2<570&&posy2>430&&posy2<460)
   {
    cleardevice();
    hidemouseptr();
    return;
   }
    if(posx2>9&&posx2<201&&posy2>47&&posy2<68)
   {
     if(tmp==4)
         tmp=0;
     else
        tmp++;
     switch(tmp)
     {
     case 0:
       bkcol=1;
       coll=4;
       break;
     case 4:
       bkcol=2;
       coll=6;
       break;
     case 1:
       bkcol=9;
       coll=7;
       break;
       case 2:
       bkcol=12;
       coll=7;
       break;
       case 3:
       bkcol=1;
       coll=9;
       break;
     }
     hidemouseptr();
     draw();
     showmouseptr();
     continue;
   }

    poscodex2=getxcode(2,d);
   poscodey2=getycode(2,d);
   if(col[poscodex2][poscodey2]==1||col[poscodex2][poscodey2]==2 ||
poscodex2==0 || poscodey2==0)
   {
   printf("a");
 hidemouseptr();
    bar(490,448,102,463);
    outtextxy(102,450,"SORRY THIS MOVE IS NOT ALLOWED");
  showmouseptr();
   delay(500);
   continue;
   }
   /*****************LINECHECK*****************************/
   if(linecheck(poscodex1,poscodey1,poscodex2,poscodey2)==1 &&
(poscodex1-poscodex2==2 || poscodex2-poscodex1==2 ||
poscodey1-poscodey2==2 || poscodey2-poscodey1==2))
   {
    cencodex=(poscodex2+poscodex1)/2;
    cencodey=(poscodey1+poscodey2)/2;
    centerx=(posx1+posx2)/2;
    centery=(posy1+posy2)/2;
   }
   else
   {
   printf("a");
   hidemouseptr();
    bar(490,448,102,463);
    outtextxy(102,450,"SORRY MOVE IS NOT ALLOWED");
   showmouseptr();
   delay(500);
   continue;
   }
   if(col[cencodex][cencodey]!=1)
   {
     printf("a");
 hidemouseptr();
    bar(490,448,102,463);
    outtextxy(102,450,"SORRY MOVE IS NOT ALLOWED");
    hidemouseptr();
   delay(500);
   continue;
   }
   setfillstyle(1,bkcol);
   hidemouseptr();
   floodfill(posx1,posy1,15);
   floodfill(centerx,centery,15);
   setfillstyle(1,coll);
   floodfill(posx2,posy2,15);
   showmouseptr();
   col[poscodex1][poscodey1]=0;
   col[cencodex][cencodey]=0;
   col[poscodex2][poscodey2]=1;
   a=0;
   balls--;
   scor+=10;
    gotoxy(11,2);
    printf("%2d",balls);
    gotoxy(69,2);
    rectangle(542,15,568,32);
    printf("%d",scor);
     delay(500);
   }
  cleardevice();
 }
/*********************************GETXCODE**************/
 int getxcode(int x,int d)
  {
   int x1=0;
   if(x==1)
   {
   if(posx1>4*d&&posx1<5*d)
   {
    x1=1;
    posx1=4*d+d/2;
    }
   else if(posx1>6*d&&posx1<7*d)
   {
    x1=2;
    posx1=6*d+d/2;
   }
   else if(posx1>8*d&&posx1<9*d)
   {
    x1=3;
    posx1=8*d+d/2;
   }
   else if(posx1>10*d&&posx1<11*d)
   {
     x1=4;
     posx1=10*d+d/2;
   }
   else if(posx1>12*d&&posx1<13*d)
   {
     x1=5;
     posx1=12*d+d/2;
   }
   else if(posx1>14*d&&posx1<15*d)
   {
    x1=6;
    posx1=14*d+d/2;
    }
   else if(posx1>16*d&&posx1<17*d)
    {
     x1=7;
     posx1=16*d+d/2;
     }
    else
    x1=0;
    }
    else if(x==2)
    {
    if(posx2>4*d&&posx2<5*d)
   {
    x1=1;
    posx2=4*d+d/2;
    }
   else if(posx2>6*d&&posx2<7*d)
   {
    x1=2;
    posx2=6*d+d/2;
   }
   else if(posx2>8*d&&posx2<9*d)
   {
    x1=3;
    posx2=8*d+d/2;
   }
   else if(posx2>10*d&&posx2<11*d)
   {
     x1=4;
     posx2=10*d+d/2;
   }
   else if(posx2>12*d&&posx2<13*d)
   {
     x1=5;
     posx2=12*d+d/2;
   }
   else if(posx2>14*d&&posx2<15*d)
   {
    x1=6;
    posx2=14*d+d/2;
    }
   else if(posx2>16*d&&posx2<17*d)
    {
     x1=7;
     posx2=16*d+d/2;
     }
    else
    x1=0;
    }

  return x1;
 }
/************************************GETYCODE******************/
  int getycode(int y,int d)
  {
   int y1=0;
   if(y==1)
   {
   if(posy1>2*d&&posy1<3*d)
   {
    y1=1;
    posy1=2*d+d/2;
    }
   else if(posy1>4*d&&posy1<5*d)
   {
    y1=2;
    posy1=4*d+d/2;
   }
   else if(posy1>6*d&&posy1<7*d)
   {
    y1=3;
    posy1=6*d+d/2;
   }
   else if(posy1>8*d&&posy1<9*d)
   {
     y1=4;
     posy1=8*d+d/2;
   }
   else if(posy1>10*d&&posy1<11*d)
   {
     y1=5;
     posy1=10*d+d/2;
   }
   else if(posy1>12*d&&posy1<13*d)
   {
    y1=6;
    posy1=12*d+d/2;
    }
   else if(posy1>14*d&&posy1<15*d)
    {
     y1=7;
     posy1=14*d+d/2;
     }
    else
    y1=0;
   }
   else if(y==2)
   {
   if(posy2>2*d&&posy2<3*d)
   {
    y1=1;
    posy2=2*d+d/2;
    }
   else if(posy2>4*d&&posy2<5*d)
   {
    y1=2;
    posy2=4*d+d/2;
   }
   else if(posy2>6*d&&posy2<7*d)
   {
    y1=3;
    posy2=6*d+d/2;
   }
   else if(posy2>8*d&&posy2<9*d)
   {
     y1=4;
     posy2=8*d+d/2;
   }
   else if(posy2>10*d&&posy2<11*d)
   {
     y1=5;
     posy2=10*d+d/2;
   }
   else if(posy2>12*d&&posy2<13*d)
   {
    y1=6;
    posy2=12*d+d/2;
    }
   else if(posy2>14*d&&posy2<15*d)
    {
     y1=7;
     posy2=14*d+d/2;
     }
    else
    y1=0;
   }
    return y1;
  }
/*******************************LINECHECK**********************/
  int linecheck(int a,int b,int c,int d)
  {
    if(a==c||b==d)
    return 1;
    else
    return 0;
   }

   score()
  {
  hidemouseptr();
  setcolor(4);
  setfillstyle(1,4);
   settextstyle(8,0,2);
   bar(200,120,400,160);
   setcolor(YELLOW);
   rectangle(200,120,400,160);
   setcolor(14);
   if(balls==1)
   {
    setcolor(14);
    outtextxy(250,130,"U WON");
    }
    else
    {
       outtextxy(250,130,"Try Again!");
    }
    getch();
    showmouseptr();
   return;
  }
  title()
  {
   int i,j;
   setcolor(3);
   cleardevice();
   sound(10);
   for(i=0,j=100;i<10;i++,j-=10)
   {
    delay(100);
    setcolor(i);
    cleardevice();
    settextstyle(7,0,i);
    outtextxy(j,100,"BRAIN VITA");
    }
    nosound();
    getch();
  }

2 comments:

Anonymous said...

sir please update the website and maintain an blog archive..gadget
nice work

Ruchin Panchal said...

Sorry Bro....I have No Time For Blogging...So Just Sorry For This....ANd Be Happy Wid This..;)