Small snake game in C
| 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdbool.h> |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include <termios.h> |
| 10 | |
| 11 | #define RIGHT_CODE 'C' |
| 12 | #define LEFT_CODE 'D' |
| 13 | #define UP_CODE 'A' |
| 14 | #define DOWN_CODE 'B' |
| 15 | |
| 16 | #define WIDTH 256 |
| 17 | #define HEIGHT 256 |
| 18 | |
| 19 | typedef enum { |
| 20 | UP, DOWN, LEFT, RIGHT |
| 21 | } direction; |
| 22 | |
| 23 | typedef struct selected { |
| 24 | uint8_t x; |
| 25 | uint8_t y; |
| 26 | } selected; |
| 27 | |
| 28 | |
| 29 | void draw_board(selected square_selected, bool redraw); |
| 30 | |
| 31 | |
| 32 | int main() { |
| 33 | |
| 34 | struct termios old, new; |
| 35 | |
| 36 | tcgetattr(STDIN_FILENO, &old); |
| 37 | |
| 38 | new = old; |
| 39 | |
| 40 | new.c_lflag &= ~(ECHO | ICANON); |
| 41 | new.c_cc[VMIN] = 0; |
| 42 | new.c_cc[VTIME] = 1; |
| 43 | |
| 44 | tcsetattr(STDIN_FILENO, TCSANOW, &new); |
| 45 | |
| 46 | |
| 47 | selected square_selected = {0, 0}; |
| 48 | |
| 49 | draw_board(square_selected, false); |
| 50 | |
| 51 | bool run = true; |
| 52 | |
| 53 | while (run) { |
| 54 | |
| 55 | char string[1000] = {}; |
| 56 | ssize_t n = read(STDIN_FILENO, string, sizeof(string) - 1); |
| 57 | |
| 58 | |
| 59 | if (n > 0) { |
| 60 | string[n] = '\0'; |
| 61 | |
| 62 | if (strlen(string) >= 3) { |
| 63 | if (string[0] == '\033') { |
| 64 | if (string[1] == '[') { |
| 65 | |
| 66 | direction shift; |
| 67 | |
| 68 | switch (string[2]) { |
| 69 | case RIGHT_CODE: |
| 70 | shift = RIGHT; |
| 71 | break; |
| 72 | case LEFT_CODE: |
| 73 | shift = LEFT; |
| 74 | break; |
| 75 | case UP_CODE: |
| 76 | shift = UP; |
| 77 | break; |
| 78 | case DOWN_CODE: |
| 79 | shift = DOWN; |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | switch (shift) { |
| 84 | case UP: |
| 85 | square_selected.y = ((square_selected.y + HEIGHT - 1) % HEIGHT); |
| 86 | break; |
| 87 | case DOWN: |
| 88 | square_selected.y = ((square_selected.y + 1) % HEIGHT); |
| 89 | break; |
| 90 | case LEFT: |
| 91 | square_selected.x = ((square_selected.x + WIDTH - 1) % WIDTH); |
| 92 | break; |
| 93 | case RIGHT: |
| 94 | square_selected.x = ((square_selected.x + 1) % WIDTH); |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | draw_board(square_selected, true); |
| 99 | |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | if (strstr(string, "q")) |
| 104 | run = false; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | tcsetattr(STDIN_FILENO, TCSANOW, &old); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void draw_board (selected square_selected, bool redraw) { |
| 116 | |
| 117 | uint8_t i, j; |
| 118 | |
| 119 | if (redraw) { |
| 120 | for (i = 0; i < HEIGHT; i++) |
| 121 | printf("\033[A"); |
| 122 | printf("\r"); |
| 123 | } |
| 124 | |
| 125 | for ( i = 0; i < HEIGHT; i++) { |
| 126 | for (j = 0; j < WIDTH; j++) { |
| 127 | char character = ' '; |
| 128 | |
| 129 | if (i == square_selected.y && j == square_selected.x) { |
| 130 | character = 'O'; |
| 131 | } |
| 132 | |
| 133 | printf("\033[32m%5c\033[0m", character); |
| 134 | } |
| 135 | printf("\n"); |
| 136 | } |
| 137 | } |