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 Arduino Copyright (C) 2016, Uri Shaked Released 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 sound tone(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); } }
结束
我会在那薪酬腐朽的日子里熠熠生辉 我会在那颠沛流离的生活里坚持不懈