qsadfasdgfgads

Untitled

Mar 31st, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include "lib.h"
  7. #include <sys/poll.h>
  8. #include <signal.h>
  9.  
  10. #define TIMEOUT 5
  11.  
  12. #define ROWS 50
  13. #define COLS 50
  14.  
  15. #define HERO_SYMBOL '@'
  16. #define FOOD_SYMBOL '$'
  17.  
  18. void clrScr(void){
  19.  
  20. printf("\033[0;0H\033[2J");
  21. fflush(stdout);
  22.  
  23. }
  24.  
  25. static int scores;
  26.  
  27. struct Food{
  28. size_t x;
  29. size_t y;
  30. char symbol;
  31. };
  32.  
  33. struct Game{
  34. char hero;
  35. size_t x;
  36. size_t y;
  37. char board[COLS][ROWS];
  38.  
  39. struct Food food;
  40. };
  41.  
  42. void generateFood(struct Game *game) {
  43. game->food.x = generate_random_in_range(0, COLS);
  44. game->food.y = generate_random_in_range(0, ROWS);
  45.  
  46.  
  47. struct Food food = game->food;
  48. game->board[food.x][food.y] = food.symbol;
  49. }
  50.  
  51. struct Game init_board(size_t x, size_t y){
  52. struct Game game;
  53. for (int i = 0; i < ROWS; i++) {
  54. for (int j = 0; j < COLS; j++) {
  55. game.board[i][j] = ' ';
  56. }
  57. }
  58.  
  59. game.x = x;
  60. game.y = y;
  61. game.board[x][y] = HERO_SYMBOL;
  62. game.hero = HERO_SYMBOL;
  63. game.food.symbol = FOOD_SYMBOL;
  64.  
  65.  
  66. generateFood(&game);
  67.  
  68.  
  69. return game;
  70. }
  71.  
  72. void drawCurrentState(struct Game game) {
  73.  
  74. int cols;
  75. int rows;
  76. tc_get_cols_rows(&cols, &rows);
  77. tc_move_cursor((cols - 12)/2, rows/2);
  78.  
  79. printf("%s%s Scores %d %s", TC_YEL, TC_BG_GRN, scores, TC_NRM);
  80. tc_move_cursor(0,0);
  81. for (size_t i = 0; i <= ROWS; ++i) {
  82. printf("--");
  83. }
  84.  
  85. printf("\n");
  86.  
  87. // Print the current state of the board
  88. for (int i = 0; i < COLS; i++) {
  89. for (int j = 0; j < ROWS; j++) {
  90. if(j == 0){
  91. printf("| ");
  92. }
  93. printf("%c ", game.board[i][j]);
  94. if(j == ROWS - 1){
  95. printf("|");
  96. }
  97. }
  98. printf("\n");
  99. }
  100.  
  101. for (size_t i = 0; i <= ROWS; ++i) {
  102. printf("--");
  103. }
  104. printf("\n");
  105. }
  106.  
  107. void exit_game() {
  108. tc_exit_alt_screen();
  109. reset_keypress();
  110. exit(0);
  111. }
  112.  
  113.  
  114. void move(struct Game *game, size_t x, size_t y){
  115. if(x >= COLS || y >= ROWS){
  116. printf("Exit");
  117. exit_game();
  118. }
  119.  
  120. if(game->food.x == x && game->food.y == y){
  121. scores++;
  122. generateFood(game);
  123. }
  124.  
  125. game->board[game->x][game->y] = ' ';
  126. game->x = x;
  127. game->y = y;
  128. game->board[x][y] = game->hero;
  129. }
  130.  
  131. void handleButton(struct Game *game, char c) {
  132. if(c == 'w'){
  133. move(game, game->x - 1, game->y);
  134. }
  135. if(c == 'a'){
  136. move(game, game->x, game->y - 1);
  137. }
  138. if(c == 's'){
  139. move(game, game->x + 1, game->y);
  140. }
  141.  
  142. if(c == 'd'){
  143. move(game, game->x, game->y + 1);
  144. }
  145.  
  146. }
  147.  
  148.  
  149. int main(int argc, char **argv){
  150. srand(time(NULL));
  151. struct Game game = init_board(1, 1);
  152.  
  153. set_keypress();
  154. tc_enter_alt_screen();
  155. signal(SIGINT, SIG_IGN); // signal interrupt
  156. signal(SIGTSTP, SIG_IGN); // terminal stop SIGSTOP cannot be ignored. SIGTSTP might be.
  157. clrScr();
  158.  
  159. struct pollfd fds[1];
  160.  
  161. fds[0].fd = 0;
  162. fds[0].events = POLLIN; // without blocking
  163.  
  164. char lastInput = 'x';
  165. while(1) {
  166. clrScr();
  167. drawCurrentState(game);
  168.  
  169. int ret = poll(fds, 1, 1000);
  170. // printf("ASD %d\n", ret);
  171.  
  172. if (ret == -1){
  173. perror("poll()");
  174. exit_game();
  175. }
  176. if(ret > 0){
  177. char c = (char)getc(stdin);
  178. if(c == 'q'){
  179. exit_game();
  180. break;
  181. }
  182. handleButton(&game, c);
  183. lastInput = c;
  184. }else{
  185. handleButton(&game, lastInput);
  186. }
  187.  
  188.  
  189.  
  190. }
  191.  
  192.  
  193.  
  194. exit_game();
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment