almost done

This commit is contained in:
Rorikstr | Rust Dev 2025-10-15 18:24:43 +03:00
parent 2f975d8e74
commit 411b2e4bb3
10 changed files with 203 additions and 138 deletions

1
.gitignore vendored
View file

@ -60,3 +60,4 @@ src/ginpee.toml
src/tetris.log src/tetris.log
src/high_score.txt src/high_score.txt
src/build/high_score.txt src/build/high_score.txt
code-samples/frogger/project.md

View file

@ -2,7 +2,7 @@
# Автоопределение компилятора и флагов # Автоопределение компилятора и флагов
CC ?= gcc CC ?= gcc
CFLAGS ?= -Wall -Wextra -std=c11 -g CFLAGS ?= -Wall -Wextra -std=c11 -g -D_POSIX_C_SOURCE=199309L
CHECK_CFLAGS ?= -I/usr/include/check CHECK_CFLAGS ?= -I/usr/include/check
# Проверяем наличие библиотек # Проверяем наличие библиотек

View file

@ -1,12 +1,12 @@
#ifndef AUTOMATO_H #ifndef AUTOMATO_H
#define AUTOMATO_H #define AUTOMATO_H
#define _POSIX_C_SOURCE 199309L // Добавляем здесь для POSIX #define _POSIX_C_SOURCE 199309L
#include "00_tetris.h" #include "00_tetris.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> // Для clock_gettime #include <time.h>
typedef enum { typedef enum {
Init, Init,
@ -50,8 +50,11 @@ typedef struct {
Moving_t moving_type; Moving_t moving_type;
int field[FIELD_HEIGHT][FIELD_WIDTH]; int field[FIELD_HEIGHT][FIELD_WIDTH];
GameInfo_t* info; GameInfo_t* info;
long long last_move_time; // Время последнего движения (мс) long long last_move_time;
long long pause_start_time; // Время начала паузы (мс) long long pause_start_time;
long long attach_start_time; // Время начала attach
int attach_completed; // Флаг завершения attach (фигура размещена)
int down_key_was_released;
} GameState_t; } GameState_t;
GameState_t* get_game_state(void); GameState_t* get_game_state(void);
@ -61,9 +64,8 @@ void do_init(void);
int load_high_score(); int load_high_score();
void save_high_score(int score); void save_high_score(int score);
void generate_next_figure(void); void generate_next_figure(void);
void terminate_and_free(void); // Добавляем прототип здесь void terminate_and_free(void);
// Вспомогательная функция для времени
long long get_current_time_ms(void); long long get_current_time_ms(void);
// spawn // spawn

View file

@ -4,63 +4,87 @@ void userInput(UserAction_t action, bool hold) {
(void)hold; (void)hold;
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
if (state->info->pause && int should_process = 1;
(action == Left || action == Right || action == Down || action == Up ||
action == Action || action == Start)) { // Проверка паузы
return; if (state->info->pause) {
if (action == Left || action == Right || action == Down ||
action == Up || action == Action || action == Start) {
should_process = 0;
}
}
// Блокируем движения во время Attaching (до завершения задержки)
if (state->state == Attaching && !state->attach_completed) {
if (action == Left || action == Right || action == Down ||
action == Up || action == Action) {
should_process = 0;
}
} }
switch (action) { if (should_process) {
case Start: switch (action) {
if (state->info->score >= state->info->high_score) { case Start:
state->info->high_score = state->info->score; if (state->info->score >= state->info->high_score) {
save_high_score(state->info->high_score); state->info->high_score = state->info->score;
} save_high_score(state->info->high_score);
state->info->high_score = load_high_score(); }
state->state = Init; state->info->high_score = load_high_score();
break; state->state = Init;
case Terminate: state->down_key_was_released = 1;
if (state->info->score > state->info->high_score) { break;
state->info->high_score = state->info->score; case Terminate:
save_high_score(state->info->high_score); if (state->info->score > state->info->high_score) {
} state->info->high_score = state->info->score;
terminate_and_free(); // Освобождаем память здесь - единственное место save_high_score(state->info->high_score);
state->state = GameOver; }
break; terminate_and_free();
case Left: state->state = GameOver;
state->state = Moving; break;
state->moving_type = LeftDown; case Left:
break; state->state = Moving;
case Right: state->moving_type = LeftDown;
state->state = Moving; break;
state->moving_type = RightDown; case Right:
break; state->state = Moving;
case Action: state->moving_type = RightDown;
state->state = Moving; break;
state->moving_type = Rotate; case Action:
break; state->state = Moving;
case Down: state->moving_type = Rotate;
state->state = Moving; break;
state->moving_type = ToDown; case Down:
break; if (state->down_key_was_released) {
case Pause: state->state = Moving;
if (!state->info->pause) { state->moving_type = ToDown;
state->pause_start_time = get_current_time_ms(); state->down_key_was_released = 0;
} else { }
long long pause_duration = get_current_time_ms() - state->pause_start_time; break;
state->last_move_time += pause_duration; case Up:
} state->down_key_was_released = 1;
state->info->pause = !state->info->pause; break;
break; case Pause:
default: if (!state->info->pause) {
break; state->pause_start_time = get_current_time_ms();
} else {
long long pause_duration = get_current_time_ms() - state->pause_start_time;
state->last_move_time += pause_duration;
// Корректируем attach_start_time если мы в Attaching
state->attach_start_time += (state->state == Attaching) * pause_duration;
}
state->info->pause = !state->info->pause;
break;
default:
break;
}
} }
} }
GameInfo_t updateCurrentState() { GameInfo_t updateCurrentState() {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
if (!state->info->pause || state->state == GameOver) { int should_update = (!state->info->pause || state->state == GameOver);
if (should_update) {
switch (state->state) { switch (state->state) {
case Init: case Init:
do_init(); do_init();

View file

@ -47,6 +47,9 @@ GameState_t* get_game_state(void) {
state.info->pause = 0; state.info->pause = 0;
state.last_move_time = get_current_time_ms(); state.last_move_time = get_current_time_ms();
state.pause_start_time = 0; state.pause_start_time = 0;
state.attach_start_time = 0;
state.attach_completed = 0;
state.down_key_was_released = 1;
state.info->high_score = load_high_score(); state.info->high_score = load_high_score();
state.state = GameOver; state.state = GameOver;

View file

@ -13,19 +13,25 @@ void generate_next_figure(void) {
state->next.sprite = rand() % FIGURE_COUNT; state->next.sprite = rand() % FIGURE_COUNT;
state->next.rotation = 0; state->next.rotation = 0;
const int (*shape)[4] = get_figure_shape(state->next.sprite, 0); const int (*shape)[4] = get_figure_shape(state->next.sprite, 0);
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) for (int j = 0; j < 4; ++j) {
state->next.mtrx[i][j] = shape[i][j]; state->next.mtrx[i][j] = shape[i][j];
}
}
} }
void do_spawn(void) { void do_spawn(void) {
GameState_t* state = get_game_state();
set_current_figure_from_next(); set_current_figure_from_next();
generate_next_figure(); generate_next_figure();
if (check_collision()) { int has_collision = check_collision();
get_game_state()->state = GameOver;
return; // TODO if (has_collision) {
state->state = GameOver;
} else {
state->state = Move;
state->down_key_was_released = 1;
} }
}
get_game_state()->state = Move;
}

View file

@ -2,14 +2,16 @@
int get_milliseconds_to_wait(void) { int get_milliseconds_to_wait(void) {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
int result = 0;
if (state->moving_type == ToDown) { if (state->moving_type == ToDown) {
return 30; result = 30;
} else {
int base_delay = 1100 - (state->info->speed * 100);
result = (base_delay > 100) ? base_delay : 100;
} }
// Скорость от 1 до 10: 1000ms -> 100ms return result;
int base_delay = 1100 - (state->info->speed * 100);
return base_delay > 100 ? base_delay : 100;
} }
void do_move(void) { void do_move(void) {
@ -18,15 +20,17 @@ void do_move(void) {
long long current_time = get_current_time_ms(); long long current_time = get_current_time_ms();
int ms_to_wait = get_milliseconds_to_wait(); int ms_to_wait = get_milliseconds_to_wait();
if (current_time - state->last_move_time < ms_to_wait) { int should_move = (current_time - state->last_move_time >= ms_to_wait);
return;
}
state->last_move_time = current_time; if (should_move) {
state->last_move_time = current_time;
state->curr.y++;
if (check_collision()) { state->curr.y++;
state->curr.y--; int has_collision = check_collision();
state->state = Attaching;
if (has_collision) {
state->curr.y--;
state->state = Attaching;
}
} }
} }

View file

@ -2,35 +2,60 @@
void do_attaching(void) { void do_attaching(void) {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
place_figure(); long long current_time = get_current_time_ms();
clear_lines();
if (is_game_over()) { // Если только что вошли в Attaching - размещаем фигуру и запускаем таймер
state->state = GameOver; if (!state->attach_completed) {
} else { // Первый вход в Attaching
state->state = Spawn; if (state->attach_start_time == 0) {
place_figure();
clear_lines();
state->attach_start_time = current_time;
state->attach_completed = 0;
}
// Проверяем, прошло ли 350мс
if (current_time - state->attach_start_time >= 350) {
state->attach_completed = 1;
state->attach_start_time = 0; // Сбрасываем таймер
// Проверяем game over и переходим
int game_over = is_game_over();
if (game_over) {
state->state = GameOver;
} else {
state->state = Spawn;
}
state->attach_completed = 0; // Сбрасываем флаг для следующего attach
}
// Иначе остаёмся в Attaching и ждём
} }
} }
int check_collision() { int check_collision() {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
Figure_t* fig = &state->curr; Figure_t* fig = &state->curr;
int collision_detected = 0;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4 && !collision_detected; ++i) {
for (int j = 0; j < 4; ++j) { for (int j = 0; j < 4 && !collision_detected; ++j) {
if (fig->mtrx[i][j]) { if (fig->mtrx[i][j]) {
int x = fig->x + j; int x = fig->x + j;
int y = fig->y + i; int y = fig->y + i;
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) { int out_of_bounds = (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT);
return 1; // TODO int hits_placed_block = (y >= 0 && state->field[y][x]);
}
if (y >= 0 && state->field[y][x]) { if (out_of_bounds || hits_placed_block) {
return 1; // TODO collision_detected = 1;
} }
} }
} }
} }
return 0;
return collision_detected;
} }
void place_figure() { void place_figure() {
@ -43,7 +68,7 @@ void place_figure() {
int x = fig->x + j; int x = fig->x + j;
int y = fig->y + i; int y = fig->y + i;
if (y >= 0 && y < FIELD_HEIGHT && x >= 0 && x < FIELD_WIDTH) { if (y >= 0 && y < FIELD_HEIGHT && x >= 0 && x < FIELD_WIDTH) {
state->field[y][x] = 2; // закреплённая фигура state->field[y][x] = 2;
} }
} }
} }
@ -68,31 +93,36 @@ void clear_lines() {
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) { for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
int full = 1; int full = 1;
for (int j = 0; j < FIELD_WIDTH; ++j) { for (int j = 0; j < FIELD_WIDTH; ++j) {
if (state->field[i][j] != 2) { if (state->field[i][j] != 2) {
full = 0; full = 0;
break; // TODO
} }
} }
if (full) { if (full) {
shift_lines_down(i); shift_lines_down(i);
lines_cleared++; lines_cleared++;
i++; // Check the same row again after shifting i++;
} }
} }
if (lines_cleared > 0) { if (lines_cleared > 0) {
int points[] = {0, 100, 300, 700, 1500}; int points[] = {0, 100, 300, 700, 1500};
state->info->score += points[lines_cleared]; state->info->score += points[lines_cleared];
if (state->info->score > state->info->high_score) { if (state->info->score > state->info->high_score) {
state->info->high_score = state->info->score; state->info->high_score = state->info->score;
} }
int new_level = (state->info->score / 600) + 1; int new_level = (state->info->score / 600) + 1;
if (new_level > 10) new_level = 10; if (new_level > 10) {
new_level = 10;
}
if (new_level > state->info->level) { if (new_level > state->info->level) {
state->info->level = new_level; state->info->level = new_level;
state->info->speed = new_level * 3; state->info->speed = new_level * 3;
} }
} }
} }

View file

@ -9,19 +9,22 @@ void do_gameover(void) {
} }
const int (*shape)[4] = empty_fig(); const int (*shape)[4] = empty_fig();
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) for (int j = 0; j < 4; ++j) {
state->next.mtrx[i][j] = shape[i][j]; state->next.mtrx[i][j] = shape[i][j];
}
}
} }
int is_game_over() { int is_game_over() {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
int game_over = 0;
for (int j = 0; j < FIELD_WIDTH; ++j) { for (int j = 0; j < FIELD_WIDTH; ++j) {
if (state->field[0][j] || state->field[1][j]) { if (state->field[0][j] || state->field[1][j]) {
return 1; // TODO game_over = 1;
} }
} }
return 0; return game_over;
} }

View file

@ -2,7 +2,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "../../brick_game/tetris/00_tetris.h" // Только этот хедер! #include "../../brick_game/tetris/00_tetris.h"
void display_game(GameInfo_t game_state); void display_game(GameInfo_t game_state);
@ -20,11 +20,12 @@ int main() {
refresh(); refresh();
int ch = 0; int ch = 0;
while (1) { int started = 0;
while (!started) {
ch = getch(); ch = getch();
if (ch == 'f' || ch == 'F') { if (ch == 'f' || ch == 'F') {
userInput(Start, false); userInput(Start, false);
break; started = 1;
} }
} }
@ -39,39 +40,30 @@ int main() {
ch = getch(); ch = getch();
action_valid = false; action_valid = false;
switch (ch) { if (ch == 'q') {
case 'q': userInput(Terminate, false);
userInput(Terminate, false); // Это освободит память через бэкенд running = false;
running = false; } else if (ch == 'r' || ch == ' ') {
break; current_action = Action;
case 'r': case ' ': action_valid = true;
current_action = Action; } else if (ch == KEY_LEFT) {
action_valid = true; current_action = Left;
break; action_valid = true;
case KEY_LEFT: } else if (ch == KEY_RIGHT) {
current_action = Left; current_action = Right;
action_valid = true; action_valid = true;
break; } else if (ch == KEY_DOWN) {
case KEY_RIGHT: current_action = Down;
current_action = Right; action_valid = true;
action_valid = true; } else if (ch == KEY_UP) {
break; current_action = Up;
case KEY_DOWN: action_valid = true;
current_action = Down; } else if (ch == 's' || ch == 'S') {
action_valid = true; current_action = Start;
break; action_valid = true;
case KEY_UP: } else if (ch == 'p' || ch == 'P') {
current_action = Up; current_action = Pause;
action_valid = true; action_valid = true;
break;
case 's': case 'S':
current_action = Start;
action_valid = true;
break;
case 'p': case 'P':
current_action = Pause;
action_valid = true;
break;
} }
if (action_valid) { if (action_valid) {