Guest User

Untitled

a guest
Dec 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include "SDL2/SDL.h"
  5.  
  6. #define FPS 20
  7.  
  8. #define SNAKE_UP 0
  9. #define SNAKE_RIGHT 1
  10. #define SNAKE_DOWN 2
  11. #define SNAKE_LEFT 3
  12.  
  13. #define POINT_WIDTH 20
  14. #define POINT_HEIGHT 20
  15. #define GAME_HEIGHT 25
  16. #define GAME_WIDTH 35
  17.  
  18. const int WINDOW_WIDTH = POINT_WIDTH * GAME_WIDTH;
  19. const int WINDOW_HEIGHT = POINT_HEIGHT * GAME_HEIGHT;
  20.  
  21. const int SNAKE_MAX_LENGTH = GAME_WIDTH * GAME_HEIGHT;
  22.  
  23. typedef struct {
  24. int x;
  25. int y;
  26. } Point;
  27.  
  28. typedef struct {
  29. Point apple;
  30. Point *snake; // list of points
  31. int snake_len;
  32. int snake_direction;
  33. SDL_Window *window;
  34. SDL_Renderer *renderer;
  35. int exit;
  36. } Game;
  37.  
  38. void handle_event(Game *game, SDL_Event *event);
  39. void move_snake(Game *game);
  40. void update(Game *game);
  41. void render(Game *game);
  42. void initialize(Game *game);
  43. void terminate(Game *game);
  44. void debug_info(Game *game);
  45.  
  46. int main() {
  47.  
  48. srand(time(NULL));
  49.  
  50. Game game;
  51. SDL_Event event;
  52.  
  53. initialize(&game);
  54.  
  55. while (!game.exit) {
  56. update(&game);
  57. render(&game);
  58. SDL_Delay(1000 / FPS);
  59. }
  60.  
  61. terminate(&game);
  62. return EXIT_SUCCESS;
  63. }
  64.  
  65. void debug_info(Game *game) {
  66. printf("\n====DEBUG====\n");
  67. printf("Apple: (%d, %d)\n", game->apple.x, game->apple.y);
  68. printf("snake_len: %d\n", game->snake_len);
  69. printf("snake_direction: %d\n", game->snake_direction);
  70. printf("snake: ");
  71. for (int i = 0; i < game->snake_len; i++) {
  72. printf("(%d, %d) ", game->snake[i].x, game->snake[i].y);
  73. }
  74. printf("\n===/DEBUG====\n");
  75. }
  76.  
  77. void handle_event(Game *game, SDL_Event *event) {
  78. switch (event->type) {
  79. case SDL_QUIT:
  80. game->exit = 1;
  81. return;
  82. case SDL_KEYDOWN:
  83. switch (event->key.keysym.scancode) {
  84. case SDL_SCANCODE_UP:
  85. printf("walk up\n");
  86. game->snake_direction = SNAKE_UP;
  87. break;
  88. case SDL_SCANCODE_RIGHT:
  89. printf("walk right\n");
  90. game->snake_direction = SNAKE_RIGHT;
  91. break;
  92. case SDL_SCANCODE_DOWN:
  93. printf("walk down\n");
  94. game->snake_direction = SNAKE_DOWN;
  95. break;
  96. case SDL_SCANCODE_LEFT:
  97. printf("walk left\n");
  98. game->snake_direction = SNAKE_LEFT;
  99. break;
  100. }
  101. break;
  102. }
  103. }
  104.  
  105. void move_snake(Game *game) {
  106. Point next_point;
  107. Point snake_head = game->snake[game->snake_len - 1];
  108.  
  109. switch (game->snake_direction) {
  110. case SNAKE_UP:
  111. next_point.x = snake_head.x;
  112. next_point.y = snake_head.y - 1;
  113. break;
  114. case SNAKE_RIGHT:
  115. next_point.x = snake_head.x + 1;
  116. next_point.y = snake_head.y;
  117. break;
  118. case SNAKE_DOWN:
  119. next_point.x = snake_head.x;
  120. next_point.y = snake_head.y + 1;
  121. break;
  122. case SNAKE_LEFT:
  123. next_point.x = snake_head.x - 1;
  124. next_point.y = snake_head.y;
  125. break;
  126. }
  127. next_point.x = (next_point.x + GAME_WIDTH) % GAME_WIDTH;
  128. next_point.y = (next_point.y + GAME_HEIGHT) % GAME_HEIGHT;
  129. if (next_point.x == game->apple.x && next_point.y == game->apple.y) {
  130. // snake eats apple and grows
  131.  
  132. if (game->snake_len >= SNAKE_MAX_LENGTH) {
  133. printf("\n\nERROR: snake length cannot be bigger than %d\n\n", SNAKE_MAX_LENGTH);
  134. terminate(game);
  135. exit(EXIT_FAILURE);
  136. }
  137.  
  138. game->snake[game->snake_len].x = next_point.x;
  139. game->snake[game->snake_len].y = next_point.y;
  140. game->snake_len++;
  141. } else {
  142. // snake remains with the same length
  143. for (int i = 0; i < game->snake_len - 1; i++) {
  144. game->snake[i].x = game->snake[i + 1].x;
  145. game->snake[i].y = game->snake[i + 1].y;
  146. }
  147. game->snake[game->snake_len - 1].x = next_point.x;
  148. game->snake[game->snake_len - 1].y = next_point.y;
  149. }
  150. }
  151.  
  152. void update(Game* game) {
  153. SDL_Event event;
  154. while (SDL_PollEvent(&event)) {
  155. handle_event(game, &event);
  156. }
  157. debug_info(game);
  158. move_snake(game);
  159.  
  160. }
  161.  
  162. void render(Game *game) {
  163.  
  164. SDL_Rect rect;
  165. rect.w = POINT_WIDTH;
  166. rect.h = POINT_HEIGHT;
  167.  
  168. // draw backgrond
  169. SDL_SetRenderDrawColor(game->renderer, 20, 20, 20, 255);
  170. SDL_RenderClear(game->renderer);
  171.  
  172. // draw snake
  173. SDL_SetRenderDrawColor(game->renderer, 255, 255, 255, 255);
  174. for (int i = 0; i < game->snake_len; i++) {
  175. rect.x = game->snake[i].x * POINT_WIDTH;
  176. rect.y = game->snake[i].y * POINT_HEIGHT;
  177. SDL_RenderFillRect(game->renderer, &rect);
  178. }
  179.  
  180. // draw apple
  181. rect.x = game->apple.x * POINT_WIDTH;
  182. rect.y = game->apple.y * POINT_HEIGHT;
  183. SDL_SetRenderDrawColor(game->renderer, 255, 20, 20, 255);
  184. SDL_RenderFillRect(game->renderer, &rect);
  185.  
  186.  
  187. SDL_RenderPresent(game->renderer);
  188. }
  189.  
  190. void initialize(Game *game) {
  191. SDL_Init(SDL_INIT_EVERYTHING);
  192. game->window = SDL_CreateWindow("Snake Game",
  193. SDL_WINDOWPOS_CENTERED,
  194. SDL_WINDOWPOS_CENTERED,
  195. WINDOW_WIDTH,
  196. WINDOW_HEIGHT,
  197. SDL_WINDOW_SHOWN);
  198.  
  199. game->renderer = SDL_CreateRenderer(game->window, -1, 0);
  200.  
  201. game->exit = 0;
  202. game->snake_len = 1;
  203. game->snake_direction = SNAKE_RIGHT;
  204.  
  205. game->snake = (Point*)malloc(SNAKE_MAX_LENGTH * sizeof(Point));
  206. game->snake[0].x = GAME_WIDTH / 2;
  207. game->snake[0].y = GAME_HEIGHT / 2;
  208. }
  209.  
  210. void terminate(Game *game) {
  211. SDL_DestroyWindow(game->window);
  212. SDL_Quit();
  213. free(game->renderer);
  214. game->renderer = NULL;
  215. free(game->snake);
  216. game->snake = NULL;
  217. }
Add Comment
Please, Sign In to add comment