Фигурки то рисуются!

This commit is contained in:
Rorikstr | Rust Dev 2025-09-26 13:28:53 +03:00
parent e20765d252
commit fef0d8cbe3
6 changed files with 176 additions and 22 deletions

View file

@ -0,0 +1,76 @@
// src/brick_game/tetris/tetris.c
#include "tetris.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static GameStateData game_state = {0};
static bool initialized = false;
const int (*get_figure_shape(FigureType type, int rotation))[4] {
static const int shapes[FIGURE_COUNT][4][4][4] = {
// I
{{{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
{{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}}},
// O
{{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}},
// T
{{{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}},
// L
{{{0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{1, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {1, 1, 1, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}},
{{1, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}},
// J
{{{1, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}},
{{0, 1, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}}},
// S
{{{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}}},
// Z
{{{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}},
{{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}}};
return shapes[type][rotation];
}
void userInput(UserAction_t action, bool hold) {
(void)hold; // Подавляем предупреждение
if (!initialized) {
memset(&game_state, 0, sizeof(game_state));
initialized = true;
}
if (action >= Figure1 && action <= Figure5) {
FigureType type = (FigureType)(action - Figure1);
game_state.current_figure.type = type;
game_state.current_figure.x = FIELD_WIDTH / 2 - 2;
game_state.current_figure.y = 0;
game_state.current_figure.rotation = 0;
game_state.figure_active = true;
}
if (game_state.figure_active) {
if (action == Left) game_state.current_figure.x--;
if (action == Right) game_state.current_figure.x++;
if (action == Down) game_state.current_figure.y++;
if (action == Up) game_state.current_figure.y--;
}
}
GameStateData* getGameState() {
return &game_state;
}

View file

@ -1,13 +1,13 @@
// src/brick_game/tetris/tetris.h
#ifndef TETRIS_H
#define TETRIS_H
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
// Константы
#define FIELD_WIDTH 10
#define FIELD_HEIGHT 20
#define NEXT_SIZE 4
// Типы фигур
typedef enum {
@ -21,16 +21,6 @@ typedef enum {
FIGURE_COUNT
} FigureType;
// Состояния конечного автомата
typedef enum {
START,
SPAWN,
MOVING,
SHIFTING,
ATTACHING,
GAME_OVER
} GameState;
// Структура фигуры
typedef struct {
int x, y; // Позиция фигуры на поле
@ -42,30 +32,31 @@ typedef struct {
typedef struct {
int field[FIELD_HEIGHT][FIELD_WIDTH]; // Игровое поле
Figure current_figure; // Текущая фигура
Figure next_figure; // Следующая фигура
int score; // Текущие очки
int high_score; // Максимальные очки
int level; // Уровень
int lines_cleared; // Удалённые линии
GameState state; // Текущее состояние КА
bool paused; // Игра на паузе?
bool figure_active; // Есть активная фигура?
} GameStateData;
// Ввод пользователя
typedef enum {
Undefined = -1,
Start,
Pause,
Terminate,
Left,
Right,
Up, // не используется
Up,
Down,
Action // вращение
Action,
Figure1, // 1
Figure2, // 2
Figure3, // 3
Figure4, // 4
Figure5 // 5
} UserAction_t;
// Основные функции библиотеки
void userInput(UserAction_t action, bool hold);
GameStateData* getGameState(void);
void updateCurrentState(void);
const int (*get_figure_shape(FigureType type, int rotation))[4];
#endif