deckle studioIm Editor öffnen

Retro-Spielkonsole (ESP32, VGA, Sound): drei Spiele

Ein Menü und drei Spiele auf einem VGA-Monitor, gespielt mit Pfeiltasten, Leertaste und Enter, mit Sound über einen echten Verstärker.

Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.

So funktioniert es

Ein ESP32 wird zur kleinen Spielkonsole: FabGL zeichnet die Spiele mit 320 × 200 Pixeln in 64 Farben in zwei Bildspeicher; I2S1 schickt das fertige Bild per DMA durch den Widerstands-DAC (zwei Bit pro Farbe) an den VGA-Monitor, 75-mal pro Sekunde. Sechs Taster sind der Controller: die Pfeiltasten, Leertaste (A) und Enter (START) auf deiner Tastatur. Drei eigene Spiele: SNAKE (eine Pfeiltaste startet; steuere die Schlange zu den Äpfeln, sie wächst und wird schneller), BRICKS (halte den Ball mit dem Schläger im Spiel und brich die Mauer ab; A schießt den Ball ab), METEORS (flieg das Schiff nach links und rechts und schieß die fallenden Brocken mit A ab). START führt zurück ins Menü, das den Rekord jedes Spiels behält. Der Ton: FabGLs SoundGenerator mischt Rechteck-, Dreieck- und Rauschstimmen und schickt sie per I2S0-DMA an den DAC an GPIO25, dann über ein Lautstärke-Potentiometer, einen PAM8403-Verstärker auf den Lautsprecher: schalte den Ton mit dem Lautsprecher-Knopf ein. Der Sketch (retro_console) steht in der Code-Ansicht: änder ein Spiel oder schreib dein eigenes.

Verdrahtung

Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.

NetzPins
GNDA 2A1 GNDA2 GNDDOWN 2J1 GNDLEFT 2RIGHT 2RV1 1START 2UP 2
V5A1 5VA2 +5V
VOLA2 L inA2 R inRV1 W
VSYNCA1 GPIO15J1 VSYNC
B0A1 GPIO4R6 1
B1A1 GPIO5R5 1
BLUEJ1 BLUER5 2R6 2
BTN_AA 1A1 GPIO13
BTN_DOWNA1 GPIO33DOWN 1
BTN_LEFTA1 GPIO26LEFT 1
BTN_RIGHTA1 GPIO27RIGHT 1
BTN_STARTA1 GPIO14START 1
BTN_UPA1 GPIO32UP 1
DACA1 GPIO25RV1 2
G0A1 GPIO18R4 1
G1A1 GPIO19R3 1
GREENJ1 GREENR3 2R4 2
HSYNCA1 GPIO23J1 HSYNC
R0A1 GPIO21R2 1
R1A1 GPIO22R1 1
REDJ1 REDR1 2R2 2
SPK_NA2 L−LS1 −
SPK_PA2 L+LS1 +
DOWN
Taster
LEFT
Taster
RIGHT
Taster
START
Taster

Bauteile

Firmware

sketch.ino · 343 Zeilen · die ersten 120 hier, alles im Editor

// ESP32 retro console: three games on a VGA monitor (FabGL, 320x200, 64 colours, double buffered),
// six buttons (arrows on GPIO32 / 33 / 26 / 27, A on GPIO13, START on GPIO14, to GND with the internal
// pull-ups) and sound (FabGL's SoundGenerator: I2S0 DMA to the DAC on GPIO25, then a volume
// potentiometer, a PAM8403 amplifier and a speaker). Games: SNAKE (eat, grow, do not bite yourself),
// BRICKS (break the wall with the ball) and METEORS (a ship against falling rocks).
#include "fabgl.h"

fabgl::VGAController VGA;
fabgl::Canvas *cv;
fabgl::SoundGenerator sound(8000);

const int W = 320, H = 200;

// ---- buttons ----------------------------------------------------------------------------------
enum { B_UP, B_DOWN, B_LEFT, B_RIGHT, B_A, B_START };
const int BTN[6] = { 32, 33, 26, 27, 13, 14 };
uint8_t held = 0, pressed = 0, seen = 0, latched = 0;
uint8_t sample() { uint8_t now = 0; for (int i = 0; i < 6; i++) if (digitalRead(BTN[i]) == LOW) now |= 1 << i; return now; }
// between two frames the buttons are sampled every millisecond: a short tap is not lost
void latchButtons() { uint8_t now = sample(); latched |= now & ~seen; seen = now; }
void readButtons() {
  latchButtons();
  uint8_t now = sample();
  pressed = latched;
  latched = 0;
  held = now | pressed;
}
bool isDown(int b) { return (held >> b) & 1; }
bool hit(int b) { return (pressed >> b) & 1; }

