如果您沒有足夠的時間,您還可以在以下時間軸中選擇您想觀看的部分!
視頻時間軸:
00:00 開始
00:40 結果展示(演示)
01:15 編碼簡報
02:13 在代碼中使用隨機
04:23 在隨機位置顯示骰子
08:53 drawDice 函數
14:59 顯示總分
16:07 選擇 u8g2 字體>
====================== ESP32 Codes ===============================
#include <Arduino.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
void setup(void)
{
Serial.begin(115200);
u8g2.begin();
randomSeed(analogRead(5)); // randomSeed() must be in setup()!
}
// OLED TEXT ROW number, vertical position
const byte ROW[5] = {0, 15, 31, 47, 63};
// 4 dice top-left {x, y} locations in tl[]
const int tl[4][2] = {{2, 20}, {34, 28}, {68, 20}, {100, 28}};
const int size = 28; // dice width and height
const int diceRoundedCorner = 5;
/**
* drawDiceImage
* n = dice serial (1 ~ 4)
* pnt = dice points (1 ~ 6)
*/
void drawDiceImage(int n, int pnt)
{
int tx = tl[n][0]; // top left x
int ty = random(17, 35); // Makes the y showing at random height
// int ty = tl[n][1]; //top left y, makes Y showing at fixed height
int centerX = tx + (size / 2); // center of the dice X
int centerY = ty + (size / 2); // center of th dice Y
const int largeDotSize = 4; // drawing dot size for 1 only
const int smallDotSize = 2; // drawing dot size for rest
u8g2.drawRFrame(tx, ty, size, size, diceRoundedCorner); // draw the dice border
// draw the points based on the variable: pnt
switch (pnt)
{
case 1:
u8g2.drawFilledEllipse(centerX, centerY, largeDotSize, largeDotSize, U8G2_DRAW_ALL);
break;
case 2:
u8g2.drawFilledEllipse(centerX, centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX, centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
break;
case 3:
u8g2.drawFilledEllipse(centerX, centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX, centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX, centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
break;
case 4:
u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
break;
case 5:
u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX, centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
break;
case 6:
u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX - (size / 4), centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
break;
default:
break;
}
}
void loop(void)
{
int dicePoint[4];
int totalPoints = 0;
u8g2.clearBuffer();
//Generate 4 random dice points
for (int i = 0; i < 4; i++)
{
dicePoint[i] = random(1, 6); // get random dice number to point
drawDiceImage(i, dicePoint[i]); // draw dice i with the random dicePoint
totalPoints += dicePoint[i]; // adding current points to totalPoints
}
//Print out the title + total points
u8g2.setCursor(0, ROW[1]);
u8g2.setFont(u8g2_font_lubBI14_te); // font list: https://github.com/olikraus/u8g2/wiki/fntlist16
u8g2.print("POINTS:");
u8g2.print(totalPoints);
u8g2.sendBuffer();
delay(1500);
}
==================================================================