rotation is working

This commit is contained in:
Rorikstr | Rust Dev 2025-09-26 16:23:22 +03:00
parent b0ab522b58
commit afab29ec18
3 changed files with 297 additions and 167 deletions

View file

@ -7,196 +7,320 @@
static GameStateData game_state = {0}; static GameStateData game_state = {0};
static bool initialized = false; static bool initialized = false;
const int (*get_figure_shape(FigureType type, int rotation))[4] {
static const int shapes[FIGURE_COUNT + 1][4][4][4] = {
// I // I
{ const int (*i_fig_up())[4] {
{ static const int shape[4][4] = {
{0, 0, 0, 0}, {0, 0, 0, 0},
{1, 1, 1, 1}, {1, 1, 1, 1},
{0, 0, 0, 0}, {0, 0, 0, 0},
{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},
{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},
{0, 0, 0, 0}, {0, 0, 0, 0},
{1, 1, 1, 1}, {1, 1, 1, 1},
{0, 0, 0, 0} {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},
{0, 1, 0, 0}, {0, 1, 0, 0},
{0, 1, 0, 0} {0, 1, 0, 0}
}
},
// O
{
{
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0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1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
},
{
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
}
}
}; };
return shapes[type][rotation]; 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) {
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;
}
return result;
} }
void user_input(UserAction_t action) { void user_input(UserAction_t action) {
@ -219,6 +343,9 @@ void user_input(UserAction_t action) {
if (action == Right) game_state.current_figure.x++; if (action == Right) game_state.current_figure.x++;
if (action == Down) game_state.current_figure.y++; if (action == Down) game_state.current_figure.y++;
if (action == Up) game_state.current_figure.y--; if (action == Up) game_state.current_figure.y--;
if (action == Rotate) {
game_state.current_figure.rotation = (game_state.current_figure.rotation + 1) % 4;
}
} }
} }

View file

@ -45,6 +45,7 @@ typedef enum {
Right, Right,
Up, Up,
Down, Down,
Rotate,
Action, Action,
Figure1, Figure1,
Figure2, Figure2,

View file

@ -18,10 +18,9 @@ int main() {
int ch; int ch;
UserAction_t action = Undefined; UserAction_t action = Undefined;
while (action != Terminate) { while (Terminate != action) {
ch = getch(); ch = getch();
switch (ch) { switch (ch) {
case 'q': action = Terminate; break; case 'q': action = Terminate; break;
case '1': action = Figure1; break; case '1': action = Figure1; break;
@ -31,10 +30,13 @@ int main() {
case '5': action = Figure5; break; case '5': action = Figure5; break;
case '6': action = Figure6; break; case '6': action = Figure6; break;
case '7': action = Figure7; break; case '7': action = Figure7; break;
case 'r': action = Rotate; break;
case ' ': action = Rotate; break;
case KEY_LEFT: action = Left; break; case KEY_LEFT: action = Left; break;
case KEY_RIGHT: action = Right; break; case KEY_RIGHT: action = Right; break;
case KEY_DOWN: action = Down; break; case KEY_DOWN: action = Down; break;
case KEY_UP: action = Up; break; case KEY_UP: action = Up; break;
default: action = Undefined;
} }
if (action != Undefined) { if (action != Undefined) {