贪吃蛇但颜色位置有点问题

This commit is contained in:
james 2024-08-04 14:43:38 +08:00
parent f4b9e5d8bd
commit c7b78edbf8
6 changed files with 126 additions and 7 deletions

View File

@ -18,6 +18,7 @@
#include <rtdbg.h>
#include <drv_lcd.h>
#include "my_func.h"
/* 配置 LED 灯引脚 */
@ -26,9 +27,9 @@
#define LCD_MAX 240
int roundxy[4][2] = {
{0, 16},
{0, 0},
{0, LCD_MAX},
{LCD_MAX, 16},
{LCD_MAX, 0},
{LCD_MAX, LCD_MAX},
};
int xymove[4][2] = {
@ -52,11 +53,7 @@ void mytime()
}
lcd_show_string(2, 2, 16, ctime((const time_t *)&cur_time));
}
void lcd_black(int x, int y)
{
lcd_address_set(x, y, x, y);
lcd_write_half_word(BLACK);
}
void xy_round(int x, int y, int x2, int y2, int r, int ii)
{
// rt_kprintf("x:%d,y:%d,x2:%d,y2:%d,r:%d\n", x, y, x2, y2, r);
@ -77,6 +74,7 @@ void xy_round(int x, int y, int x2, int y2, int r, int ii)
}
void my_round(int r)
{
// 这个范围涂黑
lcd_fill(0, 0, roundxy[2][0], roundxy[2][1],BLACK);
lcd_write_half_word(BLACK);
@ -104,6 +102,7 @@ void xy_sink()
// }
int main(void)
{
// system("snake game");
char str[] = "wifi join Dong abcd07691234";
my_round(20);
rt_wlan_config_autoreconnect(RT_TRUE);

30
dayandnight/my_func.c Normal file
View File

@ -0,0 +1,30 @@
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <stdlib.h>
#include <drv_lcd.h>
void lcd_black(int x, int y)
{
lcd_address_set(x, y, x, y);
lcd_write_half_word(BLACK);
}
void lcd_white(int x, int y)
{
lcd_address_set(x, y, x, y);
lcd_write_half_word(WHITE);
}
void snake_address(int x, int y,int r,const rt_uint16_t da)
{
lcd_fill(x*r, y*r, x*r+r-1, y*r+r-1,da);
lcd_write_half_word(da);
}
// bt 命令行
int color_cmd(int argc, char **argv)
{
snake_address(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]),BLACK);
return 1;
}
MSH_CMD_EXPORT_ALIAS(color_cmd, snk, bt );

17
dayandnight/my_func.h Normal file
View File

@ -0,0 +1,17 @@
#include <drv_lcd.h>
#include <rtthread.h>
#include <stdio.h>
#include <malloc.h>
void lcd_black(int x, int y);
void lcd_white(int x, int y);
void snake_address(int x, int y, int r, const rt_uint16_t da);
// typedef int QDataType;
// typedef struct QListNode
// {
// QDataType data;
// struct QListNode* next;
// } QueueNode;

View File

@ -9,6 +9,7 @@
#include <ap3216c.h>
#include <dfs_posix.h>
#include <drv_lcd.h>
#include "mysnake.h"
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
@ -282,6 +283,7 @@ static void mqtt_example_main(void *parameter)
#define THREAD_TIMESLICE 5
rt_thread_t MQTT_Thread = RT_NULL;
rt_thread_t Snake_Thread = RT_NULL;
void ath_init(void)
{
@ -313,6 +315,20 @@ int ap3_init(void){
return 0;
}
void snk_init(void)
{
Snake_Thread = rt_thread_create("Snake_Thread", snake_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (Snake_Thread != RT_NULL)
{
rt_thread_startup(Snake_Thread);
}
else
{
rt_kprintf("Snake Thread Create Failed!\n");
}
}
MSH_CMD_EXPORT_ALIAS(snk_init, snake, "snake game");
// void i20_init(void)
// {
// const char* i2c_bus_name = "i2c2";

51
dayandnight/mysnake.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <rtthread.h>
#include <stdbool.h>
#include "my_func.h"
#include "mysnake.h"
#define LCD_MAX 240
#define SNAKE_SIZE 20
#define SNAKE_MAX LCD_MAX / SNAKE_SIZE
// bool snake_table[SNAKE_MAX][SNAKE_MAX] = {0};
// struct My_snake
// {
// int x;
// int y;
// }snake_list[SNAKE_MAX*SNAKE_MAX] = {0};
// /* rt_event_t 是指向事件结构体的指针类型 */
// typedef struct My_snake* my_snake_t;
void snake_entry(void)
{
int snake_list[SNAKE_MAX][2] = {0};
int snake_direction[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; // 上,下,左,右
int snake_head = 2, snake_tail = 0, former_head; // 蛇头,蛇尾
int now_direction = 3; // 当前方向
snake_list[1][0] = SNAKE_MAX / 2;
snake_list[1][1] = SNAKE_MAX / 2;
snake_list[0][0] = snake_list[1][0] - 1;
snake_list[0][1] = snake_list[1][1];
snake_list[2][0] = snake_list[1][0] + 1;
snake_list[2][1] = snake_list[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[2][0]][snake_list[2][1]] = 1;
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[2][0], snake_list[2][1], SNAKE_SIZE, BLACK);
int xx = 5;
while (1)
{
rt_thread_mdelay(500);
snake_address(snake_list[snake_tail][0], snake_list[snake_tail][1], SNAKE_SIZE, WHITE);
former_head = snake_head;
snake_head = (snake_head + 1) % (SNAKE_MAX);
snake_tail = (snake_tail + 1) % (SNAKE_MAX);
snake_list[snake_head][0] = snake_list[former_head][0] + snake_direction[now_direction][0];
snake_list[snake_head][1] = snake_list[former_head][1] + snake_direction[now_direction][1];
snake_address(snake_list[snake_head][0], snake_list[snake_head][1], SNAKE_SIZE, BLACK);
}
}

6
dayandnight/mysnake.h Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
#include <rtthread.h>
#include <stdbool.h>
#include "my_func.h"
void snake_entry(void);