clang-format
This commit is contained in:
parent
ace9659ef5
commit
9e712ae326
12 changed files with 720 additions and 769 deletions
|
|
@ -35,7 +35,7 @@ TEST_TARGET = $(BUILDDIR)/test.out
|
|||
PREFIX ?= $(HOME)/.local
|
||||
BINDIR = $(PREFIX)/bin
|
||||
|
||||
all: $(TARGET)
|
||||
all: dvi dist install gcov_report run
|
||||
|
||||
run: clean $(TARGET)
|
||||
./$(TARGET)
|
||||
|
|
@ -100,14 +100,14 @@ dist: clean
|
|||
tar -czf tetris.tar.gz Makefile $(TETRISDIR) $(CLIDIR) $(TESTDIR) README.md doc.md
|
||||
|
||||
style:
|
||||
@if [ -f .clang-format ]; then \
|
||||
@if [ -f ../materials/linters/.clang-format ]; then \
|
||||
clang-format -n $(TETRIS_SRC) $(CLI_SRC); \
|
||||
else \
|
||||
echo ".clang-format not found"; \
|
||||
fi
|
||||
|
||||
format:
|
||||
@if [ -f .clang-format ]; then \
|
||||
@if [ -f ../materials/linters/.clang-format ]; then \
|
||||
clang-format -i $(TETRIS_SRC) $(CLI_SRC); \
|
||||
else \
|
||||
echo ".clang-format not found"; \
|
||||
|
|
|
|||
|
|
@ -1,136 +1,138 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
void userInput(UserAction_t action, bool hold) {
|
||||
(void)hold;
|
||||
GameState_t* state = get_game_state();
|
||||
(void)hold;
|
||||
GameState_t *state = get_game_state();
|
||||
|
||||
int should_process = 1;
|
||||
|
||||
// Проверка паузы
|
||||
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;
|
||||
}
|
||||
}
|
||||
int should_process = 1;
|
||||
|
||||
if (should_process) {
|
||||
switch (action) {
|
||||
case Start:
|
||||
if (state->info->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->down_key_was_released = 1;
|
||||
break;
|
||||
case Terminate:
|
||||
if (state->info->score > state->info->high_score) {
|
||||
state->info->high_score = state->info->score;
|
||||
save_high_score(state->info->high_score);
|
||||
}
|
||||
terminate_and_free();
|
||||
state->state = GameOver;
|
||||
break;
|
||||
case Left:
|
||||
state->state = Moving;
|
||||
state->moving_type = LeftDown;
|
||||
break;
|
||||
case Right:
|
||||
state->state = Moving;
|
||||
state->moving_type = RightDown;
|
||||
break;
|
||||
case Action:
|
||||
state->state = Moving;
|
||||
state->moving_type = Rotate;
|
||||
break;
|
||||
case Down:
|
||||
if (state->down_key_was_released) {
|
||||
state->state = Moving;
|
||||
state->moving_type = ToDown;
|
||||
state->down_key_was_released = 0;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
state->down_key_was_released = 1;
|
||||
break;
|
||||
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;
|
||||
// Корректируем attach_start_time если мы в Attaching
|
||||
state->attach_start_time += (state->state == Attaching) * pause_duration;
|
||||
}
|
||||
state->info->pause = !state->info->pause;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// Проверка паузы
|
||||
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) {
|
||||
case Start:
|
||||
if (state->info->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->down_key_was_released = 1;
|
||||
break;
|
||||
case Terminate:
|
||||
if (state->info->score > state->info->high_score) {
|
||||
state->info->high_score = state->info->score;
|
||||
save_high_score(state->info->high_score);
|
||||
}
|
||||
terminate_and_free();
|
||||
state->state = GameOver;
|
||||
break;
|
||||
case Left:
|
||||
state->state = Moving;
|
||||
state->moving_type = LeftDown;
|
||||
break;
|
||||
case Right:
|
||||
state->state = Moving;
|
||||
state->moving_type = RightDown;
|
||||
break;
|
||||
case Action:
|
||||
state->state = Moving;
|
||||
state->moving_type = Rotate;
|
||||
break;
|
||||
case Down:
|
||||
if (state->down_key_was_released) {
|
||||
state->state = Moving;
|
||||
state->moving_type = ToDown;
|
||||
state->down_key_was_released = 0;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
state->down_key_was_released = 1;
|
||||
break;
|
||||
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;
|
||||
// Корректируем 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() {
|
||||
GameState_t* state = get_game_state();
|
||||
|
||||
int should_update = (!state->info->pause || state->state == GameOver);
|
||||
if (should_update) {
|
||||
switch (state->state) {
|
||||
case Init:
|
||||
do_init();
|
||||
break;
|
||||
case Spawn:
|
||||
do_spawn();
|
||||
break;
|
||||
case Move:
|
||||
do_move();
|
||||
break;
|
||||
case Moving:
|
||||
do_moving();
|
||||
break;
|
||||
case Attaching:
|
||||
do_attaching();
|
||||
break;
|
||||
case GameOver:
|
||||
do_gameover();
|
||||
break;
|
||||
}
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
|
||||
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];
|
||||
}
|
||||
int should_update = (!state->info->pause || state->state == GameOver);
|
||||
if (should_update) {
|
||||
switch (state->state) {
|
||||
case Init:
|
||||
do_init();
|
||||
break;
|
||||
case Spawn:
|
||||
do_spawn();
|
||||
break;
|
||||
case Move:
|
||||
do_move();
|
||||
break;
|
||||
case Moving:
|
||||
do_moving();
|
||||
break;
|
||||
case Attaching:
|
||||
do_attaching();
|
||||
break;
|
||||
case GameOver:
|
||||
do_gameover();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
state->info->next[i][j] = state->next.mtrx[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return *state->info;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
state->info->next[i][j] = state->next.mtrx[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return *state->info;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,91 +1,91 @@
|
|||
#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;
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
|
||||
}
|
||||
|
||||
int load_high_score() {
|
||||
FILE* file = fopen("build/high_score.txt", "r");
|
||||
int high_score = 0;
|
||||
if (file) {
|
||||
if (fscanf(file, "%d", &high_score) != 1) {
|
||||
high_score = 0;
|
||||
}
|
||||
fclose(file);
|
||||
FILE *file = fopen("build/high_score.txt", "r");
|
||||
int high_score = 0;
|
||||
if (file) {
|
||||
if (fscanf(file, "%d", &high_score) != 1) {
|
||||
high_score = 0;
|
||||
}
|
||||
return high_score;
|
||||
fclose(file);
|
||||
}
|
||||
return high_score;
|
||||
}
|
||||
|
||||
void save_high_score(int score) {
|
||||
FILE* file = fopen("build/high_score.txt", "w");
|
||||
if (file) {
|
||||
fprintf(file, "%d", score);
|
||||
fclose(file);
|
||||
}
|
||||
FILE *file = fopen("build/high_score.txt", "w");
|
||||
if (file) {
|
||||
fprintf(file, "%d", score);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
GameState_t* get_game_state(void) {
|
||||
static GameState_t state = {0};
|
||||
static int initialized = 0;
|
||||
GameState_t *get_game_state(void) {
|
||||
static GameState_t state = {0};
|
||||
static int initialized = 0;
|
||||
|
||||
if (!initialized) {
|
||||
state.info = malloc(sizeof(GameInfo_t));
|
||||
state.info->field = malloc(FIELD_HEIGHT * sizeof(int*));
|
||||
for (int i = 0; i < FIELD_HEIGHT; i++) {
|
||||
state.info->field[i] = malloc(FIELD_WIDTH * sizeof(int));
|
||||
}
|
||||
|
||||
state.info->next = malloc(4 * sizeof(int*));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
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.last_move_time = get_current_time_ms();
|
||||
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.state = GameOver;
|
||||
initialized = 1;
|
||||
if (!initialized) {
|
||||
state.info = malloc(sizeof(GameInfo_t));
|
||||
state.info->field = malloc(FIELD_HEIGHT * sizeof(int *));
|
||||
for (int i = 0; i < FIELD_HEIGHT; i++) {
|
||||
state.info->field[i] = malloc(FIELD_WIDTH * sizeof(int));
|
||||
}
|
||||
|
||||
return &state;
|
||||
|
||||
state.info->next = malloc(4 * sizeof(int *));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
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.last_move_time = get_current_time_ms();
|
||||
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.state = GameOver;
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
return &state;
|
||||
}
|
||||
|
||||
void terminate_and_free() {
|
||||
GameState_t* state = get_game_state();
|
||||
GameState_t *state = get_game_state();
|
||||
|
||||
if (state->info) {
|
||||
if (state->info->field != NULL) {
|
||||
for (int i = 0; i < FIELD_HEIGHT; i++) {
|
||||
if (state->info->field[i] != NULL) {
|
||||
free(state->info->field[i]);
|
||||
state->info->field[i] = NULL;
|
||||
}
|
||||
}
|
||||
free(state->info->field);
|
||||
state->info->field = NULL;
|
||||
if (state->info) {
|
||||
if (state->info->field != NULL) {
|
||||
for (int i = 0; i < FIELD_HEIGHT; i++) {
|
||||
if (state->info->field[i] != NULL) {
|
||||
free(state->info->field[i]);
|
||||
state->info->field[i] = NULL;
|
||||
}
|
||||
|
||||
if (state->info->next != NULL) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (state->info->next[i] != NULL) {
|
||||
free(state->info->next[i]);
|
||||
state->info->next[i] = NULL;
|
||||
}
|
||||
}
|
||||
free(state->info->next);
|
||||
state->info->next = NULL;
|
||||
}
|
||||
|
||||
free(state->info);
|
||||
state->info = NULL;
|
||||
}
|
||||
free(state->info->field);
|
||||
state->info->field = NULL;
|
||||
}
|
||||
|
||||
if (state->info->next != NULL) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (state->info->next[i] != NULL) {
|
||||
free(state->info->next[i]);
|
||||
state->info->next[i] = NULL;
|
||||
}
|
||||
}
|
||||
free(state->info->next);
|
||||
state->info->next = NULL;
|
||||
}
|
||||
|
||||
free(state->info);
|
||||
state->info = NULL;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
void clear_field(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
for (int i = 0; i < FIELD_HEIGHT; ++i)
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j)
|
||||
state->field[i][j] = 0;
|
||||
GameState_t *state = get_game_state();
|
||||
for (int i = 0; i < FIELD_HEIGHT; ++i)
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j)
|
||||
state->field[i][j] = 0;
|
||||
}
|
||||
|
||||
void reset_game_stats(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
state->info->score = 0;
|
||||
state->info->level = 1;
|
||||
state->info->speed = 1;
|
||||
state->last_move_time = get_current_time_ms();
|
||||
GameState_t *state = get_game_state();
|
||||
state->info->score = 0;
|
||||
state->info->level = 1;
|
||||
state->info->speed = 1;
|
||||
state->last_move_time = get_current_time_ms();
|
||||
}
|
||||
|
||||
void do_init(void) {
|
||||
clear_field();
|
||||
reset_game_stats();
|
||||
generate_next_figure();
|
||||
get_game_state()->state = Spawn;
|
||||
clear_field();
|
||||
reset_game_stats();
|
||||
generate_next_figure();
|
||||
get_game_state()->state = Spawn;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,37 +1,37 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
void set_current_figure_from_next(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
state->curr = state->next;
|
||||
state->curr.x = FIELD_WIDTH / 2 - 2;
|
||||
state->curr.y = 0;
|
||||
state->moving_type = DoNothing;
|
||||
GameState_t *state = get_game_state();
|
||||
state->curr = state->next;
|
||||
state->curr.x = FIELD_WIDTH / 2 - 2;
|
||||
state->curr.y = 0;
|
||||
state->moving_type = DoNothing;
|
||||
}
|
||||
|
||||
void generate_next_figure(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
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];
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void do_spawn(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
|
||||
set_current_figure_from_next();
|
||||
generate_next_figure();
|
||||
|
||||
int has_collision = check_collision();
|
||||
|
||||
if (has_collision) {
|
||||
state->state = GameOver;
|
||||
} else {
|
||||
state->state = Move;
|
||||
state->down_key_was_released = 1;
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
|
||||
set_current_figure_from_next();
|
||||
generate_next_figure();
|
||||
|
||||
int has_collision = check_collision();
|
||||
|
||||
if (has_collision) {
|
||||
state->state = GameOver;
|
||||
} else {
|
||||
state->state = Move;
|
||||
state->down_key_was_released = 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,38 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
int get_milliseconds_to_wait(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
int result = 0;
|
||||
|
||||
if (state->moving_type == ToDown) {
|
||||
result = INSTANT_DROP_DELAY_MS;
|
||||
} else {
|
||||
int base_delay = BASE_FALL_DELAY_MS - (state->info->speed * SPEED_MULTIPLIER_MS);
|
||||
result = (base_delay > SPEED_MULTIPLIER_MS) ? base_delay : SPEED_MULTIPLIER_MS;
|
||||
}
|
||||
|
||||
return result;
|
||||
GameState_t *state = get_game_state();
|
||||
int result = 0;
|
||||
|
||||
if (state->moving_type == ToDown) {
|
||||
result = INSTANT_DROP_DELAY_MS;
|
||||
} else {
|
||||
int base_delay =
|
||||
BASE_FALL_DELAY_MS - (state->info->speed * SPEED_MULTIPLIER_MS);
|
||||
result =
|
||||
(base_delay > SPEED_MULTIPLIER_MS) ? base_delay : SPEED_MULTIPLIER_MS;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void do_move(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
|
||||
long long current_time = get_current_time_ms();
|
||||
int ms_to_wait = get_milliseconds_to_wait();
|
||||
|
||||
int should_move = (current_time - state->last_move_time >= ms_to_wait);
|
||||
|
||||
if (should_move) {
|
||||
state->last_move_time = current_time;
|
||||
|
||||
state->curr.y++;
|
||||
int has_collision = check_collision();
|
||||
|
||||
if (has_collision) {
|
||||
state->curr.y--;
|
||||
state->state = Attaching;
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
|
||||
long long current_time = get_current_time_ms();
|
||||
int ms_to_wait = get_milliseconds_to_wait();
|
||||
|
||||
int should_move = (current_time - state->last_move_time >= ms_to_wait);
|
||||
|
||||
if (should_move) {
|
||||
state->last_move_time = current_time;
|
||||
|
||||
state->curr.y++;
|
||||
int has_collision = check_collision();
|
||||
|
||||
if (has_collision) {
|
||||
state->curr.y--;
|
||||
state->state = Attaching;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,73 +1,74 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
void handle_move_direction(Moving_t direction) {
|
||||
GameState_t* state = get_game_state();
|
||||
switch (direction) {
|
||||
case LeftDown:
|
||||
state->curr.x--;
|
||||
break;
|
||||
case RightDown:
|
||||
state->curr.x++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
switch (direction) {
|
||||
case LeftDown:
|
||||
state->curr.x--;
|
||||
break;
|
||||
case RightDown:
|
||||
state->curr.x++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_rotate(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
state->curr.rotation = (state->curr.rotation + 1) % 4;
|
||||
const int (*shape)[4] = get_figure_shape(state->curr.sprite, state->curr.rotation);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
state->curr.mtrx[i][j] = shape[i][j];
|
||||
GameState_t *state = get_game_state();
|
||||
state->curr.rotation = (state->curr.rotation + 1) % 4;
|
||||
const int(*shape)[4] =
|
||||
get_figure_shape(state->curr.sprite, state->curr.rotation);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
state->curr.mtrx[i][j] = shape[i][j];
|
||||
}
|
||||
|
||||
void handle_horizontal_rotate_move(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
Figure_t old = state->curr;
|
||||
|
||||
switch (state->moving_type) {
|
||||
case LeftDown:
|
||||
case RightDown:
|
||||
handle_move_direction(state->moving_type);
|
||||
break;
|
||||
case Rotate:
|
||||
handle_rotate();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (check_collision()) {
|
||||
state->curr = old;
|
||||
}
|
||||
state->state = Move;
|
||||
GameState_t *state = get_game_state();
|
||||
Figure_t old = state->curr;
|
||||
|
||||
switch (state->moving_type) {
|
||||
case LeftDown:
|
||||
case RightDown:
|
||||
handle_move_direction(state->moving_type);
|
||||
break;
|
||||
case Rotate:
|
||||
handle_rotate();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (check_collision()) {
|
||||
state->curr = old;
|
||||
}
|
||||
state->state = Move;
|
||||
}
|
||||
|
||||
void handle_to_down_move(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
while (!check_collision()) {
|
||||
state->curr.y++;
|
||||
}
|
||||
state->curr.y--;
|
||||
state->state = Attaching;
|
||||
GameState_t *state = get_game_state();
|
||||
while (!check_collision()) {
|
||||
state->curr.y++;
|
||||
}
|
||||
state->curr.y--;
|
||||
state->state = Attaching;
|
||||
}
|
||||
|
||||
void do_moving(void) {
|
||||
GameState_t* state = get_game_state();
|
||||
GameState_t *state = get_game_state();
|
||||
|
||||
switch (state->moving_type) {
|
||||
case LeftDown:
|
||||
case RightDown:
|
||||
case Rotate:
|
||||
handle_horizontal_rotate_move();
|
||||
break;
|
||||
case ToDown:
|
||||
handle_to_down_move();
|
||||
break;
|
||||
case DoNothing:
|
||||
state->state = Move;
|
||||
break;
|
||||
}
|
||||
switch (state->moving_type) {
|
||||
case LeftDown:
|
||||
case RightDown:
|
||||
case Rotate:
|
||||
handle_horizontal_rotate_move();
|
||||
break;
|
||||
case ToDown:
|
||||
handle_to_down_move();
|
||||
break;
|
||||
case DoNothing:
|
||||
state->state = Move;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,128 +1,129 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
void do_attaching(void) {
|
||||
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();
|
||||
clear_lines();
|
||||
state->attach_start_time = current_time;
|
||||
state->attach_completed = 0;
|
||||
}
|
||||
|
||||
// Проверяем, прошло ли 350мс
|
||||
if (current_time - state->attach_start_time >= ATTACH_DELAY_MS) {
|
||||
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 и ждём
|
||||
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();
|
||||
clear_lines();
|
||||
state->attach_start_time = current_time;
|
||||
state->attach_completed = 0;
|
||||
}
|
||||
|
||||
// Проверяем, прошло ли 350мс
|
||||
if (current_time - state->attach_start_time >= ATTACH_DELAY_MS) {
|
||||
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() {
|
||||
GameState_t* state = get_game_state();
|
||||
Figure_t* fig = &state->curr;
|
||||
int collision_detected = 0;
|
||||
GameState_t *state = get_game_state();
|
||||
Figure_t *fig = &state->curr;
|
||||
int collision_detected = 0;
|
||||
|
||||
for (int i = 0; i < 4 && !collision_detected; ++i) {
|
||||
for (int j = 0; j < 4 && !collision_detected; ++j) {
|
||||
if (fig->mtrx[i][j]) {
|
||||
int x = fig->x + j;
|
||||
int y = fig->y + i;
|
||||
for (int i = 0; i < 4 && !collision_detected; ++i) {
|
||||
for (int j = 0; j < 4 && !collision_detected; ++j) {
|
||||
if (fig->mtrx[i][j]) {
|
||||
int x = fig->x + j;
|
||||
int y = fig->y + i;
|
||||
|
||||
int out_of_bounds = (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT);
|
||||
int hits_placed_block = (y >= 0 && state->field[y][x]);
|
||||
|
||||
if (out_of_bounds || hits_placed_block) {
|
||||
collision_detected = 1;
|
||||
}
|
||||
}
|
||||
int out_of_bounds = (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT);
|
||||
int hits_placed_block = (y >= 0 && state->field[y][x]);
|
||||
|
||||
if (out_of_bounds || hits_placed_block) {
|
||||
collision_detected = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return collision_detected;
|
||||
}
|
||||
|
||||
return collision_detected;
|
||||
}
|
||||
|
||||
void place_figure() {
|
||||
GameState_t* state = get_game_state();
|
||||
Figure_t* fig = &state->curr;
|
||||
GameState_t *state = get_game_state();
|
||||
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->field[y][x] = 2;
|
||||
}
|
||||
}
|
||||
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->field[y][x] = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void shift_lines_down(int from_row) {
|
||||
GameState_t* state = get_game_state();
|
||||
for (int y = from_row; y > 0; --y) {
|
||||
for (int x = 0; x < FIELD_WIDTH; ++x) {
|
||||
state->field[y][x] = state->field[y - 1][x];
|
||||
}
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
for (int y = from_row; y > 0; --y) {
|
||||
for (int x = 0; x < FIELD_WIDTH; ++x) {
|
||||
state->field[0][x] = 0;
|
||||
state->field[y][x] = state->field[y - 1][x];
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < FIELD_WIDTH; ++x) {
|
||||
state->field[0][x] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void clear_lines() {
|
||||
GameState_t* state = get_game_state();
|
||||
int lines_cleared = 0;
|
||||
GameState_t *state = get_game_state();
|
||||
int lines_cleared = 0;
|
||||
|
||||
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
|
||||
int full = 1;
|
||||
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (state->field[i][j] != 2) {
|
||||
full = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (full) {
|
||||
shift_lines_down(i);
|
||||
lines_cleared++;
|
||||
i++;
|
||||
}
|
||||
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
|
||||
int full = 1;
|
||||
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (state->field[i][j] != 2) {
|
||||
full = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (lines_cleared > 0) {
|
||||
int points[] = {0, POINTS_ONE_LINE, POINTS_TWO_LINES, POINTS_THREE_LINES, POINTS_FOUR_LINES};
|
||||
state->info->score += points[lines_cleared];
|
||||
|
||||
if (state->info->score > state->info->high_score) {
|
||||
state->info->high_score = state->info->score;
|
||||
}
|
||||
|
||||
int new_level = (state->info->score / SCORE_PER_LEVEL) + 1;
|
||||
if (new_level > MAX_LEVEL) {
|
||||
new_level = MAX_LEVEL;
|
||||
}
|
||||
|
||||
if (new_level > state->info->level) {
|
||||
state->info->level = new_level;
|
||||
state->info->speed = new_level + (new_level / 2);
|
||||
}
|
||||
if (full) {
|
||||
shift_lines_down(i);
|
||||
lines_cleared++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (lines_cleared > 0) {
|
||||
int points[] = {0, POINTS_ONE_LINE, POINTS_TWO_LINES, POINTS_THREE_LINES,
|
||||
POINTS_FOUR_LINES};
|
||||
state->info->score += points[lines_cleared];
|
||||
|
||||
if (state->info->score > state->info->high_score) {
|
||||
state->info->high_score = state->info->score;
|
||||
}
|
||||
|
||||
int new_level = (state->info->score / SCORE_PER_LEVEL) + 1;
|
||||
if (new_level > MAX_LEVEL) {
|
||||
new_level = MAX_LEVEL;
|
||||
}
|
||||
|
||||
if (new_level > state->info->level) {
|
||||
state->info->level = new_level;
|
||||
state->info->speed = new_level + (new_level / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,30 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
void do_gameover(void) {
|
||||
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);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int is_game_over() {
|
||||
GameState_t* state = get_game_state();
|
||||
int game_over = 0;
|
||||
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (state->field[0][j] || state->field[1][j]) {
|
||||
game_over = 1;
|
||||
}
|
||||
GameState_t *state = get_game_state();
|
||||
int game_over = 0;
|
||||
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (state->field[0][j] || state->field[1][j]) {
|
||||
game_over = 1;
|
||||
}
|
||||
|
||||
return game_over;
|
||||
}
|
||||
|
||||
return game_over;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,327 +1,272 @@
|
|||
#include "01_automato.h"
|
||||
|
||||
const int (*get_figure_shape(Sprite_t sprite, int rotation))[4] {
|
||||
const int (*result)[4] = NULL;
|
||||
switch (sprite) {
|
||||
case I:
|
||||
switch (rotation % 4) {
|
||||
case 0: result = i_fig_up(); break;
|
||||
case 1: result = i_fig_right(); break;
|
||||
case 2: result = i_fig_down(); break;
|
||||
case 3: result = i_fig_left(); break;
|
||||
}
|
||||
break;
|
||||
case O:
|
||||
result = o_fig();
|
||||
break;
|
||||
case T:
|
||||
switch (rotation % 4) {
|
||||
case 0: result = t_fig_up(); break;
|
||||
case 1: result = t_fig_right(); break;
|
||||
case 2: result = t_fig_down(); break;
|
||||
case 3: result = t_fig_left(); break;
|
||||
}
|
||||
break;
|
||||
case L:
|
||||
switch (rotation % 4) {
|
||||
case 0: result = l_fig_up(); break;
|
||||
case 1: result = l_fig_right(); break;
|
||||
case 2: result = l_fig_down(); break;
|
||||
case 3: result = l_fig_left(); break;
|
||||
}
|
||||
break;
|
||||
case J:
|
||||
switch (rotation % 4) {
|
||||
case 0: result = j_fig_up(); break;
|
||||
case 1: result = j_fig_right(); break;
|
||||
case 2: result = j_fig_down(); break;
|
||||
case 3: result = j_fig_left(); break;
|
||||
}
|
||||
break;
|
||||
case S:
|
||||
switch (rotation % 4) {
|
||||
case 0: result = s_fig_up(); break;
|
||||
case 1: result = s_fig_right(); break;
|
||||
case 2: result = s_fig_down(); break;
|
||||
case 3: result = s_fig_left(); break;
|
||||
}
|
||||
break;
|
||||
case Z:
|
||||
switch (rotation % 4) {
|
||||
case 0: result = z_fig_up(); break;
|
||||
case 1: result = z_fig_right(); break;
|
||||
case 2: result = z_fig_down(); break;
|
||||
case 3: result = z_fig_left(); break;
|
||||
}
|
||||
break;
|
||||
default: result = NULL;
|
||||
const int(*result)[4] = NULL;
|
||||
switch (sprite) {
|
||||
case I:
|
||||
switch (rotation % 4) {
|
||||
case 0:
|
||||
result = i_fig_up();
|
||||
break;
|
||||
case 1:
|
||||
result = i_fig_right();
|
||||
break;
|
||||
case 2:
|
||||
result = i_fig_down();
|
||||
break;
|
||||
case 3:
|
||||
result = i_fig_left();
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
break;
|
||||
case O:
|
||||
result = o_fig();
|
||||
break;
|
||||
case T:
|
||||
switch (rotation % 4) {
|
||||
case 0:
|
||||
result = t_fig_up();
|
||||
break;
|
||||
case 1:
|
||||
result = t_fig_right();
|
||||
break;
|
||||
case 2:
|
||||
result = t_fig_down();
|
||||
break;
|
||||
case 3:
|
||||
result = t_fig_left();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case L:
|
||||
switch (rotation % 4) {
|
||||
case 0:
|
||||
result = l_fig_up();
|
||||
break;
|
||||
case 1:
|
||||
result = l_fig_right();
|
||||
break;
|
||||
case 2:
|
||||
result = l_fig_down();
|
||||
break;
|
||||
case 3:
|
||||
result = l_fig_left();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case J:
|
||||
switch (rotation % 4) {
|
||||
case 0:
|
||||
result = j_fig_up();
|
||||
break;
|
||||
case 1:
|
||||
result = j_fig_right();
|
||||
break;
|
||||
case 2:
|
||||
result = j_fig_down();
|
||||
break;
|
||||
case 3:
|
||||
result = j_fig_left();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case S:
|
||||
switch (rotation % 4) {
|
||||
case 0:
|
||||
result = s_fig_up();
|
||||
break;
|
||||
case 1:
|
||||
result = s_fig_right();
|
||||
break;
|
||||
case 2:
|
||||
result = s_fig_down();
|
||||
break;
|
||||
case 3:
|
||||
result = s_fig_left();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Z:
|
||||
switch (rotation % 4) {
|
||||
case 0:
|
||||
result = z_fig_up();
|
||||
break;
|
||||
case 1:
|
||||
result = z_fig_right();
|
||||
break;
|
||||
case 2:
|
||||
result = z_fig_down();
|
||||
break;
|
||||
case 3:
|
||||
result = z_fig_left();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
result = NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// I
|
||||
const int (*i_fig_up())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 1},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*i_fig_right())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 1, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*i_fig_down())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 1},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*i_fig_left())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
// O
|
||||
const int (*o_fig())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
// T
|
||||
const int (*t_fig_up())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*t_fig_right())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*t_fig_down())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*t_fig_left())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{1, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
// L
|
||||
const int (*l_fig_up())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*l_fig_right())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{1, 0, 0, 0},
|
||||
{1, 0, 0, 0},
|
||||
{1, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{1, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*l_fig_down())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{1, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0}, {1, 1, 1, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*l_fig_left())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{1, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{1, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
// J
|
||||
const int (*j_fig_up())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{1, 0, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{1, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*j_fig_right())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*j_fig_down())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*j_fig_left())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{1, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
// S
|
||||
const int (*s_fig_up())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0},
|
||||
{1, 1, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*s_fig_right())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*s_fig_down())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0},
|
||||
{1, 1, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*s_fig_left())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
// Z
|
||||
const int (*z_fig_up())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{1, 1, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*z_fig_right())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*z_fig_down())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{1, 1, 0, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*z_fig_left())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0},
|
||||
{0, 1, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
||||
const int (*empty_fig())[4] {
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
return (const int (*)[4])shape;
|
||||
static const int shape[4][4] = {
|
||||
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
return (const int(*)[4])shape;
|
||||
}
|
||||
|
|
@ -1,41 +1,41 @@
|
|||
// src/gui/cli/display.c
|
||||
#include <ncurses.h>
|
||||
#include "../../brick_game/tetris/00_tetris.h"
|
||||
#include <ncurses.h>
|
||||
|
||||
void display_game(GameInfo_t game_state) {
|
||||
clear();
|
||||
clear();
|
||||
|
||||
for (int i = 0; i < FIELD_HEIGHT; ++i) {
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (game_state.field[i][j] == 2) {
|
||||
mvaddch(i + 1, j * 2 + 1, '#');
|
||||
} else if (game_state.field[i][j] == 1) {
|
||||
mvaddch(i + 1, j * 2 + 1, '$');
|
||||
} else {
|
||||
mvaddch(i + 1, j * 2 + 1, '.');
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < FIELD_HEIGHT; ++i) {
|
||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||
if (game_state.field[i][j] == 2) {
|
||||
mvaddch(i + 1, j * 2 + 1, '#');
|
||||
} else if (game_state.field[i][j] == 1) {
|
||||
mvaddch(i + 1, j * 2 + 1, '$');
|
||||
} else {
|
||||
mvaddch(i + 1, j * 2 + 1, '.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mvaddstr(1, FIELD_WIDTH * 2 + 5, "Next figure:");
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
if (game_state.next[i][j]) {
|
||||
mvaddch(i + 3, (FIELD_WIDTH * 2 + 5) + j * 2, '@');
|
||||
} else {
|
||||
mvaddch(i + 3, (FIELD_WIDTH * 2 + 5) + j * 2, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
mvaddstr(1, FIELD_WIDTH * 2 + 5, "Next figure:");
|
||||
|
||||
mvprintw(FIELD_HEIGHT + 2, 1, "Score: %d", game_state.score);
|
||||
mvprintw(FIELD_HEIGHT + 3, 1, "High Score: %d", game_state.high_score);
|
||||
mvprintw(FIELD_HEIGHT + 4, 1, "Level: %d", game_state.level);
|
||||
mvprintw(FIELD_HEIGHT + 5, 1, "Speed: %d", game_state.speed);
|
||||
|
||||
if (game_state.pause) {
|
||||
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH * 2 + 1, "PAUSED");
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
if (game_state.next[i][j]) {
|
||||
mvaddch(i + 3, (FIELD_WIDTH * 2 + 5) + j * 2, '@');
|
||||
} else {
|
||||
mvaddch(i + 3, (FIELD_WIDTH * 2 + 5) + j * 2, ' ');
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
mvprintw(FIELD_HEIGHT + 2, 1, "Score: %d", game_state.score);
|
||||
mvprintw(FIELD_HEIGHT + 3, 1, "High Score: %d", game_state.high_score);
|
||||
mvprintw(FIELD_HEIGHT + 4, 1, "Level: %d", game_state.level);
|
||||
mvprintw(FIELD_HEIGHT + 5, 1, "Speed: %d", game_state.speed);
|
||||
|
||||
if (game_state.pause) {
|
||||
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH * 2 + 1, "PAUSED");
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
|
@ -1,81 +1,81 @@
|
|||
#include "../../brick_game/tetris/00_tetris.h"
|
||||
#include <ncurses.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "../../brick_game/tetris/00_tetris.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void display_game(GameInfo_t game_state);
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
nodelay(stdscr, FALSE);
|
||||
curs_set(0);
|
||||
srand(time(NULL));
|
||||
|
||||
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "Press F to Start");
|
||||
refresh();
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
keypad(stdscr, TRUE);
|
||||
nodelay(stdscr, FALSE);
|
||||
curs_set(0);
|
||||
|
||||
int ch = 0;
|
||||
int started = 0;
|
||||
while (!started) {
|
||||
ch = getch();
|
||||
if (ch == 'f' || ch == 'F') {
|
||||
userInput(Start, false);
|
||||
started = 1;
|
||||
}
|
||||
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "Press F to Start");
|
||||
refresh();
|
||||
|
||||
int ch = 0;
|
||||
int started = 0;
|
||||
while (!started) {
|
||||
ch = getch();
|
||||
if (ch == 'f' || ch == 'F') {
|
||||
userInput(Start, false);
|
||||
started = 1;
|
||||
}
|
||||
}
|
||||
|
||||
nodelay(stdscr, TRUE);
|
||||
timeout(10);
|
||||
|
||||
UserAction_t current_action = {0};
|
||||
bool action_valid = false;
|
||||
bool running = true;
|
||||
|
||||
while (running) {
|
||||
ch = getch();
|
||||
action_valid = false;
|
||||
|
||||
if (ch == 'q') {
|
||||
userInput(Terminate, false);
|
||||
running = false;
|
||||
} else if (ch == 'r' || ch == ' ') {
|
||||
current_action = Action;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_LEFT) {
|
||||
current_action = Left;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_RIGHT) {
|
||||
current_action = Right;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_DOWN) {
|
||||
current_action = Down;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_UP) {
|
||||
current_action = Up;
|
||||
action_valid = true;
|
||||
} else if (ch == 's' || ch == 'S') {
|
||||
current_action = Start;
|
||||
action_valid = true;
|
||||
} else if (ch == 'p' || ch == 'P') {
|
||||
current_action = Pause;
|
||||
action_valid = true;
|
||||
}
|
||||
|
||||
nodelay(stdscr, TRUE);
|
||||
timeout(10);
|
||||
|
||||
UserAction_t current_action = {0};
|
||||
bool action_valid = false;
|
||||
bool running = true;
|
||||
|
||||
while (running) {
|
||||
ch = getch();
|
||||
action_valid = false;
|
||||
|
||||
if (ch == 'q') {
|
||||
userInput(Terminate, false);
|
||||
running = false;
|
||||
} else if (ch == 'r' || ch == ' ') {
|
||||
current_action = Action;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_LEFT) {
|
||||
current_action = Left;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_RIGHT) {
|
||||
current_action = Right;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_DOWN) {
|
||||
current_action = Down;
|
||||
action_valid = true;
|
||||
} else if (ch == KEY_UP) {
|
||||
current_action = Up;
|
||||
action_valid = true;
|
||||
} else if (ch == 's' || ch == 'S') {
|
||||
current_action = Start;
|
||||
action_valid = true;
|
||||
} else if (ch == 'p' || ch == 'P') {
|
||||
current_action = Pause;
|
||||
action_valid = true;
|
||||
}
|
||||
|
||||
if (action_valid) {
|
||||
userInput(current_action, false);
|
||||
}
|
||||
|
||||
if (running) {
|
||||
GameInfo_t game_state = updateCurrentState();
|
||||
display_game(game_state);
|
||||
}
|
||||
if (action_valid) {
|
||||
userInput(current_action, false);
|
||||
}
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
if (running) {
|
||||
GameInfo_t game_state = updateCurrentState();
|
||||
display_game(game_state);
|
||||
}
|
||||
}
|
||||
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue