home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_13 / friction.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  5.0 KB  |  222 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. // D E F I N E S /////////////////////////////////////////////////////////////
  21.  
  22. #define FRICTION 1   // virtual frictional coefficeint of water
  23.  
  24. // G L O B A L S  ////////////////////////////////////////////////////////////
  25.  
  26. pcx_picture imagery_pcx,      // the game imagery
  27.             background_pcx;   // the backdrop
  28.  
  29.  
  30. // the sprites used in the game
  31.  
  32. sprite object;                // the object
  33.  
  34. // F U N C T I O N S //////////////////////////////////////////////////////////
  35.  
  36. void main(void)
  37. {
  38.  
  39. int torpedo_xv=0,  // initial velocities
  40.     torpedo_yv=0,
  41.     fired=0,       // state of torpedo
  42.     done=0;        // exit variable
  43.  
  44.  
  45. // SECTION 1 //////////////////////////////////////////////////////////////////
  46.  
  47. // set video mode to 320x200 256 color mode
  48.  
  49. Set_Video_Mode(VGA256);
  50.  
  51. // create a double buffer
  52.  
  53. if (!Create_Double_Buffer(SCREEN_HEIGHT))
  54.    {
  55.    printf("\nNot enough memory to create double buffer.");
  56.  
  57.    } // end if
  58.  
  59. // clear the double buffer
  60.  
  61. Fill_Double_Buffer(0);
  62.  
  63. // SECTION 2 //////////////////////////////////////////////////////////////////
  64.  
  65. // load in the background image into the double buffer
  66.  
  67. PCX_Init((pcx_picture_ptr)&background_pcx);
  68.  
  69. PCX_Load("ship.pcx", (pcx_picture_ptr)&background_pcx,1);
  70.  
  71. // copy the background into the double buffer
  72.  
  73. _fmemcpy((char far *)double_buffer,
  74.          (char far *)(background_pcx.buffer),
  75.          SCREEN_WIDTH*SCREEN_HEIGHT);
  76.  
  77. PCX_Delete((pcx_picture_ptr)&background_pcx);
  78.  
  79. // load in imagery for object
  80.  
  81. PCX_Init((pcx_picture_ptr)&imagery_pcx);
  82.  
  83. PCX_Load("torpedo.pcx", (pcx_picture_ptr)&imagery_pcx,1);
  84.  
  85. // SECTION 3 //////////////////////////////////////////////////////////////////
  86.  
  87. // initialize player and extract bitmaps
  88.  
  89. sprite_width  = 8;
  90. sprite_height = 8;
  91.  
  92. Sprite_Init((sprite_ptr)&object,0,0,0,0,0,0);
  93.  
  94. PCX_Grab_Bitmap((pcx_picture_ptr)&imagery_pcx,(sprite_ptr)&object,0,0,0);
  95.  
  96. object.x          = 154;
  97. object.y          = 178;
  98. object.curr_frame = 0;
  99. object.state      = 1;
  100.  
  101. // draw instructions
  102.  
  103. Blit_String_DB(8,8,15,"Press 'Q' to quit, 'F' to fire.",1);
  104.  
  105. // scan behind all objects before entering event loop
  106.  
  107. Behind_Sprite_DB((sprite_ptr)&object);
  108.  
  109. // SECTION 4 //////////////////////////////////////////////////////////////////
  110.  
  111. // main event loop
  112.  
  113. while(!done)
  114.      {
  115.  
  116.      // erase all objects
  117.  
  118.      Erase_Sprite_DB((sprite_ptr)&object);
  119.  
  120. // SECTION 5 //////////////////////////////////////////////////////////////////
  121.  
  122.      // test if user is trying to fire torpedo
  123.  
  124.      if (kbhit())
  125.         {
  126.  
  127.         switch(getch())
  128.               {
  129.  
  130.               case 'q': // just exit?
  131.                    {
  132.                    done=1;
  133.  
  134.                    } break;
  135.  
  136.               case 'f':
  137.               case 'F': // fire the torpedo
  138.                       {
  139.                       // make sure torpedo hasn't been fired
  140.  
  141.                       if (!fired)
  142.                       {
  143.  
  144.                       // set flag that torpedo has been fired
  145.  
  146.                       fired=1;
  147.  
  148.                       // set initial velocity or torpedo
  149.  
  150.                       torpedo_xv = 0;   // no movement X direction
  151.  
  152.                       torpedo_yv = -16; // initial velocity, note it is
  153.                                         // negative since torped is moving
  154.                                         // upward
  155.  
  156.                       } // end if
  157.  
  158.                       } break;
  159.  
  160.  
  161.               } // end switch
  162.  
  163.         } // end if kbhit
  164.  
  165.  
  166. // SECTION 6 //////////////////////////////////////////////////////////////////
  167.  
  168.      // test if torpedo has been fired
  169.  
  170.      if (fired)
  171.         {
  172.  
  173.         // do translation
  174.  
  175.         object.x+=torpedo_xv;
  176.         object.y+=torpedo_yv;
  177.  
  178.         // apply water friction only to vertical component of velocity
  179.  
  180.         torpedo_yv+=FRICTION;
  181.  
  182.         // test if velocity has become postive or zero
  183.  
  184.         if (torpedo_yv>0)
  185.             torpedo_yv = 0;
  186.  
  187.         } // end if fired
  188.  
  189. // SECTION 7 //////////////////////////////////////////////////////////////////
  190.  
  191.      // scan background under objects
  192.  
  193.      Behind_Sprite_DB((sprite_ptr)&object);
  194.  
  195.      // draw all the imagery
  196.  
  197.      Draw_Sprite_DB((sprite_ptr)&object);
  198.  
  199.      // copy the double buffer to the screen
  200.  
  201.      Show_Double_Buffer(double_buffer);
  202.  
  203.      // wait a sec
  204.  
  205.      Delay(1);
  206.  
  207.      } // end while
  208.  
  209. // SECTION 8 //////////////////////////////////////////////////////////////////
  210.  
  211. // reset the video mode back to text
  212.  
  213. Set_Video_Mode(TEXT_MODE);
  214.  
  215. // free the double buffer
  216.  
  217. Delete_Double_Buffer();
  218.  
  219. } // end main
  220.  
  221.  
  222.