Small block game in C
Commit b19f80d9ec2d
tarball11 files changed+68-55
@@ -1,11 +1,14 @@
11
CC = gcc2
-CFLAGS = -Wall -Wextra2
+CFLAGS = -Wall -Wextra -MMD -MP33
44
SRC = $(wildcard source/*.c)55
OBJ = $(SRC:.c=.o)6
+DEP = $(OBJ:.o=.d)67
78
tetoris: $(OBJ)89
$(CC) $(CFLAGS) -o $@ $(OBJ)910
1011
clean:11
- rm -f $(OBJ) tetoris12
+ rm -f $(OBJ) $(DEB) tetoris13
+14
+-include $(DEB)@@ -9,8 +9,10 @@
99
#define PURPLE ((color){127, 0, 255})1010
#define WHITE ((color){255, 255, 255})1111
12
+#define DARK 0.61213
13
-void draw_color (tetromino current_square, uint8_t gray) {14
+15
+void draw_color (tetromino current_square, double dark) {1416
1517
color col = {};1618
@@ -41,13 +43,12 @@ void draw_color (tetromino current_square, uint8_t gray) {
4143
break;4244
} 4345
44
- if(gray) {46
+ if(dark > 0) {4547
46
- uint8_t gray_hue = (uint8_t)((299 * col.r + 587 * col.g + 114 * col.b) / 1000);4748
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;49
+ col.r *= dark;50
+ col.g *= dark;51
+ col.b *= dark;5152
}5253
5354
printf("\033[38;2;%i;%i;%im", col.r, col.g, col.b);@@ -72,7 +73,7 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
7273
7374
for (j = 0; j < FIELD_WIDTH; j++) {7475
75
- uint8_t gray = 0;76
+ double dark = DARK;7677
7778
if ( blink != NULL && blink[i]) {7879
@@ -83,7 +84,6 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
8384
8485
8586
tetromino current_square = playfield[i][j + BORDER_WIDTH];86
- gray = graying[i][j + BORDER_WIDTH];8787
8888
8989
if (is_piece) {@@ -101,6 +101,7 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
101101
current_piece.piece[k][l]) 102102
{103103
current_square = current_piece.name;104
+ dark = 0;104105
}105106
}106107
}@@ -111,7 +112,7 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
111112
continue;112113
}113114
114
- draw_color(current_square, gray);115
+ draw_color(current_square, dark);115116
}116117
117118
printf("██\033[0m");@@ -161,6 +162,9 @@ void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piec
161162
printf("\033[%iC", FIELD_WIDTH * 2 + 1);162163
printf("┠──────────┘\n");163164
165
+ printf("\033[%iC", FIELD_WIDTH * 2 + 2);166
+ printf("LEVEL: %u\n", level);167
+164168
}165169
166170
void draw_pause () {@@ -0,0 +1,2 @@
1
+source/draw.o: source/draw.c source/main.h2
+source/main.h:Binary file not shown.
@@ -246,3 +246,15 @@ bool valid_position(piece current_piece) {
246246
247247
return true;248248
}249
+250
+251
+uint8_t delay[] = {252
+ 53, 49, 45, 41, 37, 33, 28, 22, 17, 11, 10, 9, 8, 7, 6, 6, 5, 5, 4, 4, 3253
+};254
+255
+uint8_t level_delay() {256
+ if (level < sizeof(delay))257
+ return delay[level];258
+259
+ return delay[0];260
+}@@ -0,0 +1,2 @@
1
+source/logic.o: source/logic.c source/main.h2
+source/main.h:Binary file not shown.
@@ -2,9 +2,7 @@
22
33
44
tetromino playfield[FULL_HEIGHT][FULL_WIDTH] = {};5
-6
-uint8_t graying[FULL_HEIGHT][FULL_WIDTH] = {};7
-5
+uint8_t level = 0;86
97
int main () {108
@@ -52,6 +50,8 @@ int main () {
5250
5351
bool paused = false;5452
53
+ uint8_t lines_cleared = 0;54
+5555
piece current_piece = {};5656
piece next_piece = init_piece((rand() % Z_PIECE) + 1);5757
@@ -66,7 +66,7 @@ int main () {
6666
uint16_t tick_fall = 0;6767
uint16_t tick_blink = 0;6868
uint16_t tick_spin = 0;69
- uint16_t tick_gray = 0;69
+ uint16_t tick_fall_prot = 0;7070
7171
uint8_t i;7272
bool is_tetris = false;@@ -195,8 +195,6 @@ int main () {
195195
196196
for (l = 0; l < FIELD_WIDTH; l++) {197197
playfield[k][l + BORDER_WIDTH] = playfield[k - 1][l + BORDER_WIDTH];198
- graying[k][l + BORDER_WIDTH] = graying[k - 1][l + BORDER_WIDTH];199
-200198
}201199
}202200
}@@ -219,7 +217,7 @@ int main () {
219217
is_piece = true;220218
draw = true;221219
222
- tick_fall = tick_blink = tick_spin = 0;220
+ tick_fall = tick_blink = tick_spin = tick_fall_prot = 0;223221
}224222
225223
@@ -265,13 +263,21 @@ int main () {
265263
/* Piece falls */266264
267265
tick_fall += ticks;266
+ 267
+ if (tick_fall_prot < FALL_PROTECTION)268
+ tick_fall_prot += ticks;269
+270
+ uint8_t fall_delay = level_delay(); 268271
269
- while (tick_fall >= FALL || down) {272
+ while (tick_fall >= fall_delay || (down && tick_fall_prot >= FALL_PROTECTION)) {270273
current_piece.y++;271274
272275
273
- if (tick_fall >= FALL)274
- tick_fall -= FALL;276
+ if (tick_fall >= fall_delay) {277
+ tick_fall -= fall_delay;278
+ } else {279
+ tick_fall = 0;280
+ }275281
276282
down = false;277283
@@ -318,12 +324,19 @@ int main () {
318324
if (blocked == 10) {319325
320326
blink[j] = true;321
- is_tetris = true; 327
+ is_tetris = true;328
+ lines_cleared++;322329
} else {323330
blink[j] = false;324331
}325332
}326333
334
+ /* Level up */335
+336
+ if (lines_cleared >= LINES_LEVEL && level < MAX_LEVEL) {337
+ level++;338
+ lines_cleared -= LINES_LEVEL;339
+ }327340
328341
/* Looks for game over */329342
@@ -338,34 +351,6 @@ int main () {
338351
}339352
}340353
341
- /* Graying out of old pieces */342
-343
- tick_gray += ticks;344
-345
- while (tick_gray >= GRAY){346
- 347
-348
- uint8_t j, k;349
-350
- for (j = 0; j < FULL_HEIGHT; j++) {351
- for (k = 0; k < FULL_WIDTH; k++) {352
- if (playfield[j][k] != EMPTY) {353
- 354
- if (graying[j][k] < 255) {355
- graying[j][k] += 1;356
- }357
-358
- } else {359
- graying[j][k] = 0;360
- }361
-362
- }363
- }364
-365
- tick_gray -= GRAY;366
- }367
-368
-369354
if (draw)370355
draw_board(current_piece, next_piece, NULL, is_piece);371356
}@@ -0,0 +1,2 @@
1
+source/main.o: source/main.c source/main.h2
+source/main.h:@@ -29,12 +29,15 @@
2929
#define FULL_WIDTH (FIELD_WIDTH + BORDER_WIDTH * 2)3030
#define FULL_HEIGHT (FIELD_HEIGHT + BORDER_WIDTH)3131
32
-#define FRAME 1700000032
+#define FRAME 167420063333
34
-#define FALL 5834
+#define FALL 533535
#define BLINK 123636
#define SPIN 837
-#define GRAY 1037
+#define FALL_PROTECTION 5038
+39
+#define LINES_LEVEL 1040
+#define MAX_LEVEL 203841
3942
4043
typedef enum {@@ -79,10 +82,10 @@ coord find_direction(piece current_piece, uint8_t x, uint8_t y);
7982
8083
bool valid_position(piece current_piece);8184
85
+uint8_t level_delay();8286
8387
8488
extern tetromino playfield[FULL_HEIGHT][FULL_WIDTH];85
-extern uint8_t graying[FULL_HEIGHT][FULL_WIDTH];86
-89
+extern uint8_t level;8790
8891
#endifBinary file not shown.