尝试使蛇能达到队列最大长度,防止蛇经过果子果子消失,调整main顺序,先运行mysnake

This commit is contained in:
james 2024-08-05 10:19:34 +08:00
parent 45e04c1b87
commit 8d67a458e5
3 changed files with 21 additions and 13 deletions

View File

@ -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"};

View File

@ -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 */

View File

@ -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);
}
}