arduino教程——UNO西蒙游戏
UNO——simom game
学到什么
1.用UNO编写simom game程序
2.理解索引(Index)的用法
点击simom game开始在线仿真
西蒙是一个简单的电子记忆游戏:用户必须重复一个不断增长的颜色序列。通过点亮 LED 来显示序列。每种颜色还具有相应的色调。
在每个回合中,游戏将播放序列,然后等待用户根据颜色序列按下按钮来重复序列。如果用户正确重复了序列,游戏将播放"升级"声音,在序列末尾添加新颜色,然后移动到下一个回合。
游戏将继续,直到用户犯错。然后播放一个声音游戏,游戏重新启动。
硬件
项目 | 数量 | 笔记 |
---|---|---|
Arduino Uno R3 | 1 | |
5 毫米发光二极管 | 4 | 红色、绿色、蓝色和黄色 |
12 毫米按钮 | 4 | 红色、绿色、蓝色和黄色 |
电阻器 | 4 | 220Ω |
压电蜂鸣器 | 1 |
图
引脚连接
Arduino Pin | 装置 |
---|---|
12 | 红色指示灯 |
11 | 绿色发光二极管 |
10 | 蓝色发光二极管 |
9 | 黄色指示灯 |
8 | 蜂鸣器 |
5 | 红色按钮 |
4 | 绿色按钮 |
3 | 蓝色按钮 |
2 | 黄色按钮 |
- LED通过每个220Ω电阻连接。
/**Simon Game for ArduinoCopyright (C) 2016, Uri ShakedReleased under the MIT License.
*/#include "pitches.h"/* Constants - define pin numbers for LEDs,buttons and speaker, and also the game tones: */const byte ledPins[] = {9, 10, 11, 12};
const byte buttonPins[] = {2, 3, 4, 5};
#define SPEAKER_PIN 8//扬声器端口#define MAX_GAME_LENGTH 100//长度const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};//音调/* Global variables - store the game state */
/*全局变量-存储游戏状态*/
byte gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;//游戏索引/**Set up the Arduino board and initialize Serial communication
*/
void setup() {Serial.begin(9600);for (byte i = 0; i < 4; i++) {pinMode(ledPins[i], OUTPUT);pinMode(buttonPins[i], INPUT_PULLUP);}pinMode(SPEAKER_PIN, OUTPUT);// The following line primes the random number generator.// It assumes pin A0 is floating (disconnected)://启动随机数生成器并赋值。
//假设引脚A0是浮动的(断开连接):randomSeed(analogRead(A0));
}/**Lights the given LED and plays a suitable tone点亮给定的LED,并播放合适的音调
*/
void lightLedAndPlayTone(byte ledIndex) {digitalWrite(ledPins[ledIndex], HIGH);tone(SPEAKER_PIN, gameTones[ledIndex]);//参数为扬声器和音调delay(300);digitalWrite(ledPins[ledIndex], LOW);noTone(SPEAKER_PIN);
}/**Plays the current sequence of notes that the user has to repeat播放用户必须重复的当前音符序列
*/
void playSequence() {for (int i = 0; i < gameIndex; i++) {byte currentLed = gameSequence[i];lightLedAndPlayTone(currentLed);delay(50);}
}/**Waits until the user pressed one of the buttons,and returns the index of that button直到用户按下其中一个按钮,并返回那个按钮的索引
*/
byte readButtons() {while (true) {for (byte i = 0; i < 4; i++) {byte buttonPin = buttonPins[i];if (digitalRead(buttonPin) == LOW) {return i;}}delay(1);}
}/**Play the game over sequence, and report the game score按顺序玩游戏,并报告游戏分数
*/
void gameOver() {Serial.print("Game over! your score: ");Serial.println(gameIndex - 1);gameIndex = 0;delay(200);// Play a Wah-Wah-Wah-Wah soundtone(SPEAKER_PIN, NOTE_DS5);delay(300);tone(SPEAKER_PIN, NOTE_D5);delay(300);tone(SPEAKER_PIN, NOTE_CS5);delay(300);for (byte i = 0; i < 10; i++) {for (int pitch = -10; pitch <= 10; pitch++) {tone(SPEAKER_PIN, NOTE_C5 + pitch);delay(5);}}noTone(SPEAKER_PIN);delay(500);
}/**Get the user's input and compare it with the expected sequence.获取用户的输入,并将其与预期序列进行比较。
*/
bool checkUserSequence() {for (int i = 0; i < gameIndex; i++) {byte expectedButton = gameSequence[i];byte actualButton = readButtons();lightLedAndPlayTone(actualButton);if (expectedButton != actualButton) {return false;}}return true;
}/**Plays a hooray sound whenever the user finishes a level玩家完成一个关卡时播放欢呼的声音
*/
void playLevelUpSound() {tone(SPEAKER_PIN, NOTE_E4);delay(150);tone(SPEAKER_PIN, NOTE_G4);delay(150);tone(SPEAKER_PIN, NOTE_E5);delay(150);tone(SPEAKER_PIN, NOTE_C5);delay(150);tone(SPEAKER_PIN, NOTE_D5);delay(150);tone(SPEAKER_PIN, NOTE_G5);delay(150);noTone(SPEAKER_PIN);
}/**The main game loop
*/
void loop() {// Add a random color to the end of the sequence 在序列的末尾添加一个随机的颜色gameSequence[gameIndex] = random(0, 4);gameIndex++;if (gameIndex >= MAX_GAME_LENGTH) {gameIndex = MAX_GAME_LENGTH - 1;}playSequence();if (!checkUserSequence()) {gameOver();}delay(300);if (gameIndex > 0) {playLevelUpSound();delay(300);}
}
结束
我会在那薪酬腐朽的日子里熠熠生辉
我会在那颠沛流离的生活里坚持不懈
arduino教程——UNO西蒙游戏相关推荐
- arduino教程——西蒙游戏
西蒙游戏 Simon Game <西蒙游戏>是一款益智休闲类小游戏,它的游戏规则是,让玩家记住不同颜色的灯的亮灯顺序后,依次点击灯,如果次序与AI给予的次序相同,则游戏继续并增加难度,否则 ...
- Arduino Mega/Uno制作宏定义键盘(不需刷firmware!!)
本人机械专业大一编程小白一枚,接触编程不到半年.近期开始自学Arduino.这应该是本人第一篇博客,有错误之处敬请斧正! - 心路&思路 家住武汉,这个年嘛,大家应该都懂,被迫宅化.年前从网上 ...
- 《Arduino开发实战指南:LabVIEW卷》6.6 基于Arduino的弹珠游戏
6.6 基于Arduino的弹珠游戏 6.6.1 实现的功能 本节将使用LabVIEW及Arduino设计实现一个简单的弹珠游戏.该弹珠游戏规则为,左右移动挡板,使弹珠在矩形区域内运动而不掉落到矩形区 ...
- Arduino教程汇总贴
初识arduino http://www.arduino.cn/thread-1083-1-1.html 关于Arduino及其周边配件的购买建议 http://www.arduino.cn/t ...
- 串口监视软件_ESP32 Arduino教程:软件重置
简介 该esp32 arduino教程旨在解释如何使用Arduino核心在ESP32开发板上执行软件重置. 本ESP32教程的测试是使用集成在ESP32开发板中的DFRobot的ESP-WROOM-3 ...
- Unity 2D游戏开发教程之为游戏场景添加多个地面
Unity 2D游戏开发教程之为游戏场景添加多个地面 为游戏场景添加多个地面 显然,只有一个地面的游戏场景太小了,根本不够精灵四处活动的.那么,本节就来介绍一种简单的方法,可以为游戏场景添加多个地面. ...
- esp32 micropython web服务器_ESP32 Arduino教程:Websocket server(服务器)
本文主要介绍如何使用Arduino内核作为编程架构在ESP32(上创建Websocket server(服务器).所创建的Websocket server(服务器)将作为回发服务器使用,也就是说它会把 ...
- 【Visual C++】游戏开发笔记四十二 浅墨DirectX教程之十 游戏输入控制利器 DirectInput专场
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 本系列文 ...
- mysql修改游戏元宝_页游源码【武斗乾坤】自带安装启动教程+元宝游戏数据修改教程+自由一键游戏启动服务端...
页游源码[武斗乾坤]自带安装启动教程+元宝游戏数据修改教程+自由一键游戏启动服务端_站长下载 资源说明: 1.本资源为一键启动服务端,只需要安装好所需组件一键启动即可运行. 2.资源默认为单机架设,无 ...
最新文章
- k8s概念入门之kube-proxy-针对早期(0.4)版本阅读
- 如何在 bash 中使用键值字典
- 遍历聚合对象中的元素——迭代器模式(二)
- java poi 只能创建?,JAVA POI创建多个sheet的Excel文件,及多个sheet也创建失败原因
- python r语言培训_r语言和python学哪个
- MySQL8的8大新SQL特性
- java的map集合_Java集合之Map
- python 列表推导
- RocketMQ核心概念(翻译)
- 根据录入的计算公式计算_预制桩极限承载力标准值快速计算方法研究
- Setup Factory打包winform程序
- 米思齐 超声波传感器控制小灯泡
- 计算机毕业设计-SSM企业OA管理系统-JavaWeb企业OA管理系统
- 【考研英语语法】名词从句的虚拟、主语从句、WH-形式的引导词引导名词从句
- 计算机桌面下方任务栏图标不见了,任务栏显示桌面图标不见怎么办
- 美版t430笔记本win8系统下装Ubuntu的一些尝试和遇到的问题
- 为什么Lisp如此先进,却永远成为不了编程主流语言?深度解析Lisp的优势与劣势
- 【detectron2】detectron2在ubuntu16.04系统下安装报错问题
- 替换Word模板占位符并用 libreOffice Word 转Pdf
- spark官网无法下载spark问题