Fix tetris
This commit is contained in:
+77
-25
@@ -15,16 +15,22 @@ static const int width = 10;
|
||||
static int board[height][width] = {0};
|
||||
static lv_obj_t* visual_board[height][width] = {0};
|
||||
|
||||
static const char* BACKGROUND_FILE_NAME = "A:/sdcard/bg.bin";
|
||||
static lv_obj_t* line_clear_img;
|
||||
|
||||
static const void* LINE_CLEAR_SRC = (void*)"A:/sdcard/clear.bin";
|
||||
static const void* BACKGROUND_SRC = (void*)"A:/sdcard/bg.bin";
|
||||
// LV_IMG_DECLARE(background);
|
||||
// static const void* BACKGROUND_SRC = (void*)&background;
|
||||
|
||||
static const char* PIECE_IMG_SRC[] = {
|
||||
"A:/sdcard/red.bin",
|
||||
NULL,
|
||||
"A:/sdcard/lb.bin",
|
||||
"A:/sdcard/db.bin",
|
||||
"A:/sdcard/orange.bin",
|
||||
"A:/sdcard/yellow.bin",
|
||||
"A:/sdcard/green.bin",
|
||||
"A:/sdcard/purple.bin",
|
||||
"A:/sdcard/red.bin",
|
||||
};
|
||||
|
||||
static bool game = true;
|
||||
@@ -44,13 +50,13 @@ lv_obj_t* piece_imgs[4] = {};
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> piece_dist(2, 6);
|
||||
std::uniform_int_distribution<> piece_dist(2, 7);
|
||||
|
||||
static void generate_block(void);
|
||||
static void show_board(void);
|
||||
static void get_node_locations(void);
|
||||
|
||||
static void line_clear(int height);
|
||||
static void line_clear(int hi);
|
||||
static void check_line_clears(void);
|
||||
static void place_piece(void);
|
||||
|
||||
@@ -111,25 +117,40 @@ static int bdca(int i) { // [0,1,2,3] -> [ 0,+2,+1,-1]
|
||||
return map[i];
|
||||
}
|
||||
|
||||
static void music_task(void* arg) {
|
||||
while (1) {
|
||||
play_raw(MOUNT_POINT "/tetris.pcm");
|
||||
}
|
||||
}
|
||||
|
||||
static void init_screen(void) {
|
||||
// create new screen
|
||||
// scr = lv_obj_create(NULL);
|
||||
// lv_style_init(&scr_style);
|
||||
// lv_style_set_bg_color(&scr_style, lv_color_black());
|
||||
// lv_obj_add_style(scr, &scr_style, LV_STATE_DEFAULT);
|
||||
// lv_scr_load(scr);
|
||||
|
||||
while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
img = lv_img_create(lv_scr_act());
|
||||
lv_img_set_src(img, BACKGROUND_FILE_NAME);
|
||||
lv_img_set_src(img, BACKGROUND_SRC);
|
||||
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
line_clear_img = lv_img_create(lv_scr_act());
|
||||
lv_obj_add_flag(line_clear_img, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_img_set_src(line_clear_img, LINE_CLEAR_SRC);
|
||||
lv_obj_align(line_clear_img, LV_ALIGN_BOTTOM_LEFT, 159, -(height*16));
|
||||
|
||||
// for (int h = 0; h < height; h++) {
|
||||
// for (int w = 0; w < width; w++) {
|
||||
// visual_board[h][w] = lv_img_create(lv_scr_act());
|
||||
// lv_obj_align(visual_board[h][w], LV_ALIGN_BOTTOM_LEFT, 159 + w*16, -(h*16));
|
||||
// lv_img_set_src(visual_board[h][w], PIECE_IMG_SRC[((w+h)%7)+1]);
|
||||
// }
|
||||
// }
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
piece_imgs[i] = lv_img_create(lv_scr_act());
|
||||
lv_obj_align(piece_imgs[i], LV_ALIGN_BOTTOM_LEFT, 159, -320);
|
||||
}
|
||||
|
||||
// play_raw(MOUNT_POINT "/tetris.pcm");
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
|
||||
xTaskCreate(music_task, "music", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
static void deinit_screen(void) {
|
||||
@@ -172,7 +193,7 @@ void step2(void) {
|
||||
int* p = piece_nodes[i];
|
||||
printf("PieceLocation: %d, %d\n", p[0], p[1]);
|
||||
}
|
||||
printf("PieceRotaition: %d\n", piece_rotation);
|
||||
printf("PieceRotation: %d\n", piece_rotation);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
@@ -208,12 +229,13 @@ static void show_board(void) {
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
|
||||
int* p = piece_nodes[i];
|
||||
lv_obj_t* piece_img = piece_imgs[i];
|
||||
|
||||
// lv_img_set_src(piece_img, FILE_NAME[piece]);
|
||||
lv_obj_align(piece_img, LV_ALIGN_BOTTOM_LEFT, 159 + p[1]*16, -(p[0]*16));
|
||||
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
|
||||
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
|
||||
int* p = piece_nodes[i];
|
||||
lv_obj_t* piece_img = piece_imgs[i];
|
||||
lv_obj_align(piece_img, LV_ALIGN_BOTTOM_LEFT, 159 + p[1]*16, -(p[0]*16));
|
||||
}
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,9 +265,12 @@ static void generate_block(void) {
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
lv_obj_t* piece_img = piece_imgs[i];
|
||||
lv_img_set_src(piece_img, PIECE_IMG_SRC[piece]);
|
||||
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
lv_obj_t* piece_img = piece_imgs[i];
|
||||
lv_img_set_src(piece_img, PIECE_IMG_SRC[piece]);
|
||||
}
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,6 +374,13 @@ static void place_piece(void) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
|
||||
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
|
||||
int* p = piece_nodes[i];
|
||||
lv_obj_align(piece_imgs[i], LV_ALIGN_BOTTOM_LEFT, 159, -(height*16));
|
||||
}
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
}
|
||||
ESP_LOGI(TAG, "Placed Piece: %d", piece);
|
||||
}
|
||||
|
||||
@@ -439,12 +471,32 @@ static void check_line_clears(void) {
|
||||
}
|
||||
|
||||
|
||||
static void line_clear(int height) {
|
||||
for (int h = height; h < height-1; h++) {
|
||||
static void line_clear(int hi) {
|
||||
for (int h = hi; h < height; h++) {
|
||||
for (int w = 0; w < width; w++) {
|
||||
board[h][w] = board[h+1][w];
|
||||
}
|
||||
}
|
||||
for (int w = 0; w < width; w++) {
|
||||
board[height-1][w] = 0;
|
||||
}
|
||||
score++;
|
||||
|
||||
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
|
||||
lv_obj_align(line_clear_img, LV_ALIGN_BOTTOM_LEFT, 159, -(hi*16));
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
|
||||
lv_obj_clear_flag(line_clear_img, LV_OBJ_FLAG_HIDDEN);
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(300));
|
||||
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
|
||||
lv_obj_add_flag(line_clear_img, LV_OBJ_FLAG_HIDDEN);
|
||||
xSemaphoreGive(xGuiSemaphore);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(300));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user