searching speed issue
This commit is contained in:
parent
280cbee0a2
commit
98035f17a2
11 changed files with 69 additions and 64 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
#define TETRIS_H
|
#define TETRIS_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define FIELD_WIDTH 10
|
#define FIELD_WIDTH 10
|
||||||
#define FIELD_HEIGHT 20
|
#define FIELD_HEIGHT 20
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,8 @@ GameState_t* get_game_state(void);
|
||||||
// Функции состояний
|
// Функции состояний
|
||||||
// init
|
// init
|
||||||
void do_init(void);
|
void do_init(void);
|
||||||
|
int load_high_score();
|
||||||
|
void save_high_score(int score);
|
||||||
|
|
||||||
// spawn
|
// spawn
|
||||||
void do_spawn(void);
|
void do_spawn(void);
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,7 @@
|
||||||
#include "01_automato.h"
|
#include "01_automato.h"
|
||||||
#include "../../logging.h"
|
#include "../../logging.h"
|
||||||
|
|
||||||
int load_high_score() {
|
|
||||||
FILE* file = fopen("high_score.txt", "r");
|
|
||||||
int high_score = 0;
|
|
||||||
if (file) {
|
|
||||||
fscanf(file, "%d", &high_score);
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
return high_score;
|
|
||||||
}
|
|
||||||
|
|
||||||
void save_high_score(int score) {
|
|
||||||
FILE* file = fopen("high_score.txt", "w");
|
|
||||||
if (file) {
|
|
||||||
fprintf(file, "%d", score);
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void userInput(UserAction_t action, bool hold) {
|
void userInput(UserAction_t action, bool hold) {
|
||||||
LOG_FUNCTION_START("userInput", "action=%d, hold=%d", action, hold);
|
|
||||||
|
|
||||||
(void)hold; // заглушка
|
(void)hold; // заглушка
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
|
|
||||||
|
|
@ -29,7 +9,6 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
if (state->info->pause &&
|
if (state->info->pause &&
|
||||||
(action == Left || action == Right || action == Down || action == Up ||
|
(action == Left || action == Right || action == Down || action == Up ||
|
||||||
action == Action || action == Start)) {
|
action == Action || action == Start)) {
|
||||||
LOG_FUNCTION_END("userInput", "ignored movement command during pause, state=%d", state->state);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,8 +48,6 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_FUNCTION_END("userInput", "state=%d", state->state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameInfo_t updateCurrentState() {
|
GameInfo_t updateCurrentState() {
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,27 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "../../logging.h"
|
#include "../../logging.h"
|
||||||
|
|
||||||
GameState_t* get_game_state(void) {
|
int load_high_score() {
|
||||||
LOG_FUNCTION_START("get_game_state", "");
|
FILE* file = fopen("high_score.txt", "r");
|
||||||
|
int high_score = 0;
|
||||||
|
if (file) {
|
||||||
|
if (fscanf(file, "%d", &high_score) != 1) {
|
||||||
|
high_score = 0; // Если не удалось прочитать, устанавливаем 0
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
return high_score;
|
||||||
|
}
|
||||||
|
|
||||||
|
void save_high_score(int score) {
|
||||||
|
FILE* file = fopen("high_score.txt", "w");
|
||||||
|
if (file) {
|
||||||
|
fprintf(file, "%d", score);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState_t* get_game_state(void) {
|
||||||
static GameState_t state = {0};
|
static GameState_t state = {0};
|
||||||
static int initialized = 0;
|
static int initialized = 0;
|
||||||
|
|
||||||
|
|
@ -25,10 +43,11 @@ GameState_t* get_game_state(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Инициализируем начальные значения
|
// Инициализируем начальные значения
|
||||||
state.info->speed = 1;
|
state.info->speed = 100;
|
||||||
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.info->high_score = load_high_score(); // Загружаем рекорд
|
||||||
|
|
||||||
// Инициализируем следующую фигуру
|
// Инициализируем следующую фигуру
|
||||||
state.next.sprite = rand() % FIGURE_COUNT;
|
state.next.sprite = rand() % FIGURE_COUNT;
|
||||||
|
|
@ -42,7 +61,6 @@ GameState_t* get_game_state(void) {
|
||||||
initialized = 1;
|
initialized = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_FUNCTION_END("get_game_state", "state=%d", state.state);
|
|
||||||
return &state;
|
return &state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ void do_init(void) {
|
||||||
|
|
||||||
state->info->score = 0;
|
state->info->score = 0;
|
||||||
state->info->level = 1;
|
state->info->level = 1;
|
||||||
state->info->speed = 1;
|
state->info->speed = 100;
|
||||||
state->state = Spawn;
|
state->state = Spawn;
|
||||||
|
|
||||||
LOG_FUNCTION_END("do_init", "score=%d, level=%d, state=%d",
|
LOG_FUNCTION_END("do_init", "score=%d, level=%d, state=%d",
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@
|
||||||
#include "../../logging.h"
|
#include "../../logging.h"
|
||||||
|
|
||||||
void do_spawn(void) {
|
void do_spawn(void) {
|
||||||
LOG_FUNCTION_START("do_spawn", "");
|
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
|
|
||||||
// Устанавливаем текущую фигуру из следующей (или генерируем первую)
|
// Устанавливаем текущую фигуру из следующей (или генерируем первую)
|
||||||
|
|
@ -23,12 +21,8 @@ void do_spawn(void) {
|
||||||
// Проверка на GameOver
|
// Проверка на GameOver
|
||||||
if (check_collision()) {
|
if (check_collision()) {
|
||||||
state->state = GameOver;
|
state->state = GameOver;
|
||||||
LOG_FUNCTION_END("do_spawn", "collision detected, state=%d", state->state);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
state->state = Move;
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
@ -9,18 +9,19 @@ long long get_time_ms() {
|
||||||
|
|
||||||
void do_move(void) {
|
void do_move(void) {
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
LOG_FUNCTION_START("do_move", "");
|
LOG_FUNCTION_START("do_move", "speed=%d, moving_type=%d, current_pos=(%d,%d)",
|
||||||
|
state->info->speed, state->moving_type, state->curr.x, state->curr.y);
|
||||||
|
|
||||||
// Добавляем проверку, чтобы избежать деления на ноль
|
// Добавляем проверку, чтобы избежать деления на ноль
|
||||||
if (state->info->speed <= 0) {
|
if (state->info->speed <= 0) {
|
||||||
state->info->speed = 1; // Устанавливаем минимальное значение
|
state->info->speed = 100; // Устанавливаем минимальное значение
|
||||||
}
|
}
|
||||||
|
|
||||||
long long current_time = get_time_ms();
|
long long current_time = get_time_ms();
|
||||||
int delay = (state->moving_type == ToDown) ? 50 : (1000 / state->info->speed);
|
int delay = (state->moving_type == ToDown) ? 50 : (1000 / state->info->speed);
|
||||||
|
|
||||||
if (current_time - state->last_time < delay) {
|
if (current_time - state->last_time < delay) {
|
||||||
LOG_FUNCTION_END("do_move", "not enough time passed, delay=%d", delay);
|
LOG_FUNCTION_END("do_move", "not enough time passed, delay=%d ms", delay);
|
||||||
return; // ещё не время
|
return; // ещё не время
|
||||||
}
|
}
|
||||||
state->last_time = current_time;
|
state->last_time = current_time;
|
||||||
|
|
@ -32,6 +33,6 @@ void do_move(void) {
|
||||||
state->state = Attaching; // переход в Attaching
|
state->state = Attaching; // переход в Attaching
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_FUNCTION_END("do_move", "curr=(%d,%d), state=%d",
|
LOG_FUNCTION_END("do_move", "moved to (%d,%d), state=%d, delay=%d ms",
|
||||||
state->curr.x, state->curr.y, state->state);
|
state->curr.x, state->curr.y, state->state, delay);
|
||||||
}
|
}
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
#include "../../logging.h"
|
#include "../../logging.h"
|
||||||
|
|
||||||
void do_attaching(void) {
|
void do_attaching(void) {
|
||||||
LOG_FUNCTION_START("do_attaching", "");
|
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
// Закрепляем фигуру на поле
|
// Закрепляем фигуру на поле
|
||||||
place_figure();
|
place_figure();
|
||||||
|
|
@ -12,7 +10,6 @@ void do_attaching(void) {
|
||||||
clear_lines();
|
clear_lines();
|
||||||
|
|
||||||
// Проверяем GameOver
|
// Проверяем GameOver
|
||||||
int is_gameov = 0;
|
|
||||||
if (is_game_over()) {
|
if (is_game_over()) {
|
||||||
state->state = GameOver;
|
state->state = GameOver;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -23,8 +20,6 @@ void do_attaching(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int check_collision() {
|
int check_collision() {
|
||||||
LOG_FUNCTION_START("check_collision", "");
|
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
Figure_t* fig = &state->curr;
|
Figure_t* fig = &state->curr;
|
||||||
|
|
||||||
|
|
@ -35,11 +30,9 @@ int check_collision() {
|
||||||
int y = fig->y + i;
|
int y = fig->y + i;
|
||||||
|
|
||||||
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
|
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; // коллизия
|
return 1; // коллизия
|
||||||
}
|
}
|
||||||
if (y >= 0 && state->field[y][x]) {
|
if (y >= 0 && state->field[y][x]) {
|
||||||
LOG_FUNCTION_END("check_collision", "collision with field, x=%d, y=%d", x, y);
|
|
||||||
return 1; // коллизия с другой фигурой
|
return 1; // коллизия с другой фигурой
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -53,7 +46,6 @@ int check_collision() {
|
||||||
void place_figure() {
|
void place_figure() {
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
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;
|
Figure_t* fig = &state->curr;
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
|
@ -67,14 +59,14 @@ void place_figure() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_FUNCTION_END("place_figure", "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_lines() {
|
void clear_lines() {
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
LOG_FUNCTION_START("clear_lines", "score=%d", state->info->score);
|
int old_level = state->info->level;
|
||||||
|
int old_speed = state->info->speed;
|
||||||
|
|
||||||
int lines_cleared = 0;
|
int lines_cleared = 0;
|
||||||
|
|
||||||
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
|
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
|
||||||
|
|
@ -104,13 +96,33 @@ void clear_lines() {
|
||||||
// Начисление очков
|
// Начисление очков
|
||||||
if (lines_cleared > 0) {
|
if (lines_cleared > 0) {
|
||||||
int points[] = {0, 100, 300, 700, 1500};
|
int points[] = {0, 100, 300, 700, 1500};
|
||||||
|
int old_score = state->info->score;
|
||||||
state->info->score += points[lines_cleared];
|
state->info->score += points[lines_cleared];
|
||||||
if (state->info->score / 600 > state->info->level - 1) {
|
|
||||||
state->info->level++;
|
// Обновляем рекорд, если нужно
|
||||||
state->info->speed = state->info->level;
|
if (state->info->score > state->info->high_score) {
|
||||||
|
state->info->high_score = state->info->score;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Увеличиваем уровень каждые 600 очков (максимум 10 уровней)
|
||||||
|
int new_level = (state->info->score / 600) + 1;
|
||||||
|
if (new_level > 10) new_level = 10;
|
||||||
|
|
||||||
|
if (new_level > state->info->level) {
|
||||||
|
state->info->level = new_level;
|
||||||
|
|
||||||
|
// СУПЕР-УСКОРЕНИЕ! В 50 раз быстрее!
|
||||||
|
state->info->speed = new_level * 50;
|
||||||
|
|
||||||
|
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d->%d, level=%d->%d, speed=%d->%d",
|
||||||
|
lines_cleared, old_score, state->info->score, old_level, state->info->level,
|
||||||
|
old_speed, state->info->speed);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d, level=%d",
|
// Добавим лог, даже если линии не очищались
|
||||||
lines_cleared, state->info->score, state->info->level);
|
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d->%d, level=%d->%d, speed=%d->%d",
|
||||||
|
lines_cleared, state->info->score, state->info->score, old_level, state->info->level,
|
||||||
|
old_speed, state->info->speed);
|
||||||
}
|
}
|
||||||
|
|
@ -2,9 +2,14 @@
|
||||||
#include "../../logging.h"
|
#include "../../logging.h"
|
||||||
|
|
||||||
void do_gameover(void) {
|
void do_gameover(void) {
|
||||||
LOG_FUNCTION_START("do_gameover", "");
|
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
|
|
||||||
|
// Сохраняем рекорд, если текущий рекорд побит
|
||||||
|
if (state->info->score > state->info->high_score) {
|
||||||
|
state->info->high_score = state->info->score;
|
||||||
|
save_high_score(state->info->high_score);
|
||||||
|
}
|
||||||
|
|
||||||
// Сброс next в пустую фигуру
|
// Сброс next в пустую фигуру
|
||||||
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)
|
||||||
|
|
@ -15,8 +20,6 @@ void do_gameover(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_game_over() {
|
int is_game_over() {
|
||||||
LOG_FUNCTION_START("is_game_over", "");
|
|
||||||
|
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
// Проверяем, есть ли блоки в верхних рядах
|
// Проверяем, есть ли блоки в верхних рядах
|
||||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||||
|
|
@ -26,6 +29,5 @@ int is_game_over() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_FUNCTION_END("is_game_over", "game not over");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -5,8 +5,6 @@
|
||||||
|
|
||||||
// display.c
|
// display.c
|
||||||
void display_game(GameInfo_t game_state) {
|
void display_game(GameInfo_t game_state) {
|
||||||
LOG_FUNCTION_START("display_game", "");
|
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
// Отображение игрового поля (всегда, даже во время паузы)
|
// Отображение игрового поля (всегда, даже во время паузы)
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ int main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
nodelay(stdscr, TRUE);
|
nodelay(stdscr, TRUE);
|
||||||
timeout(100);
|
timeout(10);
|
||||||
|
|
||||||
UserAction_t current_action = {0};
|
UserAction_t current_action = {0};
|
||||||
bool action_valid = false;
|
bool action_valid = false;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue