home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / examples / ex_4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-16  |  9.5 KB  |  166 lines

  1. #include <dos.h>
  2. #include <stdlib.h>
  3. #include "colors.h"
  4. #include "!bestlib.h"
  5.  
  6.    /*** NOTE  the names and structure of the routines used in these examples
  7.               have changed in The Best Library 2.00; the updated examples will
  8.               be released soon                                            ***/
  9.  
  10. #define ROCK 219                       /* ASCII code for the oncoming rocks */
  11. #define FACEMAN 1                      /* ASCII code for the main character */
  12. #define MAXROCKS 200                   /* the maximum number of rocks       */
  13. #define MAXROCKDELAY 6                 /* the maximum delay in clock ticks  */
  14. #define SCREENX 80                     /* number of characters on x axis    */
  15. #define SCREENY 50                     /* number of characters on y axis    */
  16.  
  17. void moverock(void);                   /* procedure definition to move rock */
  18. void movefaceman(void);      /* procedure definition to move main character */
  19.  
  20.    /*** NOTE  even though this program only uses the "keyp" structure, the
  21.               other three are necessary for the assembler routines in the
  22.               !BESTLIB.LIB library to function properly.  C would also
  23.               produce a "linker warning" if you have enabled that warning ***/
  24. filldata fidata;                       /* create a "filldata" structure     */
  25. printdata prdata;                      /* create a "printdata" structure    */
  26. mousedata msdata;                      /* create a "mousedata" structure    */
  27. asciiscan keyp;                        /* create an "asciiscan" structure   */
  28.  
  29. struct {                               /* the rock data structure           */
  30.    int x;                              /* the current x-coordinate          */
  31.    int y;                              /* the current y-coordinate          */
  32.    int oldx;                           /* the old x-coordinate              */
  33.    int oldy;                           /* the old y-coordinate              */
  34.    int dir;                            /* the direction                     */
  35. } rock[MAXROCKS];                      /* create an array for all the rocks */
  36.  
  37. struct {                               /* the main character data structure */
  38.    int x;                              /* the current x-coordinate          */
  39.    int y;                              /* the current y-coordinate          */
  40.    int oldx;                           /* the old x-coordinate              */
  41.    int oldy;                           /* the old y-coordinate              */
  42.    int dir;                            /* the direction                     */
  43. } faceman;                             /* only one main character           */
  44.  
  45. void main(void)
  46. {
  47.    int oldmode;
  48.    register int i;                     /* register class speeds up loops    */
  49.  
  50.    for (i = 0; i < MAXROCKS; i++) {    /* initialize rock structures        */
  51.       stopw(i, random(MAXROCKDELAY));  /* initialize stop watch             */
  52.       rock[i].x = rock[i].oldx = 0;    /* initialize x-coordinate           */
  53.       rock[i].y = rock[i].oldy = random(SCREENY);    /* random y-coordinate */
  54.       rock[i].dir = 3;                 /* initialize default direction      */
  55.    }
  56.    faceman.dir = 0;                /* initialize main character's direction */
  57.    faceman.x = faceman.oldx = 40;    /* initialize main character's y-coord */
  58.    faceman.y = faceman.oldy = 13;    /* initialize main character's x-coord */
  59.  
  60.    oldmode = readvideomode();          /* save the current video mode       */
  61.    textm(2);                           /* change to 50 line text mode       */
  62.    textmem(3);                         /* store text video memory           */
  63.    cursor(3, 1);                       /* hide and store cursor position    */
  64.    clear(LIGHTGREEN, BLUE);            /* clear the screen and set colors   */
  65.    printcatxy(FACEMAN, faceman.x, faceman.y);    /* draw the main character */
  66.  
  67.    do {
  68.       movefaceman();                   /* move the main character           */
  69.       moverock();                      /* move one rock at a time           */
  70.       printsatxy("Press <ESC> to exit", 30, 24);
  71.    } while (keyp.ascii != 27);         /* loop until <ESC> key is pressed   */
  72.  
  73.    changevideomode(oldmode);           /* restore the original video mode   */
  74.    if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
  75.                                        /* if the old mode was a text mode.. */
  76.       textmem(1);                      /* restore text video memory         */
  77.       cursor(1, 1);                    /* show and restore cursor position  */
  78.    }
  79. }
  80.  
  81. /*
  82.  * MOVE ONE ROCK AT A TIME AND RANDOMLY SET THE NEW DELAY BEFORE THE NEXT MOVE
  83.  */
  84. void moverock(void)
  85. {
  86.    static int i = 0;                   /* this variable is never lost       */
  87.  
  88.    if (stopw(i, 0) > 0) {              /* if it is time to move this rock.. */
  89.       if (++rock[i].x >= SCREENX) {    /* if the rock is at the boundary..  */
  90.          rock[i].x = 0;                /* reset its x-coordinate            */
  91.          rock[i].y = random(SCREENY);  /* randomly reset its y-coordinate   */
  92.       }
  93.       printcatxy(32, rock[i].oldx, rock[i].oldy);     /* erase the old rock */
  94.       printcatxy(ROCK, rock[i].x, rock[i].y);         /* draw the new rock  */
  95.       rock[i].oldx = rock[i].x;  /* modify the old x-coordinate of the rock */
  96.       rock[i].oldy = rock[i].y;  /* modify the old y-coordinate of the rock */
  97.       stopw(i, random(MAXROCKDELAY)); /* new random delay for the next move */
  98.    }
  99.    if (++i >= MAXROCKS) i = 0;  /* if all rocks have moved, restart from #1 */
  100. }
  101.  
  102. /*
  103.  * MOVES THE MAIN CHARACTER ACCORDING TO KEYPRESS, REGARDLESS OF NUM LOCK
  104.  *
  105.  *                                      1         5   6       5 1 6
  106.  *                                     \|/         \|/         \|/
  107.  *                    DIRECTIONS:    2--+--3      --+--      2--+--3
  108.  *                                     /|\         /|\         /|\
  109.  *                                      4         7   8       7 4 8
  110.  */
  111. void movefaceman(void)
  112. {
  113.    register int i;
  114.  
  115.    for (i = 0; i < MAXROCKS; i++) {    /* check for a collision with a rock */
  116.       if (rock[i].x == faceman.x && rock[i].y == faceman.y) {
  117.      sound(120); msec(400);        /* create a 120 Hz sound for 400 ms  */
  118.          sound(40); msec(850);         /* create a 40 Hz sound for 850 ms   */
  119.          nosound();                    /* silence the speaker               */
  120.       }
  121.    }
  122.    if (keyhit() == FALSE) return;      /* if not keypress ready, return     */
  123.    getchr();                 /* read the keypress into the "keyp" structure */
  124.    kbclear();         /* clear all other character from the keyboard buffer */
  125.    keyp.ascii = dncase(keyp.ascii);    /* convert to lowercase if UPPERCASE */
  126.    if (!keyp.ascii)                    /* if there is no ASCII code..       */
  127.       keyp.ascii = keyp.scan;          /* read the scan code                */
  128.    switch(keyp.ascii) {
  129.    case 56 :                           /* direction 1, NUM LOCK active      */
  130.    case 72 :                           /* direction 1, NUM LOCK inactive    */
  131.    case 119: faceman.y--; break;       /* direction 1, right letter-pad     */
  132.    case 52 :                           /* direction 2, NUM LOCK active      */
  133.    case 75 :                           /* direction 2, NUM LOCK inactive    */
  134.    case 97 : faceman.x--; break;       /* direction 2, right letter-pad     */
  135.    case 54 :                           /* direction 3, NUM LOCK active      */
  136.    case 77 :                           /* direction 3, NUM LOCK inactive    */
  137.    case 100: faceman.x++; break;       /* direction 3, right letter-pad     */
  138.    case 50 :                           /* direction 4, NUM LOCK active      */
  139.    case 80 :                           /* direction 4, NUM LOCK inactive    */
  140.    case 120: faceman.y++; break;       /* direction 4, right letter-pad     */
  141.    case 55 :                           /* direction 5, NUM LOCK active      */
  142.    case 71 :                           /* direction 5, NUM LOCK inactive    */
  143.    case 113: faceman.x--, faceman.y--; break;
  144.                                        /* direction 5, right letter-pad     */
  145.    case 57 :                           /* direction 6, NUM LOCK active      */
  146.    case 73 :                           /* direction 6, NUM LOCK inactive    */
  147.    case 101: faceman.x++, faceman.y--; break;
  148.                                        /* direction 6, right letter-pad     */
  149.    case 49 :                           /* direction 7, NUM LOCK active      */
  150.    case 79 :                           /* direction 7, NUM LOCK inactive    */
  151.    case 122: faceman.x--, faceman.y++; break;
  152.                                        /* direction 7, right letter-pad     */
  153.    case 51 :                           /* direction 8, NUM LOCK active      */
  154.    case 81 :                           /* direction 8, NUM LOCK inactive    */
  155.    case 99 : faceman.x++, faceman.y++; /* direction 8, right letter-pad     */
  156.    }
  157.    if (faceman.x < 0) faceman.x = 0;   /* movement beyond the screen edge.. */
  158.    if (faceman.y < 0) faceman.y = 0;   /* not allowed                       */
  159.    if (faceman.y > SCREENY - 1) faceman.y = SCREENY - 1;
  160.    if (faceman.x > SCREENX - 1) faceman.x = SCREENX - 1;
  161.    printcatxy(32, faceman.oldx, faceman.oldy);  /* erase the main character */
  162.    printcatxy(FACEMAN, faceman.x, faceman.y);   /* print the main character */
  163.    faceman.oldx = faceman.x;    /* modify old x-coord of the main character */
  164.    faceman.oldy = faceman.y;    /* modify old y-coord of the main character */
  165. }
  166.