// ---- sound ------------------------------------------------------------------------------------
void beep(int hz, int ms, int vol = 90) { sound.playSound(SquareWaveformGenerator(), hz, ms, vol); }
void tone2(int hz, int ms, int vol = 110) { sound.playSound(TriangleWaveformGenerator(), hz, ms, vol); }
void crash(int ms) { sound.playSound(NoiseWaveformGenerator(), 1, ms, 90); }
// a short melody, one note after the other (frequency, milliseconds; 0 ends it)
const int *tune = nullptr;
uint32_t tuneAt = 0;
void play(const int *t) { tune = t; tuneAt = 0; }
void tickTune() {
  if (!tune || millis() < tuneAt) return;
  if (!tune[0]) { tune = nullptr; return; }
  if (tune[0] > 1) beep(tune[0], tune[1] - 10);
  tuneAt = millis() + tune[1];
  tune += 2;
}
const int TUNE_START[] = { 523, 90, 659, 90, 784, 90, 1047, 180, 0 };
const int TUNE_OVER[] = { 392, 160, 330, 160, 262, 160, 196, 360, 0 };
const int TUNE_LEVEL[] = { 784, 80, 988, 80, 1175, 80, 1568, 200, 0 };

// ---- drawing helpers ----------------------------------------------------------------------------
RGB888 rgb(int r, int g, int b) { return RGB888(r, g, b); }
void text(int x, int y, const char *s, RGB888 c) { cv->setPenColor(c); cv->drawText(x, y, s); }
void textC(int y, const char *s, RGB888 c) { text((W - (int)strlen(s) * 8) / 2, y, s, c); }
void box(int x, int y, int w, int h, RGB888 c) { cv->setBrushColor(c); cv->fillRectangle(x, y, x + w - 1, y + h - 1); }
void hud(const char *name, int score, int extra, const char *extraName) {
  box(0, 0, W, 12, rgb(0, 0, 0));
  char s[48];
  text(4, 2, name, rgb(255, 255, 0));
  snprintf(s, sizeof s, "SCORE %d", score); text(120, 2, s, rgb(255, 255, 255));
  if (extraName) { snprintf(s, sizeof s, "%s %d", extraName, extra); text(232, 2, s, rgb(0, 255, 255)); }
}

int hiScore[3];
enum Mode { MENU, SNAKE, BRICKS, METEORS };
Mode mode = MENU;
void toMenu();

// =================================================================================================
// SNAKE: steer with the arrows, eat the apples, the snake grows and gets faster
// =================================================================================================
namespace snake {
  const int CS = 10, GW = 32, GH = 18, OY = 16;
  int8_t sx[GW * GH], sy[GW * GH];
  int len, dir, nextDir, fx, fy, score;
  uint32_t stepMs, lastStep;
  bool over, waiting;
  void place() {
    for (;;) {
      fx = random(GW); fy = random(GH);
      bool onSnake = false;
      for (int i = 0; i < len; i++) if (sx[i] == fx && sy[i] == fy) onSnake = true;
      if (!onSnake) return;
    }
  }
  void start() {
    len = 4;
    for (int i = 0; i < len; i++) { sx[i] = 10 - i; sy[i] = 9; }
    dir = nextDir = 0; score = 0; stepMs = 150; lastStep = millis(); over = false; waiting = true;
    place();
  }
  void update() {
    if (over) { if (hit(B_A) || hit(B_START)) toMenu(); return; }
    if (hit(B_UP) && dir != 1) nextDir = 3;
    if (hit(B_DOWN) && dir != 3) nextDir = 1;
    if (hit(B_LEFT) && dir != 0) nextDir = 2;
    if (hit(B_RIGHT) && dir != 2) nextDir = 0;
    if (hit(B_START)) { toMenu(); return; }
    if (waiting) { if (pressed & 0x0F) waiting = false; else return; }
    if (millis() - lastStep < stepMs) return;
    lastStep = millis();
    dir = nextDir;
    int nx = sx[0] + (dir == 0) - (dir == 2), ny = sy[0] + (dir == 1) - (dir == 3);
    bool dead = nx < 0 || ny < 0 || nx >= GW || ny >= GH;
    for (int i = 0; i < len - 1 && !dead; i++) if (sx[i] == nx && sy[i] == ny) dead = true;
    if (dead) { over = true; crash(250); play(TUNE_OVER); if (score > hiScore[0]) hiScore[0] = score; return; }
    bool eat = nx == fx && ny == fy;
    if (eat) { len++; score += 10; if (stepMs > 60) stepMs -= 7; tone2(880, 50); tone2(1320, 50); }
    for (int i = len - 1; i > 0; i--) { sx[i] = sx[i - 1]; sy[i] = sy[i - 1]; }
    sx[0] = nx; sy[0] = ny;
    if (eat) place();
  }
  void draw() {
    box(0, 0, W, H, rgb(0, 0, 64));
    box(0, OY - 2, W, 2, rgb(0, 128, 255));
    // the apple
    cv->setBrushColor(rgb(255, 0, 0)); cv->fillEllipse(fx * CS + 5, OY + fy * CS + 5, 9, 9);
    box(fx * CS + 5, OY + fy * CS, 2, 3, rgb(0, 255, 0));
    // the snake, the head brighter
    for (int i = len - 1; i >= 0; i--) box(sx[i] * CS + 1, OY + sy[i] * CS + 1, CS - 2, CS - 2, i ? rgb(0, 192, 64) : rgb(128, 255, 128));
    hud("SNAKE", score, len, "LENGTH");
Im Simulator öffnen

Weitere Beispiele: Mikrocontroller