From 8d67a458e56135a423e29fec7c67db6f11bc8e48 Mon Sep 17 00:00:00 2001 From: james <1943357252@qq.com> Date: Mon, 5 Aug 2024 10:19:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BD=BF=E8=9B=87=E8=83=BD?= =?UTF-8?q?=E8=BE=BE=E5=88=B0=E9=98=9F=E5=88=97=E6=9C=80=E5=A4=A7=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=EF=BC=8C=E9=98=B2=E6=AD=A2=E8=9B=87=E7=BB=8F=E8=BF=87?= =?UTF-8?q?=E6=9E=9C=E5=AD=90=E6=9E=9C=E5=AD=90=E6=B6=88=E5=A4=B1=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E6=95=B4main=E9=A1=BA=E5=BA=8F=EF=BC=8C=E5=85=88?= =?UTF-8?q?=E8=BF=90=E8=A1=8Cmysnake?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/init.c | 2 -- applications/main.c | 2 ++ dayandnight/mysnake.c | 30 +++++++++++++++++++----------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/applications/init.c b/applications/init.c index eafb3d6..a07d508 100644 --- a/applications/init.c +++ b/applications/init.c @@ -52,8 +52,6 @@ int main_init(void) char str[] = "wifi join Dong abcd07691234"; my_round(20); - rt_wlan_config_autoreconnect(RT_TRUE); - rt_wlan_connect("Dong", "abcd07691234"); system(str); // rt_thread_mdelay(18000); // char *argv[] = {"wifi", "join", "Dong", "abcd07691234"}; diff --git a/applications/main.c b/applications/main.c index e199e38..4a9b996 100644 --- a/applications/main.c +++ b/applications/main.c @@ -36,6 +36,8 @@ int main(void) rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT); rt_pin_write(PIN_LED_B, PIN_LOW); system("snake game"); + rt_wlan_config_autoreconnect(RT_TRUE); + rt_wlan_connect("Dong", "abcd07691234"); mytime(); /* init Wi-Fi auto connect feature */ diff --git a/dayandnight/mysnake.c b/dayandnight/mysnake.c index dc5a973..e784175 100644 --- a/dayandnight/mysnake.c +++ b/dayandnight/mysnake.c @@ -26,7 +26,7 @@ void snake_entry(void *parameter) time_t t; /* 初始化随机数发生器 */ srand((unsigned)time(&t)); - int snake_list[SNAKE_MAX+1][2] = {0}; + int snake_list[SNAKE_MAX + 1][2] = {0}; int snake_direction[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; // 上,左,下,右 int snake_food[2]; bool food_flag = false; @@ -49,7 +49,7 @@ void snake_entry(void *parameter) snake_food[0] = rand() % SNAKE_MAX; snake_food[1] = rand() % SNAKE_MAX; snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN); - int new_direction = 0; + int new_head_x = 0, new_head_y = 0; while (1) { // if(snake) @@ -57,19 +57,17 @@ void snake_entry(void *parameter) // now_direction = (now_direction+3+new_direction)%4;//防止反向,走回头路 former_head = snake_head; - snake_head = (snake_head + 1) % (SNAKE_MAX); - snake_list[snake_head][0] = (snake_list[former_head][0] + snake_direction[now_direction][0] + SNAKE_MAX) % (SNAKE_MAX); - snake_list[snake_head][1] = (snake_list[former_head][1] + snake_direction[now_direction][1] + SNAKE_MAX) % (SNAKE_MAX); + new_head_x = (snake_list[snake_head][0] + snake_direction[now_direction][0] + SNAKE_MAX) % (SNAKE_MAX); + new_head_y = (snake_list[snake_head][1] + snake_direction[now_direction][1] + SNAKE_MAX) % (SNAKE_MAX); - sprintf(tmp, "(%d,%d)", snake_list[snake_head][0], snake_list[snake_head][1]); + sprintf(tmp, "(%d,%d)", new_head_x, new_head_y); // rt_kprintf("head:%d,%d\n", snake_list[snake_head][0], snake_list[snake_head][1]); lcd_show_string(20, 20, 16, snake_dirshow[now_direction]); lcd_show_string(20 + 16 * 4, 20, 16, tmp); - rt_thread_mdelay(900); - snake_address(snake_list[snake_head][0], snake_list[snake_head][1], SNAKE_SIZE, BLACK); - if (snake_list[snake_head][0] == snake_food[0] && snake_list[snake_head][1] == snake_food[1]) + snake_address(new_head_x, new_head_y, SNAKE_SIZE, BLACK); + if (new_head_x == snake_food[0] && new_head_y == snake_food[1]) { snake_food[0] = rand() % SNAKE_MAX; snake_food[1] = rand() % SNAKE_MAX; @@ -77,7 +75,7 @@ void snake_entry(void *parameter) snake_len++; sprintf(tmp, "%d", snake_len); lcd_show_string(100, 105, 32, tmp); - if (snake_len >= SNAKE_MAX-2) + if (snake_len >= SNAKE_MAX) { snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE); snake_tail = (snake_tail + 1) % (SNAKE_MAX); @@ -85,8 +83,18 @@ void snake_entry(void *parameter) } else { - snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE); + if (snake_list[snake_tail][0] == snake_food[0] && snake_list[snake_tail][1] == snake_food[1]) + { + } + else + { + snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE); + } snake_tail = (snake_tail + 1) % (SNAKE_MAX); } + snake_head = (snake_head + 1) % (SNAKE_MAX); + snake_list[snake_head][0] = new_head_x; + snake_list[snake_head][1] = new_head_y; + rt_thread_mdelay(900); } }