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 / MUNCH.C < prev    next >
C/C++ Source or Header  |  1992-09-15  |  12KB  |  468 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. /* WordUp Graphics Toolkit Scrolling Demo
  8.    Feel free to use any part of this program in your own...
  9.  
  10.    Munchkin: A simple platform type game starring a munchkin as
  11.          the main character
  12. */
  13.  
  14.  
  15. #define YOU 37
  16.  
  17.  
  18. void checkfeet(void);
  19. void checkhead(void);
  20. void checkright(void);
  21. void checkleft(void);
  22. // These find out if you are hitting a wall in each direction
  23.  
  24. void findelevators(void);
  25. // Search for elevators and store in an array, similar to the technique
  26. // used in forest.c
  27.  
  28. void upelev(void);
  29. void downelev(void);
  30. // Move and elevator up or down.
  31.  
  32. void checkelevators(void);
  33. // Sees if elevators needs to drop
  34.  
  35. void moveguys(void);
  36. // Move the bad guys around, take care of them bumping into walls.
  37.  
  38.  
  39. int ox,oy;  // old x and y coords, in case you hit a wall
  40. int dir;    // the direction you are heading,
  41. int anim;   // Running animation counter
  42. int jumping;// Nothing under your feet?
  43. int addy;   // Amount to add to y coord if you're falling or jumping.
  44.  
  45. int spx,spy;
  46. int feet1,feet2,head1,head2;
  47. int i;
  48.  
  49. wgtmap mymap;            // our world map
  50.  
  51. int kbdon[35]={0,0,0,0,0,0};    // our keyboard on/off array;
  52. int kbdscanlist[35]={72,80,75,77,1,29};    // our keyboard scan codes;
  53. // You must have the above two if you want the keyboard interrupt 
  54.  
  55. color palette[256];        // our palette of colours
  56.  
  57. block blocks[201];        // our blocks for the map
  58. block sprites[201];        // our sprites 
  59.  
  60.  
  61. typedef struct {
  62.     int curheight;   // current height
  63.     int origy,origx; // original x and y, before game started
  64.     int timer;       // count down before it starts falling
  65.     } elevator;
  66.  
  67. elevator elev[30]; // make room for 30 elevators
  68.  
  69.  
  70. int replace[200]; // stores which tile was behind the elevator
  71. int elevup=-1;
  72. int numelev=0;    // total number of elevators
  73.  
  74.  
  75. void main(void)
  76. {
  77. printf("Wordup Graphics Toolkit     4-WAY SCROLLING DEMO\n");
  78. printf("Arrow keys move, CTRL jumps. Up/down looks in direction or operates\n");
  79. printf("elevators.\n");
  80. printf("\nPress any key\n\n");
  81. getch();
  82.  
  83. vga256();                    // init
  84. wloadsprites(&palette,"munchmap.spr",blocks);        // load blocks
  85. wloadsprites(&palette,"munchkin.spr",sprites);        // load sprites
  86.  
  87. mymap=wloadmap("munch.wmp");        // load our world map
  88.  
  89. findelevators();  // search and store the elevators
  90.  
  91. winitscroll(13,9);                // make a 13x9 box
  92.                         // for the scrolling
  93.  
  94. wnormscreen();                    // go back to normal screen
  95. wcls(0);                    // clear it
  96. wbutt(0,0,319,199);
  97.  
  98. wshowwindow(0,10,mymap);            // start looking at world
  99.                         // at 0,10
  100. installkbd();                    // start new keyboard interrupt
  101.  
  102. numsprites=40;
  103. wobject[YOU].on=1; wobject[YOU].x=16; wobject[YOU].y=242; wobject[YOU].num=1;
  104.  
  105. jumping=0; addy=0;
  106. anim=2;
  107.  
  108. do
  109. {
  110. spx=0;         // scrolling x speed
  111. spy=0;        // scrolling y speed
  112. ox=wobject[YOU].x;  // store old x and y coords
  113. oy=wobject[YOU].y;
  114.  
  115. if (jumping==1) // increase addy to simulate gravity
  116.   addy+=2;
  117. if (addy>15)    // can't fall more than 15 pixels at a time
  118.    addy=15;
  119.  
  120. if ((kbdon[5]==1) & (jumping==0))  // Jump up
  121.    {
  122.    jumping=1;
  123.    addy=-14;
  124.    }
  125.  
  126. if (kbdon[2]==1)        // Pressing left
  127.   {
  128.   wobject[YOU].x-=8;
  129.   checkleft();
  130.   if (dir !=1)   // if you are facing right, reset the animation
  131.     {
  132.     dir=1;
  133.     anim=5;
  134.     }
  135.   anim++;
  136.   if (anim>8)  // cycle through running animation
  137.     anim=5;
  138.   }
  139. else if (kbdon[3]==1)    // Pressing right
  140.   {
  141.   wobject[YOU].x+=8;
  142.   checkright();
  143.   if (dir !=2) // reset animation
  144.     {
  145.     dir=2;
  146.     anim=1;
  147.     }
  148.   anim++;
  149.   if (anim>4) // running animatio loop
  150.     anim=1;
  151.   }
  152.  
  153. wobject[YOU].num=anim;    // set the object to the current animation
  154.  
  155. if (wobject[YOU].x==ox)    // If you didn't move left or right,
  156.   if (dir==1)
  157.      wobject[YOU].num=9;    // show the non-running sprite
  158.   else wobject[YOU].num=1;  // for each direction
  159.  
  160. wobject[YOU].y+=addy;     // add the jumping or falling amount
  161.  
  162. if (wobject[YOU].y<0) wobject[YOU].y=0; // can't jump off top of screen
  163. if (addy<0)    // if jumping up,
  164.    checkhead(); // make sure you don't bump your head
  165.  
  166.  
  167. if ((jumping==1))      // set to a different sprite if you are jumping
  168.   if (dir==1)            // so you don't run in the air
  169.      wobject[YOU].num=6;
  170.   else wobject[YOU].num=2;
  171.  
  172.  checkfeet();        // check what is under your feet
  173.  
  174. spx=wobject[YOU].x-worldx-windowmaxx/2;    // find out if we need to scroll
  175. spy=wobject[YOU].y-worldy-windowmaxy/2;
  176.  
  177. if (kbdon[0]==1)        // Pressing up
  178.   {
  179.   if ((feet1==105) | (feet2==105))  // if standing on an elevator, move it up
  180.      upelev();
  181.   else
  182.      spy=-4;    // otherwise look up
  183.   }
  184. if (kbdon[1]==1)    // Pressing down
  185.   {
  186.   if ((feet1==105) | (feet2==105))   // and the same for down...
  187.      downelev();
  188.   else
  189.      spy=+4;
  190.  }
  191.  
  192. checkelevators(); // make sure they come back down when not standing on them
  193.  
  194. moveguys();  // move all the bad guys
  195.  
  196.  
  197. wscrollwindow(spx,spy,mymap);    // update the scrolling window
  198. wshowobjects();
  199.  
  200. wcopyscroll(30,10);
  201. } while (kbdon[4] !=1);            // until ESC is pressed
  202.  
  203. uninstallkbd();
  204.  
  205. wendscroll();
  206. wfreesprites(blocks);
  207. wfreesprites(sprites);
  208. free(mymap);
  209. textmode(C80);
  210. }
  211.  
  212.  
  213. // see what is to the right of you
  214. void checkright(void)
  215. {
  216. /*
  217. To find out where the main character is on the map, and what is around
  218. him/her, you need to use wgetworldblock to return what is at a certain
  219. location on the map.
  220.  
  221. If the main character is 16x16, and is moving right:
  222.  
  223.  
  224.            --------- \
  225.            |       | |
  226.            |Block 1| | 16 pixels high
  227.      ----------|       | |
  228.      |   /\   ||       | /
  229.      |   \/   |---------
  230.      |  ----  ||       |
  231.      |   /\   ||Block 2|
  232.      ----------|       |
  233.      Main Char.|       |
  234.      16 pixels ---------
  235.     high
  236.  
  237. Since you could be halfway between two blocks, it is necessary to
  238. check two places. If you only check one, you will probably be able to
  239. walk through walls.
  240.  
  241. Now suppose the main character was 32 pixels high.
  242. You will need to check 3 places. The diagram would look like this:
  243.  
  244.            --------- \
  245.            |Block 1| | 16 pixels high
  246.      ----------|       | |
  247.      |   /\   ||-------| /
  248.      |   \/   ||Block 2|
  249.      |  ----  ||       |
  250.      |   /\   ||-------|
  251.      ----------|Block 3|
  252.      Main Char.|       |
  253.      32 pixels ---------
  254.     high
  255.  
  256. As you can see, it depends on how large the main character is, and
  257. what increments he/she moves by. Try to keep your characters small,
  258. to eliminate some of the checks needs.
  259. */
  260.  
  261. int j,k,l;
  262.  
  263.   j=wgetworldblock(wobject[YOU].x+16,wobject[YOU].y+1,mymap);
  264.   k=wgetworldblock(wobject[YOU].x+16,wobject[YOU].y+15,mymap);
  265.   if ((j>=100) | (k>=100))
  266.     {
  267.     j=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+1,mymap);
  268.     k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+15,mymap);
  269.    while ((j>=100) | (k>=100)) {
  270.    wobject[YOU].x--;
  271.   j=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+1,mymap);
  272.   k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+15,mymap);
  273.  
  274.    }
  275.    }
  276. }
  277.  
  278.  
  279. void checkleft(void)
  280. {
  281. int j,k,l;
  282.  
  283.   j=wgetworldblock(wobject[YOU].x-1,wobject[YOU].y,mymap);
  284.   k=wgetworldblock(wobject[YOU].x-1,wobject[YOU].y+15,mymap);
  285.   if ((j>=100) | (k>=100))
  286.     {
  287.   j=wgetworldblock(wobject[YOU].x,wobject[YOU].y,mymap);
  288.   k=wgetworldblock(wobject[YOU].x,wobject[YOU].y+15,mymap);
  289.    while ((j>=100) | (k>=100)) {
  290.    wobject[YOU].x++;
  291.   j=wgetworldblock(wobject[YOU].x,wobject[YOU].y,mymap);
  292.   k=wgetworldblock(wobject[YOU].x,wobject[YOU].y+15,mymap);
  293.    }
  294.    }
  295. }
  296.  
  297. void checkfeet(void)
  298. {
  299. int j,k;
  300.  
  301.   feet1=wgetworldblock(wobject[YOU].x,wobject[YOU].y+16,mymap);
  302.   feet2=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+16,mymap);
  303.   if ((feet1<50) & (feet2<50))
  304.     jumping=1;
  305.     else {
  306.    jumping=0;
  307.    addy=0;
  308.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+15,mymap);
  309.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+15,mymap);
  310.    while ((j>=100) | (k>=100)) {
  311.    wobject[YOU].y--;
  312.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+15,mymap);
  313.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+15,mymap);
  314.    }
  315.    }
  316. }
  317.  
  318. void checkhead(void)
  319. {
  320. int j,k;
  321.  
  322.   head1=wgetworldblock(wobject[YOU].x,wobject[YOU].y-1,mymap);
  323.   head2=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y-1,mymap);
  324.   if ((head1<50) & (head2<50))
  325.     jumping=1;
  326.     else {
  327.    jumping=0;
  328.    addy=0;
  329.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y,mymap);
  330.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y,mymap);
  331.    while ((j>=100) | (k>=100)) {
  332.    wobject[YOU].y++;
  333.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y,mymap);
  334.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y,mymap);
  335.    }
  336.    }
  337. }
  338.  
  339. // search for the elevators on the map.
  340. void findelevators(void)
  341. {
  342. int i,j,k;
  343. for (i=0; i<=mapheight; i++)
  344.   for (j=0; j<=mapwidth; j++)
  345.     {
  346.     k=wgetworldblock(j*16,i*16,mymap);
  347.     if (k==105)
  348.       {
  349.       elev[numelev].curheight=i;
  350.       elev[numelev].origx=j;
  351.       elev[numelev].origy=i;
  352.       elev[numelev].timer=0;
  353.  
  354.       for (k=0; k<200; k++)
  355.       replace[k]=0;
  356.       numelev++;
  357.     }
  358.     }
  359.   }
  360.  
  361. // move an elevator up
  362. void upelev(void)
  363. {
  364. int ii,jj;
  365. for (ii=0; ii<numelev; ii++)
  366.  {
  367.  if ( (elev[ii].origx>=(wobject[YOU].x/16)-1)
  368.     & (elev[ii].curheight>=(wobject[YOU].y/16)-1)
  369.     & (elev[ii].origx<=(wobject[YOU].x/16)+1)
  370.     & (elev[ii].curheight<=(wobject[YOU].y/16)+1)
  371.     & ((elevup==-1) | (elevup==ii))
  372.     & (wobject[YOU].y>16))
  373.     {
  374.      checkhead();
  375.      if ((head1<50) & (head2<50))
  376.      {
  377.      replace[elev[ii].curheight-1]=wgetworldblock(elev[ii].origx*16,(elev[ii].curheight-1)*16,mymap);
  378.      wputworldblock(elev[ii].origx*16,elev[ii].curheight*16,104,mymap);
  379.      wputworldblock(elev[ii].origx*16,(elev[ii].curheight-1)*16,105,mymap);
  380.      elev[ii].curheight--;
  381.      elevup=ii;
  382.      wobject[YOU].y-=16;
  383.       elev[ii].timer=10;
  384.      }
  385.      }
  386.     }
  387. }
  388.  
  389. void downelev(void)
  390. {
  391. int ii,jj;
  392. for (ii=0; ii<numelev; ii++)
  393.  {
  394.  if ((elev[ii].origx>=(wobject[YOU].x/16)-1)
  395.     & (elev[ii].curheight>=(wobject[YOU].y/16)-1)
  396.     & (elev[ii].origx<=(wobject[YOU].x/16)+1)
  397.     & (elev[ii].curheight<=(wobject[YOU].y/16)+1)
  398.     & (elev[ii].curheight !=elev[ii].origy))
  399.     {
  400.      wputworldblock(elev[ii].origx*16,elev[ii].curheight*16,replace[elev[ii].curheight],mymap);
  401.      wputworldblock(elev[ii].origx*16,(elev[ii].curheight+1)*16,105,mymap);
  402.      elev[ii].curheight++;
  403.      if (elev[ii].curheight==elev[ii].origy)
  404.      elevup=-1;
  405.      wobject[YOU].y+=16;
  406.      elev[ii].timer=10;
  407.      }
  408.     }
  409. }
  410.  
  411. void checkelevators(void)
  412. {
  413. int ii;
  414.  
  415. for (ii=0; ii<numelev; ii++)
  416.   {
  417.   if ((elev[ii].curheight !=elev[ii].origy))
  418.   {
  419.   if (elev[ii].timer==0)
  420.   {
  421.      wputworldblock(elev[ii].origx*16,elev[ii].curheight*16,replace[elev[ii].curheight],mymap);
  422.      wputworldblock(elev[ii].origx*16,(elev[ii].curheight+1)*16,105,mymap);
  423.      elev[ii].curheight++;
  424.      if (elev[ii].curheight==elev[ii].origy)
  425.      elevup=-1;
  426.    elev[ii].timer=0;
  427.   }
  428.   else elev[ii].timer--;
  429.   }
  430. }
  431. }
  432.  
  433. void moveguys(void)
  434. {
  435. int j,k;
  436.  
  437. for (i=0; i<=36; i++)
  438.   {
  439.   if ((wobject[i].on==1) &                // sprite on
  440.       (sprites[wobject[i].num] !=NULL))        // sprite made
  441.     {
  442.     if ((wobject[i].x<worldx+windowmaxx) &        // and on the screen
  443.         (wobject[i].y<worldy+windowmaxy) &
  444.         (wobject[i].x+spritewidth[wobject[i].num]>worldx) &
  445.         (wobject[i].y+spriteheight[wobject[i].num]>worldy))
  446.         {
  447.         if (wobject[i].num<16)  // walking right
  448.            {
  449.            wobject[i].num++;
  450.            if (wobject[i].num>15) wobject[i].num=12; // walking animation loop
  451.            wobject[i].x+=3;
  452.            j=wgetworldblock(wobject[i].x+16,wobject[i].y+16,mymap);
  453.            k=wgetworldblock(wobject[i].x+16,wobject[i].y+8,mymap);
  454.            if ((j<50) | (k>=50)) wobject[i].num=16;
  455.            }
  456.         if (wobject[i].num>15)  // walking left
  457.            {
  458.            wobject[i].num++;
  459.            if (wobject[i].num>19) wobject[i].num=16; // walking animation loop
  460.            wobject[i].x-=3;
  461.            j=wgetworldblock(wobject[i].x,wobject[i].y+16,mymap);
  462.            k=wgetworldblock(wobject[i].x,wobject[i].y+8,mymap);
  463.            if ((j<50) | (k>=50)) wobject[i].num=12;
  464.            }
  465.     }
  466.   }
  467.  }
  468. }