Advertisement
bhushan23

pong.cpp

Jul 8th, 2012
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1.  
  2. #include<graphics.h>
  3. #include<iostream.h>
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<dos.h>
  7. #include<stdlib.h>
  8. #include<time.h>
  9. int getkey();
  10. struct time t;
  11. void time()
  12. {
  13. //   struct  time t;
  14.  
  15.      gettime(&t);
  16.      printf("   The current time is: %2d:%02d:%02d.%02d",t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
  17. }
  18. void date()
  19. {
  20.      struct date d;
  21.  
  22.      getdate(&d);
  23.      printf("    DATE: %d/%d/%d" ,d.da_day, d.da_mon,d.da_year);
  24.  
  25. }
  26. void main()
  27. {
  28. //registerbgidriver(EGAVGA_driver);
  29. int gd,gm;
  30. gd=DETECT;
  31. initgraph(&gd,&gm,"..//bgi ");
  32. cleardevice();
  33.  
  34. int ar,xc,yc,xr=0,yr=195,a=1,b=1,c=0,are;
  35. void *bu,*buf;
  36. int X=0,Y=0,s,area1;
  37. void *buff1;
  38.  
  39. setcolor(3);
  40. rectangle(80,445,159,452);
  41. setfillstyle(1,1);
  42. floodfill(82,447,3);
  43. area1=imagesize(80,445,159,452);
  44. buff1=malloc(area1);
  45. getimage(80,445,159,452,buff1);
  46.  
  47.  
  48.  int area2;
  49. void *buffcomp;
  50.  
  51. rectangle(80,20,160,27);
  52. setfillstyle(1,RED);
  53. floodfill(85,23,3);
  54. area2=imagesize(80,20,160,27);
  55. buffcomp=malloc(area2);
  56. getimage(80,20,160,27,buffcomp);
  57. setcolor(4);
  58. line(0,479,640,479);
  59.  
  60.         int area,x=325,y=325,ch,xdirn=-1,ydirn=-1,step;
  61.         int maxx,maxy;
  62.         void *buff;
  63.  
  64.         setcolor(WHITE);
  65.         setfillstyle(SOLID_FILL,RED);
  66.         circle(350,350,5);
  67.         floodfill(350,350,WHITE);
  68.  
  69.         area=imagesize(345,345,355,355);
  70.         buff=malloc(area);
  71.         getimage(345,345,355,355,buff);
  72.         putimage(345,345,buff,XOR_PUT);
  73.                 int xd;
  74.                 time();
  75.                  int hour=  t.ti_hour;
  76.                  int min=t.ti_min;
  77.                  int sec= t.ti_sec;
  78.                  int hund= t.ti_hund;
  79.  
  80.         maxx=getmaxx();
  81.     while (1)
  82.  
  83.     {
  84.  gotoxy(1,1);
  85.     time();
  86.     date();
  87.         putimage(x, y, buff, XOR_PUT);
  88.         delay(15);
  89.         putimage(x, y, buff, XOR_PUT);
  90.  
  91.         x=x+(xdirn*2);
  92.         y=y+(ydirn*3);
  93.  
  94.         if ( x + 10 - 1 > 640 )
  95.          {
  96.              xdirn*=-1;
  97.              x = 640 - 10 + 1;
  98.          }
  99.         if (x < 0)
  100.          {
  101.              xdirn*=-1;
  102.              x = 0;
  103.          }
  104.  
  105.             if(x>maxx-40)
  106.             xd=maxx-60;
  107.             else if(x<41)
  108.             xd=22;
  109.             else
  110.             xd=x;
  111.  
  112.             setfillstyle(1,BLACK);
  113.         bar(0,0,getmaxx(),28);
  114.         putimage(xd-20,20,buffcomp,XOR_PUT);
  115.  
  116.  
  117.         if ( y + 10 - 1 > 470 )
  118.          {
  119. //       ydirn*=-1;
  120. //       y = 470 - 10 + 1;
  121.              cleardevice();
  122.              outtextxy(200,200,"Sorry! You loose the game.");
  123.              outtextxy(250,240,"Try Again!!!");
  124.              gotoxy(30,8);
  125.              cout<<"Total Score : "<<c;
  126.              getch();
  127.              cleardevice();
  128.              goto tt;
  129.          }
  130.         if(y==0)
  131.         {
  132.              outtextxy(180,200,"Congrats! You Dominated.");
  133.              gotoxy(30,8);
  134.  
  135.              delay(5000);
  136.              cleardevice();
  137.              getch();
  138.              goto tt;
  139.  
  140.          }
  141.  
  142.         if ( getpixel(x,y)==1||getpixel(x,y)==RED )
  143.          {
  144.              sound(800);
  145.              delay(15);
  146.              sound(800);
  147.              delay(50);
  148.              ydirn*=-1;
  149.          }
  150.         if( y < 0 )
  151.          {
  152.             ydirn*=-1;
  153.             y=0;
  154.          }
  155.  
  156.  
  157. if( kbhit() )
  158. {
  159.  s=getkey();
  160.  if(s!=1)
  161.  {
  162.      if(X>480)
  163.         {
  164.     X=480;
  165.     putimage(160+X,445+Y,buff1,XOR_PUT);
  166.         }
  167.      if(X<-80)
  168.         {
  169.     X=-80;
  170.     putimage(80+X,445+Y,buff1,XOR_PUT);
  171.         }
  172.      putimage(80+X,445+Y,buff1,XOR_PUT);
  173. //if(s==72)
  174. // Y+=-40;
  175.      if(s==75)
  176.     X+=-40;
  177. //if(s==80)
  178. // Y+=40;
  179.      if(s==77)
  180.     X+=40;
  181.      putimage(80+X,445+Y,buff1,XOR_PUT);
  182. //cout<<X;
  183.     }
  184.     if(s==1)
  185.     {
  186. tt:
  187.  int hour1= t.ti_hour;
  188.                  int min1=t.ti_min;
  189.                  int sec1= t.ti_sec;
  190.                  int hund1= t.ti_hund;
  191.                  gotoxy(getmaxx()/2,getmaxy()/2);
  192.                  printf("You have played for %d:%d:%d:%d",hour1-hour,min1-
  193.  
  194. min,sec1=sec,hund1-hund);
  195.                  getchar();
  196. free(bu);
  197.         free(buff);
  198.         free(buff1);
  199.         closegraph();
  200.         exit(0);
  201.     }
  202.  }
  203. }
  204. free(buff);
  205. free(buff1);
  206. closegraph();
  207. }
  208.  
  209. int getkey()
  210. {
  211. union REGS j,o;
  212. j.h.ah=0;
  213. int86(22,&j,&o);
  214. return(o.h.ah);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement