风筝
发表于: 2019-9-23 15:50:58 | 显示全部楼层

你玩过棋盘游戏吗?你喜欢玩骰子吗?我们在许多棋盘游戏中使用骰子,但有时它们会引起问题。例如,当我和朋友一起玩thriller游戏时,有时我们会忘记玩了多少回合。您是否想知道如何解决这个问题?如果是这样,请阅读本文。


在本篇文章中,我们将展示如何使用电子组件制作智能助手,包含KY-037声音检测模块、RGB LED、TFT LCD扩展板,当然还有Arduino UNO。


什么是骰子塔?

许多棋盘游戏使用骰子。对于那些喜欢真实游戏而不是计算机游戏的人来说,握住并掷骰子至关重要。但是不能保证公平,玩家可能会作弊!此外,一些棋盘游戏有很多骰子。同时扔几个骰子可能很困难,有些甚至可能会弄丢。骰子塔是解决这些问题的好主意。骰子从塔楼上方进入,接触内板并向下旋转,固定在底部。

Smart-Dice-Tower-simple-dice-tower.jpg


棋盘游戏:更多魅力,更少作弊!

不管是哪种游戏,都会有可能存在作弊。特别是骰子游戏。游戏的快感和节奏可能会使玩家不记得回合数。因此,存在公正的判断可能会有所帮助。通过一些电子创新,我们可以制造出一个智能骰子塔,以帮助保持点数并将其显示在五颜六色的灯光下。


您可能会在游戏中弄丢骰子。因此,您必须使用其他物体(石头!)代替骰子。智能骰子塔为您提供最佳选择。即使没有任何可用的骰子,也不要担心,我们的塔楼也有一个虚拟的骰子。


所需的组件

●    Arduino UNO R3开发板

●    2.4英寸TFT LCD显示屏扩展板

●    KY-037声音检测传感器模块

●    5mm RGB三色LED灯

●    3 X 1.5V AAA电池盒固定器

●    1.5V AAA电池

Smart-Dice-Tower-required-materials-1500x872.jpg


如何组装骰子塔的主体结构

要制造智能骰子塔,首先,我们需要构建主体。它可以由2.8mm MDF或有机玻璃制成。请在此处免费下载激光切割草图。设计的骰子塔有58个零件。

Smart-Dice-Tower-mechanical-component-1500x732.jpg


电路

在本文中,我们使用Arduino TFT LCD扩展板。要操作该模块,只需将其安装在Arduino UNO上(注意引脚的位置和方向)。为了设置KY-037模块,请将模块的电源引脚连接到VCC和GND引脚,并将数字输出引脚连接到Arduino的D13引脚。


要使用RGB LED,请将阴极引脚连接到Arduino的GND引脚,并将红色、绿色和蓝色引脚连接到Arduino板上的D5、D6和D7。

Smart-Dice-Tower-circuit.png


如何使用声音检测模块知道骰子何时掉落

我们可以使用或声音检测模块来知道骰子何时掉落。如果我们将震动传感器放在一块内板的下面,那么给定的震动将是不够的。将传感器直接放在骰子路径上也不正确。因此,声音模块是正确的选择。我们可以仅根据图4设置KY-037声音模块。由于在此项目中只检测一种音调,因此我们仅使用声音模块的数字引脚。要识别骰子声音,我们可以使用digitalRead()命令读取Arduino的D13引脚。数字引脚在此模块中上拉,当声音达到阈值时,数字引脚电压将变为零。我们可以通过旋转板上的电位器来更改声音阈值。要精确地校准此传感器,请按照下列步骤操作:

1.    将以下代码上传到Arduino开发板:

  1. void setup() {
  2. Serial.begin(9600); // setup serial
  3. pinMode(13,INPUT);
  4. }
  5. void loop() {
  6. Serial.println(digitalRead(13));
  7. delay(100);
  8. }
复制代码

