field.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdlib.h>
  2. #include <curses.h>
  3. #include <math.h>
  4. #include "game.h"
  5. #include "ball.h"
  6. #include "player.h"
  7. #include "field.h"
  8. void add_ball(FIELD* f, int max_value) {
  9. POINT* playerhead = f -> player -> body[f -> player -> head_ptr];
  10. BALL* b = malloc(sizeof(BALL));
  11. //find a value
  12. int value = (random() % max_value) + 1;
  13. //get random position that's at least 10 away from the player's head
  14. int x;
  15. int y;
  16. int dir = random() % 4;
  17. do {
  18. x = (random() % (f -> width - 2)) + 1;
  19. y = (random() % (f -> height - 2)) + 1;
  20. } while (sqrt((double)((x - playerhead -> x) * (x - playerhead -> x) +
  21. (y - playerhead -> y) * (y - playerhead -> y))) < (double) (13 + value));
  22. init_ball(b, x, y, dir, value);
  23. f -> ball_count++;
  24. //if ball buffer is too small, double it
  25. if (f -> ball_count >= f -> max_allocated_balls) {
  26. f -> max_allocated_balls *= 2;
  27. f -> balls = realloc(f -> balls, f -> max_allocated_balls);
  28. }
  29. f -> balls[f -> ball_count - 1] = b;
  30. }
  31. void init_field(FIELD* f, int width, int height, GAME_STATE* state) {
  32. f -> state = state;
  33. f -> win = subwin(stdscr, height, 2 * width, 0, 0);
  34. f -> width = width;
  35. f -> height = height;
  36. f -> max_allocated_balls = 1;
  37. f -> balls = malloc(sizeof(BALL) * f -> max_allocated_balls);
  38. f -> ball_count = 0;
  39. f -> player = malloc(sizeof(PLAYER));
  40. }
  41. void destroy_field(FIELD* f) {
  42. destroy_player(f -> player);
  43. for (int i = 0; i < f -> ball_count; i++) {
  44. free(f -> balls[i]);
  45. }
  46. free(f -> balls);
  47. delwin(f -> win);
  48. free(f);
  49. }
  50. void draw_outline(FIELD* f) {
  51. wattrset(f -> win, COLOR_PAIR(1));
  52. for (int i = 0; i < f -> width * 2 - 1; i++) {
  53. mvwprintw(f -> win, 0, i , " ");
  54. mvwprintw(f -> win, f -> height - 1, i , " ");
  55. }
  56. for (int i = 1; i < f -> height - 1; i++) {
  57. mvwprintw(f -> win, i, 0 , " ");
  58. mvwprintw(f -> win, i, f -> width * 2 - 2, " ");
  59. }
  60. }
  61. void draw_player(FIELD* f) {
  62. wattrset(f -> win, COLOR_PAIR(2));
  63. PLAYER* player = f -> player;
  64. POINT* p;
  65. for (int i = 0; i < player -> length; i++) {
  66. p = player -> body[i];
  67. mvwprintw(f -> win, p -> y, p -> x * 2, " ");
  68. }
  69. }
  70. void redraw_field(FIELD* f) {
  71. clear();
  72. wattrset(f -> win, COLOR_PAIR(1));
  73. draw_outline(f);
  74. for (int i = 0; i < f -> ball_count; i++) {
  75. BALL* b = f -> balls[i];
  76. wattrset(f -> win, COLOR_PAIR(b -> color));
  77. mvwprintw(f -> win, b -> position.y, b -> position.x * 2, " ");
  78. }
  79. draw_player(f);
  80. wrefresh(f -> win);
  81. }
  82. void update_ballpos(FIELD* f) {
  83. for (int i = 0; i < f -> ball_count; i++) {
  84. BALL* b = f -> balls[i];
  85. // clear old ball position if it's not on player
  86. if (! is_on_player(f -> player, & b -> old_position)) {
  87. wattrset(f -> win, COLOR_PAIR(7));
  88. mvwprintw(f -> win, b -> old_position.y, b -> old_position.x * 2, " ");
  89. }
  90. // draw ball at new position
  91. wattrset(f -> win, COLOR_PAIR(b -> color));
  92. mvwprintw(f -> win, b -> position.y, b -> position.x * 2, " ");
  93. }
  94. wrefresh(f -> win);
  95. }
  96. void update_playerpos(FIELD* f) {
  97. PLAYER* player = f -> player;
  98. wattrset(f -> win, COLOR_PAIR(7));
  99. mvwprintw(f -> win, player -> old_tail -> y, player -> old_tail -> x * 2, " ");
  100. wattrset(f -> win, COLOR_PAIR(2));
  101. mvwprintw(f -> win,
  102. player -> body[player -> head_ptr] -> y,
  103. player -> body[player -> head_ptr] -> x * 2,
  104. " ");
  105. wrefresh(f -> win);
  106. }