西蒙游戏 Simon Game
《西蒙游戏》是一款益智休闲类小游戏,它的游戏规则是,让玩家记住不同颜色的灯的亮灯顺序后,依次点击灯,如果次序与AI给予的次序相同,则游戏继续并增加难度,否则游戏结束,重置游戏。

在线arduino西蒙游戏

代码

/**Simon Game for ATTiny85Copyright (C) 2018, Uri ShakedLicensed under the MIT License.
*/#include <avr/sleep.h>
#include <avr/interrupt.h>#include "pitches.h"/* Constants - define pin numbers for leds, buttons and speaker, and also the game tones */
byte buttonPins[] = {1, 2, 3, 4};
#define SPEAKER_PIN 0#define MAX_GAME_LENGTH 100int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};/* Global variales - store the game state */
byte gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;/**Set up the GPIO pins
*/
void setup() {// The following line primes the random number generator. It assumes pin A0 is floating (disconnected)randomSeed(analogRead(1));// Disable ADC - saves about 324.5uA in sleep mode!ADCSRA = 0;set_sleep_mode(SLEEP_MODE_PWR_DOWN);for (int i = 0; i < 4; i++) {pinMode(buttonPins[i], INPUT_PULLUP);}pinMode(SPEAKER_PIN, INPUT);
}ISR(PCINT0_vect) {GIMSK &= ~0b00100000;  // Turn off pin change interruptssleep_disable();
}void sleep() {sleep_enable();noInterrupts();GIMSK |= 0b00100000;  // Turn on pin change interruptsfor (byte i = 0; i < 4; i++) {PCMSK |= 1 << buttonPins[i];}interrupts();sleep_cpu();
}// The sound-producing function
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.htmlint  x;long delayAmount = (long)(1000000 / frequencyInHertz);long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));pinMode(speakerPin, OUTPUT);for (x = 0; x < loopTime; x++) {digitalWrite(speakerPin, HIGH);delayMicroseconds(delayAmount);digitalWrite(speakerPin, LOW);delayMicroseconds(delayAmount);}pinMode(speakerPin, INPUT);
}/**Lights the given led and plays the suitable tone
*/
void lightLedAndPlaySound(byte ledIndex) {pinMode(buttonPins[ledIndex], OUTPUT);digitalWrite(buttonPins[ledIndex], LOW);beep(SPEAKER_PIN, gameTones[ledIndex], 300);pinMode(buttonPins[ledIndex], INPUT_PULLUP);
}/**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];lightLedAndPlaySound(currentLed);delay(50);}
}/**Waits until the user pressed one of the buttons, and returns the index of that button
*/
byte readButton() {for (;;) {for (int i = 0; i < 4; i++) {byte buttonPin = buttonPins[i];if (digitalRead(buttonPin) == LOW) {return i;}}sleep();}
}/**Plays the tone associated with a specific button + debouncing mechanism
*/
void playButtonTone(byte buttonIndex) {beep(SPEAKER_PIN, gameTones[buttonIndex], 150);// Wait until button is released.while (digitalRead(buttonPins[buttonIndex]) == LOW);delay(50);
}/**Play the game over sequence, and report the game score
*/
void gameOver() {gameIndex = 0;delay(200);// Play a Wah-Wah-Wah-Wah soundbeep(SPEAKER_PIN, NOTE_DS5, 300);beep(SPEAKER_PIN, NOTE_D5, 300);beep(SPEAKER_PIN, NOTE_CS5, 300);for (int i = 0; i < 200; i++) {beep(SPEAKER_PIN, NOTE_C5 + (i % 20 - 10), 5);}delay(500);
}/**Get the user input and compare it with the expected sequence. If the user fails, play the game over sequence and restart the game.
*/
void checkUserSequence() {for (int i = 0; i < gameIndex; i++) {byte expectedButton = gameSequence[i];byte actualButton = readButton();playButtonTone(actualButton);if (expectedButton == actualButton) {/* good */} else {gameOver();return;}}
}/**Plays an hooray sound whenever the user finishes a level
*/
void levelUp() {beep(SPEAKER_PIN, NOTE_E4, 150);beep(SPEAKER_PIN, NOTE_G4, 150);beep(SPEAKER_PIN, NOTE_E5, 150);beep(SPEAKER_PIN, NOTE_C5, 150);beep(SPEAKER_PIN, NOTE_D5, 150);beep(SPEAKER_PIN, NOTE_G5, 150);
}/**The main game loop
*/
void loop() {// Add a random color to the end of the sequencegameSequence[gameIndex] = random(0, 4);gameIndex++;playSequence();checkUserSequence();delay(300);if (gameIndex > 0) {levelUp();delay(300);}
}

arduino教程——西蒙游戏相关推荐

  1. 串口监视软件_ESP32 Arduino教程:软件重置

    简介 该esp32 arduino教程旨在解释如何使用Arduino核心在ESP32开发板上执行软件重置. 本ESP32教程的测试是使用集成在ESP32开发板中的DFRobot的ESP-WROOM-3 ...

  2. 微信小游戏开发教程-2D游戏原理讲解

    微信小游戏开发教程-2D游戏原理讲解 原理 为了更加形象的描述,这里先上一张图: 背景 a. 首先,我们看到背景好像是一张无限长的图片在向下移动.实际则不然,这是一张顶部和底部刚好重叠的图片.这是一种 ...

  3. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

  4. C#开发Unity游戏教程之游戏对象的行为逻辑方法

    C#开发Unity游戏教程之游戏对象的行为逻辑方法 游戏对象的行为逻辑--方法 方法(method),读者在第1章新建脚本时就见过了,而且在第2章对脚本做整体上的介绍时也介绍过,那么上一章呢,尽管主要 ...

  5. esp32 micropython web服务器_ESP32 Arduino教程:Websocket server(服务器)

    本文主要介绍如何使用Arduino内核作为编程架构在ESP32(上创建Websocket server(服务器).所创建的Websocket server(服务器)将作为回发服务器使用,也就是说它会把 ...

  6. mysql修改游戏元宝_页游源码【武斗乾坤】自带安装启动教程+元宝游戏数据修改教程+自由一键游戏启动服务端...

    页游源码[武斗乾坤]自带安装启动教程+元宝游戏数据修改教程+自由一键游戏启动服务端_站长下载 资源说明: 1.本资源为一键启动服务端,只需要安装好所需组件一键启动即可运行. 2.资源默认为单机架设,无 ...

  7. 传奇单机架设教程及游戏GM设置方法

    传奇技术教学 第二课:传奇单机架设教程及游戏GM设置方法 架设前关杀毒 确保自己的热血传奇客户端是13周年以后的 最好用最新的. 不要使用已经淘汰的10周年客户端和微端客户端 否则会出现显示不全情况. ...

  8. git 分支教程小游戏

    git 分支教程小游戏:https://learngitbranching.js.org/

  9. 绘画武器设计教程,游戏CG原画学习教程之高级武器设计!

    游戏CG原画学习教程之高级武器设计,绘画武器设计教程! 第一章:金箍棒与降魔杵的设计视频地址:http://www.qingwk.com/course/detail/22 装备类游戏道具一般可以分为武 ...

最新文章

  1. 用原生JavaScript实现图片瀑布流的浏览效果
  2. 二阶系统响应指标图_15. 闭环系统的频域性能指标
  3. 大剑无锋之分布式和微服务分别是什么?【面试推荐】
  4. RabbbitMq 消费端自定义监听
  5. Linux的工作队列work queue和延时工作队列
  6. 数据抽取常见的几种模式
  7. macos 下 vmware fusion 安装 vmware tools
  8. 【数据库】Oracle更改时间显示格式
  9. 一只菜鸟的前端实习记录(碎碎念)
  10. 海龟图c语言编程,【编程课堂】海龟作图
  11. java mac算法_Mac算法的java实现
  12. HDU 5234 Happy birthday 01背包
  13. linux远程管理工具:putty
  14. ubuntu显卡的参数说明
  15. ExoPlayer+Shaka-packager播放自制DRM视频
  16. 好的 免费 报表控件 很多
  17. IDEA--字体大小设置
  18. Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
  19. 什么数据库可以撑起微信支付每天数亿笔交易?
  20. 如何关闭AndroidX?

热门文章

  1. 模型机设计(VERILOG)-模型机结构与Verilog语言
  2. Linux基础篇——Linux磁盘操作(磁盘基础知识、分类、分区、挂载、卸载、扩容)详解
  3. 百度谷歌双搜1.3.0已发布
  4. 移动APP界面设计切图指南
  5. expect脚本中使用普通用户执行sh脚本命令,expect脚本中解决``反引号获取值有问题方法、expect遍历登录不同主机不同密码的主机执行指定命令方法
  6. 智能汽车数据安全与法律法规盘点
  7. 漏洞扫描之OpenVas
  8. USB耳麦_从无到有(一)
  9. 计算机及科学与技术航空方向,沈阳航空航天大学计算机学院计算机科学与技术专业简介...
  10. 年净赚15亿美元,数学教授做对冲基金,詹姆斯·西蒙斯破译通往财富密码!