bhushan23

pong.cpp

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