13浏览
查看: 13|回复: 2

[项目] 【Arduino 动手做】 基于街机游戏《Cyclone》的LED环

[复制链接]
基于街机游戏《Cyclone》,玩家需要尝试在特定位置停止 LED 的圆圈滚动。

这次我将向你展示如何制作一款有趣的游戏,它基于一款名为“Cyclone”的街机游戏。游戏中,玩家需要尝试在特定位置停止 LED 灯的循环滚动。游戏的目标是在 LED 灯到达指示点(红色)时停止循环滚动。如果成功,难度级别将会增加。如果失败,游戏将重新开始。

圆环由 60 个 LED 灯组成(4 个四分之一圆 Neopixel 灯,每个灯由 15 个 LED 组成)。圆环支架由 3D 打印机制作,您可以下载下方的 .stl 文件。圆环中间是一条由 6 个 RGB LED 组成的灯带,代表六个等级。基本代码取自 oKeeg 的 Instructable,我做了一些修改,使游戏更加有趣。

首先,我降低了二极管的光强度,因为在我的游戏版本中 LED 是直接可见的。
然后,环中包含 60 个 LED,而不是 40 个,因为我从我之前的一个项目(LED 环形时钟)中使用了它。
我还添加了两个级别,这样游戏现在总共有 6 个级别。
下一个非常重要的修改是我在游戏中添加了声音,现在玩起来更有趣了。
-最后,我制作了一个大的街机按钮,以便更准确地玩游戏。

游戏需要 5V/3A 或更高的电源供电。这是一个非常简单的项目,仅包含几个组件:

- Arduino 纳米微控制器

- WS2812 LED 环,带 60 个 RGB LED

- WS2812 LED 灯带,带 6 个 RGB LED

- 一个晶体管

- 蜂鸣器

- 以及大型 DIY 街机按钮

您在图片中看到的实时时钟模块未处于活动状态,并且是我之前项目的一部分。

我们也可以使用上一个项目剩下的旋转编码器按钮。在这种情况下,设备小巧紧凑,但操作起来比较困难。

启动游戏时,所有 LED 都会亮起不同的颜色。按下按钮,游戏开始。目标是在旋转二极管恰好​​位于静态二极管上时按下按钮。在前两级中,三个二极管是静态的,在接下来的级别中只有一个。而且,随着每个级别的增加,二极管的速度都会增加,因此在所需的时刻按下按钮会变得越来越困难。每完成一个级别,水平行中的一个二极管就会亮起。总共有六个级别,所以这一行包含六个二极管。如果我们未能通过该级别,游戏将从头开始。如果您通过了所有六个级别,游戏也将从头开始。

最后,将该装置装入由PVC板制成并涂有自粘彩色壁纸的合适盒子中。

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图1

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图5

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图2

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图4

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图3

驴友花雕  中级技神
 楼主|

发表于 4 小时前

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环

