一些注释

This commit is contained in:
james 2024-08-12 22:40:38 +08:00
parent f7fdcaced2
commit 0701d37776
2 changed files with 26 additions and 19 deletions

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
#define PAGE_MAX 4 #define PAGE_MAX 4
#define LCD_MAX 240
#define EVENT_MQTT_ENABLE (1<<3) #define EVENT_MQTT_ENABLE (1<<3)
// struct rt_event my_event; // struct rt_event my_event;

View File

@ -6,9 +6,9 @@
#include <time.h> #include <time.h>
#include <rtatomic.h> #include <rtatomic.h>
#define LCD_MAX 240 #define LCD_MAX 240 //屏幕大小(长宽像素)
#define SNAKE_SIZE 20 #define SNAKE_SIZE 40 //蛇身大小(格子大小)
#define SNAKE_MAX (LCD_MAX / SNAKE_SIZE) #define SNAKE_MAX (LCD_MAX / SNAKE_SIZE) //每行格子数(最大蛇长+1
rt_atomic_t now_direction = 3; rt_atomic_t now_direction = 3;
rt_atomic_t snake_pressed = 0; rt_atomic_t snake_pressed = 0;
extern rt_atomic_t page_chosen; extern rt_atomic_t page_chosen;
@ -32,13 +32,13 @@ void snake_entry(void *parameter)
time_t t; time_t t;
/* 初始化随机数发生器 */ /* 初始化随机数发生器 */
srand((unsigned)time(&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_direction[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; // 上,左,下,右
int snake_food[2]; int snake_food[2];//果子位置
bool food_flag = false; char snake_dirshow[4][7] = {"upup", "left", "down", "rigt"};//显示操作用词
char snake_dirshow[4][7] = {"upup", "left", "down", "rigt"}; char tmp[10];//用于字符串转换
char tmp[10];
int snake_head = 2, snake_tail = 0; // 蛇头,蛇尾 int snake_head = 2, snake_tail = 0; // 蛇头,蛇尾
//设置初始位置
snake_list[1][0] = SNAKE_MAX / 2; snake_list[1][0] = SNAKE_MAX / 2;
snake_list[1][1] = SNAKE_MAX / 2; snake_list[1][1] = SNAKE_MAX / 2;
snake_list[0][0] = snake_list[1][0] - 1; snake_list[0][0] = snake_list[1][0] - 1;
@ -48,19 +48,20 @@ void snake_entry(void *parameter)
// snake_table[snake_list[0][0]][snake_list[0][1]] = 1; // snake_table[snake_list[0][0]][snake_list[0][1]] = 1;
// snake_table[snake_list[1][0]][snake_list[1][1]] = 1; // snake_table[snake_list[1][0]][snake_list[1][1]] = 1;
// snake_table[snake_list[2][0]][snake_list[2][1]] = 1; // snake_table[snake_list[2][0]][snake_list[2][1]] = 1;
snake_address(snake_list[0][0], snake_list[0][1], SNAKE_SIZE, BLACK); // snake_address(snake_list[0][0], snake_list[0][1], SNAKE_SIZE, BLACK);
snake_address(snake_list[1][0], snake_list[1][1], SNAKE_SIZE, BLACK); // snake_address(snake_list[1][0], snake_list[1][1], SNAKE_SIZE, BLACK);
snake_address(snake_list[2][0], snake_list[2][1], SNAKE_SIZE, BLACK); // snake_address(snake_list[2][0], snake_list[2][1], SNAKE_SIZE, BLACK);
snake_food[0] = rand() % SNAKE_MAX; snake_food[0] = rand() % SNAKE_MAX;
snake_food[1] = rand() % SNAKE_MAX; snake_food[1] = rand() % SNAKE_MAX;
// snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN); // snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN);
int new_head_x = 0, new_head_y = 0; int new_head_x = 0, new_head_y = 0;//新的蛇头位置
int new_direction = 0,snake_now=0; int new_direction = 0,snake_now=0;//随机生成的新方向,重新显示时当前位置的队列编号
while (1) while (1)
{ {
//当页面符合且不处于暂停状态才运行
if (page_chosen == 1&&!page_stop) if (page_chosen == 1&&!page_stop)
{ {
// 刚切换页面过来,重新显示
if (page_first == 1) if (page_first == 1)
{ {
rt_kprintf("page:snake\n"); rt_kprintf("page:snake\n");
@ -74,10 +75,10 @@ void snake_entry(void *parameter)
snake_address(snake_list[snake_now][0], snake_list[snake_now][1], SNAKE_SIZE, BLACK); snake_address(snake_list[snake_now][0], snake_list[snake_now][1], SNAKE_SIZE, BLACK);
}while(snake_now!=snake_head); }while(snake_now!=snake_head);
} }
//如果长期约3*SNAKE_MAX没有按键则随机改变方向
if (!snake_pressed) if (!snake_pressed)
{ {
// 50%的概率保持当前方向20%的概率随机改变方向 // 50%的概率保持当前方向
if (rand() % 100 < 50) if (rand() % 100 < 50)
{ {
new_direction = rand() % 3; new_direction = rand() % 3;
@ -89,24 +90,27 @@ void snake_entry(void *parameter)
rt_atomic_add(&snake_pressed, -1); rt_atomic_add(&snake_pressed, -1);
} }
// 计算新蛇头位置、并显示坐标
new_head_x = (snake_list[snake_head][0] + snake_direction[now_direction][0] + 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); new_head_y = (snake_list[snake_head][1] + snake_direction[now_direction][1] + SNAKE_MAX) % (SNAKE_MAX);
rt_sprintf(tmp, "(%d,%d)", new_head_x, new_head_y); rt_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]); // 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, 20, 16, snake_dirshow[now_direction]);
lcd_show_string(20 + 16 * 4, 20, 16, tmp); lcd_show_string(20 + 16 * 4, 20, 16, tmp);
snake_address(new_head_x, new_head_y, SNAKE_SIZE, BLACK); snake_address(new_head_x, new_head_y, SNAKE_SIZE, BLACK);
// 蛇头碰到果子
if (new_head_x == snake_food[0] && new_head_y == snake_food[1]) if (new_head_x == snake_food[0] && new_head_y == snake_food[1])
{ {
//新生成、显示果子
snake_food[0] = rand() % SNAKE_MAX; snake_food[0] = rand() % SNAKE_MAX;
snake_food[1] = rand() % SNAKE_MAX; snake_food[1] = rand() % SNAKE_MAX;
snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN); snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN);
// 长度增加、打印
snake_len++; snake_len++;
rt_sprintf(tmp, "%d", snake_len); rt_sprintf(tmp, "%d", snake_len);
lcd_show_string(100, 105, 32, tmp); lcd_show_string(100, 105, 32, tmp);
// 防止蛇咬尾出现bug // 防止蛇咬尾出现bug限制最长为SNAKE_MAX-1将蛇尾涂白
if (snake_len >= SNAKE_MAX) if (snake_len >= SNAKE_MAX)
{ {
snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE); snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE);
@ -115,6 +119,7 @@ void snake_entry(void *parameter)
} }
else else
{ {
//若果子在蛇尾则不涂白
if (snake_list[snake_tail][0] == snake_food[0] && snake_list[snake_tail][1] == snake_food[1]) if (snake_list[snake_tail][0] == snake_food[0] && snake_list[snake_tail][1] == snake_food[1])
{ {
} }
@ -124,6 +129,7 @@ void snake_entry(void *parameter)
} }
snake_tail = (snake_tail + 1) % (SNAKE_MAX); snake_tail = (snake_tail + 1) % (SNAKE_MAX);
} }
//记录新的蛇头
snake_head = (snake_head + 1) % (SNAKE_MAX); snake_head = (snake_head + 1) % (SNAKE_MAX);
snake_list[snake_head][0] = new_head_x; snake_list[snake_head][0] = new_head_x;
snake_list[snake_head][1] = new_head_y; snake_list[snake_head][1] = new_head_y;