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
|
/*
* =====================================================================================
*
* Filename: Tetris.c
*
* Description: Tetris in raylib
*
* Version: 1.0
* Created: 01/17/2025 09:58:53
* Revision: 1.0
* Compiler: gcc
*
* Author: nsrddyn,
* Organization:
*
* =====================================================================================
*/
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// GAME SETTINGS
#define FPS 60
#define FONT_SIZE 10
#define TETROMINO_SIZE_HEIGHT 2
#define TETROMINO_SIZE_WIDTH 4
#define CYAN_I (Color){0, 255, 255, 255}
#define YELLOW_O (Color){255, 255, 102, 255}
#define PURPLE_T (Color){138, 43, 226, 255}
#define GREEN_S (Color){34, 139, 34, 255}
#define RED_Z (Color){255, 69, 0, 255}
#define BLUE_J (Color){70, 130, 180, 255}
#define ORANGE_L (Color){255, 140, 0, 255}
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 600
#define COLUMNS 10
#define ROWS 20
#define CELL_WIDTH SCREEN_WIDTH / COLUMNS
#define CELL_HEIGHT SCREEN_HEIGHT / ROWS
static int SCORE = 0;
typedef struct {
int blocks[TETROMINO_SIZE_HEIGHT][TETROMINO_SIZE_WIDTH];
Color color;
int x;
int y;
} TETROMINO;
TETROMINO TETROMINOES[7] = {
// Making the tetrominoes in an array design
{{
{0, 0, 0, 0},
{1, 1, 1, 1},
},
CYAN_I,
COLUMNS / 2,
0},
// supposed to have 2 forms of rotations
// default spawn location is: center
{{
{0, 1, 1, 0},
{0, 1, 1, 0},
},
YELLOW_O,
COLUMNS / 2,
0},
// supposed to have no rotation forms
// default spawn location is: center
{{
{0, 1, 0, 0},
{1, 1, 1, 0},
},
PURPLE_T,
0,
0},
// flat side left center
// 4 rotational forms
{{
{0, 1, 1, 0},
{1, 1, 0, 0},
},
GREEN_S,
0,
0},
// left center
// 4 rotational forms
{{
{1, 1, 0, 0},
{0, 1, 1, 0},
},
RED_Z,
0,
0},
// left center
// 4 rotational forms
{{
{1, 0, 0, 0},
{1, 1, 1, 0},
},
BLUE_J,
0,
0},
// flat side first and left center
// 4 rotational forms
{{
{0, 0, 1, 0},
{1, 1, 1, 0},
},
ORANGE_L,
0,
0},
// flat side first and left center
// 4 rotational forms
};
void DRAW_BACKGROUND_GRID() {
for (int i = 0; i <= COLUMNS; i++) {
DrawLine(i * CELL_WIDTH, 0, i * CELL_WIDTH, SCREEN_HEIGHT, GRAY);
}
for (int j = 0; j <= ROWS; j++) {
DrawLine(0, j * CELL_HEIGHT, SCREEN_WIDTH, j * CELL_HEIGHT, GRAY);
}
}
int **CREATE_TETROMINOS_GRID(int rows, int columns) {
int **array = malloc(rows * sizeof(int *));
if (!array)
return NULL;
for (int i = 0; i < rows; i++)
{
array[i] = calloc(columns, sizeof(int));
if (!array[i]) {
for (int j = 0; j < i; j++)
free(array[j]);
free(array);
return NULL;
}
}
return array;
}
void CHECK_FULL_LINE(int **GRID) {
for (int y = 0; y < ROWS; y++) {
bool FULL_LINE = true;
for (int x = 0; x < COLUMNS; x++) {
if (GRID[y][x] == 0) {
FULL_LINE = false;
break;
}
}
if (FULL_LINE) {
for (int i = y; i > 0; i--) {
for (int x = 0; x < COLUMNS; x++)
GRID[i][x] = GRID[i - 1][x];
}
for (int x = 0; x < COLUMNS; x++)
GRID[ROWS - 2][x] = 0;
SCORE += 10;
}
}
}
// Detect if the active tetromino is hitting another tetromino
// Detect if the active tetromino is hitting another tetromino
int COLLISION_DETECTION(TETROMINO *tetromino, int **GRID) {
int TETROMINO_WIDTH = 0;
int TETROMINO_HEIGHT = 0;
// Determine the bounding width and height of the tetromino
for (int y = 0; y < TETROMINO_SIZE_HEIGHT; y++) {
for (int x = 0; x < TETROMINO_SIZE_WIDTH; x++) {
if (tetromino->blocks[y][x] == 1) {
// Update width and height based on the furthest active blocks
if (x + 1 > TETROMINO_WIDTH)
TETROMINO_WIDTH = x + 1;
if (y + 1 > TETROMINO_HEIGHT)
TETROMINO_HEIGHT = y + 1;
}
}
}
// Check for collision with the grid
for (int i = 0; i < TETROMINO_HEIGHT; i++) {
for (int j = 0; j < TETROMINO_WIDTH; j++) {
if (tetromino->blocks[i][j] == 1) {
int GRID_X = tetromino->x + j;
int GRID_Y = tetromino->y + i;
// Check if we are within the grid boundaries and if there is a block
if (GRID_Y >= 0 && GRID_X >= 0 && GRID[GRID_Y][GRID_X] != 0) {
return 1; // Collision detected
}
}
}
}
// No collision
return 0;
}
int *ROTATE_ACTIVE_TETROMINO(TETROMINO *tetromino, int **GRID) {
int *ROTATED_TETROMINO;
for (int x = 0; x < COLUMNS; x++) {
for (int y = 0; y < ROWS; y++) {
ROTATED_TETROMINO = 0;
}
}
return ROTATED_TETROMINO;
}
void FREE_GRID(int **array, int rows) {
for (int i = 0; i < rows; i++)
free(array[i]);
free(array);
}
void DRAW_TETROMINO(TETROMINO *tetromino) {
for (int y = 0; y < TETROMINO_SIZE_HEIGHT; y++) {
for (int x = 0; x < TETROMINO_SIZE_WIDTH; x++) {
if (tetromino->blocks[y][x] == 1) {
DrawRectangle((tetromino->x + x) * CELL_WIDTH,
(tetromino->y + y) * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT,
tetromino->color);
}
}
}
}
TETROMINO SPAWN_TETROMINO() {
int RANDOM_INDEX = GetRandomValue(0, 6);
TETROMINO tetromino = TETROMINOES[RANDOM_INDEX];
if (tetromino.x == 0) {
tetromino.x = COLUMNS / 2 - 2;
tetromino.y = 0;
}
return tetromino;
}
void MOVE_TETROMINO(TETROMINO *tetromino, int **GRID) {
// RIGHT WALL COLLISION DETECTION BY STOPPING MOVEMENT
if (IsKeyDown(KEY_RIGHT) && COLLISION_DETECTION(tetromino, GRID) == 0 &&
tetromino->x < COLUMNS - 2)
tetromino->x++;
// LEFT WALL COLLISION DETECTION BY STOPPING MOVEMENT
if (IsKeyDown(KEY_LEFT) && COLLISION_DETECTION(tetromino, GRID) == 0 &&
tetromino->x > 0)
tetromino->x--;
// BOTTOM ROW COLLISION DETECTION BY STOPPING MOVEMENT
if (IsKeyDown(KEY_DOWN) && COLLISION_DETECTION(tetromino, GRID) == 0 &&
tetromino->y < ROWS - 2)
tetromino->y++;
}
void SAVE_TETROMINO(TETROMINO *tetromino, int **GRID) {
for (int y = 0; y < TETROMINO_SIZE_HEIGHT; y++) {
for (int x = 0; x < TETROMINO_SIZE_WIDTH; x++) {
if (tetromino->blocks[y][x] == 1) {
int GRIDX = tetromino->x + x;
int GRIDY = tetromino->y + y;
if (GRIDX < COLUMNS && GRIDY >= 0 && GRIDY < ROWS)
GRID[GRIDY][GRIDX] = 1;
}
}
}
}
void DRAW_SAVED_TETROMINO(int **GRID, TETROMINO *tetromino) {
for (int y = 0; y < ROWS; y++) {
for (int x = 0; x < COLUMNS; x++) {
if (GRID[y][x] == 1)
DrawRectangle((x * CELL_WIDTH), (y * CELL_HEIGHT), CELL_WIDTH,
CELL_HEIGHT, tetromino->color);
}
}
}
void DRAW_STATS(TETROMINO *tetromino) {
char SCORE_TEXT[16];
sprintf(SCORE_TEXT, "SCORE: %d", SCORE);
DrawText(SCORE_TEXT, 10, 10, FONT_SIZE, BLUE);
char CURRENT_Y_POSITION[32], CURRENT_X_POSITION[32];
sprintf(CURRENT_Y_POSITION, "Y COÖRDINATE: %d", tetromino->y);
sprintf(CURRENT_X_POSITION, "X COÖRDINATE: %d", tetromino->x);
DrawText(CURRENT_Y_POSITION, 10, 50, FONT_SIZE, BLUE);
DrawText(CURRENT_X_POSITION, 10, 80, FONT_SIZE, BLUE);
int POS_X = SCREEN_WIDTH / 2;
int POS_Y = SCREEN_HEIGHT / 2;
const char ERROR_TEXT[] = "LOW FPS";
if (GetFPS() < 40)
DrawText(ERROR_TEXT, POS_X, POS_Y, FONT_SIZE, RED);
}
int main() {
// Initialize the window and set the wanted FPS
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tetris");
SetTargetFPS(FPS);
// Spawn a new tetromino
TETROMINO ACTIVE_TETROMINO = SPAWN_TETROMINO();
int **GRID = CREATE_TETROMINOS_GRID(ROWS, COLUMNS);
if (!GRID) {
printf("Failed to allocate memory for the grid.");
CloseWindow();
return 1;
}
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(BLACK);
DRAW_BACKGROUND_GRID();
DRAW_SAVED_TETROMINO(GRID, &ACTIVE_TETROMINO);
DRAW_TETROMINO(&ACTIVE_TETROMINO);
DRAW_STATS(&ACTIVE_TETROMINO);
MOVE_TETROMINO(&ACTIVE_TETROMINO, GRID);
if (ACTIVE_TETROMINO.y == ROWS - 2 ||
COLLISION_DETECTION(&ACTIVE_TETROMINO, GRID) != 0) {
SCORE++;
SAVE_TETROMINO(&ACTIVE_TETROMINO, GRID);
ACTIVE_TETROMINO = SPAWN_TETROMINO();
printf("DEBUGGING INFORMATION - XOR %p: ", &ACTIVE_TETROMINO.x);
printf("DEBUGGING INFORMATION - YOR %p: ", &ACTIVE_TETROMINO.y);
}
EndDrawing();
}
FREE_GRID(GRID, ROWS);
CloseWindow();
return 0;
}
|