酷代码 AI
菜单
服务商

写一个arduino代码做贪吃蛇小游戏,oled显示屏0.96寸,arduino开发板,摇杆模块,生成注解

```cpp #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define JOYSTICK_X A0 #define JOYSTICK_Y A1 #define JOYSTICK_BUTTON 2 #define SNAKE_MAX_LENGTH 100 #define GRID_SIZE 4 #define GRID_WIDTH (SCREEN_WIDTH / GRID_SIZE) #define GRID_HEIGHT (SCREEN_HEIGHT / GRID_SIZE) struct Point { int x; int y; }; Point snake[SNAKE_MAX_LENGTH]; int snakeLength = 3; Point food; int directionX = 1; int directionY = 0; bool gameOver = false; unsigned long lastMoveTime = 0; const int moveInterval = 150; void setup() { Serial.begin(9600); pinMode(JOYSTICK_BUTTON, INPUT_PULLUP); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("OLED初始化失败")); for(;;); } display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); initializeGame(); } void loop() { if (!gameOver) { readJoystick(); updateGame(); drawGame(); } else { displayGameOver(); if (digitalRead(JOYSTICK_BUTTON) == LOW) { delay(200); initializeGame(); gameOver = false; } } } void initializeGame() { snakeLength = 3; for (int i = 0; i < snakeLength; i++) { snake[i].x = GRID_WIDTH / 2 - i; snake[i].y = GRID_HEIGHT / 2; } directionX = 1; directionY = 0; generateFood(); gameOver = false; } void readJoystick() { int xValue = analogRead(JOYSTICK_X); int yValue = analogRead(JOYSTICK_Y); int newDirectionX = directionX; int newDirectionY = directionY; if (xValue < 300 && directionX != 1) { newDirectionX = -1; newDirectionY = 0; } else if (xValue > 700 && directionX != -1) { newDirectionX = 1; newDirectionY = 0; } if (yValue < 300 && directionY != 1) { newDirectionX = 0; newDirectionY = -1; } else if (yValue > 700 && directionY != -1) { newDirectionX = 0; newDirectionY = 1; } directionX = newDirectionX; directionY = newDirectionY; } void updateGame() { unsigned long currentTime = millis(); if (currentTime - lastMoveTime < moveInterval) { return; } lastMoveTime = currentTime; Point newHead; newHead.x = snake[0].x + directionX; newHead.y = snake[0].y + directionY; if (newHead.x < 0 || newHead.x >= GRID_WIDTH || newHead.y < 0 || newHead.y >= GRID_HEIGHT) { gameOver = true; return; } for (int i = 0; i < snakeLength; i++) { if (snake[i].x == newHead.x && snake[i].y == newHead.y) { gameOver = true; return; } } for (int i = snakeLength; i > 0; i--) { snake[i] = snake[i - 1]; } snake[0] = newHead; if (snake[0].x == food.x && snake[0].y == food.y) { snakeLength++; if (snakeLength >= SNAKE_MAX_LENGTH) { snakeLength = SNAKE_MAX_LENGTH; } generateFood(); } } void generateFood() { bool validPosition = false; while (!validPosition) { food.x = random(0, GRID_WIDTH); food.y = random(0, GRID_HEIGHT); validPosition = true; for (int i = 0; i < snakeLength; i++) { if (snake[i].x == food.x && snake[i].y == food.y) { validPosition = false; break; } } } } void drawGame() { display.clearDisplay(); for (int i = 0; i < snakeLength; i++) { display.fillRect(snake[i].x * GRID_SIZE, snake[i].y * GRID_SIZE, GRID_SIZE, GRID_SIZE, SSD1306_WHITE); } display.fillRect(food.x * GRID_SIZE, food.y * GRID_SIZE, GRID_SIZE, GRID_SIZE, SSD1306_WHITE); display.setCursor(0, 0); display.print("Score: "); display.print(snakeLength - 3); display.display(); } void displayGameOver() { display.clearDisplay(); display.setCursor(20, 20); display.print("GAME OVER"); display.setCursor(10, 35); display.print("Score: "); display.print(snakeLength - 3); display.setCursor(15, 50); display.print("Press Button"); display.display(); } ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 摇杆模块的模拟输入值范围需要根据实际硬件校准,不同模块可能有差异 贪吃蛇移动速度需要根据GRID_SIZE调整,格子太小会导致移动过快 确保食物生成位置不与蛇身重叠,避免无限循环 [2025-12-02 21:55:54 | AI写代码神器 | 1362点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多