player.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <curses.h>
  4. #include "game.h"
  5. #include "player.h"
  6. void init_player(PLAYER* player, int x, int y) {
  7. player -> head_dir_x = 1;
  8. player -> head_dir_y = 0;
  9. player -> next_head_dir_x = 1;
  10. player -> next_head_dir_y = 0;
  11. player -> length = 6;
  12. player -> head_ptr = player -> length - 1;
  13. player -> body = malloc(sizeof(POINT*) * player -> length);
  14. player -> old_tail = NULL;
  15. for (int i = 0; i < player -> length; i++) {
  16. POINT* pt = malloc(sizeof(POINT));
  17. pt -> x = x + i;
  18. pt -> y = y;
  19. player -> body[i] = pt;
  20. }
  21. }
  22. void destroy_player(PLAYER* player) {
  23. for (int i = 0; i < player -> length; i++) {
  24. free(player -> body[i]);
  25. }
  26. if (player -> old_tail)
  27. free(player -> old_tail);
  28. free(player -> body);
  29. free(player);
  30. }
  31. int bit_self(PLAYER* player, POINT* head_pos) {
  32. for (int i = 0; i < player -> length; i++) {
  33. if (player -> body[i] != head_pos &&
  34. player -> body[i] -> x == head_pos -> x &&
  35. player -> body[i] -> y == head_pos -> y) {
  36. return 1;
  37. }
  38. }
  39. return 0;
  40. }
  41. int hit_head(FIELD* field, POINT* head) {
  42. return head -> x == 0 ||
  43. head -> y == 0 ||
  44. head -> x == field -> width - 1 ||
  45. head -> y == field -> height - 1;
  46. }
  47. void move_player(FIELD* field) {
  48. PLAYER* player = field -> player;
  49. player -> head_dir_x = player -> next_head_dir_x;
  50. player -> head_dir_y = player -> next_head_dir_y;
  51. POINT* oldhead = player -> body[player -> head_ptr];
  52. player -> head_ptr = (player -> head_ptr + 1) % player -> length;
  53. free(player -> old_tail);
  54. player -> old_tail = player -> body[player -> head_ptr];
  55. POINT* newhead = malloc(sizeof(POINT));
  56. newhead -> x = oldhead -> x + player -> head_dir_x;
  57. newhead -> y = oldhead -> y + player -> head_dir_y;
  58. for (int i = 0; i < field -> ball_count; i++) {
  59. if (field -> balls[i] -> position.x == newhead -> x &&
  60. field -> balls[i] -> position.y == newhead -> y) {
  61. eat_ball(field, i);
  62. }
  63. }
  64. player -> body[player -> head_ptr] = newhead;
  65. if (bit_self(player, newhead) ||
  66. hit_head(field, newhead)) {
  67. set_lost(field -> state);
  68. }
  69. }
  70. void handle_control(PLAYER* player) {
  71. int ch = getch();
  72. switch(ch) {
  73. case CTRL_LEFT:
  74. case 'h':
  75. case 'a':
  76. if (player -> head_dir_x != 1) {
  77. player -> next_head_dir_x = -1;
  78. player -> next_head_dir_y = 0;
  79. }
  80. break;
  81. case CTRL_RIGHT:
  82. case 'l':
  83. case 'd':
  84. if (player -> head_dir_x != -1) {
  85. player -> next_head_dir_x = 1;
  86. player -> next_head_dir_y = 0;
  87. }
  88. break;
  89. case CTRL_UP:
  90. case 'k':
  91. case 'w':
  92. if (player -> head_dir_y != 1) {
  93. player -> next_head_dir_x = 0;
  94. player -> next_head_dir_y = -1;
  95. }
  96. break;
  97. case CTRL_DOWN:
  98. case 'j':
  99. case 's':
  100. if (player -> head_dir_y != -1) {
  101. player -> next_head_dir_x = 0;
  102. player -> next_head_dir_y = 1;
  103. }
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. int is_on_player(PLAYER* player, POINT* point) {
  110. for (int i = 0; i < player -> length; i++) {
  111. if (player -> body[i] -> x == point -> x &&
  112. player -> body[i] -> y == point -> y) {
  113. return 1;
  114. }
  115. }
  116. return 0;
  117. }