from frames speed to timespeed

This commit is contained in:
Rorikstr | Rust Dev 2025-10-15 15:59:56 +03:00
parent 7694d697e7
commit 2f975d8e74
7 changed files with 62 additions and 38 deletions

View file

@ -1,9 +1,12 @@
#ifndef AUTOMATO_H #ifndef AUTOMATO_H
#define AUTOMATO_H #define AUTOMATO_H
#define _POSIX_C_SOURCE 199309L // Добавляем здесь для POSIX
#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
typedef enum { typedef enum {
Init, Init,
@ -34,10 +37,10 @@ typedef enum {
} Sprite_t; } Sprite_t;
typedef struct { typedef struct {
int x, y; // Позиция фигуры на поле int x, y;
int mtrx[4][4]; // сама матрица int mtrx[4][4];
Sprite_t sprite; // Тип фигуры Sprite_t sprite;
int rotation; // Поворот (03) int rotation;
} Figure_t; } Figure_t;
typedef struct { typedef struct {
@ -47,18 +50,21 @@ 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 frame_count; // Общий счётчик кадров long long last_move_time; // Время последнего движения (мс)
long long last_move_frame; // Кадр, когда фигура последний раз двигалась long long pause_start_time; // Время начала паузы (мс)
} GameState_t; } GameState_t;
GameState_t* get_game_state(void); GameState_t* get_game_state(void);
// Функции состояний // Функции состояний
// init
void do_init(void); 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); // Добавляем прототип здесь
// Вспомогательная функция для времени
long long get_current_time_ms(void);
// spawn // spawn
void do_spawn(void); void do_spawn(void);
@ -82,7 +88,6 @@ int is_game_over();
// Функции фигур // Функции фигур
const int (*get_figure_shape(Sprite_t sprite, int rotation))[4]; const int (*get_figure_shape(Sprite_t sprite, int rotation))[4];
// Остальные фигуры...
const int (*i_fig_up())[4]; const int (*i_fig_up())[4];
const int (*i_fig_right())[4]; const int (*i_fig_right())[4];
const int (*i_fig_down())[4]; const int (*i_fig_down())[4];
@ -110,4 +115,4 @@ const int (*z_fig_down())[4];
const int (*z_fig_left())[4]; const int (*z_fig_left())[4];
const int (*empty_fig())[4]; const int (*empty_fig())[4];
#endif #endif

View file

@ -24,6 +24,7 @@ void userInput(UserAction_t action, bool hold) {
state->info->high_score = state->info->score; state->info->high_score = state->info->score;
save_high_score(state->info->high_score); save_high_score(state->info->high_score);
} }
terminate_and_free(); // Освобождаем память здесь - единственное место
state->state = GameOver; state->state = GameOver;
break; break;
case Left: case Left:
@ -43,6 +44,12 @@ void userInput(UserAction_t action, bool hold) {
state->moving_type = ToDown; state->moving_type = ToDown;
break; break;
case Pause: case Pause:
if (!state->info->pause) {
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;
}
state->info->pause = !state->info->pause; state->info->pause = !state->info->pause;
break; break;
default: default:
@ -52,8 +59,6 @@ void userInput(UserAction_t action, bool hold) {
GameInfo_t updateCurrentState() { GameInfo_t updateCurrentState() {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
state->frame_count++;
if (!state->info->pause || state->state == GameOver) { if (!state->info->pause || state->state == GameOver) {
switch (state->state) { switch (state->state) {
@ -91,7 +96,7 @@ GameInfo_t updateCurrentState() {
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->info->field[y][x] = 1; // активная фигура state->info->field[y][x] = 1;
} }
} }
} }
@ -104,4 +109,4 @@ GameInfo_t updateCurrentState() {
} }
return *state->info; return *state->info;
} }

View file

