diff --git a/final/game.c b/final/game.c new file mode 100644 index 0000000..3701b43 --- /dev/null +++ b/final/game.c @@ -0,0 +1,71 @@ +#include "world.h" +#include + +typedef struct { + int grid[20][10]; // Игровое поле + int current_x, current_y; // Текущее положение падающего блока + int game_over; // Флаг завершения игры +} GameState; + +void init_game(GameState *game) { + for (int i = 0; i < 20; i++) { + for (int j = 0; j < 10; j++) { + game->grid[i][j] = 0; + } + } + game->current_x = 5; + game->current_y = 0; + game->game_over = 0; +} + +void draw_game(GameState *game) { + clear_screen(); + for (int i = 0; i < 20; i++) { + for (int j = 0; j < 10; j++) { + if (game->grid[i][j] == 1) { + set_color_cell('#', j, i, COLOR_GREEN, COLOR_BLACK); + } + } + } + set_color_cell('X', game->current_x, game->current_y, COLOR_RED, COLOR_BLACK); + refresh(); +} + +void update_game(GameState *game, struct event *event) { + if (event->key == KEY_DOWN) { + game->current_y++; + } + if (game->current_y >= 20) { // Если достигли дна + game->game_over = 1; + } +} + +int game_event(struct event *event, void *game_ptr) { + GameState *game = (GameState *)game_ptr; + if (event->type == EVENT_KEY) { + update_game(game, event); + } + draw_game(game); + if (game->game_over) { + set_message("Game Over!", 5, 10); + return 1; // Завершить игру + } + return 0; // Продолжить игру +} + +void* init_tetris_game() { + GameState *game = malloc(sizeof(GameState)); + if (!game) return NULL; + init_game(game); + return game; +} + +void destroy_tetris_game(void *game) { + free(game); +} + +int main() { + start_world(init_tetris_game, game_event, destroy_tetris_game); + return 0; +} +