Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

269 rader
7.6 KiB

  1. /*
  2. * ttyvaders Textmode shoot'em up
  3. * Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  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 "config.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "common.h"
  28. static void start_game (game *);
  29. int main (int argc, char **argv)
  30. {
  31. game *g = malloc(sizeof(game));
  32. if(g == NULL)
  33. exit(1);
  34. srand(time(NULL));
  35. if(ee_init())
  36. {
  37. return 1;
  38. }
  39. ee_set_delay(100000);
  40. /* Initialize our program */
  41. g->w = ee_get_width();
  42. g->h = ee_get_height();
  43. intro();
  44. /* Go ! */
  45. start_game(g);
  46. /* Clean up */
  47. ee_end();
  48. return 0;
  49. }
  50. static void start_game (game *g)
  51. {
  52. int quit = 0;
  53. int poz = 0;
  54. int skip = 0;
  55. int purcompteur = 0;
  56. box *pausebox = NULL;
  57. g->sf = create_starfield(g);
  58. g->wp = malloc(sizeof(weapons));
  59. if(g->wp == NULL)
  60. exit(1);
  61. g->ex = malloc(sizeof(explosions));
  62. if(g->ex == NULL)
  63. exit(1);
  64. g->bo = malloc(sizeof(bonus));
  65. if(g->bo == NULL)
  66. exit(1);
  67. g->t = create_tunnel(g, g->w, g->h);
  68. g->p = create_player(g);
  69. g->al = malloc(sizeof(aliens));
  70. if(g->al == NULL)
  71. exit(1);
  72. init_bonus(g, g->bo);
  73. init_weapons(g, g->wp);
  74. init_explosions(g, g->ex);
  75. init_aliens(g, g->al);
  76. /* Temporary stuff */
  77. g->t->w = 25;
  78. while(!quit)
  79. {
  80. char key;
  81. while((key = ee_get_key()))
  82. {
  83. switch(key)
  84. {
  85. case 'q':
  86. quit = 1;
  87. break;
  88. case 'p':
  89. poz = !poz;
  90. if(poz)
  91. {
  92. pausebox = create_box(g, g->w / 2, g->h / 2,
  93. g->w - 16, 8);
  94. }
  95. else
  96. {
  97. free_box(pausebox);
  98. }
  99. break;
  100. case '\t':
  101. ceo_alert(g);
  102. poz = 1;
  103. break;
  104. case 's':
  105. skip = 1;
  106. break;
  107. default:
  108. if(g->p->dead)
  109. {
  110. break;
  111. }
  112. switch(key)
  113. {
  114. case 'h':
  115. g->p->vx = -2;
  116. break;
  117. case 'j':
  118. if(g->p->y < g->h - 3) g->p->y += 1;
  119. break;
  120. case 'k':
  121. if(g->p->y > 2) g->p->y -= 1;
  122. break;
  123. case 'l':
  124. g->p->vx = 2;
  125. break;
  126. case 'n':
  127. if(g->p->special >= COST_NUKE)
  128. {
  129. g->p->special -= COST_NUKE;
  130. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_NUKE);
  131. }
  132. break;
  133. case 'f':
  134. if(g->p->special >= COST_FRAGBOMB)
  135. {
  136. g->p->special -= COST_FRAGBOMB;
  137. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_FRAGBOMB);
  138. }
  139. break;
  140. case 'b':
  141. if(g->p->special >= COST_BEAM)
  142. {
  143. g->p->special -= COST_BEAM;
  144. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, 0, WEAPON_BEAM);
  145. }
  146. break;
  147. case ' ':
  148. if(g->p->weapon == 0)
  149. {
  150. g->p->weapon = 4;
  151. add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
  152. add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 0, -32, WEAPON_LASER);
  153. /* Extra schtuph */
  154. add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -24, -16, WEAPON_SEEKER);
  155. add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 24, -16, WEAPON_SEEKER);
  156. /* More schtuph */
  157. add_weapon(g, g->wp, (g->p->x - 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  158. add_weapon(g, g->wp, (g->p->x + 2) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  159. /* Even more schtuph */
  160. add_weapon(g, g->wp, g->p->x << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  161. add_weapon(g, g->wp, (g->p->x + 1) << 4, (g->p->y - 1) << 4, 0, -32, WEAPON_LASER);
  162. /* Extra schtuph */
  163. add_weapon(g, g->wp, (g->p->x - 2) << 4, g->p->y << 4, -32, 0, WEAPON_SEEKER);
  164. add_weapon(g, g->wp, (g->p->x + 3) << 4, g->p->y << 4, 32, 0, WEAPON_SEEKER);
  165. /* MORE SCHTUPH! */
  166. add_weapon(g, g->wp, g->p->x << 4, g->p->y << 4, 0, -16, WEAPON_BOMB);
  167. }
  168. break;
  169. }
  170. }
  171. }
  172. if(!poz || skip)
  173. {
  174. skip = 0;
  175. /* XXX: to be removed */
  176. if(ee_rand(0, 9) == 0)
  177. {
  178. int list[3] = { ALIEN_FOO, ALIEN_BAR, ALIEN_BAZ };
  179. add_alien(g, g->al, 0, rand() % g->h / 2, list[ee_rand(0,2)]);
  180. }
  181. /* Update game rules */
  182. if(g->t->right[1] - g->t->left[1] == g->t->w)
  183. {
  184. g->t->w = 85 - g->t->w;
  185. }
  186. /* Scroll and update positions */
  187. collide_player_tunnel(g, g->p, g->t, g->ex);
  188. update_player(g, g->p);
  189. collide_player_tunnel(g, g->p, g->t, g->ex);
  190. update_starfield(g, g->sf);
  191. update_bonus(g, g->bo);
  192. update_aliens(g, g->al);
  193. collide_weapons_tunnel(g, g->wp, g->t, g->ex);
  194. collide_weapons_aliens(g, g->wp, g->al, g->ex);
  195. update_weapons(g, g->wp);
  196. collide_weapons_tunnel(g, g->wp, g->t, g->ex);
  197. collide_weapons_aliens(g, g->wp, g->al, g->ex);
  198. update_explosions(g, g->ex);
  199. update_tunnel(g, g->t);
  200. }
  201. /* Clear screen */
  202. ee_clear();
  203. /* Print starfield, tunnel, aliens, player and explosions */
  204. draw_starfield(g, g->sf);
  205. draw_aliens(g, g->al);
  206. draw_tunnel(g, g->t);
  207. draw_bonus(g, g->bo);
  208. draw_explosions(g, g->ex);
  209. draw_weapons(g, g->wp);
  210. draw_player(g, g->p);
  211. draw_status(g);
  212. /* Print pause box if needed */
  213. if(poz)
  214. {
  215. pausebox->frame++;
  216. draw_box(g, pausebox);
  217. }
  218. /* Refresh */
  219. ee_refresh();
  220. purcompteur++;
  221. }
  222. if(pausebox)
  223. {
  224. free_box(pausebox);
  225. }
  226. free_starfield(g, g->sf);
  227. free_tunnel(g->t);
  228. free_player(g->p);
  229. }