home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / programm / 5179 / kaz.c
Encoding:
C/C++ Source or Header  |  1992-11-06  |  4.8 KB  |  226 lines

  1. /* Shifty Death Effect started by Noah Vawter on Halloween, 1992 */
  2. /* Special thanks to Pete Kazmier */
  3.  
  4. /* stuff to add:
  5.    aliens that shoot
  6.    explosions for aliens when hit
  7.    better keyboard routine
  8.    more random placement
  9.    better algorithim for aliens appearing
  10.    no-blink erase routine
  11.    */
  12. #include <stdio.h>
  13. #include <cursesX.h>       /* Ultrix uses cursesX not curses */
  14. #define SHOTS 6   /* don't forget shots=SHOTS-1 !*/
  15. #define SHIPY 15
  16. #define RIGHTEDGE 40
  17. #define LEFTEDGE 10
  18. #define TOP 3
  19. #define ALIENBASEMENT 13
  20. #define ALIENS 20
  21. int score;
  22. void main (void)
  23. {
  24.   int sh_flag[SHOTS],shot_x[SHOTS],shot_y[SHOTS];
  25.   int al_flag[ALIENS],al_x[ALIENS],al_y[ALIENS];
  26.   int al_dy[ALIENS],al_dx[ALIENS];
  27.   void          drawship (int);
  28.   void draw_shots (int sh_flag[], int shot_y[], int shot_x[]);
  29.   void move_shots (int shot_y[],int sh_flag[]);
  30.   void move_ship  (int *, int sh_flag[], int shot_x[], int shot_y[]);
  31.   void move_aliens (int al_flag[],int al_y[],int al_x[],int al_dx[],int al_dy[]);
  32.   void draw_aliens (int al_flag[],int al_y[],int al_x[]);
  33.   void add_aliens (int al_flag[],int al_y[],int al_x[]);
  34.   void int_aliens (int al_y[],int al_x[],int al_dy[],int al_dy[]);
  35.   void al_die (int al_flag[],int al_y[],int al_x[],int shot_y[],int shot_x[]);
  36.   void show_score();
  37.   int         loop;
  38.   int         shipx =10,
  39.         nlines = 20,
  40.         ncols = 40,
  41.             begin_x = 1,
  42.               begin_y = 1;     
  43.  
  44.   initscr ();   /* must be called first before you use curses functions */
  45.   clear ();     /* clears the main screen */
  46.   refresh ();   /* clear screen now not later */
  47.   
  48.   int_aliens (al_y,al_x,al_dy,al_dx);
  49.   al_flag[1]=1;
  50.   al_flag[2]=1;
  51.   al_flag[3]=1;
  52.   for (loop = 1; loop < 9991; loop++) 
  53.     {
  54.       move_ship(&shipx, sh_flag, shot_x, shot_y);
  55.       drawship(shipx);
  56.       move_shots(shot_y,sh_flag);
  57.       draw_shots(sh_flag, shot_y, shot_x);
  58.       move_aliens(al_flag,al_y,al_x,al_dx,al_dy);
  59.       draw_aliens(al_flag,al_y,al_x);
  60.       al_die(al_flag,al_y,al_x,shot_y,shot_x);
  61.       show_score();
  62.       refresh();
  63.     }
  64.   endwin ();
  65. }
  66.  
  67. void drawship (int shipx)   
  68. {
  69.   move(SHIPY,shipx);
  70.   printw ("A");       /* QUESTION: why not use putch */
  71.   refresh();
  72.  
  73. void move_ship (int *shpx, int shflag[], int shotx[], int shoty[])
  74. {
  75.   chtype widget;
  76.   widget = getch();
  77.   clear();
  78.   /* putch (widget); */
  79.   if (widget == 'j') 
  80.     {
  81.       *shpx = *shpx - 2;
  82.       if (*shpx < LEFTEDGE)
  83.     *shpx = LEFTEDGE;
  84.     }
  85.   if (widget == 'k')
  86.     {
  87.       *shpx = *shpx + 2;
  88.       if (*shpx > RIGHTEDGE)
  89.     *shpx = RIGHTEDGE;
  90.     }
  91.  
  92.   if (widget == 'a')/* shot is fired! */
  93.     {
  94.       int loop;
  95.       for (loop = 1; loop < (SHOTS) ; loop++)
  96.     {
  97.       if (shflag[loop] != 1)
  98.         {
  99.           shflag[loop] = 1;
  100.           shotx[loop] = *shpx;
  101.           shoty[loop] = (SHIPY-1);
  102.           break;
  103.         }
  104.     }
  105.     }
  106. }
  107.  
  108. void draw_shots (int shflag[], int shoty[], int shotx[])
  109. {
  110.   int loop;
  111.   for (loop=1 ; loop < (SHOTS); loop++)
  112.     {
  113.       if (shflag[loop] == 1)
  114.     {
  115.       move (shoty[loop], shotx[loop]);
  116.       printw("|",loop);
  117.     }
  118.     }
  119. }
  120.  
  121. void move_shots (int shoty[], int shflag[])
  122. {
  123.   int loop;
  124.   for (loop=1 ; loop < (SHOTS); loop++)
  125.     {
  126.       if (shflag[loop] == 1)
  127.     {
  128.       shoty[loop] = shoty[loop] -1;
  129.       if (shoty[loop] == (TOP))
  130.         {
  131.           shflag[loop]= 0;
  132.         }
  133.     }
  134.     }
  135. }
  136.  
  137. void draw_aliens (int al_flag[],int al_y[],int al_x[])
  138. {
  139.   int loop;
  140.   for (loop = 1;loop < ALIENS+1;loop++)
  141.     {
  142.       if (al_flag[loop] == 1)
  143.     {
  144.       move (al_y[loop],al_x[loop]);
  145.       printw ("%d",loop);
  146.     }
  147.     }
  148. }
  149.  
  150. void move_aliens (int al_flag[],int al_y[],int al_x[],int al_dx[],int al_dy[])
  151. {
  152.   int loop;
  153.   for (loop = 1;loop < ALIENS+1;loop++)
  154.     {
  155.       if (al_flag[loop] == 1)
  156.     {
  157.       al_y[loop]=al_y[loop]+al_dy[loop];
  158.       if (al_y[loop] > ALIENBASEMENT)
  159.         al_dy[loop]= -al_dy[loop];
  160.       if (al_y[loop] < TOP)
  161.         al_dy[loop]= -al_dy[loop];
  162.       al_x[loop]=al_x[loop]+al_dx[loop];
  163.       if (al_x[loop] > RIGHTEDGE)
  164.         al_dx[loop]= -al_dx[loop];
  165.       if (al_x[loop] < LEFTEDGE)
  166.         al_dx[loop]= -al_dx[loop];
  167.     }
  168.     }
  169. }
  170.  
  171. void int_aliens (int al_y[], int al_x[], int al_dx[], int al_dy[])
  172. {
  173.   int loop;
  174.   for (loop = 1;loop < ALIENS+1; loop++)
  175.     {
  176.       al_y[loop]=10;
  177.       al_x[loop]=10;
  178.       al_dy[loop]=1;
  179.       al_dx[loop]=1;
  180.     }
  181.   al_y[2]=7;
  182.   al_x[2]=11;
  183.   al_y[3]=9;
  184.   al_x[3]=10;
  185. }
  186.  
  187. void al_die (int al_flag[], int al_y[], int al_x[], int shot_y[], int shot_x[])
  188. {
  189.   int loop1,loop2;
  190.   for (loop1=1;loop1<ALIENS+1;loop1++)
  191.     {
  192.       for (loop2=1;loop2<SHOTS;loop2++)
  193.     {
  194.       if (al_y[loop1] == shot_y[loop2])
  195.         if (al_x[loop1] == shot_x[loop2])
  196.           if (al_flag[loop1] == 1)
  197.         {
  198.           al_flag[loop1] = 0;
  199.           score=score+100;
  200.         }
  201.     }
  202.     }
  203. }
  204.  
  205. void show_score()
  206. {
  207.   move(SHIPY+1,RIGHTEDGE);
  208.   printw("%d",score);
  209. }
  210.  
  211. void add_aliens (int al_flag[], int al_y[], int al_x[])
  212. {
  213.   int loop, used[ALIENS];
  214.   for (loop=1;loop<ALIENS+1;loop++)
  215.     {
  216.       if (al_flag[loop] !=1)
  217.     {
  218.       al_flag[loop] =1;
  219.       al_y[loop] = TOP+2;
  220.       al_x[loop] = 23;
  221.       break;
  222.     }
  223.     }
  224. }
  225.