Small block game in C
| 1 | #include "main.h" |
| 2 | |
| 3 | |
| 4 | #define RED "\033[38;2;255;0;0m" |
| 5 | #define GREEN "\033[38;2;0;255;0m" |
| 6 | #define BLUE "\033[38;2;0;0;255m" |
| 7 | #define YELLOW "\033[38;2;255;255;0m" |
| 8 | #define MAGENTA "\033[38;2;255;0;255m" |
| 9 | #define TEAL "\033[38;2;0;255;255m" |
| 10 | #define PURPLE "\033[38;2;127;0;255m" |
| 11 | #define WHITE "\033[38;2;255;255;255m" |
| 12 | |
| 13 | |
| 14 | void draw_color (tetromino current_square) { |
| 15 | switch (current_square) { |
| 16 | case I_PIECE: |
| 17 | printf("%s", RED); |
| 18 | break; |
| 19 | case J_PIECE: |
| 20 | printf("%s", GREEN); |
| 21 | break; |
| 22 | case L_PIECE: |
| 23 | printf("%s", BLUE); |
| 24 | break; |
| 25 | case O_PIECE: |
| 26 | printf("%s", YELLOW); |
| 27 | break; |
| 28 | case S_PIECE: |
| 29 | printf("%s", MAGENTA); |
| 30 | break; |
| 31 | case T_PIECE: |
| 32 | printf("%s", TEAL); |
| 33 | break; |
| 34 | case Z_PIECE: |
| 35 | printf("%s", PURPLE); |
| 36 | break; |
| 37 | default: |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void draw_board(piece current_piece, bool *blink, bool is_piece) { |
| 44 | |
| 45 | uint8_t i, j; |
| 46 | |
| 47 | |
| 48 | printf("\033[H"); |
| 49 | printf("┏"); |
| 50 | for (i = 0; i < FIELD_WIDTH; i++) |
| 51 | printf("━━"); |
| 52 | printf("┓\n"); |
| 53 | |
| 54 | |
| 55 | for (i = 0; i < FIELD_HEIGHT; i++) { |
| 56 | |
| 57 | printf("┃"); |
| 58 | |
| 59 | for (j = 0; j < FIELD_WIDTH; j++) { |
| 60 | |
| 61 | if ( blink != NULL && blink[i]) { |
| 62 | |
| 63 | printf("%s", WHITE); |
| 64 | } else { |
| 65 | |
| 66 | |
| 67 | tetromino current_square = current_square = playfield[i][j + BORDER_WIDTH]; |
| 68 | |
| 69 | if (is_piece) { |
| 70 | |
| 71 | uint8_t k, l; |
| 72 | |
| 73 | for (k = 0; k < current_piece.size; k++) { |
| 74 | for (l = 0; l < current_piece.size; l++) { |
| 75 | |
| 76 | coord points = find_direction(current_piece, l, k); |
| 77 | |
| 78 | if ( |
| 79 | i == current_piece.y + points.y && |
| 80 | j == current_piece.x + points.x - BORDER_WIDTH && |
| 81 | current_piece.piece[k][l]) |
| 82 | { |
| 83 | current_square = current_piece.name; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (current_square <= EMPTY || current_square > Z_PIECE) { |
| 90 | printf("· "); |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | draw_color(current_square); |
| 95 | } |
| 96 | |
| 97 | printf("██\033[0m"); |
| 98 | } |
| 99 | printf("┃\n"); |
| 100 | } |
| 101 | |
| 102 | printf("┗"); |
| 103 | for (i = 0; i < FIELD_WIDTH; i++) |
| 104 | printf("━━"); |
| 105 | printf("┛\n"); |
| 106 | } |