home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_13 / accel.c next >
Encoding:
Text File  |  1994-07-18  |  5.2 KB  |  231 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. // P R O T O T Y P E S //////////////////////////////////////////////////////
  21.  
  22. // D E F I N E S /////////////////////////////////////////////////////////////
  23.  
  24.  
  25. // S T R U C T U R E S ///////////////////////////////////////////////////////
  26.  
  27.  
  28. // G L O B A L S  ////////////////////////////////////////////////////////////
  29.  
  30. pcx_picture imagery_pcx,      // the game imagery
  31.             background_pcx;   // the backdrop
  32.  
  33.  
  34. // the sprites used in the game
  35.  
  36. sprite object;                // the object
  37.  
  38. // F U N C T I O N S //////////////////////////////////////////////////////////
  39.  
  40. void main(void)
  41. {
  42. int accel=0,            // acceleration input by user
  43.     velocity=0,         // current velocity of dragster
  44.     peddle_to_metal=0,  // has the user punched it?
  45.     engine=0,           // state of 500CI engine
  46.     done=0;             // system exit variable
  47.  
  48. char buffer[80];        // used for printing
  49.  
  50. // this is the main function
  51.  
  52. // SECTION 1 //////////////////////////////////////////////////////////////////
  53.  
  54. printf("\nEnter the acceleration of the dragster (1-10) ?");
  55. scanf("%d",&accel);
  56.  
  57. // set video mode to 320x200 256 color mode
  58.  
  59. Set_Video_Mode(VGA256);
  60.  
  61. // create a double buffer
  62.  
  63. if (!Create_Double_Buffer(SCREEN_HEIGHT))
  64.    {
  65.    printf("\nNot enough memory to create double buffer.");
  66.  
  67.    } // end if
  68.  
  69. // clear the double buffer
  70.  
  71. Fill_Double_Buffer(0);
  72.  
  73. // SECTION 2 //////////////////////////////////////////////////////////////////
  74.  
  75. // load in the background image into the double buffer
  76.  
  77. PCX_Init((pcx_picture_ptr)&background_pcx);
  78.  
  79. PCX_Load("road.pcx", (pcx_picture_ptr)&background_pcx,1);
  80.  
  81. // copy the background into the double buffer
  82.  
  83. _fmemcpy((char far *)double_buffer,
  84.          (char far *)(background_pcx.buffer),
  85.          SCREEN_WIDTH*SCREEN_HEIGHT);
  86.  
  87. PCX_Delete((pcx_picture_ptr)&background_pcx);
  88.  
  89. Blit_String_DB(8,8,10,"'Q' to quit, 'G' to start.",1);
  90.  
  91. // load in imagery for object
  92.  
  93. PCX_Init((pcx_picture_ptr)&imagery_pcx);
  94.  
  95. PCX_Load("drag.pcx", (pcx_picture_ptr)&imagery_pcx,1);
  96.  
  97. // SECTION 3 //////////////////////////////////////////////////////////////////
  98.  
  99. // initialize player and extract bitmaps
  100.  
  101. sprite_width  = 36;
  102. sprite_height = 8;
  103.  
  104. Sprite_Init((sprite_ptr)&object,0,0,0,0,0,0);
  105.  
  106. PCX_Grab_Bitmap((pcx_picture_ptr)&imagery_pcx,(sprite_ptr)&object,0,0,0);
  107.  
  108. object.x          = 0;
  109. object.y          = 170;
  110. object.curr_frame = 0;
  111. object.state      = 1;
  112.  
  113. // scan behind all objects before entering event loop
  114.  
  115. Behind_Sprite_DB((sprite_ptr)&object);
  116.  
  117. // SECTION 4 //////////////////////////////////////////////////////////////////
  118.  
  119. // main event loop
  120.  
  121. while(!done)
  122.      {
  123.  
  124.      // erase all objects
  125.  
  126.      Erase_Sprite_DB((sprite_ptr)&object);
  127.  
  128.      // test if user is tryin to go
  129.  
  130.      if (kbhit())
  131.         {
  132.         // what key?
  133.  
  134.         switch(getch())
  135.               {
  136.               case 'g': // g for gas, gone, go!
  137.                    {
  138.                    // we can only start dragster if it hasn't already started
  139.  
  140.                    if (!peddle_to_metal)
  141.                       {
  142.                       peddle_to_metal=engine=1;
  143.  
  144.                       } // end if
  145.  
  146.                    } break;
  147.  
  148.               case 'q': // to quit
  149.                    {
  150.  
  151.                    done=1;
  152.  
  153.                    } break;
  154.  
  155.               default:break;
  156.  
  157.               } // end switch
  158.  
  159.         } // end if
  160.  
  161. // SECTION 5 //////////////////////////////////////////////////////////////////
  162.  
  163.      // test if it's time to move the dragster
  164.  
  165.      if (peddle_to_metal && engine)
  166.         {
  167.  
  168.         // move object with velocity
  169.  
  170.         object.x+=velocity;
  171.  
  172.         // apply the horsepower baby (acceleration)
  173.  
  174.         velocity+=accel;
  175.  
  176.         // test if dragster has hit end of stip, if so stop it rather abrubtly
  177.  
  178.         if (object.x > 319-36)
  179.            {
  180.            engine=0; // turn off engine and apply infinite braking force!
  181.  
  182.            // push dragster back a bit
  183.  
  184.            object.x = 319-36;
  185.  
  186.            } // end if
  187.  
  188.         } // end if
  189.  
  190. // SECTION 6 //////////////////////////////////////////////////////////////////
  191.  
  192.      // scan background under objects
  193.  
  194.      Behind_Sprite_DB((sprite_ptr)&object);
  195.  
  196.      // draw all the imagery
  197.  
  198.      Draw_Sprite_DB((sprite_ptr)&object);
  199.  
  200.      // show some info
  201.  
  202.      sprintf(buffer,"Speed = %d  ",velocity);
  203.      Blit_String_DB(110,100,10,buffer,0);
  204.  
  205.      sprintf(buffer,"Acceration = %d  ",accel);
  206.      Blit_String_DB(110,110,10,buffer,0);
  207.  
  208.      // copy the double buffer to the screen
  209.  
  210.      Show_Double_Buffer(double_buffer);
  211.  
  212.      // wait a sec
  213.  
  214.      Delay(1);
  215.  
  216.      } // end while
  217.  
  218. // SECTION 7 //////////////////////////////////////////////////////////////////
  219.  
  220. // reset the video mode back to text
  221.  
  222. Set_Video_Mode(TEXT_MODE);
  223.  
  224. // free the double buffer
  225.  
  226. Delete_Double_Buffer();
  227.  
  228. } // end main
  229.  
  230.  
  231.