当前位置: 首页 > news >正文

基于STM32的恐龙小跳与躲避障碍游戏

一、项目概述

本项目在STM32平台上实现两款经典小游戏:恐龙小跳(类似Chrome离线游戏)和躲避障碍小游戏。两款游戏均使用OLED显示屏(SSD1306)和简单按键控制,具有完整的游戏逻辑、动画效果和计分系统。

二、系统架构

+-------------------+     +-------------------+     +-------------------+
|    输入处理模块    |     |    游戏逻辑模块    |     |    显示驱动模块    |
| (按键扫描与消抖)   |<--->| (双游戏状态机)     |<--->| (OLED显示控制)    |
+-------------------+     +-------------------+     +-------------------+|  |  |  |  |  |  |  |  |  |  |  |  ||  |  |  |  |  |  |  |  |  |  +---->+-------------------+|  |  |  |  |  |  |  |  |  +-------->|  游戏状态管理     ||  |  |  |  |  |  |  |  +------------>|  分数计算         ||  |  |  |  |  |  |  +---------------->|  动画效果         ||  |  |  |  |  |  +-------------------->|  音效反馈(可选)   ||  |  |  |  |  +-------------------------------->|                 ||  |  |  |  +------------------------------------>|                 ||  |  |  +---------------------------------------->|                 ||  |  +-------------------------------------------->|                 ||  +--------------------------------------------------->|                 |+--------------------------------------------------->|                 |

三、硬件配置

  • 主控芯片:STM32F103C8T6

  • 显示模块:0.96寸OLED(SSD1306,128×64,I2C)

  • 输入设备

    • 按键1:游戏选择/跳跃/上移

    • 按键2:开始/下移/左移

    • 按键3:返回/右移

  • 音频输出:无源蜂鸣器(可选)

四、游戏1:恐龙小跳(Dino Jump)

1. 游戏机制

  • 玩家控制恐龙跳跃躲避仙人掌

  • 随着分数增加,游戏速度逐渐加快

  • 碰撞仙人掌游戏结束

2. 核心代码实现

// dino.h
#define DINO_WIDTH 8
#define DINO_HEIGHT 8
#define GROUND_Y 56
#define GRAVITY 1
#define JUMP_FORCE -12typedef struct {int x, y;           // 位置int vel_y;          // 垂直速度int is_jumping;     // 跳跃状态int width, height;  // 尺寸
} Dino;typedef struct {int x;              // 位置int width, height;  // 尺寸
} Cactus;// dino.c
void InitDino(Dino* dino) {dino->x = 20;dino->y = GROUND_Y - DINO_HEIGHT;dino->vel_y = 0;dino->is_jumping = 0;dino->width = DINO_WIDTH;dino->height = DINO_HEIGHT;
}void UpdateDino(Dino* dino) {// 应用重力dino->vel_y += GRAVITY;dino->y += dino->vel_y;// 地面碰撞检测if (dino->y > GROUND_Y - DINO_HEIGHT) {dino->y = GROUND_Y - DINO_HEIGHT;dino->vel_y = 0;dino->is_jumping = 0;}
}void JumpDino(Dino* dino) {if (!dino->is_jumping) {dino->vel_y = JUMP_FORCE;dino->is_jumping = 1;}
}void InitCactus(Cactus* cactus) {cactus->x = 128;cactus->width = 6;cactus->height = 12;
}void UpdateCactus(Cactus* cactus, int speed) {cactus->x -= speed;if (cactus->x < -10) {cactus->x = 128;}
}int CheckCollision(Dino* dino, Cactus* cactus) {return (dino->x < cactus->x + cactus->width &&dino->x + dino->width > cactus->x &&dino->y < GROUND_Y &&dino->y + dino->height > GROUND_Y - cactus->height);
}

五、游戏2:躲避障碍(Obstacle Avoidance)

1. 游戏机制

  • 玩家控制飞船左右移动躲避陨石

  • 随机生成不同大小和速度的陨石

  • 随着时间增加,陨石数量增多

  • 被陨石击中游戏结束

2. 核心代码实现

// obstacle.h
#define SHIP_WIDTH 12
#define SHIP_HEIGHT 8
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64typedef struct {int x, y;           // 位置int width, height;  // 尺寸
} Ship;typedef struct {int x, y;           // 位置int width, height;  // 尺寸int speed;          // 速度
} Asteroid;// obstacle.c
void InitShip(Ship* ship) {ship->x = SCREEN_WIDTH / 2 - SHIP_WIDTH / 2;ship->y = SCREEN_HEIGHT - SHIP_HEIGHT - 5;ship->width = SHIP_WIDTH;ship->height = SHIP_HEIGHT;
}void MoveShip(Ship* ship, int dir) {ship->x += dir * 5;if (ship->x < 0) ship->x = 0;if (ship->x > SCREEN_WIDTH - SHIP_WIDTH) ship->x = SCREEN_WIDTH - SHIP_WIDTH;
}void GenerateAsteroid(Asteroid* ast) {ast->x = SCREEN_WIDTH;ast->y = rand() % (SCREEN_HEIGHT - 20) + 10;ast->width = rand() % 10 + 5;ast->height = rand() % 10 + 5;ast->speed = rand() % 3 + 1;
}void UpdateAsteroid(Asteroid* ast) {ast->x -= ast->speed;
}int CheckHit(Ship* ship, Asteroid* ast) {return (ship->x < ast->x + ast->width &&ship->x + ship->width > ast->x &&ship->y < ast->y + ast->height &&ship->y + ship->height > ast->y);
}

六、双游戏整合实现

1. 主游戏状态机

// game.h
typedef enum {MENU,DINO_GAME,OBSTACLE_GAME,GAME_OVER
} GameState;typedef struct {GameState state;Dino dino;Cactus cactus;Ship ship;Asteroid asteroids[5];int asteroid_count;int score;int game_speed;int selected_game;
} GameManager;

2. 主游戏循环

