jag

tetoris_c

public

Small block game in C

Star0
Watch0
77d71a4dea0a8f2e8f139ffad706a75c3323df71
1
ssh://git@git.jaglekon.de:30/jag/tetoris_c.git
source/draw.cC171 lines3.3 KB
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, piece next_piece, bool *blink, bool is_piece) {
44
45
  int8_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 = 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
107
108
  /* next piece preview */
109
110
  printf("\033[H");
111
  printf("\033[%iC", FIELD_WIDTH * 2 + 1);
112
  printf("┱──────────┐\n");
113
114
  for (i = 4; i > 0; i--) {
115
    printf("\033[%iC", FIELD_WIDTH * 2 + 1);
116
    printf("┃");
117
118
    draw_color(next_piece.name);
119
120
    for (j = 4; j >= 0; j--) {
121
    
122
      int8_t y = next_piece.size - i;
123
      int8_t x = next_piece.size - j;
124
        
125
126
      if (
127
        y < next_piece.size && x < next_piece.size &&
128
        y >= 0 && x >= 0 &&
129
        next_piece.piece[y][x]
130
      ) {
131
        printf("██");
132
        continue;
133
      }
134
       
135
      printf("  ");
136
    }
137
138
    printf("\033[0m│\n");
139
  }
140
141
  printf("\033[%iC", FIELD_WIDTH * 2 + 1);
142
  printf("┠──────────┘\n");
143
144
}
145
146
void draw_pause () {
147
  
148
  uint8_t i;
149
150
  printf("\033[H");
151
  printf("\033[%iB", 1 + (FIELD_HEIGHT >> 2));
152
153
  for (i = 0; i < 3; i++) {
154
155
    printf("\033[%iC", 1 + (((FIELD_WIDTH * 2  - 7) >> 1)));
156
157
    switch(i) {
158
      case 0:
159
        printf("╭─────╮");
160
        break;
161
      case 1:
162
        printf("│PAUSE│");
163
        break;
164
      case 2:
165
        printf("╰─────╯");
166
        break;
167
    }
168
169
    printf("\n");
170
  } 
171
}