Small snake game in C
| 1 | #ifndef SNAKE_H |
| 2 | #define SNAKE_H |
| 3 | |
| 4 | |
| 5 | #define _POSIX_C_SOURCE 199309L |
| 6 | |
| 7 | #include <termios.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include <stdbool.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #include <sys/ioctl.h> |
| 16 | #include <sys/stat.h> |
| 17 | |
| 18 | #include <errno.h> |
| 19 | |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <ctype.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #define RIGHT_CODE 'C' |
| 26 | #define LEFT_CODE 'D' |
| 27 | #define UP_CODE 'A' |
| 28 | #define DOWN_CODE 'B' |
| 29 | |
| 30 | #define WIDTH 20 |
| 31 | #define HEIGHT 20 |
| 32 | |
| 33 | #define MIN_SIZE 10 |
| 34 | #define MAX_SIZE 100 |
| 35 | |
| 36 | #define STRING_LEN 20 |
| 37 | #define PATH_LEN 200 |
| 38 | |
| 39 | #define SPEED 50 /* in ms */ |
| 40 | |
| 41 | #define MAX_LENGTH 1000 |
| 42 | #define STARTING_LENGTH 2 |
| 43 | |
| 44 | #define SETTING_FILE "snake_c.settings" |
| 45 | |
| 46 | typedef enum { NEW_GAME, SETTINGS, EXIT, MAIN_MENU } main_menu; |
| 47 | |
| 48 | typedef enum {HEIGHT_S, WIDTH_S, SPEED_S, BACK_S, SETTINGS_MENU} settings; |
| 49 | |
| 50 | typedef enum {V_FAST, FAST, AVERAGE, SLOW, V_SLOW, SPEED_L} speed; |
| 51 | |
| 52 | typedef enum { |
| 53 | UP, LEFT, DOWN, RIGHT, DIRECTIONS, |
| 54 | ENTER, P_KEY, Q_KEY, ESCAPE |
| 55 | } direction; |
| 56 | |
| 57 | typedef enum { |
| 58 | AIR, FRUIT, HEAD_1, HEAD_2, HORIZONTAL, VERTICAL, |
| 59 | TOP_RIGHT, TOP_LEFT, BOTTOM_RIGHT, BOTTOM_LEFT |
| 60 | } board_state; |
| 61 | |
| 62 | typedef struct selected { |
| 63 | int8_t x; |
| 64 | int8_t y; |
| 65 | } selected; |
| 66 | |
| 67 | typedef struct snake_part { |
| 68 | selected coords; |
| 69 | direction directions[2]; |
| 70 | } snake_part; |
| 71 | |
| 72 | typedef struct snake { |
| 73 | snake_part * position; |
| 74 | uint16_t length; |
| 75 | } snake; |
| 76 | |
| 77 | |
| 78 | uint8_t update_snake (selected game_size, snake *my_snake, direction shift); |
| 79 | void update_field(snake my_snake, selected game_size, |
| 80 | board_state **board, bool init); |
| 81 | selected new_fruit(selected game_size, board_state **board); |
| 82 | |
| 83 | bool start_animation(); |
| 84 | |
| 85 | bool draw_menu(main_menu menu, struct winsize window_size); |
| 86 | void draw_settings(settings selected_setting, speed game_speed, selected game_size); |
| 87 | |
| 88 | bool draw_board(selected game_size, board_state **board, |
| 89 | struct winsize window_size, bool redraw, bool pause, uint16_t score); |
| 90 | |
| 91 | void draw_pause(struct winsize window_size, |
| 92 | uint8_t field_width,uint8_t field_height); |
| 93 | |
| 94 | char *give_state(board_state square); |
| 95 | |
| 96 | direction read_direction(char *string, bool wasd_enabled); |
| 97 | |
| 98 | uint8_t make_folder(char *path, char *append, bool create_folder); |
| 99 | |
| 100 | #endif |