collision done

This commit is contained in:
Rorikstr | Rust Dev 2025-09-27 00:09:18 +03:00
parent 15703b5f28
commit c4171dc5d5
7 changed files with 436 additions and 266 deletions

View file

@ -0,0 +1,258 @@
#include "tetris.h"
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}

View file

@ -7,263 +7,6 @@
static GameStateData game_state = {0};
static bool initialized = false;
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
}
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;
}
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;
}
const int (*get_figure_shape(FigureType type, int rotation))[4] {
const int (*result)[4] = NULL;
switch (type) {
@ -339,6 +82,10 @@ void user_input(UserAction_t action) {
}
if (game_state.figure_active) {
int old_x = game_state.current_figure.x;
int old_y = game_state.current_figure.y;
int old_rot = game_state.current_figure.rotation;
if (action == Left) game_state.current_figure.x--;
if (action == Right) game_state.current_figure.x++;
if (action == Down) game_state.current_figure.y++;
@ -346,9 +93,34 @@ void user_input(UserAction_t action) {
if (action == Rotate) {
game_state.current_figure.rotation = (game_state.current_figure.rotation + 1) % 4;
}
if (check_collision()) {
// Возвращаем старые значения
game_state.current_figure.x = old_x;
game_state.current_figure.y = old_y;
game_state.current_figure.rotation = old_rot;
}
}
}
GameStateData* get_game_state() {
return &game_state;
}
static bool check_collision() {
Figure* f = &game_state.current_figure;
const int (*shape)[4] = get_figure_shape(f->type, f->rotation);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (shape[i][j]) {
int x = f->x + j;
int y = f->y + i;
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
return true; // Коллизия
}
}
}
}
return false; // Нет коллизии
}

View file

@ -58,8 +58,41 @@ typedef enum {
// Основные функции библиотеки
void user_input(UserAction_t action);
static bool check_collision();
GameStateData* get_game_state(void);
const int (*get_figure_shape(FigureType type, int rotation))[4];
const int (*i_fig_up())[4];
const int (*i_fig_right())[4];
const int (*i_fig_down())[4];
const int (*i_fig_left())[4];
const int (*o_fig())[4];
const int (*t_fig_up())[4];
const int (*t_fig_right())[4];
const int (*t_fig_down())[4];
const int (*t_fig_left())[4];
const int (*l_fig_up())[4];
const int (*l_fig_right())[4];
const int (*l_fig_down())[4];
const int (*l_fig_left())[4];
const int (*j_fig_up())[4];
const int (*j_fig_right())[4];
const int (*j_fig_down())[4];
const int (*j_fig_left())[4];
const int (*s_fig_up())[4];
const int (*s_fig_right())[4];
const int (*s_fig_down())[4];
const int (*s_fig_left())[4];
const int (*z_fig_up())[4];
const int (*z_fig_right())[4];
const int (*z_fig_down())[4];
const int (*z_fig_left())[4];
#endif