项目代码

  1. #include "FastLED.h"
  2. #define NUM_LEDS 60
  3. #define DATA_PIN A0
  4. #define SCORE_PIN 6
  5. #define SCORE_LEDS 6
  6. #define BRIGHTNESS 55
  7. CRGB leds[NUM_LEDS];
  8. CRGB sleds[NUM_LEDS];
  9. bool reachedEnd = false;
  10. byte gameState = 0;
  11. //byte ledSpeed = 0;
  12. int period = 1000;
  13. unsigned long time_now = 0;
  14. byte Position = 0;
  15. byte level = 0;
  16. const byte ledSpeed[6] = {50, 40, 30, 20, 14, 7};
  17. //Debounce
  18. bool findRandom = false;
  19. byte spot = 0;
  20. void setup() {
  21.   // put your setup code here, to run once:
  22.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  23.   FastLED.addLeds<WS2812B, SCORE_PIN, GRB>(sleds, SCORE_LEDS);
  24.   pinMode(A3, INPUT_PULLUP);
  25.   Serial.begin(9600);
  26.   Serial.println("Reset");
  27. }
  28. void loop() {
  29.   // put your main code here, to run repeatedly:
  30.   FastLED.setBrightness(BRIGHTNESS );
  31.   if (gameState == 0) {
  32.     fill_rainbow(leds, NUM_LEDS, 0, 20); //2 = longer gradient strip
  33.     fill_rainbow(sleds, SCORE_LEDS, 0, 40); //2 = longer gradient strip
  34.     if (digitalRead(A3) == LOW) {
  35.       Position = 0;
  36.       findRandom = true;
  37.       delay(500);
  38.       for (byte i = 0; i < NUM_LEDS; i++) {
  39.         leds[i].setRGB(0, 0, 0);
  40.         delay(40);
  41.         FastLED.show();
  42.        int thisPitch = map (i, 60, 0, 100, 1500);
  43.        tone(9, thisPitch,120);
  44.       }
  45.       for (byte i = 0; i < SCORE_LEDS; i++) {
  46.         sleds[i].setRGB(0, 0, 0);
  47.         delay(100);
  48.         FastLED.show();
  49.       }
  50.       gameState = 1;
  51.     }
  52.     FastLED.show();
  53.   }
  54.   if (gameState == 1) {
  55.     period = ledSpeed[0];
  56.     if (millis() > time_now + period) {
  57.       time_now = millis();
  58.       if (findRandom) {
  59.         spot = random(56) + 3;
  60.         findRandom = false;
  61.       }
  62.       leds[spot - 1].setRGB(255, 140, 0);
  63.       leds[spot].setRGB(0, 255, 0);
  64.       leds[spot + 1].setRGB(255, 110, 0);
  65.       sleds[0].setRGB(0, 255, 0);
  66.       PlayGame(spot - 1, spot + 1);
  67.     }
  68.     if (digitalRead(A3) == LOW) {
  69.       delay(300);
  70.       findRandom = false;
  71.       if (Position > spot - 1 && Position < spot + 3) {
  72.         level = gameState;
  73.         gameState = 98;
  74.       } else {
  75.         gameState = 99;
  76.       }
  77.     }
  78.   }
  79.   if (gameState == 2) {
  80. //    period = 320;
  81.     period = ledSpeed[1];
  82.     if (millis() > time_now + period) {
  83.       time_now = millis();
  84.       if (findRandom) {
  85.         spot = random(56) + 3;
  86.         findRandom = false;
  87.       }
  88.       leds[spot - 1].setRGB(255, 190, 0);
  89.       leds[spot].setRGB(0, 255, 0);
  90.       leds[spot + 1].setRGB(255, 190, 0);
  91.       sleds[1].setRGB(255, 255, 0);
  92.       PlayGame(spot - 1, spot + 1);
  93.     }
  94.     if (digitalRead(A3) == LOW) {
  95.       delay(300);
  96.       if (spot - 1 && Position < spot + 3) {
  97.         level = gameState;
  98.         gameState = 98;
  99.       } else {
  100.         gameState = 99;
  101.       }
  102.     }
  103.   }
  104.   if (gameState == 3) {
  105.     period = ledSpeed[2];
  106.     if (millis() > time_now + period) {
  107.       time_now = millis();
  108.       if (findRandom) {
  109.         spot = random(56) + 3;
  110.         findRandom = false;
  111.       }
  112.       leds[spot].setRGB(0, 255, 0);
  113.       sleds[2].setRGB(255, 50, 0);
  114.       PlayGame(spot, spot);
  115.     }
  116.     if (digitalRead(A3) == LOW) {
  117.       delay(300);
  118.       if (Position == spot+1) {
  119.         level = gameState;
  120.         gameState = 98;
  121.       } else {
  122.         gameState = 99;
  123.       }
  124.     }
  125.   }
  126.   if (gameState == 4) {
  127.     period = ledSpeed[3];
  128.     if (millis() > time_now + period) {
  129.       time_now = millis();
  130.       if (findRandom) {
  131.         spot = random(56) + 3;
  132.         findRandom = false;
  133.       }
  134.       leds[spot].setRGB(0, 255, 0);
  135.       sleds[3].setRGB(255, 0, 0);
  136.       PlayGame(spot, spot);
  137.     }
  138.     if (digitalRead(A3) == LOW) {
  139.       delay(300);
  140.       if (Position == spot+1) {
  141.         level = gameState;
  142.         gameState = 98;
  143.       } else {
  144.         gameState = 99;
  145.       }
  146.     }
  147.   }
  148.   if (gameState == 5) {
  149.     period = ledSpeed[4];
  150.     if (millis() > time_now + period) {
  151.       time_now = millis();
  152.       if (findRandom) {
  153.         spot = random(56) + 3;
  154.         findRandom = false;
  155.       }
  156.       leds[spot].setRGB(0, 255, 0);
  157.       sleds[4].setRGB(0, 50, 255);
  158.       PlayGame(spot , spot);
  159.     }
  160.     if (digitalRead(A3) == LOW) {
  161.       delay(300);
  162.       if (Position == spot+1) {
  163.         level = gameState;
  164.         gameState = 98;
  165.       } else {
  166.         gameState = 99;
  167.       }
  168.     }
  169.   }
  170.   if (gameState == 6) {
  171.     period = ledSpeed[5];
  172.     if (millis() > time_now + period) {
  173.       time_now = millis();
  174.       if (findRandom) {
  175.         spot = random(56) + 3;
  176.         findRandom = false;
  177.       }
  178.       leds[spot].setRGB(0, 255, 0);
  179.       sleds[5].setRGB(0, 150, 255);
  180.       PlayGame(spot , spot);
  181.     }
  182.     if (digitalRead(A3) == LOW) {
  183.       delay(300);
  184.       if (Position == spot+1) {
  185.         level = gameState;
  186.         gameState = 98;
  187.       } else {
  188.         gameState = 99;
  189.       }
  190.     }
  191.   }
  192.   
  193.   if (gameState == 98) {
  194.     winner();
  195.   }
  196.   if (gameState == 99) {
  197.     loser();
  198.   }
  199. }
  200. void PlayGame(byte bound1, byte bound2) {
  201.   leds[Position].setRGB(255, 0, 0);
  202.   if (Position < bound1 + 1 || Position > bound2 + 1) {
  203.     leds[Position - 1].setRGB(0, 0, 0);
  204.   }
  205.   FastLED.show();
  206.   Position++;
  207.   if (Position >= NUM_LEDS) {
  208.     leds[Position - 1].setRGB(0, 0, 0);
  209.     Position = 0;
  210.   }
  211. }
  212. void winner() {
  213.   for (byte i = 0; i < 3; i++) {
  214.     for (byte j = 0; j < NUM_LEDS; j++) {
  215.       leds[j].setRGB(0, 255, 0);
  216.         tone(9, 1000, 250);
  217.     }
  218.     FastLED.show();
  219.     delay(500);
  220.     clearLEDS();
  221.     FastLED.show();
  222.     delay(500);
  223.   
  224.   }
  225.   findRandom = true;
  226.   Position = 0;
  227.   gameState = level + 1;
  228.   if (gameState > 6) {
  229.     gameState = 0;
  230.   }
  231. }
  232. void loser() {
  233.   for (byte i = 0; i < 3; i++) {
  234.     for (byte j = 0; j < NUM_LEDS; j++) {
  235.       leds[j].setRGB(255, 0, 0);
  236.       tone(9, 200, 250);
  237.     }
  238.     FastLED.show();
  239.     delay(500);
  240.     clearLEDS();
  241.     FastLED.show();
  242.     delay(500);
  243.   }
  244.   gameState = 0;
  245. }
  246. void clearLEDS() {
  247.   for (byte i = 0; i < NUM_LEDS; i++) {
  248.     leds[i].setRGB(0, 0, 0);
  249.   }
  250. }
  251. void winAll(){
  252.   
  253. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 4 小时前

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环
项目链接:https://www.hackster.io/mircemk/ ... 12b-led-ring-738c58
项目作者:北马其顿 米尔塞姆克(Mirko Pavleski)

项目视频 :https://www.youtube.com/watch?v=1K0vr-hrh0k
项目代码:https://www.hackster.io/code_files/581838/download
3D 文件:https://hacksterio.s3.amazonaws. ... ring_Vvyxo74B6t.stl

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图2

【Arduino 动手做】 基于街机游戏《Cyclone》的LED环图1

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail