No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

230 líneas
7.0 KiB

  1. /*
  2. * ttyvaders Textmode shoot'em up
  3. * Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id: main.c,v 1.10 2002/12/23 10:06:27 sam Exp $
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include "common.h"
  27. static void start_game (game *);
  28. int main (int argc, char **argv)
  29. {
  30. game *g = malloc(sizeof(game));
  31. //srand(time(NULL));
  32. if( init_graphics() )
  33. {
  34. return 1;
  35. }
  36. /* Initialize our program */
  37. init_game(g);
  38. /* Go ! */
  39. start_game(g);
  40. /* Clean up */
  41. end_graphics();
  42. return 0;
  43. }
  44. static void start_game (game *g)
  45. {
  46. int i;
  47. int quit = 0;
  48. int poz = 0;
  49. int skip = 0;
  50. int purcompteur = 0;
  51. g->sf = create_starfield( g );
  52. g->wp = malloc(sizeof(weapons));
  53. g->ex = malloc(sizeof(explosions));
  54. g->bo = malloc(sizeof(bonus));
  55. g->t = create_tunnel( g, g->w, g->h );
  56. g->p = create_player( g );
  57. g->al = malloc(sizeof(aliens));
  58. init_bonus( g, g->bo );
  59. init_weapons( g, g->wp );
  60. init_explosions( g, g->ex );
  61. init_aliens( g, g->al );
  62. /* Temporary stuff */
  63. for( i = 0; i < 5; i++ )
  64. {
  65. add_alien( g, g->al, rand() % g->w, rand() % g->h / 2, ALIEN_POOLP );
  66. }
  67. g->t->w = 25;
  68. while( !quit )
  69. {
  70. char key;
  71. while( ( key = get_key() ) )
  72. {
  73. switch( key )
  74. {
  75. case 'q':
  76. quit = 1;
  77. break;
  78. case 'p':
  79. poz = !poz;
  80. break;
  81. case '\t':
  82. ceo_alert();
  83. poz = 1;
  84. break;
  85. case 's':
  86. skip = 1;
  87. break;
  88. case 'h':
  89. g->p->dir = -3;
  90. break;
  91. case 'j':
  92. if( g->p->y < g->h - 2 ) g->p->y += 1;
  93. break;
  94. case 'k':
  95. if( g->p->y > 1 ) g->p->y -= 1;
  96. break;
  97. case 'l':
  98. g->p->dir = 3;
  99. break;
  100. case 'n':
  101. if( g->p->nuke == 0 )
  102. {
  103. g->p->nuke = 40;
  104. add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_NUKE );
  105. }
  106. break;
  107. case '\r':
  108. if( g->p->nuke == 0 )
  109. {
  110. g->p->nuke = 40;
  111. add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, 0, WEAPON_BEAM );
  112. }
  113. break;
  114. case 'f':
  115. if( g->p->nuke == 0 )
  116. {
  117. g->p->nuke = 40;
  118. add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB );
  119. }
  120. break;
  121. case 'b':
  122. if( g->p->weapon == 0 )
  123. {
  124. g->p->weapon = 4;
  125. add_weapon( g, g->wp, (g->p->x + 2) << 4, g->p->y << 4, 0, -16, WEAPON_BOMB );
  126. }
  127. case ' ':
  128. if( g->p->weapon == 0 )
  129. {
  130. g->p->weapon = 4;
  131. add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_LASER );
  132. add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 0, -16, WEAPON_LASER );
  133. /* Extra shtuph */
  134. add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER );
  135. add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER );
  136. /* More shtuph */
  137. add_weapon( g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
  138. add_weapon( g, g->wp, (g->p->x + 4) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
  139. /* Even more shtuph */
  140. add_weapon( g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
  141. add_weapon( g, g->wp, (g->p->x + 3) << 4, (g->p->y - 1) << 4, 0, -16, WEAPON_LASER );
  142. /* Extra shtuph */
  143. add_weapon( g, g->wp, g->p->x << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER );
  144. add_weapon( g, g->wp, (g->p->x + 5) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER );
  145. }
  146. break;
  147. }
  148. }
  149. usleep(40000);
  150. if( !poz || skip )
  151. {
  152. skip = 0;
  153. /* XXX: to be removed */
  154. if( GET_RAND(0,10) == 0 )
  155. {
  156. int list[3] = { ALIEN_POOLP, ALIEN_BOOL, ALIEN_BRAH };
  157. add_alien( g, g->al, 0, rand() % g->h / 2, list[GET_RAND(0,3)] );
  158. }
  159. /* Update game rules */
  160. if( g->t->right[1] - g->t->left[1] == g->t->w )
  161. {
  162. g->t->w = 85 - g->t->w;
  163. }
  164. /* Scroll and update positions */
  165. collide_player_tunnel( g, g->p, g->t, g->ex );
  166. update_player( g, g->p );
  167. collide_player_tunnel( g, g->p, g->t, g->ex );
  168. update_starfield( g, g->sf );
  169. update_bonus( g, g->bo );
  170. update_aliens( g, g->al );
  171. collide_weapons_tunnel( g, g->wp, g->t, g->ex );
  172. collide_weapons_aliens( g, g->wp, g->al, g->ex );
  173. update_weapons( g, g->wp );
  174. collide_weapons_tunnel( g, g->wp, g->t, g->ex );
  175. collide_weapons_aliens( g, g->wp, g->al, g->ex );
  176. update_explosions( g, g->ex );
  177. /*if(purcompteur%2)*/ update_tunnel( g, g->t );
  178. }
  179. /* Clear screen */
  180. clear_graphics();
  181. /* Print starfield, tunnel, aliens, player and explosions */
  182. draw_starfield( g, g->sf );
  183. draw_tunnel( g, g->t );
  184. draw_bonus( g, g->bo );
  185. draw_aliens( g, g->al );
  186. draw_explosions( g, g->ex );
  187. draw_weapons( g, g->wp );
  188. draw_player( g, g->p );
  189. /* Refresh */
  190. refresh_graphics();
  191. purcompteur++;
  192. }
  193. free_starfield( g, g->sf );
  194. free_tunnel( g->t );
  195. // free_player( g->p );
  196. }