// main.c
int main(void) {HAL_Init();SystemClock_Config();OLED_Init();Input_Init();GameManager game = {0};game.state = MENU;game.selected_game = 0; // 0: Dino, 1: Obstaclewhile (1) {ProcessInput(&game);UpdateGame(&game);RenderGame(&game);HAL_Delay(30); // 控制游戏速度}
}void ProcessInput(GameManager* game) {if (game->state == MENU) {if (KeyPressed(KEY_UP)) {game->selected_game = (game->selected_game + 1) % 2;} else if (KeyPressed(KEY_DOWN)) {game->selected_game = (game->selected_game + 1) % 2;} else if (KeyPressed(KEY_ENTER)) {if (game->selected_game == 0) {game->state = DINO_GAME;InitDinoGame(game);} else {game->state = OBSTACLE_GAME;InitObstacleGame(game);}}} else if (game->state == DINO_GAME) {if (KeyPressed(KEY_JUMP)) {JumpDino(&game->dino);}} else if (game->state == OBSTACLE_GAME) {if (KeyPressed(KEY_LEFT)) {MoveShip(&game->ship, -1);} else if (KeyPressed(KEY_RIGHT)) {MoveShip(&game->ship, 1);}} else if (game->state == GAME_OVER) {if (KeyPressed(KEY_ENTER)) {game->state = MENU;}}
}void UpdateGame(GameManager* game) {if (game->state == DINO_GAME) {UpdateDino(&game->dino);UpdateCactus(&game->cactus, game->game_speed);if (CheckCollision(&game->dino, &game->cactus)) {game->state = GAME_OVER;}// 计分if (game->cactus.x < 0) {game->score += 10;game->game_speed = 3 + game->score / 100;InitCactus(&game->cactus);}} else if (game->state == OBSTACLE_GAME) {// 随机生成新陨石if (rand() % 20 == 0 && game->asteroid_count < 5) {GenerateAsteroid(&game->asteroids[game->asteroid_count]);game->asteroid_count++;}// 更新所有陨石for (int i = 0; i < game->asteroid_count; i++) {UpdateAsteroid(&game->asteroids[i]);if (CheckHit(&game->ship, &game->asteroids[i])) {game->state = GAME_OVER;}// 移除超出屏幕的陨石if (game->asteroids[i].x < -20) {// 移动最后一个陨石到当前位置if (i < game->asteroid_count - 1) {game->asteroids[i] = game->asteroids[game->asteroid_count - 1];}game->asteroid_count--;}}// 计分game->score++;}
}

七、显示渲染系统

1. 双游戏渲染

void RenderGame(GameManager* game) {OLED_Clear();switch (game->state) {case MENU:RenderMenu(game);break;case DINO_GAME:RenderDinoGame(game);break;case OBSTACLE_GAME:RenderObstacleGame(game);break;case GAME_OVER:RenderGameOver(game);break;}OLED_Refresh();
}void RenderDinoGame(GameManager* game) {// 绘制地面OLED_DrawLine(0, GROUND_Y, 127, GROUND_Y);// 绘制恐龙OLED_DrawRect(game->dino.x, game->dino.y, game->dino.width, game->dino.height);// 绘制仙人掌OLED_DrawRect(game->cactus.x, GROUND_Y - game->cactus.height,game->cactus.width, game->cactus.height);// 显示分数char score_str[20];sprintf(score_str, "Score: %d", game->score);OLED_ShowString(0, 0, score_str);
}void RenderObstacleGame(GameManager* game) {// 绘制飞船OLED_DrawTriangle(game->ship.x, game->ship.y + game->ship.height,game->ship.x + game->ship.width/2, game->ship.y,game->ship.x + game->ship.width, game->ship.y + game->ship.height);// 绘制所有陨石for (int i = 0; i < game->asteroid_count; i++) {OLED_DrawCircle(game->asteroids[i].x + game->asteroids[i].width/2,game->asteroids[i].y + game->asteroids[i].height/2,game->asteroids[i].width/2);}// 显示分数char score_str[20];sprintf(score_str, "Score: %d", game->score);OLED_ShowString(0, 0, score_str);
}

八、游戏特色功能

1. 动画效果

// 恐龙动画(奔跑效果)
void AnimateDino(Dino* dino, int frame) {if (frame % 10 < 5) {// 绘制站立状态OLED_DrawRect(dino->x, dino->y, 8, 8);} else {// 绘制抬腿状态OLED_DrawRect(dino->x, dino->y, 8, 4);OLED_DrawRect(dino->x, dino->y+4, 4, 4);}
}// 爆炸效果
void DrawExplosion(int x, int y) {for (int i = 0; i < 8; i++) {int angle = i * 45;int dx = cos(angle) * 5;int dy = sin(angle) * 5;OLED_DrawPixel(x + dx, y + dy, 1);}
}

2. 音效系统(可选)

// 使用PWM驱动蜂鸣器
void PlaySound(int frequency, int duration) {uint32_t period = 1000000 / frequency;uint32_t cycles = duration * 1000 / period;for (uint32_t i = 0; i < cycles; i++) {HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);Delay_us(period / 2);HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_RESET);Delay_us(period / 2);}
}// 游戏音效
void PlayJumpSound() {PlaySound(523, 50); // C5
}void PlayCrashSound() {PlaySound(196, 200); // G3PlaySound(131, 300); // C3
}

3. 游戏难度曲线

