一.前言
1.使用到的屏幕是TFT屏,如果是OLED屏需要改一下显示的接口
2.上下左右四个按键按照自己的实际硬件配置就行
3.我是在rt-thread例程上改的,部分延时函数等需要另外实现
二.完整代码
#include <rtdevice.h>
#include <board.h>
#include <drv_lcd.h>
#include <rttlogo.h>
#include "stm32l4xx_hal.h"
#include <stdlib.h>
#include <time.h>
#include <rtdbg.h>
#ifndef bool
#define bool _Bool
#define true 1
#define false 0
#endif
// 假设的Direction枚举定义
typedef enum {
Direction_Up,
Direction_Down,
Direction_Left,
Direction_Right,
} Direction;
Direction dir = Direction_Right;
// 屏幕尺寸
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 240
// 蛇的尺寸
#define SNAKE_SIZE 10
// 更新间隔时间
#define DELAY_MS 100
#define MAX_SNAKE_LENGTH 100 // 假设蛇的最大长度
typedef struct {
int x;
int y;
} Point;
// 初始化蛇体数组
Point snakeBody[MAX_SNAKE_LENGTH];
int snakeLength = 0; // 记录蛇的实际长度
Point generateRandomFoodPosition(const Point snakeBody[], int length);
bool isPositionInSnake(const Point *position, const Point snakeBody[], int length);
void initSnake(Point snakeBody[], int* snakeLength);
void resetSnake(Point snakeBody[], int* snakeLength, Direction* dir);
int updateSnake(Point snakeBody[], int* snakeLength, Direction dir, Point* food, bool* hasEaten);
//3x3像素合并的绘制函数
void lcd_draw_super_pixel(int x, int y, uint16_t color) {
// 计算超像素左上角的实际像素坐标
int superPixelX = x * 3;
int superPixelY = y * 3;
// 填充3x3像素块

最低0.47元/天 解锁文章
912

被折叠的 条评论
为什么被折叠?