@ -1,5 +1,11 @@
#include "01_automato.h" #include "01_automato.h"
long long get_current_time_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
}
int load_high_score() { int load_high_score() {
FILE* file = fopen("build/high_score.txt", "r"); FILE* file = fopen("build/high_score.txt", "r");
int high_score = 0; int high_score = 0;
@ -35,12 +41,12 @@ GameState_t* get_game_state(void) {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
state.info->next[i] = malloc(4 * sizeof(int)); state.info->next[i] = malloc(4 * sizeof(int));
} }
state.info->speed = 10; state.info->speed = 1;
state.info->score = 0; state.info->score = 0;
state.info->level = 1; state.info->level = 1;
state.info->pause = 0; state.info->pause = 0;
state.frame_count = 0; state.last_move_time = get_current_time_ms();
state.last_move_frame = 0; state.pause_start_time = 0;
state.info->high_score = load_high_score(); state.info->high_score = load_high_score();
state.state = GameOver; state.state = GameOver;
@ -79,4 +85,4 @@ void terminate_and_free() {
free(state->info); free(state->info);
state->info = NULL; state->info = NULL;
} }
} }

View file

@ -11,12 +11,13 @@ void reset_game_stats(void) {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
state->info->score = 0; state->info->score = 0;
state->info->level = 1; state->info->level = 1;
state->info->speed = 10; state->info->speed = 1;
state->last_move_time = get_current_time_ms();
} }
void do_init(void) { void do_init(void) {
clear_field(); clear_field();
reset_game_stats(); reset_game_stats();
generate_next_figure(); generate_next_figure();
get_game_state()->state = Spawn; // Переход в Spawn get_game_state()->state = Spawn;
} }

View file

@ -1,28 +1,32 @@
#include "01_automato.h" #include "01_automato.h"
int get_frames_to_wait(void) { int get_milliseconds_to_wait(void) {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
if (state->moving_type == ToDown) { if (state->moving_type == ToDown) {
return 1; // TODO return 30;
} else {
return 1000 / state->info->speed; // TODO
} }
// Скорость от 1 до 10: 1000ms -> 100ms
int base_delay = 1100 - (state->info->speed * 100);
return base_delay > 100 ? base_delay : 100;
} }
void do_move(void) { void do_move(void) {
GameState_t* state = get_game_state(); GameState_t* state = get_game_state();
int frames_to_wait = get_frames_to_wait(); long long current_time = get_current_time_ms();
int ms_to_wait = get_milliseconds_to_wait();
if (state->frame_count - state->last_move_frame < frames_to_wait) {
return; // TODO if (current_time - state->last_move_time < ms_to_wait) {
return;
} }
state->last_move_frame = state->frame_count; state->last_move_time = current_time;
state->curr.y++; state->curr.y++;
if (check_collision()) { if (check_collision()) {
state->curr.y--; state->curr.y--;
state->state = Attaching; state->state = Attaching;
} }
} }

View file

@ -92,7 +92,7 @@ void clear_lines() {
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 * 10; state->info->speed = new_level * 3;
} }
} }
} }

View file

@ -1,10 +1,14 @@
#include <ncurses.h> #include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include "../../brick_game/tetris/00_tetris.h" #include <stdlib.h>
#include <time.h>
#include "../../brick_game/tetris/00_tetris.h" // Только этот хедер!
void display_game(GameInfo_t game_state); void display_game(GameInfo_t game_state);
int main() { int main() {
srand(time(NULL));
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
@ -12,7 +16,6 @@ int main() {
nodelay(stdscr, FALSE); nodelay(stdscr, FALSE);
curs_set(0); curs_set(0);
// Цикл ожидания нажатия F/f
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "Press F to Start"); mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "Press F to Start");
refresh(); refresh();
@ -38,7 +41,7 @@ int main() {
switch (ch) { switch (ch) {
case 'q': case 'q':
userInput(Terminate, false); userInput(Terminate, false); // Это освободит память через бэкенд
running = false; running = false;
break; break;
case 'r': case ' ': case 'r': case ' ':
@ -83,4 +86,4 @@ int main() {
endwin(); endwin();
return 0; return 0;
} }