Small block game in C
Commit 992df8d5d7cd
tarball7 files changed+88-30
@@ -1,42 +1,56 @@
11
#include "main.h"22
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})311
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"1212
13
+void draw_color (tetromino current_square, uint8_t gray) {14
+ 15
+ color col = {};1316
14
-void draw_color (tetromino current_square) {1517
switch (current_square) {18
+1619
case I_PIECE:17
- printf("%s", RED);20
+ col = RED;1821
break;1922
case J_PIECE:20
- printf("%s", GREEN);23
+ col = GREEN;2124
break;2225
case L_PIECE:23
- printf("%s", BLUE);26
+ col = BLUE;2427
break;2528
case O_PIECE:26
- printf("%s", YELLOW);29
+ col = YELLOW;2730
break;2831
case S_PIECE:29
- printf("%s", MAGENTA);32
+ col = MAGENTA;3033
break;3134
case T_PIECE:32
- printf("%s", TEAL);35
+ col = TEAL;3336
break;3437
case Z_PIECE:35
- printf("%s", PURPLE);38
+ col = PURPLE;3639
break;3740
default:3841
break;3942
} 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);4054
}4155
4256
@@ -57,14 +71,20 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
5771
printf("┃");5872
5973
for (j = 0; j < FIELD_WIDTH; j++) {74
+75
+ uint8_t gray = 0;6076
6177
if ( blink != NULL && blink[i]) {6278
63
- printf("%s", WHITE);79
+ color col = WHITE;80
+81
+ printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b);6482
} else {6583
6684
6785
tetromino current_square = playfield[i][j + BORDER_WIDTH];86
+ gray = graying[i][j + BORDER_WIDTH];87
+6888
6989
if (is_piece) {7090
@@ -84,14 +104,14 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
84104
}85105
}86106
}87
- }107
+ } 88108
89109
if (current_square <= EMPTY || current_square > Z_PIECE) {90110
printf("· ");91111
continue;92112
}93113
94
- draw_color(current_square);114
+ draw_color(current_square, gray);95115
}96116
97117
printf("██\033[0m");@@ -115,7 +135,7 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
115135
printf("\033[%iC", FIELD_WIDTH * 2 + 1);116136
printf("┃");117137
118
- draw_color(next_piece.name);138
+ draw_color(next_piece.name, 0);119139
120140
for (j = 4; j >= 0; j--) {121141
Binary file not shown.
@@ -225,9 +225,19 @@ bool valid_position(piece current_piece) {
225225
226226
coord points = find_direction(current_piece, j, i);227227
228
+ int8_t y = current_piece.y + points.y;229
+ int8_t x = current_piece.x + points.x;230
+231
+ if (232
+ y < 0 || y >= FULL_HEIGHT ||233
+ x < 0 || x >= FULL_WIDTH234
+ ) {235
+ return false;236
+ }237
+228238
if (229
- current_piece.piece[i][j] &&230
- playfield[current_piece.y + points.y][current_piece.x + points.x] != EMPTY239
+ (current_piece.piece[i][j] &&240
+ playfield[y][x] != EMPTY)231241
) {232242
return false;233243
}Binary file not shown.
@@ -3,6 +3,8 @@
33
44
tetromino playfield[FULL_HEIGHT][FULL_WIDTH] = {};55
6
+uint8_t graying[FULL_HEIGHT][FULL_WIDTH] = {};7
+68
79
int main () {810
@@ -29,7 +31,7 @@ int main () {
2931
ioctl(STDOUT_FILENO, TIOCGWINSZ, &init_size);3032
current_size = init_size;3133
32
- printf("\033[?25l\033[?1049h");34
+ printf("\033[?25l\033[?1049h\033[2J\033[H");3335
3436
3537
/* Polling */@@ -61,7 +63,7 @@ int main () {
6163
clock_gettime(CLOCK_MONOTONIC_RAW, &start);6264
6365
64
- uint8_t i;66
+ uint8_t i, j;6567
bool is_tetris = false;6668
6769
while (true) {@@ -140,9 +142,10 @@ int main () {
140142
start.tv_sec += time_diff / 1000000000ULL;141143
start.tv_nsec += time_diff % 1000000000ULL;142144
143
- draw_board(current_piece, (i % 2 == 0) ? NULL : blink, is_piece);145
+ draw_board(current_piece, next_piece, (i % 2 == 0) ? NULL : blink, is_piece);144146
i++;145147
}148
+ 146149
147150
if (i == 4) {148151
@@ -156,6 +159,8 @@ int main () {
156159
157160
for (l = 0; l < FIELD_WIDTH; l++) {158161
playfield[k][l + BORDER_WIDTH] = playfield[k - 1][l + BORDER_WIDTH];162
+ graying[k][l + BORDER_WIDTH] = graying[k - 1][l + BORDER_WIDTH];163
+159164
}160165
}161166
}@@ -226,10 +231,27 @@ int main () {
226231
time_diff -= time_diff % (FRAME * FALL);227232
start.tv_sec += time_diff / 1000000000ULL;228233
start.tv_nsec += time_diff % 1000000000ULL;229
- 234
+235
+ uint8_t j, k;236
+237
+ for (j = 0; j < FULL_HEIGHT; j++) {238
+ for (k = 0; k < FULL_WIDTH; k++) {239
+ if (playfield[j][k] != EMPTY) {240
+ 241
+ if (graying[j][k] < 255) {242
+ graying[j][k] += 1;243
+ }244
+245
+ } else {246
+ graying[j][k] = 0;247
+ }248
+249
+ }250
+ }230251
231252
draw = true;232253
}254
+ 233255
234256
235257
/* When a piece is stuck */@@ -281,7 +303,7 @@ int main () {
281303
282304
for (j = 0; j < FIELD_WIDTH; j++) {283305
if (playfield[TOP_GAP][j + BORDER_WIDTH] > 0) {284
- draw_board(current_piece, NULL, is_piece);306
+ draw_board(current_piece, next_piece, NULL, is_piece);285307
sleep(2);286308
printf("\033[2J\033[H");287309
printf("HAHA LOSER\n");@@ -292,7 +314,7 @@ int main () {
292314
293315
294316
if (draw)295
- draw_board(current_piece, NULL, is_piece);317
+ draw_board(current_piece, next_piece, NULL, is_piece);296318
}297319
298320
end:@@ -1,6 +1,6 @@
1
-#ifndef MAIN_C1
+#ifndef MAIN_H22
3
-#define MAIN_C3
+#define MAIN_H44
55
#include <stdio.h>66
#include <stdint.h>@@ -57,6 +57,11 @@ typedef struct {
5757
int8_t y;5858
} coord;5959
60
+typedef struct {61
+ uint8_t r;62
+ uint8_t g;63
+ uint8_t b;64
+} color;6065
6166
6267
void init_field();@@ -74,6 +79,7 @@ bool valid_position(piece current_piece);
7479
7580
7681
extern tetromino playfield[FULL_HEIGHT][FULL_WIDTH];82
+extern uint8_t graying[FULL_HEIGHT][FULL_WIDTH];7783
7884
7985
#endifBinary file not shown.