Fix tetris spinning

This commit is contained in:
Mitchell Marino 2024-08-07 23:51:08 -05:00
parent 517a6484eb
commit 417ea02743
2 changed files with 63 additions and 23 deletions

View File

@ -6,6 +6,7 @@ static lv_obj_t* scr;
static lv_obj_t* img; static lv_obj_t* img;
static bool invisible_blocks = false; static bool invisible_blocks = false;
static bool text_output = false;
static const int height = 22; static const int height = 22;
static const int width = 10; static const int width = 10;
@ -24,6 +25,10 @@ static int piece_nodes[4][2] = {
{0, 0}, {0, 0},
}; };
static lv_color_t piece_colors[] = {
(lv_color_t { .full = 0xfee0 }),
};
std::random_device rd; std::random_device rd;
std::mt19937 gen(rd()); std::mt19937 gen(rd());
std::uniform_int_distribution<> piece_dist(2, 7); std::uniform_int_distribution<> piece_dist(2, 7);
@ -43,26 +48,32 @@ static void rotate_block(void);
static int bbcc(int i) { // [0,1,2,3] -> [ 0, 0,+1,+1] static int bbcc(int i) { // [0,1,2,3] -> [ 0, 0,+1,+1]
return (i/2); const int map[] = {0, 0, 1, 1};
return map[i];
} }
static int bccb(int i) { // [0,1,2,3] -> [ 0,+1,+1, 0] static int bccb(int i) { // [0,1,2,3] -> [ 0,+1,+1, 0]
return (((i+1)/2)%2); const int map[] = {0, 1, 1, 0};
return map[i];
} }
static int cbbc(int i) { // [0,1,2,3] -> [+1, 0, 0,+1] static int cbbc(int i) { // [0,1,2,3] -> [+1, 0, 0,+1]
return (1-(((i+1)/2)%2)); const int map[] = {1, 0, 0, 1};
return map[i];
} }
static int ccbb(int i) { // [0,1,2,3] -> [+1,+1, 0, 0] static int ccbb(int i) { // [0,1,2,3] -> [+1,+1, 0, 0]
return (1-i/2); const int map[] = {1, 1, 0, 0};
return map[i];
} }
static int acca(int i) { // [0,1,2,3] -> [-1,+1,+1,-1] static int acca(int i) { // [0,1,2,3] -> [-1,+1,+1,-1]
return (((((i+1)/2)%2)*2)-1); const int map[] = {-1, 1, 1, -1};
return map[i];
} }
static int aacc(int i) { // [0,1,2,3] -> [-1,-1,+1,+1] static int aacc(int i) { // [0,1,2,3] -> [-1,-1,+1,+1]
return (((i/2)*2)-1); const int map[] = {-1, -1, 1, 1};
return map[i];
} }
static int babc(int i) { // [0,1,2,3] -> [ 0,+1, 0,-1] static int babc(int i) { // [0,1,2,3] -> [ 0,-1, 0,+1]
const int map[] = {0, 1, 0, -1}; const int map[] = {0, -1, 0, 1};
return map[i]; return map[i];
} }
static int abcb(int i) { // [0,1,2,3] -> [-1, 0,+1, 0] static int abcb(int i) { // [0,1,2,3] -> [-1, 0,+1, 0]
@ -71,25 +82,33 @@ static int abcb(int i) { // [0,1,2,3] -> [-1, 0,+1, 0]
} }
static int acdb(int i) { // [0,1,2,3] -> [-1,+1,+2, 0] static int acdb(int i) { // [0,1,2,3] -> [-1,+1,+2, 0]
return (acca(i) + bbcc(i)); const int map[] = {-1, +1, 2, 0};
return map[i];
} }
static int bacd(int i) { // [0,1,2,3] -> [ 0,-1,+1,+2] static int bacd(int i) { // [0,1,2,3] -> [ 0,-1,+1,+2]
return (babc(i) + bbcc(i)); const int map[] = {0, -1, 1, 2};
return map[i];
} }
static int dcab(int i) { // [0,1,2,3] -> [+2,+1,-1, 0] static int dcab(int i) { // [0,1,2,3] -> [+2,+1,-1, 0]
return (-abcb(i) + ccbb(i)); const int map[] = {2, 1, -1, 0};
return map[i];
} }
static int bdca(int i) { // [0,1,2,3] -> [ 0,+2,+1,-1] static int bdca(int i) { // [0,1,2,3] -> [ 0,+2,+1,-1]
return (acca(i) + ccbb(i)); const int map[] = {0, 2, 1, -1};
return map[i];
} }
static void init_screen(void) { static void init_screen(void) {
LV_IMG_DECLARE(tetris); LV_IMG_DECLARE(tetris);
// scr = lv_obj_create(screen); // scr = lv_obj_create(screen);
// img = lv_img_create(lv_scr_act());
// lv_img_set_src(img, &tetris);
// lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
img = lv_img_create(lv_scr_act()); img = lv_img_create(lv_scr_act());
lv_img_set_src(img, &tetris); lv_img_set_src(img, "A:/sdcard/db.bin");
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0); lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
// play_raw(MOUNT_POINT "/tetris.pcm"); // play_raw(MOUNT_POINT "/tetris.pcm");
@ -105,10 +124,9 @@ void step2(void) {
generate_block(); generate_block();
ButtonKey button; ButtonKey button;
SwitchKey switch_;
while(game) { while(game) {
show_board();
if (get_pressed_button(&button)) { if (get_pressed_button(&button)) {
switch (button) { switch (button) {
case ButtonKey::b1: case ButtonKey::b1:
@ -124,9 +142,19 @@ void step2(void) {
rotate_block(); rotate_block();
break; break;
} }
show_board();
}
if (get_flipped_switch(&switch_)) {
printf("%d\n", piece);
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
int* p = piece_nodes[i];
printf("PieceLocation: %d, %d\n", p[0], p[1]);
}
printf("PieceRotaition: %d\n", piece_rotation);
} }
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(10));
} }
// game over // game over
@ -149,13 +177,17 @@ static void show_board(void) {
board[p[0]][p[1]] = 9; board[p[0]][p[1]] = 9;
} }
for (int h = height-1; h >= 0; h--) { if (text_output) {
for (int w = 0; w < width; w++) { for (int h = height-1; h >= 0; h--) {
printf("|%c|", board[h][w] == 9 ? 'X' : (invisible_blocks ? '0' : ('0' + board[h][w]))); for (int w = 0; w < width; w++) {
printf("|%c", board[h][w] == 9 ? 'X' : (invisible_blocks ? '0' : ('0' + board[h][w])));
}
printf("|\n");
} }
printf("|\n"); printf("\n");
} else {
} }
printf("\n");
} }
static void generate_block(void) { static void generate_block(void) {
@ -235,8 +267,11 @@ static void move_left(void) {
int* p = piece_nodes[i]; int* p = piece_nodes[i];
if (p[1] == 0) { if (p[1] == 0) {
return; return;
} else if (board[p[0]][p[1]-1] != 0 && board[p[0]][p[1]-1] != 9) {
return;
} }
} }
piece_location[1]--; piece_location[1]--;
get_node_locations(); get_node_locations();
} }
@ -246,6 +281,8 @@ static void move_right(void) {
int* p = piece_nodes[i]; int* p = piece_nodes[i];
if (p[1] == width-1) { if (p[1] == width-1) {
return; return;
} else if (board[p[0]][p[1]+1] != 0 && board[p[0]][p[1]+1] != 9) {
return;
} }
} }
piece_location[1]++; piece_location[1]++;

View File

@ -2133,7 +2133,10 @@ CONFIG_LV_USE_GRID=y
# #
# 3rd Party Libraries # 3rd Party Libraries
# #
# CONFIG_LV_USE_FS_STDIO is not set CONFIG_LV_USE_FS_STDIO=y
CONFIG_LV_FS_STDIO_LETTER=65
CONFIG_LV_FS_STDIO_PATH=""
CONFIG_LV_FS_STDIO_CACHE_SIZE=0
# CONFIG_LV_USE_FS_POSIX is not set # CONFIG_LV_USE_FS_POSIX is not set
# CONFIG_LV_USE_FS_WIN32 is not set # CONFIG_LV_USE_FS_WIN32 is not set
# CONFIG_LV_USE_FS_FATFS is not set # CONFIG_LV_USE_FS_FATFS is not set