亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

Arduino LCD 电子游戏

组件和用品

面包板(通用)
× 1
跳线(通用)
× 1
Arduino UNO
× 1
USB-A 转 B 线
× 1
旋转电位器(通用)
× 1
模拟操纵杆(通用)
× 1
蜂鸣器
× 1
Adafruit 标准 LCD - 16x2 蓝底白字
× 1

应用和在线服务

Arduino IDE

关于这个项目

这是一个基于我在互联网上找到的视频游戏的项目。该项目具有许多功能,例如当您的角色碰撞或跳过障碍物时发出蜂鸣声的蜂鸣器。此外,为了给它一种电子游戏的感觉,它有一个操纵杆(必须有一个按钮),因为你只需按下操纵杆的按钮即可跳跃。这个视频游戏是关于你猜对了! (一个人跑过障碍物并避开障碍物。)这个游戏的目的是尽可能多地获得积分(你在赛道上的每一秒并避开障碍物就获得一分)。

代码

  • 代码
代码Arduino
#include #define PIN_BUTTON 2#define PIN_AUTOPLAY 1#define PIN_BUZZER 8#define PIN_READWRITE 10#define PIN_CONTRAST 12#define SPRITE_RUN1 1#define SPRITE_RUN2 2#define SPRITE_JUMP_SPRITE_JUMP_JUMP_RE // 使用 '。'头部字符#define SPRITE_JUMP_LOWER 4#define SPRITE_TERRAIN_EMPTY ' ' // 使用' ' 字符#define SPRITE_TERRAIN_SOLID 5#define SPRITE_TERRAIN_SOLID_RIGHT 6#define SPRITE_TERRAIN_SOLID_LEFT 7#define HEROIN_Horizo​​ n 7#define HEROIN_Horizo​​ n 1的英雄位置#define HEROIN_Horizo​​ n 6 TERRAIN_EMPTY 0#define TERRAIN_LOWER_BLOCK 1#define TERRAIN_UPPER_BLOCK 2#define HERO_POSITION_OFF 0 // 英雄不可见#define HERO_POSITION_RUN_LOWER_1 1 // 英雄跑在下排(pose 1)#define HERO_POSITION_RUN_LOWER_2) //define HERO_POSITION_RUN_LOWER_2) //定义HERO_POSITION_OFF 0 / 开始跳跃#define HERO_POSITION_JUMP_2 4 // 中途#define HERO_POSITION_JUMP_3 5 // 跳跃在上排#define HERO_POSITION_JUMP_4 6 // 跳跃在上排#define HERO_POSITION_JUMP_5 7 // 跳跃在上排#define HERO_POSITION_JUMP_JUMP 8 // 跳跃在上排#define HERO_POSITION_JUMP_7 9 // 中途下降#define HERO_POSITION_JUMP_8 10 // 即将着陆#define HERO_POSITION_RUN_UPPER_1 11 // 英雄跑在上排(pose 1)#define HERO_POSITION_RUN_UPPER_2 12 //(pose 2)LiquidCrystal lcd(11, 9, 6, 5, 4, 3);static char terrainUpper[TERRAIN_WIDTH + 1];static char terrainLower [TERRAIN_WIDTH + 1];static bool buttonPushed =false;void initializeGraphics(){ static byte graphics[] ={ // 运行位置 1 B01100, B01100, B00000, B01110, B11100, B01100, B11010, B10011 // 运行位置B01100,B01100,B00000,B01100,B01100,B01100,B01100,B01110,//跳转B01100,B01100,B00000,B11110,B01101,B11111,B10000,B00000,//跳转下B11110,B01101,B11111,B10000,B00000,B00000 , B00000, B00000, // 接地 B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111, // 右侧接地 B00011, B00011, B000010, B000010, B000010, B000010, B000010, B000010, B000010, B000010, 左侧接地, B11000, B11000, B11000, B11000, B11000, B11000, B11000, };国际我; // 跳过使用字符 0,这允许使用 lcd.print() // 快速绘制多个字符 for (i =0; i <7; ++i) { lcd.createChar(i + 1, &graphics[i * 8]); } for (i =0; i  9999)? 5 :(得分> 999) ? 4 :(得分> 99) ? 3 :(得分> 9) ? 2 :1; // 绘制场景terrainUpper[TERRAIN_WIDTH] ='\0'; TerrainLower[TERRAIN_WIDTH] ='\0'; char temp =terrainUpper[16-digits];地形上[16位] ='\0'; lcd.setCursor(0,0);液晶打印(地形上); terrainUpper[16-digits] =temp; lcd.setCursor(0,1);液晶打印(地形下); lcd.setCursor(16 - 数字,0); lcd.print(分数); TerrainUpper[HERO_HORIZONTAL_POSITION] =upperSave; terrainLower[HERO_HORIZONTAL_POSITION] =lowerSave; return collide;}// 将按钮按下作为中断处理void buttonPush() { buttonPushed =true;}void setup(){ pinMode(PIN_READWRITE, OUTPUT);数字写入(PIN_READWRITE,低); pinMode(PIN_CONTRAST,输出);数字写入(PIN_CONTRAST,低); pinMode(PIN_BUTTON,输入);数字写入(PIN_BUTTON,高); pinMode(PIN_AUTOPLAY,输出);数字写入(PIN_AUTOPLAY,高); pinMode(PIN_BUZZER,OUTPUT);//初始化蜂鸣器引脚为输出 digitalWrite(PIN_BUZZER, LOW); // 数字引脚 2 映射到中断 0 attachInterrupt(0/*PIN_BUTTON*/, buttonPush, FALLING);初始化图形(); lcd.begin(16, 2);}void loop(){ 静态字节 heroPos =HERO_POSITION_RUN_LOWER_1;静态字节 newTerrainType =TERRAIN_EMPTY;静态字节 newTerrainDuration =1;静态布尔播放 =false;静态布尔闪烁 =假;静态无符号整数距离 =0; if (!playing) { drawHero((blink) ? HERO_POSITION_OFF :heroPos,terrainUpper,terrainLower,距离>> 3);如果(闪烁){ lcd.setCursor(0,0); lcd.print("按开始"); } 延迟(250);眨眼 =!眨眼; if (buttonPushed) { initializeGraphics(); heroPos =HERO_POSITION_RUN_LOWER_1;播放 =真; buttonPushed =假;距离 =0; } 返回; } // 将地形向左移动 AdvanceTerrain(terrainLower, newTerrainType ==TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID :SPRITE_TERRAIN_EMPTY); AdvanceTerrain(terrainUpper, newTerrainType ==TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID :SPRITE_TERRAIN_EMPTY); // 让新地形进入右侧 if (--newTerrainDuration ==0) { if (newTerrainType ==TERRAIN_EMPTY) { newTerrainType =(random(3) ==0) ? TERRAIN_UPPER_BLOCK :TERRAIN_LOWER_BLOCK; newTerrainDuration =2 + 随机(10); } else { newTerrainType =TERRAIN_EMPTY; newTerrainDuration =10 + 随机(10); } } if (buttonPushed) { if (heroPos <=HERO_POSITION_RUN_LOWER_2) heroPos =HERO_POSITION_JUMP_1; buttonPushed =假;数字写入(PIN_BUZZER,高); } 如果(drawHero(heroPos,terrainUpper,terrainLower,距离>> 3)){playing =false; // 英雄撞到了什么东西。太糟糕了。 for (int i =0; i <=2; i++) { digitalWrite(PIN_BUZZER, HIGH);延迟(100);数字写入(PIN_BUZZER,低);延迟(100); } } else { if (heroPos ==HERO_POSITION_RUN_LOWER_2 || heroPos ==HERO_POSITION_JUMP_8) { heroPos =HERO_POSITION_RUN_LOWER_1; } else if ((heroPos>=HERO_POSITION_JUMP_3 &&heroPos <=HERO_POSITION_JUMP_5) &&terrainLower[HERO_HORIZONTAL_POSITION] !=SPRITE_TERRAIN_EMPTY) { heroPos =HERO_POSITION_RUN_UPPER_1; } else if (heroPos>=HERO_POSITION_RUN_UPPER_1 &&terrainLower[HERO_HORIZONTAL_POSITION] ==SPRITE_TERRAIN_EMPTY) { heroPos =HERO_POSITION_JUMP_5; } else if (heroPos ==HERO_POSITION_RUN_UPPER_2) { heroPos =HERO_POSITION_RUN_UPPER_1; } else { ++heroPos; ++距离;数字写入(PIN_AUTOPLAY,terrainLower[HERO_HORIZONTAL_POSITION + 2] ==SPRITE_TERRAIN_EMPTY ?高:低); } 延迟(100); digitalWrite(PIN_BUZZER,LOW);}

示意图

这将向您展示如何将所有部件连接在一起,因为封面图片中并未显示所有接线。

制造工艺

  1. TinyML-Language Detector-基于 Edge Impulse &Arduino
  2. Arduino Gyroscope Game with MPU-6050
  3. Arduino 数字骰子
  4. DIY 37 LED 轮盘游戏
  5. Arduino 游戏控制器
  6. 摇杆游戏
  7. 找到我
  8. 像素追逐游戏
  9. DIY 10Hz-50kHz 示波器在 128x64 LCD 显示器上
  10. Arduino Audio Reactive Desk Light
  11. NeoMatrix Arduino Pong
  12. Arduino Joystick