IT WORKS NOW
This commit is contained in:
parent
f5b65a390b
commit
0f2d03526e
14 changed files with 19477 additions and 31 deletions
|
|
@ -1,9 +1,19 @@
|
|||
#include "01_automato.h"
|
||||
#include "../../logging.h"
|
||||
|
||||
void userInput(UserAction_t action, bool hold) {
|
||||
LOG_FUNCTION_START("userInput", "action=%d, hold=%d", action, hold);
|
||||
|
||||
(void)hold; // заглушка
|
||||
GameState_t* state = get_game_state();
|
||||
|
||||
// Команды движения игнорируются до первого спавна (пока state = Init или первый Spawn)
|
||||
if ((state->state == Init || state->state == Spawn) &&
|
||||
(action == Left || action == Right || action == Down || action == Up || action == Action)) {
|
||||
LOG_FUNCTION_END("userInput", "ignored movement command during initialization, state=%d", state->state);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case Start:
|
||||
state->state = Init;
|
||||
|
|
@ -36,9 +46,13 @@ void userInput(UserAction_t action, bool hold) {
|
|||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("userInput", "state=%d", state->state);
|
||||
}
|
||||
|
||||
GameInfo_t updateCurrentState() {
|
||||
LOG_FUNCTION_START("updateCurrentState", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
switch (state->state) {
|
||||
case Init:
|
||||
|
|
@ -61,13 +75,28 @@ GameInfo_t updateCurrentState() {
|
|||
break;
|
||||
}
|
||||
|
||||
// Копируем данные в уже выделенную память
|
||||
// Копируем state->field в info->field
|
||||
for (int i = 0; i < FIELD_HEIGHT; i++) {
|
||||
for (int j = 0; j < FIELD_WIDTH; j++) {
|
||||
state->info->field[i][j] = state->field[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
// Накладываем активную фигуру на поле
|
||||
Figure_t* fig = &state->curr;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
if (fig->mtrx[i][j]) {
|
||||
int x = fig->x + j;
|
||||
int y = fig->y + i;
|
||||
if (y >= 0 && y < FIELD_HEIGHT && x >= 0 && x < FIELD_WIDTH) {
|
||||
state->info->field[y][x] = 1; // активная фигура
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Копируем next
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
state->info->next[i][j] = state->next.mtrx[i][j];
|
||||
|
|
@ -75,5 +104,9 @@ GameInfo_t updateCurrentState() {
|
|||
}
|
||||
|
||||
state->info->pause = 0;
|
||||
|
||||
LOG_FUNCTION_END("updateCurrentState", "score=%d, level=%d, state=%d",
|
||||
state->info->score, state->info->level, state->state);
|
||||
|
||||
return *state->info;
|
||||
}
|
||||
|
|
@ -1,7 +1,10 @@
|
|||
#include "01_automato.h"
|
||||
#include <stdlib.h>
|
||||
#include "../../logging.h"
|
||||
|
||||
GameState_t* get_game_state(void) {
|
||||
LOG_FUNCTION_START("get_game_state", "");
|
||||
|
||||
static GameState_t state = {0};
|
||||
static int initialized = 0;
|
||||
|
||||
|
|
@ -21,13 +24,31 @@ GameState_t* get_game_state(void) {
|
|||
state.info->next[i] = malloc(4 * sizeof(int));
|
||||
}
|
||||
|
||||
// Инициализируем начальные значения
|
||||
state.info->speed = 1;
|
||||
state.info->score = 0;
|
||||
state.info->level = 1;
|
||||
state.info->pause = 0;
|
||||
|
||||
// Инициализируем следующую фигуру
|
||||
state.next.sprite = rand() % FIGURE_COUNT;
|
||||
state.next.rotation = 0;
|
||||
const int (*shape)[4] = get_figure_shape(state.next.sprite, 0);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
state.next.mtrx[i][j] = shape[i][j];
|
||||
|
||||
state.state = GameOver;
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("get_game_state", "state=%d", state.state);
|
||||
return &state;
|
||||
}
|
||||
|
||||
void terminate_and_free() {
|
||||
LOG_FUNCTION_START("terminate_and_free", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
|
||||
if (state->info) {
|
||||
|
|
@ -56,4 +77,6 @@ void terminate_and_free() {
|
|||
free(state->info);
|
||||
state->info = NULL;
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("terminate_and_free", "");
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
#include "01_automato.h"
|
||||
#include "../../logging.h"
|
||||
|
||||
void do_init(void) {
|
||||
LOG_FUNCTION_START("do_init", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
// Очистка поля
|
||||
for (int i = 0; i < FIELD_HEIGHT; ++i)
|
||||
|
|
@ -11,4 +14,7 @@ void do_init(void) {
|
|||
state->info->level = 1;
|
||||
state->info->speed = 1;
|
||||
state->state = Spawn;
|
||||
|
||||
LOG_FUNCTION_END("do_init", "score=%d, level=%d, state=%d",
|
||||
state->info->score, state->info->level, state->state);
|
||||
}
|
||||
|
|
@ -1,14 +1,18 @@
|
|||
#include "01_automato.h"
|
||||
#include <stdlib.h>
|
||||
#include "../../logging.h"
|
||||
|
||||
void do_spawn(void) {
|
||||
LOG_FUNCTION_START("do_spawn", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
// Присваиваем curr = next
|
||||
|
||||
// Устанавливаем текущую фигуру из следующей (или генерируем первую)
|
||||
state->curr = state->next;
|
||||
state->curr.x = FIELD_WIDTH / 2 - 2;
|
||||
state->curr.y = 0;
|
||||
|
||||
// Генерим следующую фигуру
|
||||
// Генерим новую следующую фигуру
|
||||
state->next.sprite = rand() % FIGURE_COUNT;
|
||||
state->next.rotation = 0;
|
||||
const int (*shape)[4] = get_figure_shape(state->next.sprite, 0);
|
||||
|
|
@ -19,8 +23,12 @@ void do_spawn(void) {
|
|||
// Проверка на GameOver
|
||||
if (check_collision()) {
|
||||
state->state = GameOver;
|
||||
LOG_FUNCTION_END("do_spawn", "collision detected, state=%d", state->state);
|
||||
return;
|
||||
}
|
||||
|
||||
state->state = Move;
|
||||
|
||||
LOG_FUNCTION_END("do_spawn", "curr=(%d,%d), next_sprite=%d, state=%d",
|
||||
state->curr.x, state->curr.y, state->next.sprite, state->state);
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
// brick_game/tetris/06_move.c
|
||||
#include <time.h>
|
||||
#include "01_automato.h"
|
||||
#include "../../logging.h"
|
||||
|
||||
long long get_time_ms() {
|
||||
return (long long)time(NULL) * 1000;
|
||||
|
|
@ -7,12 +9,18 @@ long long get_time_ms() {
|
|||
|
||||
void do_move(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
LOG_FUNCTION_START("do_move", "");
|
||||
|
||||
// Добавляем проверку, чтобы избежать деления на ноль
|
||||
if (state->info->speed <= 0) {
|
||||
state->info->speed = 1; // Устанавливаем минимальное значение
|
||||
}
|
||||
|
||||
long long current_time = get_time_ms();
|
||||
|
||||
int delay = (state->moving_type == ToDown) ? 50 : (1000 / state->info->speed);
|
||||
|
||||
if (current_time - state->last_time < delay) {
|
||||
LOG_FUNCTION_END("do_move", "not enough time passed, delay=%d", delay);
|
||||
return; // ещё не время
|
||||
}
|
||||
state->last_time = current_time;
|
||||
|
|
@ -23,4 +31,7 @@ void do_move(void) {
|
|||
state->curr.y--; // откат
|
||||
state->state = Attaching; // переход в Attaching
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("do_move", "curr=(%d,%d), state=%d",
|
||||
state->curr.x, state->curr.y, state->state);
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
#include "01_automato.h"
|
||||
#include "../../logging.h"
|
||||
|
||||
void do_moving(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
LOG_FUNCTION_START("do_moving", "moving_type=%d", state->moving_type);
|
||||
|
||||
switch (state->moving_type) {
|
||||
case LeftDown:
|
||||
|
|
@ -34,10 +36,10 @@ void do_moving(void) {
|
|||
|
||||
case ToDown:
|
||||
// Мгновенное падение: двигаем вниз, пока не упрёмся
|
||||
do {
|
||||
while (!check_collision()) {
|
||||
state->curr.y++;
|
||||
} while (!check_collision());
|
||||
state->curr.y--; // откат на 1 назад
|
||||
}
|
||||
state->curr.y--; // откат на 1 назад, чтобы убрать последний шаг, вызвавший коллизию
|
||||
state->state = Attaching; // сразу в Attaching
|
||||
break;
|
||||
|
||||
|
|
@ -45,4 +47,7 @@ void do_moving(void) {
|
|||
state->state = Move;
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("do_moving", "curr=(%d,%d), state=%d",
|
||||
state->curr.x, state->curr.y, state->state);
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
#include "01_automato.h"
|
||||
#include "../../logging.h"
|
||||
|
||||
void do_attaching(void) {
|
||||
LOG_FUNCTION_START("do_attaching", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
// Закрепляем фигуру на поле
|
||||
place_figure();
|
||||
|
|
@ -15,9 +18,13 @@ void do_attaching(void) {
|
|||
} else {
|
||||
state->state = Spawn;
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("do_attaching", "state=%d", state->state);
|
||||
}
|
||||
|
||||
int check_collision() {
|
||||
LOG_FUNCTION_START("check_collision", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
Figure_t* fig = &state->curr;
|
||||
|
||||
|
|
@ -28,19 +35,25 @@ int check_collision() {
|
|||
int y = fig->y + i;
|
||||
|
||||
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
|
||||
LOG_FUNCTION_END("check_collision", "collision with boundary, x=%d, y=%d", x, y);
|
||||
return 1; // коллизия
|
||||
}
|
||||
if (y >= 0 && state->field[y][x]) {
|
||||
LOG_FUNCTION_END("check_collision", "collision with field, x=%d, y=%d", x, y);
|
||||
return 1; // коллизия с другой фигурой
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("check_collision", "no collision");
|
||||
return 0; // нет коллизии
|
||||
}
|
||||
|
||||
void place_figure() {
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
LOG_FUNCTION_START("place_figure", "curr=(%d,%d)", state->curr.x, state->curr.y);
|
||||
Figure_t* fig = &state->curr;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
|
|
@ -54,10 +67,14 @@ void place_figure() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("place_figure", "");
|
||||
}
|
||||
|
||||
void clear_lines() {
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
LOG_FUNCTION_START("clear_lines", "score=%d", state->info->score);
|
||||
int lines_cleared = 0;
|
||||
|
||||
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
|
||||
|
|
@ -93,4 +110,7 @@ void clear_lines() {
|
|||
state->info->speed = state->info->level;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d, level=%d",
|
||||
lines_cleared, state->info->score, state->info->level);
|
||||
}
|
||||
|
|
@ -1,21 +1,31 @@
|
|||
#include "01_automato.h"
|
||||
#include "../../logging.h"
|
||||
|
||||
void do_gameover(void) {
|
||||
LOG_FUNCTION_START("do_gameover", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
// Сброс next в пустую фигуру
|
||||
const int (*shape)[4] = empty_fig();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
state->next.mtrx[i][j] = shape[i][j];
|
||||
|
||||
LOG_FUNCTION_END("do_gameover", "");
|
||||
}
|
||||
|
||||
int is_game_over() {
|
||||
LOG_FUNCTION_START("is_game_over", "");
|
||||
|
||||
GameState_t* state = get_game_state();
|
||||
// Проверяем, есть ли блоки в верхних рядах
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (state->field[0][j] || state->field[1][j]) {
|
||||
LOG_FUNCTION_END("is_game_over", "game over detected");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
LOG_FUNCTION_END("is_game_over", "game not over");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue