Decomposed and fixed saves
This commit is contained in:
parent
65d2c2e287
commit
cc6f9bb2d1
9 changed files with 112 additions and 72 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -58,3 +58,4 @@ ginpee.toml
|
||||||
src/ginpee.toml
|
src/ginpee.toml
|
||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
src/tetris.log
|
src/tetris.log
|
||||||
|
src/high_score.txt
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ GameState_t* get_game_state(void);
|
||||||
void do_init(void);
|
void do_init(void);
|
||||||
int load_high_score();
|
int load_high_score();
|
||||||
void save_high_score(int score);
|
void save_high_score(int score);
|
||||||
|
void generate_next_figure(void);
|
||||||
|
|
||||||
// spawn
|
// spawn
|
||||||
void do_spawn(void);
|
void do_spawn(void);
|
||||||
|
|
@ -78,7 +79,6 @@ void clear_lines();
|
||||||
void do_gameover(void);
|
void do_gameover(void);
|
||||||
int is_game_over();
|
int is_game_over();
|
||||||
|
|
||||||
|
|
||||||
// Функции фигур
|
// Функции фигур
|
||||||
const int (*get_figure_shape(Sprite_t sprite, int rotation))[4];
|
const int (*get_figure_shape(Sprite_t sprite, int rotation))[4];
|
||||||
|
|
||||||
|
|
@ -110,5 +110,4 @@ const int (*z_fig_down())[4];
|
||||||
const int (*z_fig_left())[4];
|
const int (*z_fig_left())[4];
|
||||||
const int (*empty_fig())[4];
|
const int (*empty_fig())[4];
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -12,7 +12,7 @@ void userInput(UserAction_t action, bool hold) {
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case Start:
|
case Start:
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,22 @@
|
||||||
#include "01_automato.h"
|
#include "01_automato.h"
|
||||||
|
|
||||||
void do_init(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) {
|
||||||
|
GameState_t* state = get_game_state();
|
||||||
state->info->score = 0;
|
state->info->score = 0;
|
||||||
state->info->level = 1;
|
state->info->level = 1;
|
||||||
state->info->speed = 10;
|
state->info->speed = 10;
|
||||||
|
}
|
||||||
|
|
||||||
state->next.sprite = rand() % FIGURE_COUNT;
|
void do_init(void) {
|
||||||
state->next.rotation = 0;
|
clear_field();
|
||||||
const int (*shape)[4] = get_figure_shape(state->next.sprite, 0);
|
reset_game_stats();
|
||||||
for (int i = 0; i < 4; ++i)
|
generate_next_figure();
|
||||||
for (int j = 0; j < 4; ++j)
|
get_game_state()->state = Spawn; // Переход в Spawn
|
||||||
state->next.mtrx[i][j] = shape[i][j];
|
|
||||||
|
|
||||||
state->state = Spawn; // Переход в Spawn
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,24 +1,31 @@
|
||||||
#include "01_automato.h"
|
#include "01_automato.h"
|
||||||
|
|
||||||
void do_spawn(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) {
|
||||||
|
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) {
|
||||||
|
set_current_figure_from_next();
|
||||||
|
generate_next_figure();
|
||||||
|
|
||||||
if (check_collision()) {
|
if (check_collision()) {
|
||||||
state->state = GameOver;
|
get_game_state()->state = GameOver;
|
||||||
return;
|
return; // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
state->state = Move;
|
get_game_state()->state = Move;
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
// brick_game/tetris/06_move.c
|
|
||||||
#include "01_automato.h"
|
#include "01_automato.h"
|
||||||
|
|
||||||
|
int get_frames_to_wait(void) {
|
||||||
|
GameState_t* state = get_game_state();
|
||||||
|
if (state->moving_type == ToDown) {
|
||||||
|
return 1; // TODO
|
||||||
|
} else {
|
||||||
|
return 1000 / state->info->speed; // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void do_move(void) {
|
void do_move(void) {
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
|
|
||||||
int frames_to_wait = (state->moving_type == ToDown) ? 1 : (1000 / state->info->speed);
|
int frames_to_wait = get_frames_to_wait();
|
||||||
|
|
||||||
if (state->frame_count - state->last_move_frame < frames_to_wait) {
|
if (state->frame_count - state->last_move_frame < frames_to_wait) {
|
||||||
return;
|
return; // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
state->last_move_frame = state->frame_count;
|
state->last_move_frame = state->frame_count;
|
||||||
|
|
|
||||||
|
|
@ -1,47 +1,73 @@
|
||||||
#include "01_automato.h"
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_to_down_move(void) {
|
||||||
|
GameState_t* state = get_game_state();
|
||||||
|
while (!check_collision()) {
|
||||||
|
state->curr.y++;
|
||||||
|
}
|
||||||
|
state->curr.y--;
|
||||||
|
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:
|
||||||
Figure_t old = state->curr;
|
handle_horizontal_rotate_move();
|
||||||
switch (state->moving_type) {
|
|
||||||
case LeftDown:
|
|
||||||
state->curr.x--;
|
|
||||||
break;
|
|
||||||
case RightDown:
|
|
||||||
state->curr.x++;
|
|
||||||
break;
|
|
||||||
case Rotate:
|
|
||||||
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];
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (check_collision()) {
|
|
||||||
state->curr = old;
|
|
||||||
}
|
|
||||||
state->state = Move;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case ToDown:
|
case ToDown:
|
||||||
while (!check_collision()) {
|
handle_to_down_move();
|
||||||
state->curr.y++;
|
|
||||||
}
|
|
||||||
state->curr.y--;
|
|
||||||
state->state = Attaching;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DoNothing:
|
case DoNothing:
|
||||||
state->state = Move;
|
state->state = Move;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
state->state = Move;
|
|
||||||
}
|
}
|
||||||
|
|
@ -22,10 +22,10 @@ int check_collision() {
|
||||||
int y = fig->y + i;
|
int y = fig->y + i;
|
||||||
|
|
||||||
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
|
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
|
||||||
return 1;
|
return 1; // TODO
|
||||||
}
|
}
|
||||||
if (y >= 0 && state->field[y][x]) {
|
if (y >= 0 && state->field[y][x]) {
|
||||||
return 1;
|
return 1; // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +34,6 @@ int check_collision() {
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
|
@ -51,10 +50,20 @@ void place_figure() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_lines() {
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int x = 0; x < FIELD_WIDTH; ++x) {
|
||||||
|
state->field[0][x] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|
@ -62,20 +71,13 @@ void clear_lines() {
|
||||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||||
if (state->field[i][j] != 2) {
|
if (state->field[i][j] != 2) {
|
||||||
full = 0;
|
full = 0;
|
||||||
break;
|
break; // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (full) {
|
if (full) {
|
||||||
for (int y = i; y > 0; --y) {
|
shift_lines_down(i);
|
||||||
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) {
|
|
||||||
state->field[0][x] = 0;
|
|
||||||
}
|
|
||||||
lines_cleared++;
|
lines_cleared++;
|
||||||
i++;
|
i++; // Check the same row again after shifting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,9 +92,7 @@ void clear_lines() {
|
||||||
|
|
||||||
if (new_level > state->info->level) {
|
if (new_level > state->info->level) {
|
||||||
state->info->level = new_level;
|
state->info->level = new_level;
|
||||||
|
|
||||||
state->info->speed = new_level * 10;
|
state->info->speed = new_level * 10;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ int is_game_over() {
|
||||||
GameState_t* state = get_game_state();
|
GameState_t* state = get_game_state();
|
||||||
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
for (int j = 0; j < FIELD_WIDTH; ++j) {
|
||||||
if (state->field[0][j] || state->field[1][j]) {
|
if (state->field[0][j] || state->field[1][j]) {
|
||||||
return 1;
|
return 1; // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue