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