arduino教程——UNO西蒙游戏

UNO——simom game

学到什么
1.用UNO编写simom game程序
2.理解索引(Index)的用法
点击simom game开始在线仿真

西蒙是一个简单的电子记忆游戏:用户必须重复一个不断增长的颜色序列。通过点亮 LED 来显示序列。每种颜色还具有相应的色调。

在每个回合中,游戏将播放序列,然后等待用户根据颜色序列按下按钮来重复序列。如果用户正确重复了序列,游戏将播放"升级"声音,在序列末尾添加新颜色,然后移动到下一个回合。

游戏将继续,直到用户犯错。然后播放一个声音游戏,游戏重新启动。

Simon Game Shield for Arduino Uno

硬件

项目数量笔记
Arduino Uno R31
5 毫米发光二极管4红色、绿色、蓝色和黄色
12 毫米按钮4红色、绿色、蓝色和黄色
电阻器4220Ω
压电蜂鸣器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);
  }
}

结束

我会在那薪酬腐朽的日子里熠熠生辉
我会在那颠沛流离的生活里坚持不懈
在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值