almost done
This commit is contained in:
parent
2f975d8e74
commit
411b2e4bb3
10 changed files with 203 additions and 138 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
# Проверяем наличие библиотек
|
# Проверяем наличие библиотек
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,25 @@ 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (should_process) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case Start:
|
case Start:
|
||||||
if (state->info->score >= state->info->high_score) {
|
if (state->info->score >= state->info->high_score) {
|
||||||
|
|
@ -18,13 +31,14 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
}
|
}
|
||||||
state->info->high_score = load_high_score();
|
state->info->high_score = load_high_score();
|
||||||
state->state = Init;
|
state->state = Init;
|
||||||
|
state->down_key_was_released = 1;
|
||||||
break;
|
break;
|
||||||
case Terminate:
|
case Terminate:
|
||||||
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;
|
||||||
save_high_score(state->info->high_score);
|
save_high_score(state->info->high_score);
|
||||||
}
|
}
|
||||||
terminate_and_free(); // Освобождаем память здесь - единственное место
|
terminate_and_free();
|
||||||
state->state = GameOver;
|
state->state = GameOver;
|
||||||
break;
|
break;
|
||||||
case Left:
|
case Left:
|
||||||
|
|
@ -40,8 +54,14 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
state->moving_type = Rotate;
|
state->moving_type = Rotate;
|
||||||
break;
|
break;
|
||||||
case Down:
|
case Down:
|
||||||
|
if (state->down_key_was_released) {
|
||||||
state->state = Moving;
|
state->state = Moving;
|
||||||
state->moving_type = ToDown;
|
state->moving_type = ToDown;
|
||||||
|
state->down_key_was_released = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Up:
|
||||||
|
state->down_key_was_released = 1;
|
||||||
break;
|
break;
|
||||||
case Pause:
|
case Pause:
|
||||||
if (!state->info->pause) {
|
if (!state->info->pause) {
|
||||||
|
|
@ -49,6 +69,8 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
} else {
|
} else {
|
||||||
long long pause_duration = get_current_time_ms() - state->pause_start_time;
|
long long pause_duration = get_current_time_ms() - state->pause_start_time;
|
||||||
state->last_move_time += pause_duration;
|
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;
|
state->info->pause = !state->info->pause;
|
||||||
break;
|
break;
|
||||||
|
|
@ -56,11 +78,13 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
break;
|
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();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
||||||
get_game_state()->state = Move;
|
if (has_collision) {
|
||||||
|
state->state = GameOver;
|
||||||
|
} else {
|
||||||
|
state->state = Move;
|
||||||
|
state->down_key_was_released = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (should_move) {
|
||||||
state->last_move_time = current_time;
|
state->last_move_time = current_time;
|
||||||
|
|
||||||
state->curr.y++;
|
state->curr.y++;
|
||||||
if (check_collision()) {
|
int has_collision = check_collision();
|
||||||
|
|
||||||
|
if (has_collision) {
|
||||||
state->curr.y--;
|
state->curr.y--;
|
||||||
state->state = Attaching;
|
state->state = Attaching;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,35 +2,60 @@
|
||||||
|
|
||||||
void do_attaching(void) {
|
void do_attaching(void) {
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
|
long long current_time = get_current_time_ms();
|
||||||
|
|
||||||
|
// Если только что вошли в Attaching - размещаем фигуру и запускаем таймер
|
||||||
|
if (!state->attach_completed) {
|
||||||
|
// Первый вход в Attaching
|
||||||
|
if (state->attach_start_time == 0) {
|
||||||
place_figure();
|
place_figure();
|
||||||
clear_lines();
|
clear_lines();
|
||||||
if (is_game_over()) {
|
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;
|
state->state = GameOver;
|
||||||
} else {
|
} else {
|
||||||
state->state = Spawn;
|
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,27 +93,32 @@ 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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
||||||
break;
|
} else if (ch == 'r' || ch == ' ') {
|
||||||
case 'r': case ' ':
|
|
||||||
current_action = Action;
|
current_action = Action;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
} else if (ch == KEY_LEFT) {
|
||||||
case KEY_LEFT:
|
|
||||||
current_action = Left;
|
current_action = Left;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
} else if (ch == KEY_RIGHT) {
|
||||||
case KEY_RIGHT:
|
|
||||||
current_action = Right;
|
current_action = Right;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
} else if (ch == KEY_DOWN) {
|
||||||
case KEY_DOWN:
|
|
||||||
current_action = Down;
|
current_action = Down;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
} else if (ch == KEY_UP) {
|
||||||
case KEY_UP:
|
|
||||||
current_action = Up;
|
current_action = Up;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
} else if (ch == 's' || ch == 'S') {
|
||||||
case 's': case 'S':
|
|
||||||
current_action = Start;
|
current_action = Start;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
} else if (ch == 'p' || ch == 'P') {
|
||||||
case 'p': case 'P':
|
|
||||||
current_action = Pause;
|
current_action = Pause;
|
||||||
action_valid = true;
|
action_valid = true;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action_valid) {
|
if (action_valid) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue