pvjc24/final/game.c

72 lines
1.7 KiB
C
Raw Permalink Normal View History

2024-05-16 07:51:45 +00:00
#include "world.h"
#include <stdlib.h>
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;
}