|
【NXP Rapid IoT评测】经典贪吃蛇 纯手打
[复制链接]
NXP Rapid IoT 原来调用的是EWWIM做显示架构的,利用原来的架构,通过 ATMO_Status_t GluttonousSnake_onDisplayed(ATMO_Value_t *in, ATMO_Value_t *out)进行自己的任务
- ATMO_Status_t GluttonousSnake_onDisplayed(ATMO_Value_t *in, ATMO_Value_t *out) {
- SX9500Touch_DisableAbilityHandle();
- GluttonousSnakeInit();
- ATMO_PLATFORM_DebugUsbPrint("%s\r\n",__FUNCTION__);
- return ATMO_Status_Success;
-
- }
复制代码
GluttonousSnake 是在 Rapid IOT Studio 拖拉出来的显示模块的命名,根据自己实际情况进行修改。
现在只做了一个架框,蛇可以动,能吃食物,大概效果如果
画边界
- //画一个蛇的最小单位
- uint16_t Trun_On_Point(uint16_t x, uint16_t y, uint16_t size) {
- GUI_FillRect(x, y, x + size, y + size);
- return size;
- }
- //擦去一个蛇的最小单位
- uint16_t Trun_Off_Point(uint16_t x, uint16_t y, uint16_t size) {
- GUI_ClearRect(x, y, x + size, y + size);
- return size;
- }
- static void CreateMap(void) {
- uint16_t ucV_XSize = 0;
- uint16_t ucV_YSize = 0;
- uint16_t i = 0;
- ucV_XSize = LCD_GetXSize();
- ucV_YSize = LCD_GetYSize();
- //ATMO_PLATFORM_DebugUsbPrint("LCD X:%d Y:%d\r\n",ucV_XSize,ucV_YSize);
- GUI_SetColor(GUI_RED); //画笔颜色
- //GUI_SetPenSize(8); //设置笔大小,5pixel粗线
- //GUI_DrawRect(2,2,ucV_XSize-2,ucV_YSize-2);
- //GUI_DrawLine(0,8, ucV_XSize-8, 8);
- for (i = 0; i < ucV_XSize; i += 4) { //打印上下边框
- //GUI_DispStringAt("A", i, 0);
- //GUI_DispStringAt("A", i, ucV_YSize);
- Trun_On_Point(i, 0, 2);
- Trun_On_Point(i, ucV_YSize - 1 - 2, 2);
- }
- for (i = 0; i < ucV_YSize; i += 4) { //打印左右边框
- //GUI_DispStringAt("A", 0, i);
- //GUI_DispStringAt("A", ucV_XSize, i);
- Trun_On_Point(0, i, 2);
- Trun_On_Point(ucV_XSize - 1 - 2, i, 2);
- }
- //GUI_Exec();
- }
复制代码
画蛇与按键的操作回调函数
- static void Snake_Init(void) {
- uint8_t i;
- snake.longth = 3; //初始化长度为3
- snake.isLive = 1; //蛇是活着的
- snake.score = 0; //当前的分为0
- food.flag = 1;
- for (i = 0; i < snake.longth; i++) {
- snake.XPos[i] = V_Snake_Size * 5 + V_Snake_Size * i;
- snake.YPos[i] = V_Snake_Size * 5;
- }
- for (i = 0; i < snake.longth; i++) {
- Trun_On_Point(snake.XPos[i], snake.YPos[i], V_Snake_Size);
- }
- ATMO_SX9500_RegisterTouchedCallback(SX9500_Touched_Down, SnakeDirKeyDown);
- ATMO_SX9500_RegisterTouchedCallback(SX9500_Touched_Up, SnakeDirKeyUp);
- ATMO_SX9500_RegisterTouchedCallback(SX9500_Touched_Left, SnakeDirKeyLeft);
- ATMO_SX9500_RegisterTouchedCallback(SX9500_Touched_Right, SnakeDirKeyRight);
- }
- static void SnakeDirKeyLeft(void *value) {
- uint8_t ucV_Key = 0;
- ucV_Key = V_Dir_Left;
- //ATMO_PLATFORM_DebugUsbPrint("%s\r\n",__FUNCTION__);
- xQueueSend(hGlutonousSnakeKeyQueue, (void * ) &ucV_Key, (TickType_t )10);
- }
- static void SnakeDirKeyRight(void *value) {
- uint8_t ucV_Key = 0;
- ucV_Key = V_Dir_Right;
- //ATMO_PLATFORM_DebugUsbPrint("%s\r\n", __FUNCTION__);
- xQueueSend(hGlutonousSnakeKeyQueue, (void * ) &ucV_Key, (TickType_t )10);
- }
- static void SnakeDirKeyDown(void *value) {
- uint8_t ucV_Key = 0;
- ucV_Key = V_Dir_Down;
- //ATMO_PLATFORM_DebugUsbPrint("%s\r\n", __FUNCTION__);
- xQueueSend(hGlutonousSnakeKeyQueue, (void * ) &ucV_Key, (TickType_t )10);
- }
- static void SnakeDirKeyUp(void *value) {
- uint8_t ucV_Key = 0;
- ucV_Key = V_Dir_Up;
- //ATMO_PLATFORM_DebugUsbPrint("%s\r\n", __FUNCTION__);
- xQueueSend(hGlutonousSnakeKeyQueue, (void * ) &ucV_Key, (TickType_t )10);
- }
复制代码
蛇运行
- bool SnakeIsDead(void) {
- if ((snake.XPos[snake.longth - 1] <= 1)
- || (snake.XPos[snake.longth - 1] >= LCD_GetXSize())
- || (snake.YPos[snake.longth - 1] <= 1)
- || (snake.YPos[snake.longth - 1] >= LCD_GetYSize())) {
- snake.isLive = 0;
- }
- if (snake.isLive != 0) {
- return false;
- }
- return false;
- }
- void Snake_Run(uint8_t ucV_Dir) {
- uint8_t i;
- //for (i = 0; i < snake.longth; i++) {
- // Trun_Off_Point(snake.XPos[i], snake.YPos[i], V_Snake_Size);
- //}
- Trun_Off_Point(snake.XPos[0], snake.YPos[0], V_Snake_Size); //去尾
- for (i = 0; i < snake.longth - 1; i++) {
- snake.XPos[i] = snake.XPos[i + 1];
- snake.YPos[i] = snake.YPos[i + 1];
- }
- switch (ucV_Dir) {
- case V_Dir_Right: {
- snake.XPos[snake.longth - 1] = snake.XPos[snake.longth - 2]
- + V_Snake_Size + 1;
- snake.YPos[snake.longth - 1] = snake.YPos[snake.longth - 2] + 0;
- //Trun_On_Point(snake.XPos[snake.longth - 1], snake.YPos[snake.longth - 1], V_Snake_Size);
- break;
- }
- case V_Dir_Left: {
- snake.XPos[snake.longth - 1] = snake.XPos[snake.longth - 2]
- - V_Snake_Size - 1;
- snake.YPos[snake.longth - 1] = snake.YPos[snake.longth - 2] + 0;
- //Trun_On_Point(snake.XPos[snake.longth - 1], snake.YPos[snake.longth - 1], V_Snake_Size);
- break;
- }
- case V_Dir_Down: {
- snake.XPos[snake.longth - 1] = snake.XPos[snake.longth - 2] + 0;
- snake.YPos[snake.longth - 1] = snake.YPos[snake.longth - 2]
- + V_Snake_Size + 1;
- //Trun_On_Point(snake.XPos[snake.longth - 1], snake.YPos[snake.longth - 1], V_Snake_Size);
- break;
- }
- case V_Dir_Up: {
- snake.XPos[snake.longth - 1] = snake.XPos[snake.longth - 2] + 0;
- snake.YPos[snake.longth - 1] = snake.YPos[snake.longth - 2]
- - V_Snake_Size - 1;
- //Trun_On_Point(snake.XPos[snake.longth - 1], snake.YPos[snake.longth - 1], V_Snake_Size);
- break;
- }
- default:
- break;
- }
- //for (i = 0; i < snake.longth; i++) {
- // Trun_On_Point(snake.XPos[i], snake.YPos[i], V_Snake_Size);
- //}
- if (SnakeIsDead()) {
- for (i = 0; i < snake.longth; i++) {
- Trun_Off_Point(snake.XPos[i], snake.YPos[i], V_Snake_Size);
- }
- } else {
- Trun_On_Point(snake.XPos[snake.longth - 1],
- snake.YPos[snake.longth - 1], V_Snake_Size);
- }
- }
复制代码
蛇吃食物
- void Eat_Food(void) {
- //uint8_t length[20]; //用于保存要显示的分数和长度
- uint8_t i;
- /*************************判断蛇是否吃到食物**********************/
- //if ((snake.XPos[snake.longth - 1] == food.x)
- // && (snake.YPos[snake.longth - 1] == food.y)) {
- if ( (D_IsInCCInterval(snake.XPos[snake.longth - 1], food.x,
- snake.XPos[snake.longth - 1] +V_Snake_Size)
- && D_IsInCCInterval(snake.YPos[snake.longth - 1], food.y,
- snake.YPos[snake.longth - 1] +V_Snake_Size))
- || ( D_IsInCCInterval(food.x , snake.XPos[snake.longth - 1] , food.x +V_Snake_Size)
- && D_IsInCCInterval(food.y, snake.YPos[snake.longth - 1],
- food.y +V_Snake_Size)) ) {
- snake.longth++;
- snake.score += 5;
- snake.XPos[snake.longth - 1] = food.x;
- snake.YPos[snake.longth - 1] = food.y;
- Trun_On_Point(snake.XPos[snake.longth - 1],
- snake.YPos[snake.longth - 1], V_Snake_Size);
- food.flag = 1;
- }
- if (food.flag == 1) {
- while (1) {
- food.x =
- rand() % (LCD_GetXSize() - V_Snake_Size * 2) + V_Snake_Size;
- food.y =
- rand() % (LCD_GetYSize() - V_Snake_Size * 2) + V_Snake_Size;
- for (i = 0; i < snake.longth; i++) { //判断产生的食物是否和蛇身重合
- if ((food.x == snake.XPos[i]) && (food.y == snake.YPos[i]))
- break;
- }
- if (i == snake.longth) {
- ATMO_PLATFORM_DebugUsbPrint("Create Food:%d %d\r\n", food.x,
- food.y);
- food.flag = 0;
- Trun_On_Point(food.x, food.y, V_Snake_Size);
- break;
- }
- }
- }
- }
复制代码
对应的任务函数
- void GluttonousSnakeTask(uint32_t param) {
- BaseType_t xResult;
- const TickType_t xMaxBlockTime = pdMS_TO_TICKS(300); /* 设置最大等待时间为 300ms */
- uint8_t ucQueueKeyMsg;
- /* Initialize GUI */
- while (1) {
- xResult = xQueueReceive(hGlutonousSnakeKeyQueue, /* 消息队列句柄 */
- (void *)&ucQueueKeyMsg, /* 存储接收到的数据到变量 ucQueueMsgValue 中 */
- (TickType_t)xMaxBlockTime);/* 设置阻塞时间 */
- //vTaskDelay((TickType_t)xMaxBlockTime);
- if (xResult == pdPASS) {
- Snake_Run(ucQueueKeyMsg);
- Eat_Food();
- }
- }
- }
复制代码
此内容由EEWORLD论坛网友dvd1478原创,如需转载或用于商业用途需征得作者同意并注明出处
|
|