space.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Simple Space Invaders: pixel graphics, keyboard input. Uses kernel API at PROGRAM_API_ADDR. */
  2. #include "prog_api.h"
  3. #define INV_ROWS 5
  4. #define INV_COLS 10
  5. #define INV_W 10
  6. #define INV_H 8
  7. #define SHIP_W 16
  8. #define SHIP_H 8
  9. #define BULLET_W 2
  10. #define BULLET_H 6
  11. #define MAX_BULLETS 4
  12. #define FRAME_MS 33
  13. #define INV_STEP 4
  14. #define BULLET_SPEED 6
  15. #define SHIP_SPEED 6
  16. static const struct prog_api *const api = (const struct prog_api *)PROGRAM_API_ADDR;
  17. /* Row colors: red, green, yellow, cyan, magenta */
  18. static const unsigned int row_r[INV_ROWS] = { 255, 0, 255, 0, 255 };
  19. static const unsigned int row_g[INV_ROWS] = { 0, 255, 255, 255, 0 };
  20. static const unsigned int row_b[INV_ROWS] = { 0, 0, 0, 255, 255 };
  21. static unsigned int fb_w, fb_h;
  22. static int ship_x;
  23. static int inv_x, inv_y;
  24. static int inv_dir;
  25. static unsigned char alive[INV_ROWS][INV_COLS];
  26. static int num_alive;
  27. static int bullet_x[MAX_BULLETS];
  28. static int bullet_y[MAX_BULLETS];
  29. static int bullet_active[MAX_BULLETS];
  30. static int num_bullets;
  31. static void put_rect(int x0, int y0, int w, int h, unsigned int r, unsigned int g, unsigned int b)
  32. {
  33. int x, y;
  34. for (y = 0; y < h; y++)
  35. for (x = 0; x < w; x++)
  36. api->put_pixel_rgb((unsigned int)(x0 + x), (unsigned int)(y0 + y), r, g, b);
  37. }
  38. static void draw_ship(void)
  39. {
  40. int x = ship_x;
  41. int y = (int)fb_h - SHIP_H - 4;
  42. put_rect(x, y, SHIP_W, SHIP_H, 0, 255, 128);
  43. }
  44. static void draw_invader(int row, int col)
  45. {
  46. int x = inv_x + col * (INV_W + 4);
  47. int y = inv_y + row * (INV_H + 2);
  48. if (!alive[row][col])
  49. return;
  50. put_rect(x, y, INV_W, INV_H, row_r[row], row_g[row], row_b[row]);
  51. }
  52. static void draw_bullets(void)
  53. {
  54. int i;
  55. for (i = 0; i < MAX_BULLETS; i++)
  56. if (bullet_active[i])
  57. put_rect(bullet_x[i], bullet_y[i], BULLET_W, BULLET_H, 255, 255, 255);
  58. }
  59. static void spawn_bullet(void)
  60. {
  61. int i;
  62. for (i = 0; i < MAX_BULLETS; i++) {
  63. if (!bullet_active[i]) {
  64. bullet_x[i] = ship_x + SHIP_W / 2 - BULLET_W / 2;
  65. bullet_y[i] = (int)fb_h - SHIP_H - 4 - BULLET_H;
  66. bullet_active[i] = 1;
  67. num_bullets++;
  68. break;
  69. }
  70. }
  71. }
  72. static void update_bullets(void)
  73. {
  74. int i, r, c;
  75. int bx, by, ix, iy;
  76. for (i = 0; i < MAX_BULLETS; i++) {
  77. if (!bullet_active[i])
  78. continue;
  79. bullet_y[i] -= BULLET_SPEED;
  80. if (bullet_y[i] < 0) {
  81. bullet_active[i] = 0;
  82. num_bullets--;
  83. continue;
  84. }
  85. bx = bullet_x[i];
  86. by = bullet_y[i];
  87. for (r = 0; r < INV_ROWS; r++) {
  88. for (c = 0; c < INV_COLS; c++) {
  89. if (!alive[r][c])
  90. continue;
  91. ix = inv_x + c * (INV_W + 4);
  92. iy = inv_y + r * (INV_H + 2);
  93. if (bx + BULLET_W > ix && bx < ix + INV_W &&
  94. by + BULLET_H > iy && by < iy + INV_H) {
  95. alive[r][c] = 0;
  96. num_alive--;
  97. bullet_active[i] = 0;
  98. num_bullets--;
  99. goto next_bullet;
  100. }
  101. }
  102. }
  103. next_bullet:;
  104. }
  105. }
  106. static void update_invaders(void)
  107. {
  108. int edge;
  109. inv_x += inv_dir * INV_STEP;
  110. edge = inv_x + INV_COLS * (INV_W + 4) - 4;
  111. if (inv_dir > 0 && edge >= (int)fb_w) {
  112. inv_dir = -1;
  113. inv_y += INV_H + 4;
  114. inv_x = (int)fb_w - (INV_COLS * (INV_W + 4) - 4);
  115. if (inv_x < 0)
  116. inv_x = 0;
  117. } else if (inv_dir < 0 && inv_x <= 0) {
  118. inv_dir = 1;
  119. inv_y += INV_H + 4;
  120. inv_x = 0;
  121. }
  122. }
  123. static void game_init(void)
  124. {
  125. int r, c;
  126. fb_w = api->get_fb_width();
  127. fb_h = api->get_fb_height();
  128. if (fb_w < 100)
  129. fb_w = 320;
  130. if (fb_h < 100)
  131. fb_h = 200;
  132. ship_x = (int)fb_w / 2 - SHIP_W / 2;
  133. if (ship_x < 0)
  134. ship_x = 0;
  135. inv_x = 20;
  136. inv_y = 20;
  137. inv_dir = 1;
  138. num_alive = INV_ROWS * INV_COLS;
  139. for (r = 0; r < INV_ROWS; r++)
  140. for (c = 0; c < INV_COLS; c++)
  141. alive[r][c] = 1;
  142. num_bullets = 0;
  143. for (c = 0; c < MAX_BULLETS; c++)
  144. bullet_active[c] = 0;
  145. }
  146. void _start(void)
  147. {
  148. unsigned int last_tick;
  149. int k;
  150. int r, c;
  151. game_init();
  152. last_tick = api->get_ticks_ms();
  153. for (;;) {
  154. int want_left = 0, want_right = 0, want_fire = 0;
  155. while (api->get_ticks_ms() - last_tick < FRAME_MS) {
  156. k = api->poll_key_with_hw();
  157. if (k == IO_KEY_ESC)
  158. return;
  159. if (k == IO_KEY_LEFT)
  160. want_left = 1;
  161. if (k == IO_KEY_RIGHT)
  162. want_right = 1;
  163. if (k == IO_KEY_SPACE)
  164. want_fire = 1;
  165. }
  166. last_tick = api->get_ticks_ms();
  167. if (want_left && ship_x > 0)
  168. ship_x -= SHIP_SPEED;
  169. if (want_right && ship_x + SHIP_W < (int)fb_w)
  170. ship_x += SHIP_SPEED;
  171. if (want_fire)
  172. spawn_bullet();
  173. update_bullets();
  174. update_invaders();
  175. api->clear_rgb(8, 8, 32);
  176. draw_ship();
  177. for (r = 0; r < INV_ROWS; r++)
  178. for (c = 0; c < INV_COLS; c++)
  179. draw_invader(r, c);
  180. draw_bullets();
  181. if (num_alive <= 0)
  182. return;
  183. if (inv_y + INV_ROWS * (INV_H + 2) >= (int)fb_h - SHIP_H - 8)
  184. return;
  185. }
  186. }