update tetris

This commit is contained in:
Mitchell Marino 2024-08-10 11:44:27 -05:00
parent a9cbb03fcd
commit 9b8e69ad26
2 changed files with 74 additions and 12 deletions

View File

@ -34,6 +34,8 @@ static const char* PIECE_IMG_SRC[] = {
};
static bool game = true;
static int target_score = 0;
static int score = 0;
static int piece = 0;
@ -48,6 +50,8 @@ static int piece_nodes[4][2] = {
lv_obj_t* piece_imgs[4] = {};
TaskHandle_t music_handle;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> piece_dist(2, 7);
@ -64,7 +68,8 @@ static void move_left(void);
static void move_right(void);
static void drop(void);
static void rotate_block(void);
static void update_score(void);
static void clear_board(void);
static int bbcc(int i) { // [0,1,2,3] -> [ 0, 0,+1,+1]
const int map[] = {0, 0, 1, 1};
@ -150,7 +155,7 @@ static void init_screen(void) {
xSemaphoreGive(xGuiSemaphore);
xTaskCreate(music_task, "music", 4096, NULL, 5, NULL);
xTaskCreate(music_task, "music", 4096, NULL, 5, &music_handle);
}
static void deinit_screen(void) {
@ -158,15 +163,24 @@ static void deinit_screen(void) {
lv_obj_clean(lv_scr_act());
xSemaphoreGive(xGuiSemaphore);
vTaskDelete(music_handle);
}
void step2(void) {
init_screen();
bool play_game(int time, int required_score) {
target_score = required_score;
score = 0;
update_score();
set_module_time(time);
vTaskDelay(1000);
start_module_timer();
clear_board();
generate_block();
ButtonKey button;
SwitchKey switch_;
// SwitchKey switch_;
while(game) {
if (get_pressed_button(&button)) {
@ -186,21 +200,43 @@ void step2(void) {
}
show_board();
if (score >= required_score) {
stop_module_timer();
return true;
}
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("PieceRotation: %d\n", piece_rotation);
if (get_module_time() <= 0) {
stop_module_timer();
strike("Out of time");
return false;
}
// 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("PieceRotation: %d\n", piece_rotation);
// }
vTaskDelay(pdMS_TO_TICKS(10));
}
// game over
ESP_LOGI(TAG, "Game Over. Score: %d", score);
stop_module_timer();
strike("Out of room");
return false;
}
void step2(void) {
init_screen();
while (!play_game(60000, 4)) vTaskDelay(2000);
while (!play_game(60000, 8)) vTaskDelay(2000);
while (!play_game(120000, 16)) vTaskDelay(2000);
deinit_screen();
}
@ -470,6 +506,13 @@ static void check_line_clears(void) {
}
}
static void update_score(void) {
char buff[16] = {};
sprintf(buff, "%d/%d", score, target_score);
lcd_clear(&lcd);
lcd_set_cursor(&lcd, 1, 1);
lcd_print(&lcd, buff);
}
static void line_clear(int hi) {
for (int h = hi; h < height; h++) {
@ -481,6 +524,7 @@ static void line_clear(int hi) {
board[height-1][w] = 0;
}
score++;
update_score();
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
lv_obj_align(line_clear_img, LV_ALIGN_BOTTOM_LEFT, 159, -(hi*16));
@ -500,3 +544,10 @@ static void line_clear(int hi) {
}
}
void clear_board(void) {
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
board[h][w] = 0;
}
}
}

View File

@ -5,6 +5,17 @@
#include "../drivers/tft.h"
#include "../drivers/speaker.h"
#include "../drivers/bottom_half.h"
#include "../drivers/game_timer.h"
#include "../drivers/wires.h"
#include "../drivers/char_lcd.h"
// TODO:
// - [ ] set up real game loop
// - [ ] stop music
// - [ ] set up strike on falure
// - [ ] set up module timer
void step2(void);