deckle studioOpen in the editor

Retro game console (ESP32, VGA, sound): three games

A menu and three games on a VGA monitor, played with the arrow keys, Space and Enter, with sound through a real amplifier.

Open in the simulatorNo account needed: press Simulate and it runs.

How it works

An ESP32 is a small game console: FabGL draws the games at 320 × 200 pixels in 64 colours into two frame buffers; I2S1 sends the finished picture by DMA through the resistor DAC (two bits per colour) to the VGA monitor, 75 times a second. Six buttons are the controller: the arrow keys, Space (A) and Enter (START) on your keyboard. Three games of our own: SNAKE (an arrow key starts it; steer the snake to the apples, it grows and gets faster), BRICKS (keep the ball in play with the paddle and break the wall; A launches the ball), METEORS (fly the ship left and right and shoot the falling rocks with A). START goes back to the menu, which keeps the best score of each game. The sound: FabGL's SoundGenerator mixes square, triangle and noise voices and sends them by I2S0 DMA to the DAC on GPIO25, then a volume potentiometer, a PAM8403 amplifier and the speaker: turn on the sound with the speaker button. The sketch (retro_console) is in the code view: change a game or write your own.

Wiring

Each line is one connection (net) and the pins it joins.

NetPins
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 +

Parts

Firmware

sketch.ino · 343 lines · the first 120 here, all of it in the 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");
Open in the simulator

More examples: Microcontrollers