#include #include #include #include int **CREATE_TETROMINOS_GRID(int rows, int columns) { } // TODO: impelment this line 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) { } TETROMINO SPAWN_TETROMINO() { } void MOVE_TETROMINO(TETROMINO *tetromino, int **GRID) { } 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_STATS(TETROMINO *tetromino) { } int main() { // Initialize the window and set the wanted FPS InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tetris"); SetTargetFPS(FPS); // Spawn a new 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; } int **GRID = malloc(rows * sizeof(int *)); { if (!GRID) { return 0; } for (int i = 0; i < rows; i++) { GRID[i] = calloc(columns, sizeof(int)); if (!GRID[i]) { for (int j = 0; j < i; j++) { free(GRID[j]); } free(GRID); } } } if (!GRID) { printf("Failed to allocate memory for the grid."); CloseWindow(); return 1; } while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(BLACK); // draw the backgroundd 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); } } // draw the saved tetrominos { 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); } } } // DRAW_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); } } } } // Draw stats { 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); } // Movement { // 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++; } if (ACTIVE_TETROMINO.y == ROWS - 2 || COLLISION_DETECTION(&ACTIVE_TETROMINO, GRID) != 0) { SCORE++; SAVE_TETROMINO(&ACTIVE_TETROMINO, GRID); ACTIVE_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; } printf("DEBUGGING INFORMATION - XOR %p: ", &ACTIVE_TETROMINO.x); printf("DEBUGGING INFORMATION - YOR %p: ", &ACTIVE_TETROMINO.y); } EndDrawing(); } FREE_GRID(GRID, ROWS); CloseWindow(); return 0; }