YorKnEz

Untitled

Oct 20th, 2019
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright 2019 YorKnEz
  3. */
  4.  
  5. #include <SDL2/SDL.h>
  6. #include <stdio.h>
  7. #include <cstdlib>
  8. #include <time.h>
  9.  
  10. #define TITLE "Snake"
  11. #define SQUARE_SIZE 20
  12. #define BOARD_SIZE 20
  13.  
  14. typedef unsigned int uint;
  15.  
  16. bool quit = false;
  17.  
  18. uint16_t window_size = BOARD_SIZE * BOARD_SIZE;
  19.  
  20. typedef struct SnakePiece {
  21. int x;
  22. int y;
  23. SnakePiece *next;
  24. } SnakePiece;
  25.  
  26. SnakePiece *head = NULL;
  27.  
  28. int headx, heady,
  29. tailx, taily,
  30. len;
  31.  
  32. int x_increment = 0, y_increment = -1;
  33.  
  34. int fruitx, fruity;
  35.  
  36. uint fps = 15;
  37. uint minframetime = 1000 / fps;
  38.  
  39. SDL_Keycode keycode = SDLK_w;
  40.  
  41. void events(SDL_Window *window);
  42.  
  43. void draw(SDL_Renderer *renderer);
  44.  
  45. void add_piece (int x_data, int y_data) {
  46. SnakePiece *new_piece = (SnakePiece *) malloc (sizeof(SnakePiece));
  47.  
  48. new_piece->x = x_data;
  49. new_piece->y = y_data;
  50.  
  51. tailx = x_data;
  52. taily = y_data;
  53.  
  54. new_piece->next = head;
  55. head = new_piece;
  56.  
  57. len++;
  58. }
  59.  
  60. void initialize_snake () {
  61. headx = tailx = (BOARD_SIZE)/2-1;
  62. heady = taily = (BOARD_SIZE)/2-1;
  63.  
  64. add_piece (headx, heady);
  65.  
  66. for (int i = 0; i < 3; i++) {
  67. add_piece (tailx, taily+1);
  68. }
  69. }
  70.  
  71. void move_snake () {
  72.  
  73. SnakePiece *ptr;
  74.  
  75. ptr = head;
  76.  
  77. int old_x, old_y;
  78.  
  79. for (int i = 0; i < len; i++) {
  80. if (i == len-1) {
  81. old_x = ptr->x;
  82. old_y = ptr->y;
  83.  
  84. if (x_increment == -1 && (headx + x_increment) == -1) {
  85. ptr->x = BOARD_SIZE-1;
  86.  
  87. headx = ptr->x;
  88. } else if (x_increment == 1 && (headx + x_increment) == BOARD_SIZE) {
  89. ptr->x = 0;
  90.  
  91. headx = ptr->x;
  92. } else if (y_increment == -1 && (heady + y_increment) == -1) {
  93. ptr->y = BOARD_SIZE-1;
  94.  
  95. heady = ptr->y;
  96. } else if (y_increment == 1 && (heady + y_increment) == BOARD_SIZE) {
  97. ptr->y = 0;
  98.  
  99. heady = ptr->y;
  100. } else {
  101. ptr->x += x_increment;
  102. ptr->y += y_increment;
  103.  
  104. headx = ptr->x;
  105. heady = ptr->y;
  106.  
  107. break;
  108. }
  109. }
  110.  
  111. ptr = ptr->next;
  112. }
  113.  
  114. ptr = head;
  115.  
  116. int old_x2, old_y2;
  117.  
  118. for (int i = 0; i < len-1; i++) {
  119. for (int j = 0; j < (len-1)-i; j++) {
  120. if (j == (len-2)-i) {
  121. old_x2 = ptr->x;
  122. old_y2 = ptr->y;
  123.  
  124. ptr->x = old_x;
  125. ptr->y = old_y;
  126.  
  127. old_x = old_x2;
  128. old_y = old_y2;
  129. }
  130.  
  131. ptr = ptr->next;
  132. }
  133.  
  134. ptr = head;
  135. }
  136.  
  137. tailx = ptr->x;
  138. taily = ptr->y;
  139. }
  140.  
  141. void is_overlapping () {
  142. SnakePiece *ptr;
  143.  
  144. ptr = head;
  145.  
  146. for (int i = 0; i < len-1; i++) {
  147. if (headx == ptr->x && heady == ptr->y) {
  148. quit = true;
  149.  
  150. break;
  151. }
  152.  
  153. ptr = ptr->next;
  154. }
  155. }
  156.  
  157. bool can_gen_fruit () {
  158. SnakePiece *ptr;
  159.  
  160. ptr = head;
  161.  
  162. for (int i = 0; i < len ; i++) {
  163. if (fruitx == ptr->x && fruity == ptr->y) {
  164. return false;
  165. }
  166.  
  167. ptr = ptr->next;
  168. }
  169.  
  170. return true;
  171. }
  172.  
  173. void generate_fruit () {
  174. fruitx = rand() % (BOARD_SIZE-2) + 1;
  175. fruity = rand() % (BOARD_SIZE-2) + 1;
  176. }
  177.  
  178. int main(int argc, char **argv) {
  179. srand(time(NULL));
  180.  
  181. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
  182.  
  183. SDL_Window *window =
  184. SDL_CreateWindow(
  185. TITLE,
  186. SDL_WINDOWPOS_CENTERED,
  187. SDL_WINDOWPOS_CENTERED,
  188. window_size,
  189. window_size,
  190. SDL_WINDOW_SHOWN);
  191.  
  192. SDL_Renderer *renderer =
  193. SDL_CreateRenderer(
  194. window,
  195. -1,
  196. SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  197.  
  198. initialize_snake();
  199.  
  200. generate_fruit();
  201.  
  202. while (!quit) {
  203. int start = SDL_GetTicks();
  204.  
  205. events(window);
  206.  
  207. is_overlapping();
  208.  
  209. move_snake();
  210.  
  211. if (headx == fruitx && heady == fruity) {
  212. add_piece(tailx, taily);
  213.  
  214. while (!can_gen_fruit()) {
  215. SDL_Rect rect = { (fruitx * 20), (fruity * 20), 20, 20 };
  216. SDL_SetRenderDrawColor(renderer, 255, 236, 179, 255);
  217. SDL_RenderFillRect(renderer, &rect);
  218. generate_fruit();
  219. }
  220. }
  221.  
  222. draw(renderer);
  223.  
  224. SDL_RenderPresent(renderer);
  225.  
  226. int time = SDL_GetTicks() - start;
  227. int sleepTime = minframetime - time;
  228. if (sleepTime > 0) {
  229. SDL_Delay(sleepTime);
  230. }
  231. }
  232.  
  233. SDL_DestroyRenderer(renderer);
  234. SDL_DestroyWindow(window);
  235. SDL_Quit();
  236.  
  237. return 0;
  238. }
  239.  
  240. void events(SDL_Window *window) {
  241. SDL_Event event;
  242.  
  243. while (SDL_PollEvent(&event) != 0) {
  244. if (event.type == SDL_QUIT) {
  245. quit = true;
  246. }
  247. else if (event.type == SDL_KEYDOWN) {
  248. SDL_Keycode new_keycode = event.key.keysym.sym;
  249.  
  250. if (new_keycode == SDLK_d && keycode != SDLK_a) {
  251. x_increment = 1;
  252. y_increment = 0;
  253.  
  254. keycode = new_keycode;
  255.  
  256. break;
  257. } else if (new_keycode == SDLK_a && keycode != SDLK_d) {
  258. x_increment = -1;
  259. y_increment = 0;
  260.  
  261. keycode = new_keycode;
  262.  
  263. break;
  264. } else if (new_keycode == SDLK_w && keycode != SDLK_s) {
  265. x_increment = 0;
  266. y_increment = -1;
  267.  
  268. keycode = new_keycode;
  269.  
  270. break;
  271. } else if (new_keycode == SDLK_s && keycode != SDLK_w) {
  272. x_increment = 0;
  273. y_increment = 1;
  274.  
  275. keycode = new_keycode;
  276.  
  277. break;
  278. }
  279. }
  280. }
  281. }
  282.  
  283. void draw(SDL_Renderer *renderer) {
  284. SDL_SetRenderDrawColor(renderer, 255, 236, 179, 255);
  285. SDL_RenderClear(renderer);
  286.  
  287. for (int i = 0; i < 20; i++) {
  288. for (int j = 0; j < 20; j++) {
  289. SDL_Rect rect = { (i * 20), (j * 20), 20, 20 };
  290. SDL_SetRenderDrawColor(renderer, 255, 236, 179, 255);
  291. SDL_RenderFillRect(renderer, &rect);
  292. }
  293. }
  294.  
  295. SnakePiece *ptr;
  296.  
  297. ptr = head;
  298.  
  299. while (ptr) {
  300. SDL_Rect rect = { (ptr->x * 20), (ptr->y * 20), 20, 20 };
  301. SDL_SetRenderDrawColor(renderer, 100, 221, 23, 255);
  302. SDL_RenderFillRect(renderer, &rect);
  303.  
  304. ptr = ptr->next;
  305. }
  306.  
  307. SDL_Rect rect = { (fruitx * 20), (fruity * 20), 20, 20 };
  308. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  309. SDL_RenderFillRect(renderer, &rect);
  310. }
RAW Paste Data