2.    打开串口监视器,将骰子持续不断放入塔中,同时旋转电位器,直到在“Serial Monitor”窗口中看到数字零。 再投入一些时间以确保模块已正确调整。 应该对其进行调整,以使环境中的其他噪声不会激活传感器。

Smart-Dice-Tower-KY037-calibration.gif


如何使用RGB LED显示点数和添加灯光效果

有各种RGB LED类型。 在此项目中,我们使用4针共阴极RGB LED。 要点亮LED,我们应将阴极连接到GND。 在其他引脚上加电压会使LED变成红色、绿色或蓝色。 如果所有三个引脚都设置为高电平,LED将发白光。

使用以下代码,您可以周期性将LED颜色更改为红色、绿色、蓝色和白色。

  1. #define rLED 10
  2. #define rLED 11
  3. #define rLED 12
  4. Void setup(){
  5. pinMode(rLED,OUTPUT);
  6. pinMode(gLED,OUTPUT);
  7. pinMode(bLED,OUTPUT);
  8. void loop(){
  9. //LED equalizer
  10.   digitalWrite(rLED,HIGH);digitalWrite(gLED,LOW);digitalWrite(bLED,LOW);
  11.   delay(100);
  12.   digitalWrite(rLED,LOW);digitalWrite(gLED,HIGH);digitalWrite(bLED,LOW);
  13.   delay(100);
  14.   digitalWrite(rLED,LOW);digitalWrite(gLED,LOW);digitalWrite(bLED,HIGH);
  15. delay(100);
  16. digitalWrite(rLED,HIGH);digitalWrite(gLED,HIGH);digitalWrite(bLED,HIGH);
  17.   delay(100);
  18. }
复制代码

使用TFT LCD显示更多信息并制作虚拟骰子

要显示玩家的回合数,我们应该知道玩家的数量及其相应的颜色。 我们可以使用带触摸屏的TFT LCD。 用于Arduino的2.4英寸Adafruit LCD扩展板是一个绝佳的简单选择。


在以下代码中,当启动LCD时,首先要询问玩家的数量,然后用户可以通过触摸屏幕上的骰子从1到4中进行选择。 根据玩家的数量,它会询问每个玩家的颜色。 在此部分的最后,显示颜色顺序,然后开始游戏。

  1. */
  2.   Smart Dice Tower – main code
  3. */
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_TFTLCD.h>
  6. #include <TouchScreen.h>

  7. #define LCD_CS A3
  8. #define LCD_CD A2
  9. #define LCD_WR A1
  10. #define LCD_RD A0
  11. #define LCD_RESET A4

  12. #define YP A3  
  13. #define XM A2  
  14. #define YM 9   
  15. #define XP 8

  16. #define rLED 5
  17. #define gLED 6
  18. #define bLED 7
  19. #define ss 13

  20. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

  21. //find these number through “SDT_TS_calibration.ino” code
  22. #define TSx_dices         600
  23. #define Tsy_1st_dice      825
  24. #define Tsy_2nd_dice      625
  25. #define Tsy_3th_dice      425
  26. #define Tsy_4th_dice      225
  27. #define Tsy_virtual_dice  760

  28. #define BLACK       0x0000
  29. #define BLUE        0x001F
  30. #define RED         0xF800
  31. #define GREEN       0x07E0
  32. #define YELLOW      0xFFE0
  33. #define WHITE       0xFFFF
  34. #define PURPLE      0x780F

  35. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

  36. int n=1,a=1,t=0,count=0;
  37. int c[4];
  38. bool pres=false,vd=false;

  39. void setup() {

  40.   Serial.begin(9600);
  41.   Serial.println(F(“TFT LCD test”));

  42. tft.reset();

  43. #ifdef USE_ADAFRUIT_SHIELD_PINOUT
  44.   Serial.println(F(“Using Adafruit 2.4\” TFT Arduino Shield Pinout”));
  45. #else
  46.   Serial.println(F(“Using Adafruit 2.4\” TFT Breakout Board Pinout”));
  47. #endif
  48.   Serial.print(“TFT size is “);
  49.   Serial.print(tft.width());
  50.   Serial.print(“x");
  51.   Serial.println(tft.height());

  52.   tft.reset();
  53. delay(1000);
  54.   uint16_t identifier = tft.readID();

  55.   if (identifier == 0x9325){
  56.     Serial.println(F(“Found ILI9325 LCD driver”));
  57.   } else if (identifier == 0x9328){
  58.     Serial.println(F(“Found ILI9328 LCD driver”));
  59.   } else if (identifier == 0x7575){
  60.     Serial.println(F(“Found HX8347G LCD driver”));
  61.   } else if (identifier == 0x9341){
  62.     Serial.println(F(“Found ILI9341 LCD driver”));
  63.   } else if (identifier == 0x8357){
  64.     Serial.println(F(“Found HX8357D LCD driver”));
  65.   } else {
  66.     Serial.print(F(“Unknown LCD driver chip: “));
  67.     Serial.println(identifier, HEX);
  68.     Serial.println(F(“If using the Adafruit 2.4\” TFT Arduino shield, the line:”));
  69.     Serial.println(F(“  #define USE_ADAFRUIT_SHIELD_PINOUT”));
  70.     Serial.println(F(“should appear in the library header (Adafruit_TFT.h).”));
  71.     Serial.println(F(“If using the breakout board, it should NOT be #defined!”));
  72.     Serial.println(F(“Also if using the breakout, double-check that all wiring”));
  73.     Serial.println(F(“matches the tutorial.”));
  74.     return;
  75.   }

  76.   pinMode(13, OUTPUT);
  77.   pinMode(XM, OUTPUT);
  78.   pinMode(YP, OUTPUT);
  79.   
  80.   tft.begin(identifier);

  81.   Serial.println(F(“Benchmark                Time (microseconds)”));
  82. //-------------------------------------------------------------------------------------
  83. //LCD starting:
  84.   tft.fillScreen(PURPLE);
  85.   tft.setTextColor(YELLOW);
  86.   tft.setRotation(1);
  87.   tft.setCursor(30, 100);
  88.   tft.setTextSize(4);
  89.   tft.println(“Electropeak”);
  90.   tft.setCursor(60, 150);
  91.   tft.setTextSize(2);
  92.   tft.println(“Smart Dice Tower”);
  93.   delay(1000);
  94. //-------------------------------------------------------------------------------------
  95. //asking number of player
  96.   tft.fillScreen(PURPLE);
  97.   tft.setRotation(1);
  98.   tft.setCursor(5, 50);
  99.   tft.setTextSize(3);
  100.   tft.println(“number of player?”)
  101.   delay(200);
  102.   tft.fillRoundRect(20,110,60,60,5,WHITE);
  103.   tft.fillCircle(50,140,10,RED);
  104.   
  105.   tft.fillRoundRect(90,110,60,60,5,WHITE);
  106.   tft.fillCircle(120,125,8,BLACK);
  107.   tft.fillCircle(120,155,8,BLACK);

  108.   tft.fillRoundRect(160,110,60,60,5,WHITE);
  109.   tft.fillCircle(170,120,8,BLACK);
  110.   tft.fillCircle(190,140,8,BLACK);
  111.   tft.fillCircle(210,160,8,BLACK);

  112.   tft.fillRoundRect(230,110,60,60,5,WHITE);
  113.   tft.fillCircle(245,125,8,BLACK);
  114.   tft.fillCircle(275,125,8,BLACK);
  115.   tft.fillCircle(245,155,8,BLACK);
  116.   tft.fillCircle(275,155,8,BLACK);

  117. while(!pres){
  118.   digitalWrite(13, HIGH);
  119.   TSPoint p = ts.getPoint();
  120.   digitalWrite(13, LOW);
  121.   pinMode(XM, OUTPUT);
  122.   pinMode(YP, OUTPUT);
  123.   if(p.z > ts.pressureThreshhold){
  124.   pres= true;
  125. // Serial.print(p.x); Serial.print(“ “); Serial.println(p.y);
  126.    
  127.     if (p.x>TSx_dices-100 && p.x<TSx_dices+100){
  128.       if(p.y>Tsy_1st_dice-75 && p.y<Tsy_1st_dice+75)
  129.       n=1;
  130.       else if(p.y>Tsy_2nd_dice-75 && p.y<Tsy_2nd_dice+75)
  131.       n=2;
  132.       else if(p.y>Tsy_3th_dice-75 && p.y<Tsy_3th_dice+75)
  133.       n=3;
  134.       else if(p.y>Tsy_4th_dice-75 && p.y<Tsy_4th_dice+75)
  135.       n=4;
  136.   }
  137. }
  138. }
  139. //-------------------------------------------------------------------------------------
  140. //asking players colors
  141. for(int j=0;j<n;j++){
  142.   pres=false;
  143.   tft.fillScreen(PURPLE);
  144.   tft.setRotation(1);
  145.   tft.setCursor(20, 50);
  146.   tft.setTextSize(3);
  147.   tft.print(“player”);tft.print(j+1);tft.print(“ color?”);
  148.   delay(200);
  149.   tft.fillRoundRect(20,110,60,60,5,WHITE);
  150.   tft.fillCircle(50,140,10,BLACK);
  151.   
  152.   tft.fillRoundRect(90,110,60,60,5,RED);
  153.   tft.fillCircle(120,140,10,BLACK);

  154.   tft.fillRoundRect(160,110,60,60,5,GREEN);
  155.   tft.fillCircle(190,140,10,BLACK);

  156.   tft.fillRoundRect(230,110,60,60,5,BLUE);
  157.   tft.fillCircle(260,140,10,BLACK);

  158.   while(!pres){
  159.     digitalWrite(13, HIGH);
  160.     TSPoint p = ts.getPoint();
  161.     digitalWrite(13, LOW);
  162.     pinMode(XM, OUTPUT);
  163.     pinMode(YP, OUTPUT);
  164.     if(p.z > ts.pressureThreshhold){
  165.       pres= true;
  166.         if (p.x>TSx_dices-100 && p.x<TSx_dices+100){
  167.         if(p.y>Tsy_1st_dice-75 && p.y<Tsy_1st_dice+75)
  168.         c[j]=WHITE;
  169.         else if(p.y>Tsy_2nd_dice-75 && p.y<Tsy_2nd_dice+75)
  170.         c[j]=RED;
  171.         else if(p.y>Tsy_3th_dice-75 && p.y<Tsy_3th_dice+75)
  172.         c[j]=GREEN;
  173.         else if(p.y>Tsy_4th_dice-75 && p.y<Tsy_4th_dice+75)
  174.         c[j]=BLUE;
  175.     }
  176.   }
  177. }
  178. }
  179.   for(int i=0;i<n;i++){
  180.     tft.fillScreen(c[i]);
  181.     delay(200);
  182. }
  183. }
复制代码

程序收到玩家的数量及其颜色后,将启动主代码。 使用KY-037声音模块,可以对骰子下落进行计数并存储在count变量中。 然后将计数除以玩家人数,其余的将显示下一个回合。 每回合,LCD都会显示玩家的颜色,并等待直到他/她掷骰子。 当骰子掉落时,RGB LED会显示随机颜色,LCD会播放简单的骰子动画。

LCD右侧显示总体丢球计数。

  1. int j=1;
  2. void loop() {
  3.   //calculation of players turn
  4.   t=count%n;
  5.   tft.fillScreen(c[t]);
  6.   tft.setTextColor(YELLOW);
  7.     tft.setRotation(1);
  8.     tft.setCursor(0, 50);
  9.     tft.setTextSize(3);
  10.     tft.print("it's player");tft.print(t+1);tft.print(" turn");
  11.   
  12.   //showing number of dice fallen
  13.   tft.fillRoundRect(225,110,60,60,5,BLACK);
  14.     tft.setTextColor(YELLOW);
  15.     tft.setRotation(1);
  16.     tft.setTextSize(2);
  17.     tft.setCursor(245, 135);
  18.     tft.print(count);
  19. //sensing with microphone
  20. while(j==0){
  21. pinMode(13,INPUT);
  22. int a=digitalRead(ss);
  23.   if(a==0)j=1;
  24. //showing the color of player whose turn is
  25.   if(c[t]==RED){
  26.   digitalWrite(rLED,HIGH);digitalWrite(gLED,LOW);digitalWrite(bLED,LOW);}
  27.   
  28.   if(c[t]==BLUE){
  29.   digitalWrite(rLED,LOW);digitalWrite(gLED,LOW);digitalWrite(bLED,HIGH);}
  30.   
  31.   if(c[t]==GREEN){
  32.   digitalWrite(rLED,LOW);digitalWrite(gLED,HIGH);digitalWrite(bLED,LOW);}
  33.   
  34.   if(t==WHITE){
  35.   digitalWrite(rLED,HIGH);digitalWrite(gLED,HIGH);digitalWrite(bLED,HIGH);}
  36.   }
  37.   //LED equalizer
  38.   if(j==1){
  39.   for(int i=0;i<6;i==){
  40.   digitalWrite(rLED,LOW);digitalWrite(gLED,HIGH);digitalWrite(bLED,HIGH);
  41.   delay(100);
  42.   digitalWrite(rLED,HIGH);digitalWrite(gLED,LOW);digitalWrite(bLED,HIGH);
  43.   delay(100);
  44.   digitalWrite(rLED,HIGH);digitalWrite(gLED,HIGH);digitalWrite(bLED,LOW);
  45.   delay(100);
  46.   digitalWrite(rLED,LOW);digitalWrite(gLED,LOW);digitalWrite(bLED,LOW);
  47.   delay(100);
  48.   //LCD dice animation
  49.   tft.setTextColor(YELLOW);
  50.     tft.setRotation(1);
  51.     tft.setCursor(20, 200);
  52.     tft.setTextSize(2);
  53.     tft.print("Dice is falling...”);
  54.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  55.   delay(200);
  56.   Dice(random(6)+1);
  57.   }
  58.   a=1;
  59.   j=0;
  60.   }
  61. count++;
  62. }
  63. //-------------------------------------------------------------------------------------
  64. //dice drawing function
  65. void Dice(int d){
  66.   if(d==1){
  67.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  68.   tft.drawRoundRect(130,110,60,60,5,BLACK);
  69.   tft.fillCircle(160,140,10,RED);
  70. }
  71.   else if(d==2){
  72.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  73.   tft.drawRoundRect(130,110,60,60,5,BLACK);
  74.   tft.fillCircle(160,125,8,BLACK);
  75.   tft.fillCircle(160,155,8,BLACK);
  76.   }
  77.   else if(d==3){
  78.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  79.   tft.drawRoundRect(130,110,60,60,5,BLACK);
  80.   tft.fillCircle(140,120,8,BLACK);
  81.   tft.fillCircle(160,140,8,BLACK);
  82.   tft.fillCircle(180,160,8,BLACK);
  83. }
  84.   else if(d==4){
  85.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  86.   tft.drawRoundRect(130,110,60,60,5,BLACK);
  87.   tft.fillCircle(145,125,8,BLACK);
  88.   tft.fillCircle(175,125,8,BLACK);
  89.   tft.fillCircle(145,155,8,BLACK);
  90.   tft.fillCircle(175,155,8,BLACK);
  91.   }
  92.   else if(d==5){
  93.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  94.   tft.drawRoundRect(130,110,60,60,5,BLACK);
  95.   tft.fillCircle(145,125,8,BLACK);
  96.   tft.fillCircle(175,125,8,BLACK);
  97.   tft.fillCircle(145,155,8,BLACK);
  98.   tft.fillCircle(175,155,8,BLACK);
  99.   tft.fillCircle(160,140,8,BLACK);
  100.   }
  101.   else if(d==6){
  102.   tft.fillRoundRect(130,110,60,60,5,WHITE);
  103.   tft.drawRoundRect(130,110,60,60,5,BLACK);
  104.   tft.fillCircle(145,125,6,BLACK);
  105.   tft.fillCircle(175,125,6,BLACK);
  106.   tft.fillCircle(145,140,6,BLACK);
  107.   tft.fillCircle(175,140,6,BLACK);
  108.   tft.fillCircle(145,155,6,BLACK);
  109.   tft.fillCircle(175,155,6,BLACK);
  110. }
  111. }
复制代码

这部分代码使用Dice (number) 函数显示骰子数。


为了使游戏更有趣,可以使用图片代替颜色。 将您喜欢的图像保存在micro SD卡中,然后放入SD卡插槽。 以下代码从SD卡读取图像。

  1. void bmpDraw(String filename, int x, int y) {
  2.   File     bmpFile;
  3.   int      bmpWidth, bmpHeight;   // W+H in pixels
  4.   uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  5.   uint32_t bmpImageoffset;        // Start of image data in file
  6.   uint32_t rowSize;               // Not always = bmpWidth; may have padding
  7.   uint8_t  sdbuffer[3 * BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
  8.   uint16_t lcdbuffer[BUFFPIXEL];  // pixel out buffer (16-bit per pixel)
  9.   uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  10.   boolean  goodBmp = false;       // Set to true on valid header parse
  11.   boolean  flip    = true;        // BMP is stored bottom-to-top
  12.   int      w, h, row, col;
  13.   uint8_t  r, g, b;
  14.   uint32_t pos = 0, startTime = millis();
  15.   uint8_t  lcdidx = 0;
  16.   boolean  first = true;
  17.   if ((x >= tft.width()) || (y >= tft.height())) return;
  18.   Serial.println();
  19.   Serial.print(F("Loading image  ‘ ‘’));
  20.   Serial.print(filename);
  21.   Serial.println(‘\’’);
  22.   // Open requested file on SD card
  23.   if ((bmpFile = SD.open(filename)) == NULL){
  24.     Serial.println(F("File not found”));
  25.     return;
  26.   }
  27.   // Parse BMP header
  28.   if (read16(bmpFile) == 0x4D42) { // BMP signature
  29.     Serial.println(F("File size: ")); Serial.println(read32(bmpFile));
  30.     (void)read32(bmpFile); // Read & ignore creator bytes
  31.     bmpImageoffset = read32(bmpFile); // Start of image data
  32.     Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
  33.     // Read DIB header
  34.     Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
  35.     bmpWidth  = read32(bmpFile);
  36.     bmpHeight = read32(bmpFile);
  37.     if (read16(bmpFile) == 1) { // # planes -- must be '1'
  38.       bmpDepth = read16(bmpFile); // bits per pixel
  39.       Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
  40.       if ((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  41.         goodBmp = true; // Supported BMP format -- proceed!
  42.         Serial.print(F("Image size:”));
  43.         Serial.print(bmpWidth);
  44.         Serial.print('x’);
  45.         Serial.println(bmpHeight);
  46.         // BMP rows are padded (if needed) to 4-byte boundary
  47.         rowSize = (bmpWidth * 3 + 3) & ~3;
  48.         // If bmpHeight is negative, image is in top-down order.
  49.         // This is not canon but has been observed in the wild.
  50.         if (bmpHeight < 0){
  51.           bmpHeight = -bmpHeight;
  52.           flip      = false;
  53.         }
  54.         // Crop area to be loaded
  55.         w = bmpWidth;
  56.         h = bmpHeight;
  57.         if ((x + w - 1) >= tft.width())  w = tft.width()  - x;
  58.         if ((y + h - 1) >= tft.height()) h = tft.height() - y;

  59.         // Set TFT address window to clipped image bounds
  60.         tft.setAddrWindow(x, y, x + w - 1, y + h – 1);
  61.         for (row = 0; row < h; row++) { // For each scanline...
  62.           // Seek to start of scan line.  It might seem labor-
  63.           // intensive to be doing this on every line, but this
  64.           // method covers a lot of gritty details like cropping
  65.           // and scanline padding.  Also, the seek only takes
  66.           // place if the file position actually needs to change
  67. //   (avoids a lot of cluster math in SD library).
  68.           if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
  69.             pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  70.           else     // Bitmap is stored top-to-bottom
  71.             pos = bmpImageoffset + row * rowSize;
  72.           if (bmpFile.position() != pos) { // Need seek?
  73.             bmpFile.seek(pos);
  74.             buffidx = sizeof(sdbuffer); // Force buffer reload
  75.           }
  76.           for (col = 0; col < w; col++) { // For each column...
  77.              // Time to read more pixel data?
  78.             if (buffidx >= sizeof(sdbuffer)) { // Indeed
  79.                       // Push LCD buffer to the display first
  80.               if (lcdidx > 0) {
  81.                 tft.pushColors(lcdbuffer, lcdidx, first);
  82.                 lcdidx = 0;
  83.                 first  = false;
  84.               }
  85.               bmpFile.read(sdbuffer, sizeof(sdbuffer));
  86.               buffidx = 0; // Set index to beginning
  87.             }
  88.             // Convert pixel from BMP to TFT format
  89.             b = sdbuffer[buffidx++];
  90.             g = sdbuffer[buffidx++];
  91.             r = sdbuffer[buffidx++];
  92.             lcdbuffer[lcdidx++] = tft.color565(r, g, b);
  93.           }// end pixel
  94.          }// end scanline
  95.          // Write any remaining data to LCD
  96.         if (lcdidx > 0){
  97.           tft.pushColors(lcdbuffer, lcdidx, first);
  98.         }
  99.         Serial.print(F("Loaded in “));
  100.         Serial.print(millis() – startTime);
  101.         Serial.println(" ms”);
  102.      }// end goodBmp
  103.     {
  104.   {
  105.   bmpFile.close();
  106.   if (!goodBmp) Serial.println(F("BMP format not recognized.”));
  107. }
  108. // These read 16- and 32-bit types from the SD card file.
  109. // BMP data is stored little-endian, Arduino is little-endian too.
  110. // May need to reverse subscript order if porting elsewhere.
  111. uint16_t read16(File f) {
  112.   uint16_t result;
  113.   ((uint8_t *)&result)[0] = f.read(); // LSB
  114. ((uint8_t *)&result)[1] = f.read(); // MSB
  115.   return result;
  116. }
  117. uint32_t read32(File f) {
  118.   uint32_t result;
  119.   ((uint8_t *)&result)[0] = f.read(); // LSB
  120.   ((uint8_t *)&result)[1] = f.read();
  121.   ((uint8_t *)&result)[2] = f.read();
  122.   ((uint8_t *)&result)[3] = f.read(); // MSB
  123.   return result;
  124. }
复制代码

将此函数放在主代码的末尾,并使用tft.fillScreen(c[t]); 替换整个代码中的bmpDraw(c[t], 0, 0); 。 然后,每回合,LCD都会显示玩家的图像而不是颜色。


变量c [t]应该是长度至少为10个字符的字符串。 它包含SD卡上图片的名称。SD卡上的所有图片必须为320×240像素,并且位图为24位格式。


以上就是本文的全部内容。您可以更改虚拟骰子的代码,以使掷出6的玩家获得另一次掷骰子机会的奖励。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-25 15:28 , Processed in 0.050488 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表