1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
#include "raylib.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define MATRIX_WIDTH 10
#define MATRIX_HEIGHT 40
#define BLOCK_SIZE 25
#define FALL_TICK 0.2
#define SOFT_DROP_MULT 4
#define LOCK_TICK 0.5
enum Phases {
GENERATION,
FALL,
LOCK,
PATTERN,
MARK,
ITERATE,
ANIMATE,
ELIMINATE,
COMPLETE
} game_phase;
#include "tetromino.h"
struct {
int pos_x;
int pos_y;
int rotation;
int type;
} current_piece;
struct Color matrix[MATRIX_WIDTH][MATRIX_HEIGHT];
int piece_queue[TETROMINO_COUNT*2];
int current_bag = 0;
int current_piece_index = 0;
int hold = 7;
int held = 0;
int hard_drop = 0;
double phase_time = 0.0;
void draw_block(int x, int y, int width, int height, struct Color color)
{
DrawRectangle(x, -y + SCREEN_HEIGHT - height, width, height, color);
}
int is_overlap(int x, int y)
{
for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
if (tetrominos[current_piece.type].directions[current_piece.rotation][j][i] == 1) {
if (current_piece.pos_x + i - 1 + x >= MATRIX_WIDTH
|| current_piece.pos_x + i - 1 + x < 0
|| current_piece.pos_y + j - 1 + y < 0
|| (!ColorIsEqual(BLACK, matrix[current_piece.pos_x + i - 1 + x][current_piece.pos_y + j - 1 + y])
&& !ColorIsEqual(WHITE, matrix[current_piece.pos_x + i - 1 + x][current_piece.pos_y + j - 1 + y]))) {
return 1;
}
}
}
}
return 0;
}
void render_matrix(void)
{
int matrix_origin_x = (SCREEN_WIDTH/2 - (MATRIX_WIDTH * BLOCK_SIZE))/2;
int matrix_origin_y = (SCREEN_HEIGHT - MATRIX_HEIGHT/2 * BLOCK_SIZE)/2;
/* draw matrix background */
draw_block(
matrix_origin_x - 1
,matrix_origin_y - 1
,MATRIX_WIDTH * BLOCK_SIZE + 2
,MATRIX_HEIGHT/2 * BLOCK_SIZE + 2
,GRAY
);
/* draw matrix */
for (int i = 0; i < MATRIX_WIDTH; i++) {
for (int j = 0; j < MATRIX_HEIGHT; j++) {
draw_block(
matrix_origin_x + i * BLOCK_SIZE + 1
,matrix_origin_y + j * BLOCK_SIZE + 1
,BLOCK_SIZE - 2
,BLOCK_SIZE - 2
,matrix[i][j]
);
}
}
/* draw ghost piece */
int ghost_y = 0;
while(!is_overlap(0, ghost_y)) {
ghost_y--;
}
for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
if (tetrominos[current_piece.type].directions[current_piece.rotation][j][i] == 1) {
draw_block(
(matrix_origin_x + ((current_piece.pos_x + i - 1) * BLOCK_SIZE)) + 3
,(matrix_origin_y + ((current_piece.pos_y + j + ghost_y) * BLOCK_SIZE)) + 3
,BLOCK_SIZE - 6
,BLOCK_SIZE - 6
,tetrominos[current_piece.type].color
);
draw_block(
(matrix_origin_x + ((current_piece.pos_x + i - 1) * BLOCK_SIZE)) + 4
,(matrix_origin_y + ((current_piece.pos_y + j + ghost_y) * BLOCK_SIZE)) + 4
,BLOCK_SIZE - 8
,BLOCK_SIZE - 8
,BLACK
);
}
}
}
/* draw current piece */
for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
if (tetrominos[current_piece.type].directions[current_piece.rotation][j][i] == 1) {
draw_block(
(matrix_origin_x + ((current_piece.pos_x + i - 1) * BLOCK_SIZE)) + 1
,(matrix_origin_y + ((current_piece.pos_y + j - 1) * BLOCK_SIZE)) + 1
,BLOCK_SIZE - 2
,BLOCK_SIZE - 2
,tetrominos[current_piece.type].color
);
}
}
}
/* draw piece hold */
draw_block(
SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE),
matrix_origin_y + MATRIX_HEIGHT/2 * BLOCK_SIZE - 3 * BLOCK_SIZE,
4*BLOCK_SIZE,
4*BLOCK_SIZE,
BLACK
);
if (hold != 7) {
for (int i = 0; i < tetrominos[hold].size; i++) {
for (int j = 0; j < tetrominos[hold].size; j++) {
if (tetrominos[hold].directions[NORTH][j][i] == 1) {
draw_block(
((SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE)) + (i * BLOCK_SIZE)) + 1
+ ((hold != I && hold != O)?(BLOCK_SIZE/2):0)
,matrix_origin_y + (MATRIX_HEIGHT/2 * BLOCK_SIZE) + ((j-3) * BLOCK_SIZE)
,BLOCK_SIZE - 2
,BLOCK_SIZE - 2
,tetrominos[hold].color
);
}
}
}
}
/* draw piece queue */
draw_block(
SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE),
matrix_origin_y + MATRIX_HEIGHT/2 * BLOCK_SIZE - 20 * BLOCK_SIZE,
4*BLOCK_SIZE,
(4*BLOCK_SIZE) * 4,
BLACK
);
for (int y = 0; y < 4; y++) {
for (int i = 0; i < tetrominos[piece_queue[(y+current_piece_index)%14]].size; i++) {
for (int j = 0; j < tetrominos[piece_queue[(y+current_piece_index)%14]].size; j++) {
if (tetrominos[piece_queue[(y+current_piece_index)%14]].directions[NORTH][j][i] == 1) {
draw_block(
((SCREEN_WIDTH/2 + 1 - (BLOCK_SIZE)) + (i * BLOCK_SIZE)) + 1
+ ((piece_queue[(y+current_piece_index)%14] != I && piece_queue[(y+current_piece_index)%14] != O)?(BLOCK_SIZE/2):0)
,((matrix_origin_y + MATRIX_HEIGHT/2 * BLOCK_SIZE - 20 * BLOCK_SIZE) + ((j - 1) * BLOCK_SIZE)) + 1 + ((3 - y) * BLOCK_SIZE * 4) + BLOCK_SIZE/2
,BLOCK_SIZE - 2
,BLOCK_SIZE - 2
,tetrominos[piece_queue[(y+current_piece_index)%14]].color
);
}
}
}
}
}
void shuffle_bag(int *bag)
{
for (int i = 0; i < TETROMINO_COUNT; i++) {
bag[i] = i;
}
for (int i = 0; i < TETROMINO_COUNT; i++) {
int r = i + rand() / (RAND_MAX / (TETROMINO_COUNT - i) + 1);
int t = bag[r];
bag[r] = bag[i];
bag[i] = t;
}
}
void generation_phase(void)
{
// bag generation
if (current_piece_index == 14) {
shuffle_bag(&(piece_queue[7]));
current_piece_index = 0;
}
if (current_piece_index == 7) {
shuffle_bag(piece_queue);
}
// set current piece
current_piece.type = piece_queue[current_piece_index++];
current_piece.rotation = NORTH;
current_piece.pos_x = 4;
current_piece.pos_y = 20;
if (is_overlap(0,0)) {
CloseWindow();
}
}
void handle_hold(void)
{
if (IsKeyDown(KEY_SPACE) && !held) {
if (hold == 7) {
hold = current_piece.type;
current_piece.type = piece_queue[current_piece_index];
current_piece_index++;
current_piece_index = current_piece_index % 14;
} else {
int temp = current_piece.type;
current_piece.type = hold;
hold = temp;
}
game_phase = FALL;
current_piece.rotation = NORTH;
current_piece.pos_x = 4;
current_piece.pos_y = 20;
if (is_overlap(0,0)) {
CloseWindow();
}
held = 1;
}
}
void handle_rotate(void)
{
static int rotated_cw = 0;
static int rotated_ccw = 0;
if (game_phase == FALL || game_phase == LOCK) {
int *kick_table;
int can_rotate = 0;
int i;
if (IsKeyDown(KEY_L) && !rotated_cw) {
kick_table = tetrominos[current_piece.type].cw_kick_table[current_piece.rotation];
current_piece.rotation++;
current_piece.rotation = current_piece.rotation % 4;
for (i = 0; i < 10 && !can_rotate; i += 2) {
if (!is_overlap(kick_table[i], kick_table[i+1])) {
current_piece.pos_x += kick_table[i];
current_piece.pos_y += kick_table[i+1];
can_rotate = 1;
if (game_phase == LOCK) {
phase_time = 0.0;
}
}
}
if (!can_rotate) {
current_piece.rotation--;
if (current_piece.rotation < 0) current_piece.rotation = 3;
}
rotated_cw = 1;
}
if (IsKeyUp(KEY_L)) {
rotated_cw = 0;
}
if (IsKeyDown(KEY_J) && !rotated_ccw) {
kick_table = tetrominos[current_piece.type].ccw_kick_table[current_piece.rotation];
current_piece.rotation--;
if (current_piece.rotation < 0) current_piece.rotation = 3;
for (i = 0; i < 10 && !can_rotate; i += 2) {
if (!is_overlap(kick_table[i], kick_table[i+1])) {
current_piece.pos_x += kick_table[i];
current_piece.pos_y += kick_table[i+1];
can_rotate = 1;
if (game_phase == LOCK) {
phase_time = 0.0;
}
}
}
if (!can_rotate) {
current_piece.rotation++;
current_piece.rotation = current_piece.rotation % 4;
}
rotated_ccw = 1;
}
if (IsKeyUp(KEY_J)) {
rotated_ccw = 0;
}
}
}
void move_right(void)
{
if (is_overlap(1, 0)) {
return;
}
if (game_phase == LOCK) {
phase_time = 0.0;
}
current_piece.pos_x++;
}
void move_left(void)
{
if (is_overlap(-1, 0)) {
return;
}
if (game_phase == LOCK) {
phase_time = 0.0;
}
current_piece.pos_x--;
}
void handle_lr(void)
{
static int moved_right = 0;
static int moved_left = 0;
if (game_phase == FALL || game_phase == LOCK) {
if (IsKeyDown(KEY_D) && !moved_right) {
move_right();
moved_right = 1;
}
if (IsKeyUp(KEY_D)) {
moved_right = 0;
}
if (IsKeyDown(KEY_A) && !moved_left) {
move_left();
moved_left = 1;
}
if (IsKeyUp(KEY_A)) {
moved_left = 0;
}
}
}
void move_piece_to_matrix(void)
{
for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
if (tetrominos[current_piece.type].directions[current_piece.rotation][j][i] == 1) {
matrix[current_piece.pos_x + i - 1][current_piece.pos_y + j - 1] = tetrominos[current_piece.type].color;
}
}
}
}
void handle_fall(void)
{
phase_time += GetFrameTime();
if (game_phase == FALL) {
if (!hard_drop && ((!IsKeyDown(KEY_S) && phase_time < FALL_TICK) || phase_time < FALL_TICK / SOFT_DROP_MULT)) {
return;
} else {
phase_time = 0.0;
}
}
if (is_overlap(0, -1)) {
game_phase = LOCK;
} else {
game_phase = FALL;
current_piece.pos_y--;
}
if (is_overlap(0, -1) && (phase_time >= LOCK_TICK || hard_drop)) {
move_piece_to_matrix();
generation_phase();
held = 0;
game_phase = FALL;
phase_time = 0.0;
hard_drop = 0;
return;
}
}
void handle_hard_drop(void)
{
static int dropped = 0;
if (game_phase == FALL || game_phase == LOCK) {
if (IsKeyDown(KEY_W) && !dropped) {
dropped = 1;
hard_drop = 1;
while(!is_overlap(0, -1)) {
current_piece.pos_y--;
}
}
if (IsKeyUp(KEY_W)) {
dropped = 0;
}
}
}
void clear_line(int line) {
for (int y = line; y < MATRIX_HEIGHT - 1; y++) {
for (int x = 0; x < MATRIX_WIDTH; x++) {
matrix[x][y] = matrix[x][y+1];
if (ColorIsEqual(matrix[x][y], WHITE) && y < 20) {
matrix[x][y] = BLACK;
}
}
}
}
void handle_line_clears(void)
{
for (int y = 0; y < MATRIX_HEIGHT; y++) {
int line_full = 1;
for (int x = 0; x < MATRIX_WIDTH; x++) {
if (ColorIsEqual(matrix[x][y], BLACK) || ColorIsEqual(matrix[x][y], WHITE)) {
line_full = 0;
}
}
if (line_full) {
clear_line(y);
}
}
}
int main(void)
{
srand(time(NULL));
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "~turnipGod's Tetris");
// set all blocks to white
for (int y = 0; y < 40; y++) {
for (int x = 0; x < 10; x++) {
matrix[x][y] = WHITE;
}
}
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 10; x++) {
matrix[x][y] = BLACK;
}
}
game_phase = FALL;
shuffle_bag(piece_queue);
shuffle_bag(&(piece_queue[7]));
generation_phase();
while (!WindowShouldClose()) {
handle_lr();
handle_rotate();
handle_hold();
handle_hard_drop();
handle_fall();
handle_line_clears();
BeginDrawing();
ClearBackground(WHITE);
render_matrix();
EndDrawing();
}
CloseWindow();
return 0;
}
|