home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_10 / thefly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-23  |  4.4 KB  |  195 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "graph3.h"  // include our graphics stuff
  17. #include "graph4.h"
  18. #include "graph6.h"
  19.  
  20. // G L O B A L S  ////////////////////////////////////////////////////////////
  21.  
  22. pcx_picture imagery_pcx,      // the game imagery
  23.             background_pcx;   // the backdrop
  24.  
  25. // the sprites used in the game
  26.  
  27. sprite thefly;                // the fly
  28.  
  29. int fly_xv=5,                 // the velocity of the fly
  30.     fly_yv=5,
  31.     fly_clock=10;             // how many cycles of frames fly will
  32.                               // move in current direction
  33.  
  34. // F U N C T I O N S //////////////////////////////////////////////////////////
  35.  
  36. void main(void)
  37. {
  38. // this is the main function
  39.  
  40. // SECTION 1 /////////////////////////////////////////////////////////////////
  41.  
  42. // set video mode to 320x200 256 color mode
  43.  
  44. Set_Video_Mode(VGA256);
  45.  
  46. // create a double buffer
  47.  
  48. if (!Create_Double_Buffer(SCREEN_HEIGHT))
  49.    {
  50.    printf("\nNot enough memory to create double buffer.");
  51.  
  52.    } // end if
  53.  
  54. // clear the double buffer
  55.  
  56. Fill_Double_Buffer(0);
  57.  
  58. // load in the background image into the double buffer
  59.  
  60. PCX_Init((pcx_picture_ptr)&background_pcx);
  61.  
  62. PCX_Load("flybak.pcx", (pcx_picture_ptr)&background_pcx,1);
  63.  
  64. // copy the background into the double buffer
  65.  
  66. _fmemcpy((char far *)double_buffer,
  67.          (char far *)(background_pcx.buffer),
  68.          SCREEN_WIDTH*SCREEN_HEIGHT);
  69.  
  70. PCX_Delete((pcx_picture_ptr)&background_pcx);
  71.  
  72. Blit_String_DB(8,8,0,"Press any key to exit.",1);
  73.  
  74. // SECTION 2 /////////////////////////////////////////////////////////////////
  75.  
  76. // load in imagery for the fly
  77.  
  78. PCX_Init((pcx_picture_ptr)&imagery_pcx);
  79.  
  80. PCX_Load("flyimg.pcx", (pcx_picture_ptr)&imagery_pcx,1);
  81.  
  82. // initialize fly and extract bitmaps
  83.  
  84. sprite_width  = 36;
  85. sprite_height = 36;
  86.  
  87. Sprite_Init((sprite_ptr)&thefly,0,0,0,0,0,0);
  88.  
  89. PCX_Grab_Bitmap((pcx_picture_ptr)&imagery_pcx,(sprite_ptr)&thefly,0,0,0);
  90. PCX_Grab_Bitmap((pcx_picture_ptr)&imagery_pcx,(sprite_ptr)&thefly,1,1,0);
  91.  
  92. thefly.x          = 160;
  93. thefly.y          = 100;
  94. thefly.curr_frame = 0;
  95. thefly.state      = 1;
  96.  
  97. // SECTION 3 /////////////////////////////////////////////////////////////////
  98.  
  99. // scan behind all objects before entering event loop
  100.  
  101. Behind_Sprite_DB((sprite_ptr)&thefly);
  102.  
  103. // main event loop
  104.  
  105. while(!kbhit())
  106.      {
  107.  
  108.      // erase all objects
  109.  
  110.      Erase_Sprite_DB((sprite_ptr)&thefly);
  111.  
  112.      // move the fly
  113.  
  114.      thefly.x+=fly_xv;
  115.      thefly.y+=fly_yv;
  116.  
  117. // SECTION 4 /////////////////////////////////////////////////////////////////
  118.  
  119.      // check if it's time to change direction
  120.  
  121.      if (--fly_clock==0)
  122.         {
  123.  
  124.         // select a new direction
  125.  
  126.         fly_xv = -10 + rand()%21;  // -10 to +10
  127.         fly_yv = -10 + rand()%21;  // -10 to +10
  128.  
  129.         // select an amount of time to do it
  130.  
  131.         fly_clock = 5 + rand()%50;
  132.  
  133.         } // end if time to change direction
  134.  
  135. // SECTION 5 /////////////////////////////////////////////////////////////////
  136.  
  137.      // do boundary collision for thefly, if it hits an edge bounce it back
  138.  
  139.      if (thefly.x<0 || thefly.x>294)
  140.         {
  141.         // bounce fly back
  142.  
  143.         fly_xv=-fly_xv;
  144.         thefly.x=thefly.x+2*fly_xv;
  145.  
  146.         } // end if over x boundary
  147.  
  148.      if (thefly.y<0 || thefly.y>164)
  149.         {
  150.         // bounce fly back
  151.  
  152.         fly_yv=-fly_yv;
  153.         thefly.y=thefly.y+2*fly_yv;
  154.  
  155.         } // end if over y boundary
  156.  
  157.      // do animation of the fly
  158.  
  159.      if (++thefly.curr_frame==2)
  160.          thefly.curr_frame = 0;
  161.  
  162. // SECTION 6 /////////////////////////////////////////////////////////////////
  163.  
  164.      // scan background under objects
  165.  
  166.      Behind_Sprite_DB((sprite_ptr)&thefly);
  167.  
  168.      // draw all the imagery
  169.  
  170.      Draw_Sprite_DB((sprite_ptr)&thefly);
  171.  
  172.      // copy the double buffer to the screen
  173.  
  174.      Show_Double_Buffer(double_buffer);
  175.  
  176.      // wait a sec
  177.  
  178.      Delay(1);
  179.  
  180.      } // end while
  181.  
  182. // SECTION 7 /////////////////////////////////////////////////////////////////
  183.  
  184. // reset the video mode back to text
  185.  
  186. Set_Video_Mode(TEXT_MODE);
  187.  
  188. // free the double buffer
  189.  
  190. Delete_Double_Buffer();
  191.  
  192. } // end main
  193.  
  194.  
  195.