写一个arduino代码做贪吃蛇小游戏,有边框,能显示分数,oled显示屏0.96寸,arduino UNO开发板,摇杆模块,生成注解和连线图
# Arduino 贪吃蛇游戏 (OLED + 摇杆) ## 硬件连接图 ``` Arduino UNO 连接示意图: OLED显示屏 (SSD1306 128x64): GND -> GND VCC -> 3.3V SCL -> A5 (SCL) SDA -> A4 (SDA) 摇杆模块: GND -> GND +5V -> 5V VRx -> A0 (X轴) VRy -> A1 (Y轴) SW -> D2 (按钮) ``` ## 代码实现 ```cpp #include <SPI.h> #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 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[100]; // 蛇身体 int snakeLength = 3; Point food; int score = 0; int direction = 0; // 0=上, 1=右, 2=下, 3=左 int lastDirection = 0; bool gameOver = false; void setup() { Serial.begin(9600); // 初始化OLED if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306分配失败")); for(;;); } display.display(); delay(2000); display.clearDisplay(); // 初始化摇杆按钮 pinMode(JOYSTICK_BUTTON, INPUT_PULLUP); // 初始化蛇 snake[0] = {GRID_WIDTH/2, GRID_HEIGHT/2}; snake[1] = {GRID_WIDTH/2, GRID_HEIGHT/2+1}; snake[2] = {GRID_WIDTH/2, GRID_HEIGHT/2+2}; // 生成食物 generateFood(); } void loop() { if (!gameOver) { readJoystick(); updateGame(); drawGame(); delay(100); // 控制游戏速度 } else { displayGameOver(); if (digitalRead(JOYSTICK_BUTTON) == LOW) { resetGame(); } } } void readJoystick() { int xValue = analogRead(JOYSTICK_X); int yValue = analogRead(JOYSTICK_Y); // 确定方向 (添加死区避免误触) if (xValue < 400 && abs(yValue - 512) < 200) direction = 3; // 左 else if (xValue > 600 && abs(yValue - 512) < 200) direction = 1; // 右 else if (yValue < 400 && abs(xValue - 512) < 200) direction = 0; // 上 else if (yValue > 600 && abs(xValue - 512) < 200) direction = 2; // 下 // 防止180度转弯 if ((direction == 0 && lastDirection == 2) || (direction == 2 && lastDirection == 0) || (direction == 1 && lastDirection == 3) || (direction == 3 && lastDirection == 1)) { direction = lastDirection; } lastDirection = direction; } void updateGame() { // 移动蛇身 for (int i = snakeLength-1; i > 0; i--) { snake[i] = snake[i-1]; } // 根据方向移动蛇头 switch(direction) { case 0: snake[0].y--; break; // 上 case 1: snake[0].x++; break; // 右 case 2: snake[0].y++; break; // 下 case 3: snake[0].x--; break; // 左 } // 检查是否撞墙 if (snake[0].x < 0 || snake[0].x >= GRID_WIDTH || snake[0].y < 0 || snake[0].y >= GRID_HEIGHT) { gameOver = true; return; } // 检查是否撞到自己 for (int i = 1; i < snakeLength; i++) { if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) { gameOver = true; return; } } // 检查是否吃到食物 if (snake[0].x == food.x && snake[0].y == food.y) { snakeLength++; score++; generateFood(); } } void generateFood() { bool onSnake; do { onSnake = false; food.x = random(0, GRID_WIDTH); food.y = random(0, GRID_HEIGHT); for (int i = 0; i < snakeLength; i++) { if (food.x == snake[i].x && food.y == snake[i].y) { onSnake = true; break; } } } while (onSnake); } void drawGame() { display.clearDisplay(); // 绘制边框 display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, WHITE); // 绘制蛇 for (int i = 0; i < snakeLength; i++) { display.fillRect(snake[i].x * GRID_SIZE, snake[i].y * GRID_SIZE, GRID_SIZE, GRID_SIZE, WHITE); } // 绘制食物 display.fillRect(food.x * GRID_SIZE, food.y * GRID_SIZE, GRID_SIZE, GRID_SIZE, WHITE); // 显示分数 display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(5, 5); display.print("Score: "); display.print(score); display.display(); } void displayGameOver() { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(SCREEN_WIDTH/2-30, SCREEN_HEIGHT/2-10); display.print("GAME OVER"); display.setCursor(SCREEN_WIDTH/2-30, SCREEN_HEIGHT/2); display.print("Score: "); display.print(score); display.setCursor(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2+10); display.print("Press button to restart"); display.display(); } void resetGame() { snakeLength = 3; score = 0; direction = 0; lastDirection = 0; gameOver = false; // 重置蛇位置 snake[0] = {GRID_WIDTH/2, GRID_HEIGHT/2}; snake[1] = {GRID_WIDTH/2, GRID_HEIGHT/2+1}; snake[2] = {GRID_WIDTH/2, GRID_HEIGHT/2+2}; generateFood(); } ``` ## 代码说明 1. 硬件初始化: - 使用I2C协议连接OLED显示屏(SSD1306) - 摇杆模块的X/Y轴连接模拟输入,按钮连接数字输入 2. 游戏逻辑: - 蛇身用数组存储坐标点 - 通过摇杆输入控制方向 - 食物随机生成且不会出现在蛇身上 - 碰撞检测包括墙壁和自身 3. 显示功能: - 实时显示蛇身、食物和分数 - 游戏结束显示最终分数和重启提示 4. 控制优化: - 添加摇杆死区防止误触 - 禁止180度急转弯 ## 注意事项 1. 摇杆模块可能需要校准,如果发现方向不准确,可以调整代码中的阈值(400和600) 2. OLED显示屏的I2C地址通常是0x3C,如果无法显示,尝试0x3D 3. 游戏速度通过delay(100)控制,可根据需要调整数值 [2025-07-06 16:55:56 | AI写代码神器 | 1952点数解答]
- [字节豆包] 年会 1.热场视频:工会2024年度活动集锦 2.馆领导新春致辞 3.串场游戏:听前奏猜歌曲 4.钢琴演奏《我爱你中国》 独舞 (待定) 5.小游戏:每轮6个人。主持人提一个问题,每个人按顺序回答,答案必须是三个字,接不上来或答错的人淘汰,最终留下的人获胜。 6.新职工亮相+拜年 7.合唱表演《星辰大海》 8.串场游戏:听前奏猜歌 9.小游戏:以心传心:每组两人搭档,每轮3组共上场6人。游戏开始时每组的书写者转身看大屏幕显示词。书写者需通过写字或简笔画的方式用手指在搭档后背传达看到的内容,不能出声、不能用手势比划其他多余动作。搭档(画画者)要凭借后背感受到的笔画轨迹,尽可能精准地把对应的词语画出来。绘画过程中,不可询问,独立完成。 10.快板儿表演《战马超》 贯口《小孩子》 11.串场游戏:听前奏猜歌 12.小游戏:改名换姓:每轮8个人。参与者每人给自己想一个昵称(5个字以内)。游戏开始后大家从1-8喊数字,喊到相同数字的要尽快说出对方昵称,说错的人淘汰。 13.本命年职工送祝福 字数:200字(270点数解答 | 2025-01-16 14:21:53)189
- [字节豆包] 年会 1.热场视频:工会2024年度活动集锦 2.馆领导新春致辞 3.串场游戏:听前奏猜歌曲 4.钢琴演奏《我爱你中国》 独舞 (待定) 5.小游戏:每轮6个人。主持人提一个问题,每个人按顺序回答,答案必须是三个字,接不上来或答错的人淘汰,最终留下的人获胜。 6.新职工亮相+拜年 7.合唱表演《星辰大海》 8.串场游戏:听前奏猜歌 9.小游戏:以心传心:每组两人搭档,每轮3组共上场6人。游戏开始时每组的书写者转身看大屏幕显示词。书写者需通过写字或简笔画的方式用手指在搭档后背传达看到的内容,不能出声、不能用手势比划其他多余动作。搭档(画画者)要凭借后背感受到的笔画轨迹,尽可能精准地把对应的词语画出来。绘画过程中,不可询问,独立完成。 10.快板儿表演《战马超》 贯口《小孩子》 11.串场游戏:听前奏猜歌 12.小游戏:改名换姓:每轮8个人。参与者每人给自己想一个昵称(5个字以内)。游戏开始后大家从1-8喊数字,喊到相同数字的要尽快说出对方昵称,说错的人淘汰。 13.本命年职工送祝福 (392点数解答 | 2025-01-16 14:34:56)145
- [字节豆包] #include "stm32f10x.h" // 有问题和需求的,可以联系VX:lwfw********* #include <stdio.h> #include <string.h> #include "sys.h" #include "delay.h" #include "OLED.h" #include "dht11.h" //温湿度传感器 #include "Key.h" #include "jidianqi.h" #include "OLED.h" #include "Serial.h" #include "LED.h" #include "Buzzer.h" #include "Motor.h" #include "CountSensor.h" #include "AD.h" uint16_t AD0; //存储ADC的值 uint8_t KeyNum; //存储按键值 uint8_t RxData; //蓝牙接收到的数据 u8 temp, humi; //存储温湿度 float temp1; //(25点数解答 | 2025-05-06 01:13:43)128
- [字节豆包] *** Using Compiler 'V5.06 update 6 (build 750)', folder: 'E:\keilmdk\ARM\ARMCC\Bin' Rebuild target 'Target 1' compiling oled.c... E:\keilmdk\ARM\PACK\Keil\STM32F1xx_DFP\2.2.0\Device\Include\stm32f10x.h(483): error: #5: cannot open source input file "core_cm3.h": No such file or directory #include "core_cm3.h" oled.c: 0 warnings, 1 error compiling dht11.c... E:\keilmdk\ARM\PACK\Keil\STM32F1xx_DFP\2.2.0\Device\Include\stm32f10x.h(483): error: #5: cannot open source input file "core_cm3.h": No (666点数解答 | 2025-11-12 18:26:21)30
- [字节豆包] *** Using Compiler 'V5.06 update 6 (build 750)', folder: 'E:\keilmdk\ARM\ARMCC\Bin' Rebuild target 'Target 1' compiling oled.c... E:\keilmdk\ARM\PACK\Keil\STM32F1xx_DFP\2.2.0\Device\Include\stm32f10x.h(483): error: #5: cannot open source input file "core_cm3.h": No such file or directory #include "core_cm3.h" oled.c: 0 warnings, 1 error compiling dht11.c... E:\keilmdk\ARM\PACK\Keil\STM32F1xx_DFP\2.2.0\Device\Include\stm32f10x.h(483): error: #5: cannot open source input file "core_cm3.h": No (560点数解答 | 2025-11-12 18:31:47)30
- [阿里通义] 阅读代码完成填空1~7题 import numpy as np # 生成 1000 个服从正态分布的随机整数(均值 100,标准差 8) np.random.seed(42) num1 = np.random.normal( ______, 8, size=1000).reshape(-1,1). ______ #第1、2空 # 生成 1000 个 1 到 10 之间的随机整数 num2 = np.random.randint(1, ______, size=1000).reshape(-1,1) #第3空 # 合并数据 data = np.__________((num1, num2), axis=_________) #第4、5空 # 保存到 CSV 文件,数据间以逗号间隔,保存格式为整数%d np.savetxt("data.csv", data, delimiter="_________", fmt='%d',header="num1,num2", comments="") #第6空 # 读取 CSV 文(506点数解答 | 2025-03-23 14:32:14)244
- [字节豆包] 阅读代码完成填空1~7题 import numpy as np # 生成 1000 个服从正态分布的随机整数(均值 100,标准差 8) np.random.seed(42) num1 = np.random.normal( ______, 8, size=1000).reshape(-1,1). ______ #第1、2空 # 生成 1000 个 1 到 10 之间的随机整数 num2 = np.random.randint(1, ______, size=1000).reshape(-1,1) #第3空 # 合并数据 data = np.__________((num1, num2), axis=_________) #第4、5空 # 保存到 CSV 文件,数据间以逗号间隔,保存格式为整数%d np.savetxt("data.csv", data, delimiter="_________", fmt='%d',header="num1,num2", comments="") #第6空 # 读取 CSV 文(116点数解答 | 2025-03-26 22:22:15)263
- [DeepSeek] 阅读代码完成填空1~7题 import numpy as np # 生成 1000 个服从正态分布的随机整数(均值 100,标准差 8) np.random.seed(42) num1 = np.random.normal( ______, 8, size=1000).reshape(-1,1). ______ #第1、2空 # 生成 1000 个 1 到 10 之间的随机整数 num2 = np.random.randint(1, ______, size=1000).reshape(-1,1) #第3空 # 合并数据 data = np.__________((num1, num2), axis=_________) #第4、5空 # 保存到 CSV 文件,数据间以逗号间隔,保存格式为整数%d np.savetxt("data.csv", data, delimiter="_________", fmt='%d',header="num1,num2", comments="") #第6空 # 读取 CSV 文(178点数解答 | 2025-03-26 22:26:30)312
- [字节豆包] 使用继承,实现“剪刀石头布的游戏”。 小时候很喜欢玩一个游戏,“剪刀石头布”,可以是出拳的形式,或跳格子的形式。现在我们用计算机来玩这个游戏。 电脑用随机数产生剪刀石头布,游戏玩家用输入1,2,3的方式出拳。 游戏玩家输入1或2或3,分别 代表剪刀(1)石头(2)布(3)。 电脑胜出时,显示"winner is computerplayer." 游戏玩家胜出时,显示“winner is personplayer.” 平局时显示"a draw." 函数接口定义: 根据主方法内容,实现三个类的定义,分别是是computerplayer、personplayer、game类。 其中computerplayer、personplayer要继承player类。 根据主方法中的用法,实现game类。 裁判测试程序样例: import java.util.scanner; class player{ string name; player(string name){ this.name = name; } int show() { //出拳方法(451点数解答 | 2024-10-20 19:57:58)373
- [字节豆包] 写一个arduino代码做贪吃蛇小游戏,oled显示屏0.96寸,arduino UNO开发板,摇杆模块,生成注解和连线图 (1745点数解答 | 2025-07-06 15:59:00)100
- [字节豆包] 写一个arduino代码做贪吃蛇小游戏,有边框,能显示分数,oled显示屏0.96寸,arduino UNO开发板,摇杆模块,生成注解和连线图(2196点数解答 | 2025-07-06 16:16:50)83
- [DeepSeek] 写一个arduino代码做贪吃蛇小游戏,有边框,能显示分数,oled显示屏0.96寸,arduino UNO开发板,摇杆模块,生成注解和连线图 (1952点数解答 | 2025-07-06 16:55:56)102