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 | #define DARK 0.6 |
| 13 | |
| 14 | |
| 15 | void draw_color (tetromino current_square, double dark) { |
| 16 | |
| 17 | color col = {}; |
| 18 | |
| 19 | switch (current_square) { |
| 20 | |
| 21 | case I_PIECE: |
| 22 | col = RED; |
| 23 | break; |
| 24 | case J_PIECE: |
| 25 | col = GREEN; |
| 26 | break; |
| 27 | case L_PIECE: |
| 28 | col = BLUE; |
| 29 | break; |
| 30 | case O_PIECE: |
| 31 | col = YELLOW; |
| 32 | break; |
| 33 | case S_PIECE: |
| 34 | col = MAGENTA; |
| 35 | break; |
| 36 | case T_PIECE: |
| 37 | col = TEAL; |
| 38 | break; |
| 39 | case Z_PIECE: |
| 40 | col = PURPLE; |
| 41 | break; |
| 42 | default: |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | if(dark > 0) { |
| 47 | |
| 48 | |
| 49 | col.r *= dark; |
| 50 | col.g *= dark; |
| 51 | col.b *= dark; |
| 52 | } |
| 53 | |
| 54 | printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piece) { |
| 59 | |
| 60 | int8_t i, j; |
| 61 | |
| 62 | |
| 63 | printf("\033[H"); |
| 64 | printf("┏"); |
| 65 | for (i = 0; i < FIELD_WIDTH; i++) |
| 66 | printf("━━"); |
| 67 | printf("┓\n"); |
| 68 | |
| 69 | |
| 70 | for (i = 0; i < FIELD_HEIGHT; i++) { |
| 71 | |
| 72 | printf("┃"); |
| 73 | |
| 74 | for (j = 0; j < FIELD_WIDTH; j++) { |
| 75 | |
| 76 | double dark = DARK; |
| 77 | |
| 78 | if ( blink != NULL && blink[i]) { |
| 79 | |
| 80 | color col = WHITE; |
| 81 | |
| 82 | printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b); |
| 83 | } else { |
| 84 | |
| 85 | |
| 86 | tetromino current_square = playfield[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 | dark = 0; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (current_square <= EMPTY || current_square > Z_PIECE) { |
| 111 | printf("· "); |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | draw_color(current_square, dark); |
| 116 | } |
| 117 | |
| 118 | printf("██\033[0m"); |
| 119 | } |
| 120 | printf("┃\n"); |
| 121 | } |
| 122 | |
| 123 | printf("┗"); |
| 124 | for (i = 0; i < FIELD_WIDTH; i++) |
| 125 | printf("━━"); |
| 126 | printf("┛\n"); |
| 127 | |
| 128 | |
| 129 | /* next piece preview */ |
| 130 | |
| 131 | printf("\033[H"); |
| 132 | printf("\033[%iC", FIELD_WIDTH * 2 + 1); |
| 133 | printf("┱──────────┐\n"); |
| 134 | |
| 135 | for (i = 4; i > 0; i--) { |
| 136 | printf("\033[%iC", FIELD_WIDTH * 2 + 1); |
| 137 | printf("┃"); |
| 138 | |
| 139 | draw_color(next_piece.name, 0); |
| 140 | |
| 141 | for (j = 4; j >= 0; j--) { |
| 142 | |
| 143 | int8_t y = next_piece.size - i; |
| 144 | int8_t x = next_piece.size - j; |
| 145 | |
| 146 | |
| 147 | if ( |
| 148 | y < next_piece.size && x < next_piece.size && |
| 149 | y >= 0 && x >= 0 && |
| 150 | next_piece.piece[y][x] |
| 151 | ) { |
| 152 | printf("██"); |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | printf(" "); |
| 157 | } |
| 158 | |
| 159 | printf("\033[0m│\n"); |
| 160 | } |
| 161 | |
| 162 | printf("\033[%iC", FIELD_WIDTH * 2 + 1); |
| 163 | printf("┠──────────┘\n"); |
| 164 | |
| 165 | for (i = 0; i < 3; i++) { |
| 166 | printf("\033[%iC", FIELD_WIDTH * 2 + 2); |
| 167 | |
| 168 | switch (i) { |
| 169 | case 0: |
| 170 | printf("LEVEL: %5u\n", level); |
| 171 | break; |
| 172 | case 1: |
| 173 | printf("LINES: %5u\n", all_cleared_lines); |
| 174 | break; |
| 175 | case 2: |
| 176 | printf("SCORE: %5u\n", score); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | } |
| 182 | |
| 183 | void draw_pause () { |
| 184 | |
| 185 | uint8_t i; |
| 186 | |
| 187 | printf("\033[H"); |
| 188 | printf("\033[%iB", 1 + (FIELD_HEIGHT >> 2)); |
| 189 | |
| 190 | for (i = 0; i < 3; i++) { |
| 191 | |
| 192 | printf("\033[%iC", 1 + (((FIELD_WIDTH * 2 - 7) >> 1))); |
| 193 | |
| 194 | switch(i) { |
| 195 | case 0: |
| 196 | printf("╭─────╮"); |
| 197 | break; |
| 198 | case 1: |
| 199 | printf("│PAUSE│"); |
| 200 | break; |
| 201 | case 2: |
| 202 | printf("╰─────╯"); |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | printf("\n"); |
| 207 | } |
| 208 | } |