home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / w / wgt3_ex.zip / PACMAN2.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  5KB  |  251 lines

  1. #include <dos.h>
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <wgt.h>
  6. #include <scroll.h>
  7.  
  8. // WordUp Graphics Toolkit Scrolling Demo
  9. // PACMAN!
  10.  
  11.  
  12. void checkpos(void);
  13.  
  14. int spx,spy;
  15. int pacanim,pacdir;
  16. int ox,oy;
  17.  
  18. wgtmap mymap;            // our world map
  19.  
  20. int kbdon[35]={0,0,0,0,0};    // our keyboard on/off array;
  21. int kbdscanlist[35]={72,80,75,77,1};    // our keyboard scan codes;
  22. // You must have the above two if you want the keyboard interrupt 
  23.  
  24. color palette[256];        // our palette of colours
  25.  
  26. block blocks[201];        // our blocks for the map
  27. block sprites[201];        // our sprites 
  28.  
  29.  
  30. int bonus,traptime[5];
  31. int killtime[5];
  32. int i;
  33. int pacspeed=8,ghostspeed=8;
  34. int moved[5],movedir[5];
  35. int bluetime;
  36.  
  37. void moveghost(int);
  38.  
  39. void main(void)
  40. {
  41. vga256();
  42. wloadsprites(&palette,"pacman.spr",blocks);        // load blocks
  43. wloadsprites(&palette,"pac.spr",sprites);        // load sprites
  44.  
  45. mymap=wloadmap("pacman.wmp");        // load our world map
  46.  
  47.  
  48. winitscroll(10,10);                // make a 12x10 box
  49.                         // for the scrolling
  50.  
  51. wnormscreen();                    // go back to normal screen
  52. wcls(0);                    // clear it
  53. wbutt(1,1,318,198);
  54.  
  55. wshowwindow(0,0,mymap);            // start looking at world
  56.                         // at 0,0
  57. installkbd();                    // start new keyboard interrupt
  58.  
  59. numsprites=5;
  60. spr[0].on=1; spr[0].x=16; spr[0].y=16; spr[0].num=1;
  61.  
  62. spr[1].on=1; spr[1].x=16; spr[1].y=16; spr[1].num=21;
  63. spr[2].on=1; spr[2].x=16; spr[2].y=16; spr[2].num=22;
  64. spr[3].on=1; spr[3].x=16; spr[3].y=16; spr[3].num=23;
  65. spr[4].on=1; spr[4].x=16; spr[4].y=16; spr[4].num=24;
  66. for (i=1; i<5; i++) moved[i]=0;
  67. pacanim=1;
  68.  
  69.  
  70. do
  71. {
  72. spx=0;
  73. spy=0;
  74. ox=spr[0].x;
  75. oy=spr[0].y;
  76.  
  77. if (kbdon[2]==1)        // Pressing left
  78.   {
  79.   spr[0].x-=pacspeed;
  80.   checkpos();
  81.   pacdir=2;
  82.   }
  83. else if (kbdon[3]==1)    // Pressing right
  84.   {
  85.   spr[0].x+=pacspeed;
  86.   checkpos();
  87.   pacdir=0;
  88.   }
  89. else if (kbdon[0]==1)        // Pressing up
  90.   {
  91.   spr[0].y-=pacspeed;
  92.   checkpos();
  93.   pacdir=1;
  94.   }
  95. else if (kbdon[1]==1)    // Pressing down
  96.   {
  97.   spr[0].y+=pacspeed;
  98.   checkpos();
  99.   pacdir=3;
  100.   }
  101.  
  102.     if (bluetime>0)
  103.     bluetime--;
  104.  
  105. for (i=1; i<5; i++)
  106.  {
  107.  if (traptime[i]==0)
  108.    {
  109.    moveghost(i);
  110.  if (bluetime>0)
  111.     spr[i].num=25;
  112.  if ((bluetime==1) | ((bluetime<40) & (bluetime % 2==1)))
  113.     spr[i].num=20+i;
  114.  if ((soverlap(i,0)==1) & (traptime[i]==0))
  115.    {
  116.    if (bluetime>0)
  117.      {
  118.      spr[i].num=27+bonus;
  119.      bonus++;
  120.      traptime[i]=100;
  121.      }
  122.    }
  123.   }
  124.   else traptime[i]--;
  125.  if (traptime[i]==1)
  126.     {
  127.     spr[i].x=256;
  128.     spr[i].y=256;
  129.     bonus=0;
  130. }
  131.  }
  132.  
  133.  
  134. pacanim++;
  135. if (pacanim>5)
  136.   pacanim=1;
  137. spr[0].num=pacanim+(pacdir*5);
  138.  
  139.  
  140. if (spr[0].x-worldx<windowmaxx/2-1)
  141.    spx=-pacspeed;
  142. else if (spr[0].x-worldx>windowmaxx/2+1)
  143.    spx=pacspeed;
  144. if (spr[0].y-worldy<windowmaxy/2-1)
  145.    spy=-pacspeed;
  146. else if (spr[0].y-worldy>windowmaxy/2+1)
  147.    spy=pacspeed;
  148.  
  149. nosound();
  150. wscrollwindow(spx,spy,mymap);    // update the scrolling window
  151. wshowsprites();
  152. wcopyscroll(80,15);
  153. } while (kbdon[4] !=1);            // until ESC is pressed
  154.  
  155. uninstallkbd();
  156. wendscroll();
  157. wfreesprites(blocks);
  158. wfreesprites(sprites);
  159. wfreemap(mymap);
  160. textmode(C80);
  161. }
  162.  
  163. void checkpos(void)
  164. {
  165. int hit=0;
  166.  
  167.   i=wgetworldblock(spr[0].x,spr[0].y,mymap);
  168.   if (i<12)
  169.     hit=1;
  170.   i=wgetworldblock(spr[0].x+15,spr[0].y,mymap);
  171.   if (i<12)
  172.     hit=1;
  173.   i=wgetworldblock(spr[0].x,spr[0].y+15,mymap);
  174.   if (i<12)
  175.     hit=1;
  176.   i=wgetworldblock(spr[0].x+15,spr[0].y+15,mymap);
  177.   if (i<12)
  178.     hit=1;
  179.   i=wgetworldblock(spr[0].x+7,spr[0].y+7,mymap);
  180.   if (i>=13)
  181.     {
  182.     sound(500);
  183.     wputworldblock(spr[0].x+7,spr[0].y+7,12,mymap);
  184.     }
  185.   if (i>13)
  186.      {
  187.      bluetime+=150;
  188.      }
  189.  
  190.   if (hit==1)
  191.      {
  192.      spr[0].x=ox;
  193.      spr[0].y=oy;                   
  194.      }
  195.   }
  196.  
  197.  
  198. void moveghost(int numb)
  199. {
  200. int gox,goy,hit=0,j;
  201.  
  202. gox=spr[numb].x;
  203. goy=spr[numb].y;
  204.  
  205. moveagain:
  206. ;
  207. if (moved[numb]==0)
  208.    movedir[numb]=(rand() % 5)+1;
  209.  
  210.    if (movedir[numb]==1)
  211.    spr[numb].x+=ghostspeed;
  212.    else if (movedir[numb]==2)
  213.    spr[numb].x-=ghostspeed;
  214.    else if (movedir[numb]==3)
  215.    spr[numb].y+=ghostspeed;
  216.    else if (movedir[numb]==4)
  217.    spr[numb].y-=ghostspeed;
  218.  
  219.    moved[numb]+=ghostspeed;
  220.    if (moved[numb]>=16)
  221.      moved[numb]=0;
  222.  
  223.  if (spr[numb].x<0)
  224.     spr[numb].x=0;
  225. else if (spr[numb].x>(mapwidth-1)*16)
  226.     spr[numb].x=(mapwidth-1)*16;
  227.  if (spr[numb].y<0)
  228.     spr[numb].y=0;
  229. else if (spr[numb].y>(mapheight-1)*16)
  230.     spr[numb].y=(mapheight-1)*16;
  231.  
  232.   j=wgetworldblock(spr[numb].x,spr[numb].y,mymap);
  233.   if (j<12)
  234.     hit=1;
  235.   j=wgetworldblock(spr[numb].x+15,spr[numb].y,mymap);
  236.   if (j<12)
  237.     hit=1;
  238.   j=wgetworldblock(spr[numb].x,spr[numb].y+15,mymap);
  239.   if (j<12)
  240.     hit=1;
  241.   j=wgetworldblock(spr[numb].x+15,spr[numb].y+15,mymap);
  242.   if (j<12)
  243.     hit=1;
  244.   if (hit==1)
  245.      {
  246.      spr[numb].x=gox;
  247.      spr[numb].y=goy;
  248.      moved[numb]=0;
  249.      }
  250. }
  251.