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.
 
 
 
 
 

180 rader
4.2 KiB

  1. /*
  2. * ttyvaders - a tty based shoot'em'up
  3. * Copyright (C) 2002 Sam Hocevar <sam@zoy.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 1, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include "common.h"
  25. static void start_game (game *);
  26. int main (int argc, char **argv)
  27. {
  28. game *g = malloc(sizeof(game));
  29. // srand(time(NULL));
  30. if( init_graphics() )
  31. {
  32. return 1;
  33. }
  34. /* Initialize our program */
  35. init_game(g);
  36. /* Go ! */
  37. start_game(g);
  38. /* Clean up */
  39. end_graphics();
  40. return 0;
  41. }
  42. static void start_game (game *g)
  43. {
  44. int i;
  45. int quit = 0;
  46. int poz = 0;
  47. int skip = 0;
  48. starfield *sf = malloc(sizeof(starfield));
  49. weapons *wp = malloc(sizeof(weapons));
  50. explosions *ex = malloc(sizeof(explosions));
  51. tunnel *t = create_tunnel( g, g->w, g->h );
  52. player *p = create_player( g );
  53. aliens *al = malloc(sizeof(aliens));
  54. init_starfield( g, sf );
  55. init_weapons( g, wp );
  56. init_explosions( g, ex );
  57. init_aliens( g, al );
  58. /* Temporary stuff */
  59. for( i = 0; i < 5; i++ )
  60. {
  61. add_alien( g, al, rand() % g->w, rand() % g->h / 2 );
  62. }
  63. t->w = 25;
  64. while( !quit )
  65. {
  66. char key;
  67. while( ( key = get_key() ) )
  68. {
  69. switch( key )
  70. {
  71. case 'q':
  72. quit = 1;
  73. break;
  74. case 'p':
  75. poz = !poz;
  76. break;
  77. case 's':
  78. skip = 1;
  79. break;
  80. case 'h':
  81. p->dir = -3;
  82. break;
  83. case 'j':
  84. //if( p->y < g->h - 2 ) p->y += 1;
  85. break;
  86. case 'k':
  87. //if( p->y > 1 ) p->y -= 1;
  88. break;
  89. case 'l':
  90. p->dir = 3;
  91. break;
  92. case '\r':
  93. add_explosion( g, ex, p->x + 2, p->y, 0, 0, 2 );
  94. break;
  95. case ' ':
  96. if( p->weapon == 0 )
  97. {
  98. p->weapon = 4;
  99. add_weapon( g, wp, p->x, p->y );
  100. add_weapon( g, wp, p->x + 5, p->y );
  101. }
  102. break;
  103. }
  104. }
  105. usleep(40000);
  106. if( GET_RAND(0,10) == 0 )
  107. {
  108. add_alien( g, al, 0, rand() % g->h / 2 );
  109. }
  110. if( poz )
  111. {
  112. if( skip )
  113. {
  114. skip = 0;
  115. }
  116. else
  117. {
  118. continue;
  119. }
  120. }
  121. /* Scroll and update positions */
  122. collide_player_tunnel( g, p, t, ex );
  123. update_player( g, p );
  124. collide_player_tunnel( g, p, t, ex );
  125. update_starfield( g, sf );
  126. update_aliens( g, al );
  127. collide_weapons_tunnel( g, wp, t, ex );
  128. collide_weapons_aliens( g, wp, al, ex );
  129. update_weapons( g, wp );
  130. collide_weapons_tunnel( g, wp, t, ex );
  131. collide_weapons_aliens( g, wp, al, ex );
  132. update_explosions( g, ex );
  133. update_tunnel( g, t );
  134. /* Clear screen */
  135. clear_graphics();
  136. /* Print starfield, tunnel, aliens, player and explosions */
  137. draw_starfield( g, sf );
  138. draw_tunnel( g, t );
  139. draw_aliens( g, al );
  140. draw_player( g, p );
  141. draw_weapons( g, wp );
  142. draw_explosions( g, ex );
  143. /* Refresh */
  144. refresh_graphics();
  145. }
  146. #if 0
  147. free_player( p );
  148. free_tunnel( t );
  149. #endif
  150. }