home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / sot.zip / SOTG.C < prev    next >
Text File  |  1988-10-24  |  5KB  |  226 lines

  1. /********** The Son of Tetris Project ************/
  2.  
  3. /************  GAME ***********/
  4.  
  5. #include <conio.h>
  6. #include <dos.h>
  7. #include <setjmp.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. #include "sot.h"
  12.  
  13. #define MAX_LEVEL 9
  14. #define ACT_LEVEL 3  /* level at which extra shapes start appearing */
  15. #define MIN_SHAPES 7 /* minimum number of shapes */
  16. #define ADD_SHAPE MIN_SHAPES - ACT_LEVEL + 1
  17. #define MAX_SHAPE 20 /* maximum number of shapes allowed before upping level */
  18.  
  19. #define DELAY_TIME 120 /* Loop, unit of time */
  20.  
  21. int      mv(drctn_type dt, OBJ_TYPE *cur_obj, int amt);   /* move object */
  22. int      insrt(OBJ_TYPE *cur_obj,SHP_TYPE *next_sh);      /* insert object */
  23. int      getkey(void);
  24.  
  25.  
  26. static  SHP_TYPE *next_sh;  /* next shape to appear */
  27. static  unsigned  level;    /* Level at which game is being played */
  28. static  long      score;
  29. static  unsigned  lines_del;/* Lines deleted */
  30. static  int       sound_on = TRUE; /* Controls use of sound FX */
  31. static  int       show_shape = TRUE; /* show next shape */
  32. static  unsigned  shape_count;
  33.  
  34. static void get_next_shape(void)
  35. {
  36.   unsigned new_shape_no, choice_of_shapes;
  37.   if (level < ACT_LEVEL)
  38.     choice_of_shapes = MIN_SHAPES;
  39.   else
  40.     choice_of_shapes = (no_of_shapes < level + ADD_SHAPE) ?
  41.                        no_of_shapes : level + ADD_SHAPE;
  42.   new_shape_no = (rand() % choice_of_shapes);
  43.   for (next_sh = shp_lst; new_shape_no; new_shape_no--)
  44.     next_sh = next_sh -> next_shp;
  45. }    /* get_next_shape */
  46.  
  47.  
  48.  
  49. static void do_blip(void)
  50. {
  51.   if (sound_on)
  52.     sound(100);
  53.   delay(40); nosound();
  54. } /* do_blip */
  55.  
  56. static void end_noise(void)
  57. {
  58.   int i;
  59.   if (!sound_on)
  60.     return;
  61.   for (i = 1000; i < 3000; i+= 10)
  62.   {
  63.     sound(i); delay(1);
  64.   }
  65.   nosound();
  66. } /* end_noise */
  67.  
  68.  
  69. static void   kill_row(int t_row)
  70. {
  71.   int x,y;
  72.   for (y = t_row; y > 1; y--)
  73.     for (x = 1; x < BLW - 1; x++)
  74.       arena[x][y] = arena[x][y-1];
  75.   for (x = 1; x < BLW - 1; x++)
  76.       arena[x][1] = CLEAR;
  77.   v_kill_row(t_row);
  78.   p_arena(1,1,BLW-1,t_row);
  79. }      /* kill_row */
  80.  
  81.  
  82. static int   check_full_row(void)
  83. {
  84.   int y,x;
  85.   int clear_found, result;
  86.  
  87.   result = FALSE;
  88.   for (y = BLH - 2; y > 1; y--)
  89.   {
  90.     clear_found = FALSE;
  91.     for (x = 1; x <= BLW -2; x++)
  92.       if (arena[x][y] == CLEAR)
  93.       {
  94.     clear_found = TRUE;
  95.     break;
  96.       }
  97.     if (clear_found)
  98.       continue;
  99.     lines_del++;  result = TRUE;
  100.     kill_row(y);
  101.     y++;
  102.   }
  103.   return(result);
  104. } /* check_full_row */
  105.  
  106.  
  107. static void  handle_keys(OBJ_TYPE *cur_obj)
  108. {
  109.   extern jmp_buf    end_game;
  110.   int  i;           /* Index to Number of keys allowed per loop */
  111.  
  112.   static unsigned delay_period[MAX_LEVEL + 1] =
  113.         {120, 98, 80, 66, 54, 44, 36, 29, 24,/* 20*/ 15};
  114.  
  115.   for (i = 0; i < NO_OF_KEY_PRESSES; i++)
  116.   {
  117.     if (kbhit())   /* get any keys */
  118.       switch(getkey())
  119.       {
  120.  
  121.         case LEFT_KEY:              /* Go Left */
  122.           mv(left, cur_obj, HORIZ_MOVE);
  123.       break;
  124.  
  125.         case ROT_KEY:                /* Rotate */
  126.           mv(anti, cur_obj, HORIZ_MOVE);
  127.       break;
  128.  
  129.  
  130.         case RIGHT_KEY:             /* Go right */
  131.           mv(right, cur_obj, HORIZ_MOVE);
  132.       break;
  133.  
  134.         case DROP_KEY:             /* Drop */
  135.     case ' ':
  136.           while (!mv(down,cur_obj, VERT_MOVE));
  137.       break;
  138.  
  139.  
  140.         case LEVEL_KEY:             /* Level up */
  141.           level = level++ % MAX_LEVEL;
  142.       shape_count  = 0;
  143.           update_score(level, score, lines_del, next_sh, show_shape);
  144.       break;
  145.  
  146.         case 'p':                /* Pause game */
  147.     case 'P':
  148.       if (!pause_game())
  149.         break;
  150.  
  151.         case ESC:                /* Quit game */
  152.       longjmp(end_game,1);
  153.       break;
  154.  
  155.  
  156.         case 's':
  157.     case 'S':
  158.       sound_on = !sound_on;
  159.       break;
  160.  
  161.     default:
  162.       /* Do nothing */
  163.       break;
  164.       } /* switch */
  165.  
  166.     delay(delay_period[level]);  /* delay after action */
  167.   } /* for */
  168. }  /* handle_keys */
  169.  
  170.  
  171.  
  172. void   game()
  173. {
  174.   OBJ_TYPE *cur_obj;
  175.   unsigned local_score;
  176.  
  177.   do
  178.   {
  179.     init_arena();
  180.     p_arena(0,0,(BLW-1),(BLH-1));   /*  display blank arena */
  181.     level = get_level();
  182.     score = 0L; lines_del = 0;
  183.     cur_obj = malloc(sizeof(OBJ_TYPE));
  184.     shape_count = 0;
  185.     get_next_shape(); /* get first shape */
  186.     while (!insrt(cur_obj,next_sh))
  187.     {
  188.       get_next_shape();
  189.       update_score(level, score, lines_del, next_sh, show_shape);
  190.       handle_keys(cur_obj);  /* at top */
  191.  
  192.       for (local_score = 24 + 3*level;
  193.          !mv(down,cur_obj, VERT_MOVE);
  194.          local_score--)
  195.         handle_keys(cur_obj);
  196.  
  197.       if (check_full_row())
  198.         do_blip();
  199.       score += local_score;
  200.       if (((lines_del/10) > level) && (level < MAX_LEVEL))
  201.       {
  202.         level++;
  203.     shape_count = 0;
  204.       }
  205.       if (!(++shape_count % MAX_SHAPE) && (level < MAX_LEVEL))
  206.         level++;
  207.     } /* while can insert */
  208.     update_score(level, score, lines_del, next_sh, FALSE);
  209.     end_noise();
  210.     while (kbhit())   /* Empty keyboard buffer */
  211.       getch();
  212.   }
  213.   while (get_another_go(score));   /* Dialogue with player between games */
  214.  
  215. } /* game */
  216.  
  217. int getkey(void)
  218. {
  219.   int key;
  220.  
  221.   if(!(key = getch()))
  222.     key = 128 + getch();
  223.  
  224.   return(key);
  225. }
  226.