// 动态调整游戏难度
void UpdateDifficulty(GameManager* game) {if (game->state == DINO_GAME) {// 每100分增加速度game->game_speed = 3 + game->score / 100;// 每200分增加障碍物频率if (game->score % 200 == 0) {// 增加新障碍物类型}} else if (game->state == OBSTACLE_GAME) {// 每500分增加陨石数量if (game->score % 500 == 0 && game->asteroid_count < 8) {// 增加陨石数量上限}}
}

参考代码 基于stm32的恐龙小跳游戏+躲避障碍小游戏 www.youwenfan.com/contentcns/183117.html

九、项目资源

1. 硬件连接

外设 STM32引脚 功能
OLED_SDA PB7 I2C数据线
OLED_SCL PB6 I2C时钟线
KEY_UP PA0 上/跳跃
KEY_DOWN PA1 下/选择
KEY_LEFT PA2 左移
KEY_RIGHT PA3 右移
KEY_ENTER PA4 确认/开始
BUZZER PA5 音频输出

2. 开发环境

  • IDE: STM32CubeIDE

  • 编译器: GCC ARM Embedded

  • 库依赖:

  • HAL库

  • SSD1306 OLED驱动

  • 标准外设库

3. 项目结构

├── Core/
│   ├── Inc/
│   │   ├── game.h
│   │   ├── dino.h
│   │   ├── obstacle.h
│   │   └── oled.h
│   ├── Src/
│   │   ├── main.c
│   │   ├── game.c
│   │   ├── dino.c
│   │   ├── obstacle.c
│   │   └── oled.c
├── Drivers/
└── STM32F1xx_HAL_Driver/

十、总结

本实现展示了如何在STM32平台上开发两款经典小游戏:恐龙小跳和躲避障碍。项目特点包括:

  1. 双游戏整合:通过状态机实现两款游戏的无缝切换

  2. 完整游戏机制:包含角色控制、碰撞检测、计分系统和难度曲线

  3. 动画效果:实现角色动画和特效

  4. 可扩展性:模块化设计便于添加新游戏或功能

  5. 资源优化:针对嵌入式平台优化资源使用

http://www.jsqmd.com/news/513034/

相关文章:

  • 深入浅出:DeepSeek-OCR、C3、VIST三种大模型Token压缩技术路线,带你理解压缩即智能
  • 在C# 上位机开发中,性能和响应速度直接决定系统的实时性、稳定性与用户体验,特别是在工业 HMI/SCADA、设备监控、生产线控制等场景下,毫秒级的延迟都可能导致误
  • 积分商城小程序如何制作,SaaS积分商城搭建教程 - 码云数智
  • 双系统用户必看:Windows更新后Ubuntu启动失败的急救指南(附详细修复步骤)
  • 线段树技巧进阶
  • B2C单用户外贸商城源码解析:从零搭建到多语言支付集成
  • Qwen3-32B-Chat百度搜索意图匹配:针对‘Qwen3部署教程‘需求的精准内容覆盖
  • 2026年羊绒衫厂家推荐:高端品牌代工与OEM定制靠谱供应商及合作避坑指南 - 品牌推荐
  • CosyVoice-300M Lite中英混合合成实战:跨语言语音生成教程
  • EEPROMReader:嵌入式系统类型安全的编译期EEPROM管理库
  • Qwen3.5-9B编码能力实战:Python/SQL/Shell代码生成与调试效果分享
  • 3D动作时序连贯性分析:HY-Motion生成结果专业评估
  • 瑜伽馆小程序制作全流程,怎么自己做小程序 - 码云数智
  • 星露谷农场规划器终极指南:3步打造完美农场布局
  • Cadence vs Synopsys:数字后端工程师的EDA工具选择指南(附实战案例)
  • MGeo模型部署教程:阿里云ECS+GPU实例上稳定运行MGeo-base的完整步骤
  • 机械臂力控(4)---对阻抗和导纳更深层次的理解
  • 永续经营:亚马逊领导者的“守城”与“拓疆”法则
  • 5G时代如何DIY一个宽带圆极化天线?从参数优化到实测效果全记录
  • 从硅视网膜到仿生听觉:类脑传感器DVS/DAS的进化史与开源项目推荐
  • ESP32嵌入式地图库:OSM瓦片加载与双核异步渲染
  • 从零构建自主空中机器人:Ubuntu 20.04 + ROS Noetic 开发环境全攻略
  • 91行代码创意赛:在约束中绽放的编程创造力
  • 找工作的平台有哪些?2026靠谱招聘平台热搜排行榜 - 博客万
  • Nanbeige 4.1-3B惊艳效果:多轮对话中PLAYER蓝色气泡与BOT绿色气泡动态演进
  • Qwen-Image定制镜像开源实操:RTX4090D环境下Qwen-VL微调与推理一体化
  • ChatTTS情感语音合成实战:如何精准设置难过与高兴情绪参数
  • 手把手教你用Dify的‘知识库’功能,把热点数据喂给AI,打造专属的赛道咨询顾问
  • AutoCAD 2024 保姆级安装教程【2025最新】(附安装包)
  • 手把手教你用Comsol模拟超声空化气泡:从模型搭建到网格划分的完整流程