Advertisement
Guest User

glow in the dark

a guest
Aug 22nd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define WIDTH 100
  6. #define HEIGHT 50
  7. #define SIZE WIDTH*HEIGHT
  8. #define INIT_SIZE 3
  9.  
  10. typedef struct bo {
  11. int pos;
  12. struct bo *next;
  13. } body;
  14.  
  15. typedef enum d{up = -50, down = +50, left = -1, right = +1} direction;
  16.  
  17. typedef enum b{false = 0, true = 1} bool;
  18.  
  19. void init_map();
  20. void print_map();
  21. void update_map();
  22. void init_snake();
  23. void generate_food();
  24. void move();
  25.  
  26. int map[SIZE];
  27. body *snake;
  28. int snake_size;
  29. direction dir;
  30. bool status;
  31.  
  32. int main(void) {
  33.  
  34. dir = right;
  35. status = true;
  36. init_map();
  37. init_snake();
  38.  
  39. while(status) {
  40. print_map();
  41. printf("%d", status);
  42. update_map();
  43. _sleep(1);
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. void init_map() {
  50. // places top and bottom walls
  51. for(int i = 0; i < WIDTH; i++) {
  52. map[i] = -1;
  53. map[i + (SIZE - WIDTH)] = -1;
  54. }
  55.  
  56. // places open space
  57. for(int i = WIDTH; i < SIZE - WIDTH; i++) {
  58. map[i] = 0;
  59. }
  60.  
  61. // places left and right walls
  62. for(int i = 0; i < HEIGHT; i++) {
  63. map[i * WIDTH] = -1;
  64. map[(i + 1) * WIDTH - 1] = -1;
  65. }
  66. }
  67.  
  68. void print_map() {
  69. system("cls");
  70. for(int i = 0; i < SIZE; i++) {
  71. if(i % WIDTH == 0 && i != 0) {
  72. printf("\n");
  73. }
  74.  
  75. switch(map[i]) {
  76. case -2:
  77. printf("%c", 0245);
  78. break;
  79. case -1:
  80. printf("%c", 987);
  81. break;
  82. case 0:
  83. printf(" ");
  84. break;
  85. default:
  86. printf("O");
  87. break;
  88. }
  89. }
  90. }
  91.  
  92. void update_map() {
  93. body *p;
  94. move();
  95. int new_pos = snake->pos + dir;
  96.  
  97. if(map[new_pos] == -2) { // snek ate food
  98. snake_size++;
  99. p = (body *) malloc(sizeof(body));
  100. p->pos = new_pos;
  101. p->next = snake;
  102. map[p->pos] = snake_size;
  103. snake = p;
  104.  
  105. generate_food();
  106.  
  107. } else if(map[new_pos] == -1 || map[new_pos] > 0) { // snek is kill
  108. status = false;
  109.  
  110. } else { // snek is moving
  111. p = snake;
  112. for(int i = 0; i < snake_size; i++) {
  113. printf("%d: %d", p->pos, map[p->pos]);
  114. map[p->pos]--;
  115. p->next;
  116. }
  117. }
  118. }
  119.  
  120. void init_snake() {
  121. int aux = snake_size = INIT_SIZE;
  122. body *p;
  123. int pos = (HEIGHT / 2) * WIDTH + WIDTH / 2; // snek's head is at the central position
  124.  
  125. for(int i = snake_size; i > 0; i--) {
  126. p = (body *) malloc(sizeof(body));
  127. p->pos = pos;
  128. pos--;
  129. snake = p;
  130. map[p->pos] = aux;
  131. aux--;
  132. p = p->next;
  133. }
  134. }
  135.  
  136. void generate_food() {
  137.  
  138. }
  139.  
  140. void move() {
  141. if(kbhit()) {
  142. char c = getch();
  143.  
  144. switch(c) {
  145. case 119:
  146. if(dir != down)
  147. dir = up;
  148. break;
  149. case 115:
  150. if(dir != up)
  151. dir = down;
  152. break;
  153. case 97:
  154. if(dir != right)
  155. dir = left;
  156. break;
  157. case 100:
  158. if(dir != left)
  159. dir = right;
  160. break;
  161. }
  162.  
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement