Small snake game in C
Commit fd57c5b0ab74
tarball1 file changed+69-40
@@ -56,6 +56,8 @@ typedef struct snake {
5656
uint16_t length;5757
} snake;5858
59
+60
+uint8_t update_snake (snake *my_snake, direction shift);5961
void update_field(snake my_snake, char board[HEIGHT][WIDTH][STRING_LEN],6062
selected fruit);6163
selected new_fruit(char board[HEIGHT][WIDTH][STRING_LEN]);@@ -287,48 +289,27 @@ int main(int argc, char *args[]) {
287289
shift = tmp_direction;288290
}289291
290
- my_snake.position[0].directions[1] = my_snake.position[0].directions[0];291
- my_snake.position[0].directions[0] = shift;292
-293
- uint16_t i;294
- for (i = my_snake.length; i > 0; i--)295
- my_snake.position[i] = my_snake.position[i - 1];296
-297
- switch (shift) {298
- case UP:299
- my_snake.position[0].coords.y =300
- ((my_snake.position[0].coords.y + HEIGHT - 1) % HEIGHT);301
- break;302
- case DOWN:303
- my_snake.position[0].coords.y = ((my_snake.position[0].coords.y + 1) % HEIGHT);304
- break;305
- case LEFT:306
- my_snake.position[0].coords.x =307
- ((my_snake.position[0].coords.x + WIDTH - 1) % WIDTH);308
- break;309
- case RIGHT:310
- my_snake.position[0].coords.x = ((my_snake.position[0].coords.x + 1) % WIDTH);311
- break;312
- default:313
- goto end_of_program;314
- break;292
+ uint8_t update = update_snake(&my_snake, shift);293
+294
+ switch (update) {295
+ case 0: 296
+ printf("\nERROR\n");297
+ goto end_of_program;298
+ break;299
+ case 2:300
+ printf("\nHAHA LOST, LOSER");301
+ goto end_of_program;302
+ break;303
+ default:304
+ break;315305
}316306
307
+ update_field(my_snake, board, fruit);308
+309
+ 317310
uint8_t head_y = my_snake.position[0].coords.y;318311
uint8_t head_x = my_snake.position[0].coords.x;319312
320
- for (i = 1; i < my_snake.length; i++) {321
- int16_t x = my_snake.position[i].coords.x;322
- int16_t y = my_snake.position[i].coords.y;323
-324
- if (x == head_x && y == head_y) {325
- printf("\n\nHAHA LOST!!!\n\n");326
- goto end_of_program;327
- }328
- }329
-330
- update_field(my_snake, board, fruit);331
-332313
if (head_y == fruit.y && head_x == fruit.x) {333314
my_snake.length++;334315
fruit = new_fruit(board);@@ -349,6 +330,50 @@ end_of_program:
349330
return 0;350331
}351332
333
+334
+uint8_t update_snake (snake *my_snake, direction shift) {335
+ 336
+ my_snake->position[0].directions[1] = my_snake->position[0].directions[0];337
+ my_snake->position[0].directions[0] = shift;338
+339
+ uint16_t i;340
+ for (i = my_snake->length; i > 0; i--)341
+ my_snake->position[i] = my_snake->position[i - 1];342
+343
+ switch (shift) {344
+ case UP:345
+ my_snake->position[0].coords.y =346
+ ((my_snake->position[0].coords.y + HEIGHT - 1) % HEIGHT);347
+ break;348
+ case DOWN:349
+ my_snake->position[0].coords.y = ((my_snake->position[0].coords.y + 1) % HEIGHT);350
+ break;351
+ case LEFT:352
+ my_snake->position[0].coords.x =((my_snake->position[0].coords.x + WIDTH - 1) % WIDTH);353
+ break;354
+ case RIGHT:355
+ my_snake->position[0].coords.x = ((my_snake->position[0].coords.x + 1) % WIDTH);356
+ break;357
+ default:358
+ return 0;359
+ break;360
+ }361
+362
+ uint8_t head_y = my_snake->position[0].coords.y;363
+ uint8_t head_x = my_snake->position[0].coords.x;364
+365
+ for (i = 1; i < my_snake->length; i++) {366
+ int16_t x = my_snake->position[i].coords.x;367
+ int16_t y = my_snake->position[i].coords.y;368
+369
+ if (x == head_x && y == head_y)370
+ return 2;371
+ }372
+373
+374
+ return 1;375
+}376
+352377
void update_field(snake my_snake, char board[HEIGHT][WIDTH][STRING_LEN],353378
selected fruit) {354379
@@ -707,9 +732,13 @@ void draw_pause(struct winsize window_size, uint8_t field_width,
707732
const uint8_t height = 5;708733
const uint8_t width = 14;709734
710
- const char *pause_menu[] = {"+------------+", "| |",711
- "| PAUSED |", "| |",712
- "+------------+"};735
+ const char *pause_menu[] = {736
+ "+------------+", 737
+ "| |",738
+ "| PAUSED |",739
+ "| |",740
+ "+------------+"741
+ };713742
714743
const uint8_t going_up = (field_height + height) >> 1;715744
const uint8_t going_right = (field_width - width) >> 1;