Small block game in C
| 1 | #include "main.h" |
| 2 | |
| 3 | #define RED ((color){255, 0, 0}) |
| 4 | #define GREEN ((color){0, 255, 0}) |
| 5 | #define BLUE ((color){0, 0, 255}) |
| 6 | #define YELLOW ((color){255, 255, 0}) |
| 7 | #define MAGENTA ((color){255, 0, 255}) |
| 8 | #define TEAL ((color){0, 255, 255}) |
| 9 | #define PURPLE ((color){127, 0, 255}) |
| 10 | #define WHITE ((color){255, 255, 255}) |
| 11 | |
| 12 | |
| 13 | void draw_color (tetromino current_square, uint8_t gray) { |
| 14 | |
| 15 | color col = {}; |
| 16 | |
| 17 | switch (current_square) { |
| 18 | |
| 19 | case I_PIECE: |
| 20 | col = RED; |
| 21 | break; |
| 22 | case J_PIECE: |
| 23 | col = GREEN; |
| 24 | break; |
| 25 | case L_PIECE: |
| 26 | col = BLUE; |
| 27 | break; |
| 28 | case O_PIECE: |
| 29 | col = YELLOW; |
| 30 | break; |
| 31 | case S_PIECE: |
| 32 | col = MAGENTA; |
| 33 | break; |
| 34 | case T_PIECE: |
| 35 | col = TEAL; |
| 36 | break; |
| 37 | case Z_PIECE: |
| 38 | col = PURPLE; |
| 39 | break; |
| 40 | default: |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | if(gray) { |
| 45 | |
| 46 | uint8_t gray_hue = (uint8_t)((299 * col.r + 587 * col.g + 114 * col.b) / 1000); |
| 47 | |
| 48 | col.r = ((255 - gray) * col.r + gray * gray_hue) / 255; |
| 49 | col.g = ((255 - gray) * col.g + gray * gray_hue) / 255; |
| 50 | col.b = ((255 - gray) * col.b + gray * gray_hue) / 255; |
| 51 | } |
| 52 | |
| 53 | printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piece) { |
| 58 | |
| 59 | int8_t i, j; |
| 60 | |
| 61 | |
| 62 | printf("\033[H"); |
| 63 | printf("┏"); |
| 64 | for (i = 0; i < FIELD_WIDTH; i++) |
| 65 | printf("━━"); |
| 66 | printf("┓\n"); |
| 67 | |
| 68 | |
| 69 | for (i = 0; i < FIELD_HEIGHT; i++) { |
| 70 | |
| 71 | printf("┃"); |
| 72 | |
| 73 | for (j = 0; j < FIELD_WIDTH; j++) { |
| 74 | |
| 75 | uint8_t gray = 0; |
| 76 | |
| 77 | if ( blink != NULL && blink[i]) { |
| 78 | |
| 79 | color col = WHITE; |
| 80 | |
| 81 | printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b); |
| 82 | } else { |
| 83 | |
| 84 | |
| 85 | tetromino current_square = playfield[i][j + BORDER_WIDTH]; |
| 86 | gray = graying[i][j + BORDER_WIDTH]; |
| 87 | |
| 88 | |
| 89 | if (is_piece) { |
| 90 | |
| 91 | uint8_t k, l; |
| 92 | |
| 93 | for (k = 0; k < current_piece.size; k++) { |
| 94 | for (l = 0; l < current_piece.size; l++) { |
| 95 | |
| 96 | coord points = find_direction(current_piece, l, k); |
| 97 | |
| 98 | if ( |
| 99 | i == current_piece.y + points.y && |
| 100 | j == current_piece.x + points.x - BORDER_WIDTH && |
| 101 | current_piece.piece[k][l]) |
| 102 | { |
| 103 | current_square = current_piece.name; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (current_square <= EMPTY || current_square > Z_PIECE) { |
| 110 | printf("· "); |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | draw_color(current_square, gray); |
| 115 | } |
| 116 | |
| 117 | printf("██\033[0m"); |
| 118 | } |
| 119 | printf("┃\n"); |
| 120 | } |
| 121 | |
| 122 | printf("┗"); |
| 123 | for (i = 0; i < FIELD_WIDTH; i++) |
| 124 | printf("━━"); |
| 125 | printf("┛\n"); |
| 126 | |
| 127 | |
| 128 | /* next piece preview */ |
| 129 | |
| 130 | printf("\033[H"); |
| 131 | printf("\033[%iC", FIELD_WIDTH * 2 + 1); |
| 132 | printf("┱──────────┐\n"); |
| 133 | |
| 134 | for (i = 4; i > 0; i--) { |
| 135 | printf("\033[%iC", FIELD_WIDTH * 2 + 1); |
| 136 | printf("┃"); |
| 137 | |
| 138 | draw_color(next_piece.name, 0); |
| 139 | |
| 140 | for (j = 4; j >= 0; j--) { |
| 141 | |
| 142 | int8_t y = next_piece.size - i; |
| 143 | int8_t x = next_piece.size - j; |
| 144 | |
| 145 | |
| 146 | if ( |
| 147 | y < next_piece.size && x < next_piece.size && |
| 148 | y >= 0 && x >= 0 && |
| 149 | next_piece.piece[y][x] |
| 150 | ) { |
| 151 | printf("██"); |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | printf(" "); |
| 156 | } |
| 157 | |
| 158 | printf("\033[0m│\n"); |
| 159 | } |
| 160 | |
| 161 | printf("\033[%iC", FIELD_WIDTH * 2 + 1); |
| 162 | printf("┠──────────┘\n"); |
| 163 | |
| 164 | } |
| 165 | |
| 166 | void draw_pause () { |
| 167 | |
| 168 | uint8_t i; |
| 169 | |
| 170 | printf("\033[H"); |
| 171 | printf("\033[%iB", 1 + (FIELD_HEIGHT >> 2)); |
| 172 | |
| 173 | for (i = 0; i < 3; i++) { |
| 174 | |
| 175 | printf("\033[%iC", 1 + (((FIELD_WIDTH * 2 - 7) >> 1))); |
| 176 | |
| 177 | switch(i) { |
| 178 | case 0: |
| 179 | printf("╭─────╮"); |
| 180 | break; |
| 181 | case 1: |
| 182 | printf("│PAUSE│"); |
| 183 | break; |
| 184 | case 2: |
| 185 | printf("╰─────╯"); |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | printf("\n"); |
| 190 | } |
| 191 | } |