Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

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