Small snake game in C
| 1 | #include "snake.h" |
| 2 | |
| 3 | |
| 4 | |
| 5 | uint8_t update_snake (selected game_size, snake *my_snake, direction shift) { |
| 6 | |
| 7 | my_snake->position[0].directions[1] = my_snake->position[0].directions[0]; |
| 8 | my_snake->position[0].directions[0] = shift; |
| 9 | |
| 10 | uint16_t i; |
| 11 | for (i = my_snake->length; i > 0; i--) |
| 12 | my_snake->position[i] = my_snake->position[i - 1]; |
| 13 | |
| 14 | switch (shift) { |
| 15 | case UP: |
| 16 | my_snake->position[0].coords.y = |
| 17 | ((my_snake->position[0].coords.y + game_size.y - 1) % game_size.y); |
| 18 | break; |
| 19 | case DOWN: |
| 20 | my_snake->position[0].coords.y = ((my_snake->position[0].coords.y + 1) % game_size.y); |
| 21 | break; |
| 22 | case LEFT: |
| 23 | my_snake->position[0].coords.x =((my_snake->position[0].coords.x + game_size.x - 1) % game_size.x); |
| 24 | break; |
| 25 | case RIGHT: |
| 26 | my_snake->position[0].coords.x = ((my_snake->position[0].coords.x + 1) % game_size.x); |
| 27 | break; |
| 28 | default: |
| 29 | return 0; |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | uint8_t head_y = my_snake->position[0].coords.y; |
| 34 | uint8_t head_x = my_snake->position[0].coords.x; |
| 35 | |
| 36 | for (i = 1; i < my_snake->length; i++) { |
| 37 | int16_t x = my_snake->position[i].coords.x; |
| 38 | int16_t y = my_snake->position[i].coords.y; |
| 39 | |
| 40 | if (x == head_x && y == head_y) |
| 41 | return 2; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | void update_field(snake my_snake, selected game_size, board_state **board, bool init) { |
| 49 | |
| 50 | uint8_t i, j; |
| 51 | |
| 52 | board_state snake_bit[DIRECTIONS][DIRECTIONS] = { |
| 53 | [UP][UP] = VERTICAL, |
| 54 | [DOWN][DOWN] = VERTICAL, |
| 55 | |
| 56 | [RIGHT][RIGHT] = HORIZONTAL, |
| 57 | [LEFT][LEFT] = HORIZONTAL, |
| 58 | |
| 59 | [DOWN][RIGHT] = BOTTOM_LEFT, |
| 60 | [LEFT][UP] = BOTTOM_LEFT, |
| 61 | |
| 62 | [UP][RIGHT] = TOP_LEFT, |
| 63 | [LEFT][DOWN] = TOP_LEFT, |
| 64 | |
| 65 | [DOWN][LEFT] = BOTTOM_RIGHT, |
| 66 | [RIGHT][UP] = BOTTOM_RIGHT, |
| 67 | |
| 68 | [UP][LEFT] = TOP_RIGHT, |
| 69 | [RIGHT][DOWN] = TOP_RIGHT |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | if (init) { |
| 74 | for (i = 0; i < game_size.y; i++) { |
| 75 | for (j = 0; j < game_size.x; j++) { |
| 76 | board[i][j] = AIR; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | for (i = 1; i < my_snake.length; i++) { |
| 81 | int16_t x = my_snake.position[i].coords.x; |
| 82 | int16_t y = my_snake.position[i].coords.y; |
| 83 | |
| 84 | if (x >= 0 && y >= 0) { |
| 85 | |
| 86 | direction first = my_snake.position[i].directions[0]; |
| 87 | direction second = my_snake.position[i].directions[1]; |
| 88 | |
| 89 | board[y][x] = snake_bit[first][second]; |
| 90 | } |
| 91 | } |
| 92 | } else { |
| 93 | |
| 94 | int16_t x = my_snake.position[my_snake.length].coords.x; |
| 95 | int16_t y = my_snake.position[my_snake.length].coords.y; |
| 96 | |
| 97 | if (x != -1 && y != -1) |
| 98 | board[y][x] = AIR; |
| 99 | |
| 100 | |
| 101 | x = my_snake.position[1].coords.x; |
| 102 | y = my_snake.position[1].coords.y; |
| 103 | |
| 104 | direction first = my_snake.position[1].directions[0]; |
| 105 | direction second = my_snake.position[1].directions[1]; |
| 106 | |
| 107 | if (x != -1 && y != -1) |
| 108 | board[y][x] = snake_bit[first][second]; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | int16_t x = my_snake.position[0].coords.x; |
| 113 | int16_t y = my_snake.position[0].coords.y; |
| 114 | |
| 115 | if (my_snake.position[0].directions[0] == RIGHT) { |
| 116 | board[y][x] = HEAD_1; |
| 117 | } else { |
| 118 | board[y][x] = HEAD_2; |
| 119 | } |
| 120 | |
| 121 | for (i = 0; i < game_size.y; i++) { |
| 122 | for (j = 0; j < game_size.x; j++) { |
| 123 | if (board[i][j] == FRUIT) |
| 124 | return; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | while (true) { |
| 129 | x = rand() % game_size.x; |
| 130 | y = rand() % game_size.y; |
| 131 | |
| 132 | if (board[y][x] == AIR) { |
| 133 | board[y][x] = FRUIT; |
| 134 | return; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | direction read_direction(char *string, bool wasd_enabled) { |
| 140 | |
| 141 | if (string[0] == '\033') { |
| 142 | |
| 143 | if (strlen(string) == 1) |
| 144 | return ESCAPE; |
| 145 | |
| 146 | if (strlen(string) >= 3) { |
| 147 | |
| 148 | if (string[1] == '[') { |
| 149 | |
| 150 | switch (string[2]) { |
| 151 | case RIGHT_CODE: |
| 152 | return RIGHT; |
| 153 | break; |
| 154 | case LEFT_CODE: |
| 155 | return LEFT; |
| 156 | break; |
| 157 | case UP_CODE: |
| 158 | return UP; |
| 159 | break; |
| 160 | case DOWN_CODE: |
| 161 | return DOWN; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (wasd_enabled) { |
| 169 | switch (toupper(string[0])) { |
| 170 | case 'A': |
| 171 | return LEFT; |
| 172 | break; |
| 173 | case 'D': |
| 174 | return RIGHT; |
| 175 | break; |
| 176 | case 'W': |
| 177 | return UP; |
| 178 | break; |
| 179 | case 'S': |
| 180 | return DOWN; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | switch (toupper(string[0])) { |
| 186 | case 'Q': |
| 187 | return Q_KEY; |
| 188 | break; |
| 189 | case 'P': |
| 190 | return P_KEY; |
| 191 | break; |
| 192 | case '\n': |
| 193 | return ENTER; |
| 194 | break; |
| 195 | default: |
| 196 | return DIRECTIONS; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | uint8_t make_folder(char *path) { |
| 202 | |
| 203 | errno = 0; |
| 204 | if (mkdir(path, 0755) && errno != EEXIST) { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | return 1; |
| 209 | } |