Community project

ESP32 Mini Game Menu

Photo of ESP32 Mini Game Menu
Generated with AI

anissovainna1

Published November 20, 2026

This project builds a portable game console on an ESP32 microcontroller with a touchscreen display. The device runs a menu system that lets players select and play eight different games including Tic-Tac-Toe, Memory, Snake, Pong, Breakout, and more, with touch controls for gameplay and navigation.

The guide provides a complete parts list, wiring diagram for connecting the ESP32 to a TFT touchscreen display and USB power, the compiled firmware with all game logic, and step-by-step assembly instructions. Builders will have everything needed to assemble the hardware and load the software onto the ESP32 to create a fully functional handheld gaming device.

Wiring diagram

Wiring diagram for ESP32 Mini Game Menu
Loading diagram…

Assemble it in 2 steps

1. Préparer le module

Utiliser le CYD2 Guition à écran 2,8 pouces, avec micro-USB et USB-C. L'écran et le tactile sont déjà connectés sur la carte. Aucun fil supplémentaire ni carte microSD n'est nécessaire.

  • Ne pas brancher les deux prises USB simultanément : cela peut relier les alimentations de deux appareils.

2. Brancher l'alimentation USB

Poser la carte sur une surface non conductrice. Brancher une seule prise USB avec un câble qui transporte les données.

  • Le tactile résistif demande une légère pression. Utiliser le doigt ou un stylet plastique, jamais un objet métallique pointu.

Deploy the firmware

#include <Arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>
#include <Preferences.h>
TFT_eSPI lcd;
TFT_eSprite canvas(&lcd);
XPT2046_Touchscreen touch(33,36);
Preferences prefs;
float ax,bx,cx,ay,by,cy;
const uint16_t bg=0x0000, blue=0x867F, purple=0xB299, pink=0xFCB6;
int page=0; bool held=false,ended=false,dirty=true,buffered=false;
uint32_t deadline=0,started=0,lastFrame=0,lastStep=0;
const char* names[]={"","Morpion","Memory","Reflexes","Snake","Pong","Casse-briques","Esquive","Taquin"};
int cells[16],cards[12],first=-1,second=-1,pairs=0,attempts=0,score=0;
bool matched[12],ready=false;
int sx[160],sy[160],length=3,dir=0,nextDir=0,foodX,foodY;
float ballX,ballY,vx,vy; int paddle=160,enemy=100;
bool bricks[24]; int tiles[16];
float obstacleX[5],obstacleY[5];
// Compare small tiles in the off-screen frame; send only changed pixels.
uint32_t tileHash[20*13];
bool frameValid=false;
void presentFrame(){
  const uint16_t *pixels=static_cast<const uint16_t*>(canvas.getPointer());
  for(int row=0;row<13;row++)for(int col=0;col<20;col++){
    int x=col*16,y=row*16,h=min(16,204-y);
    uint32_t hash=2166136261u;
    for(int j=0;j<h;j++)for(int i=0;i<16;i++){
      hash^=pixels[(y+j)*320+x+i];hash*=16777619u;
    }
    int index=row*20+col;
    if(!frameValid||tileHash[index]!=hash){
      canvas.pushSprite(x,36+y,x,y,16,h);
      tileHash[index]=hash;
    }
  }
  frameValid=true;
}
void text(String s,int x,int y,uint16_t c=blue){lcd.setTextColor(c,bg);lcd.drawString(s,x,y,2);}
void button(String s,int x,int y,int w,int h,uint16_t c=purple){lcd.fillRoundRect(x,y,w,h,8,c);lcd.setTextColor(bg,c);lcd.drawCentreString(s,x+w/2,y+h/2-8,2);}
void header(String s){lcd.fillScreen(bg);text(s,8,8);button("Menu",248,2,68,28,pink);}
void menu(){page=0;lcd.fillScreen(bg);text("CYD ARCADE",10,6,pink);text("8 jeux",252,6,blue);for(int i=0;i<8;i++){int x=8+(i%2)*156,y=34+(i/2)*44;uint16_t c=i%3==0?blue:i%3==1?purple:pink;button(String(i+1)+"  "+names[i+1],x,y,148,38,c);}text("Calibration : maintenir ici 3 s",40,218,purple);}
int winner(){const int lines[8][3]={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};for(auto &l:lines)if(cells[l[0]]&&cells[l[0]]==cells[l[1]]&&cells[l[0]]==cells[l[2]])return cells[l[0]];for(int i=0;i<9;i++)if(!cells[i])return 0;return 3;}
void drawTT(){header("Morpion : toi X");for(int i=0;i<9;i++){int x=65+(i%3)*62,y=35+(i/3)*58;lcd.drawRoundRect(x,y,60,56,7,purple);if(cells[i])text(cells[i]==1?"X":"O",x+24,y+18,cells[i]==1?blue:pink);}int w=winner();ended=w;text(w==1?"Gagne ! Touche pour rejouer":w==2?"Perdu ! Touche pour rejouer":w==3?"Egalite ! Touche pour rejouer":"Touche une case libre",10,216);}
void drawMemory(){header("Memory");for(int i=0;i<12;i++){int x=8+(i%4)*78,y=35+(i/4)*56;bool show=matched[i]||i==first||i==second;button(show?String(cards[i]+1):"?",x,y,72,50,show?pink:purple);}text("Paires "+String(pairs)+"/6  Essais "+String(attempts),8,209);if(pairs==6){ended=true;text("Bravo ! Touche pour rejouer",8,226);}}
void reflexStart(){ready=false;ended=false;deadline=millis()+random(1800,5000);header("Reflexes");button("Attends le ROSE...",15,65,290,110,blue);}
void food(){bool occupied;do{foodX=random(20);foodY=random(10);occupied=false;for(int i=0;i<length;i++)if(sx[i]==foodX&&sy[i]==foodY)occupied=true;}while(occupied);}
void render(){if(!dirty)return;dirty=false;
// Only the changing playing field is transferred. The header stays on screen.
TFT_eSPI &g=buffered?static_cast<TFT_eSPI&>(canvas):lcd;
int off=buffered?0:36;
g.fillRect(0,off,320,204,bg);g.setTextColor(blue,bg);
if(page==4){for(int i=0;i<length;i++)g.fillRoundRect(sx[i]*16+1,off+sy[i]*16+1,14,14,3,i?purple:blue);g.fillCircle(foodX*16+8,off+foodY*16+8,6,pink);g.drawCentreString("Touche un cote : direction",160,off+178,2);}
if(page==5){g.drawFastVLine(160,off,166,purple);g.fillRoundRect(6,off+enemy-22,7,44,3,purple);g.fillRoundRect(307,off+paddle-22,7,44,3,blue);g.fillCircle(ballX,off+ballY,5,pink);g.drawCentreString("Glisse a droite : raquette",160,off+178,2);}
if(page==6){for(int i=0;i<24;i++)if(bricks[i])g.fillRoundRect(4+(i%8)*39,off+8+(i/8)*15,35,11,3,i%2?pink:purple);g.fillRoundRect(paddle-25,off+163,50,7,3,blue);g.fillCircle(ballX,off+ballY,4,pink);g.drawCentreString("Glisse : raquette",160,off+178,2);}
if(page==7){for(int i=0;i<5;i++)g.fillRoundRect(obstacleX[i],off+obstacleY[i],20,16,4,pink);g.fillTriangle(paddle,off+150,paddle-10,off+166,paddle+10,off+166,blue);g.drawCentreString("Glisse pour eviter les blocs",160,off+178,2);}
if(page==8){for(int i=0;i<16;i++)if(tiles[i]){int x=70+(i%4)*45,y=off+(i/4)*43;uint16_t c=tiles[i]==i+1?blue:purple;g.fillRoundRect(x,y,41,39,6,c);g.setTextColor(bg,c);g.drawCentreString(String(tiles[i]),x+20,y+11,2);}g.setTextColor(blue,bg);g.drawCentreString("Touche a cote du trou",160,off+178,2);}
if(ended){g.fillRoundRect(20,off+62,280,70,8,purple);g.setTextColor(bg,purple);g.drawCentreString("Score : "+String(score),160,off+72,2);g.drawCentreString("Touche pour rejouer",160,off+104,2);}
if(buffered)presentFrame();
}
void startGame(int p){frameValid=false;page=p;ended=false;score=0;dirty=true;lastStep=millis();header(names[p]);
if(p==1){memset(cells,0,sizeof(cells));drawTT();}
if(p==2){first=second=-1;pairs=attempts=0;memset(matched,0,sizeof(matched));for(int i=0;i<12;i++)cards[i]=i/2;for(int i=11;i>0;i--){int j=random(i+1);std::swap(cards[i],cards[j]);}drawMemory();}
if(p==3)reflexStart();
if(p==4){length=3;dir=nextDir=0;for(int i=0;i<3;i++){sx[i]=6-i;sy[i]=5;}food();}
if(p==5||p==6){ballX=160;ballY=100;vx=p==5?2.8:2;vy=2;paddle=p==5?100:160;enemy=100;for(bool &b:bricks)b=true;}
if(p==7){paddle=160;for(int i=0;i<5;i++){obstacleX[i]=random(300);obstacleY[i]=-i*45;}}
if(p==8){for(int i=0;i<15;i++)tiles[i]=i+1;tiles[15]=0;int hole=15,previous=-1;for(int k=0;k<200;k++){int opts[4],n=0;for(int i=0;i<16;i++)if(i!=previous&&abs(i%4-hole%4)+abs(i/4-hole/4)==1)opts[n++]=i;int j=opts[random(n)];tiles[hole]=tiles[j];tiles[j]=0;previous=hole;hole=j;}}
if(p>=4)render();}
void waitRelease(){while(touch.touched())delay(10);delay(100);}
void calibrate(){float rx[3],ry[3];const int tx[3]={25,295,25},ty[3]={45,45,215};waitRelease();for(int i=0;i<3;i++){lcd.fillScreen(bg);text("Touche le centre de la croix",10,8);lcd.drawLine(tx[i]-10,ty[i],tx[i]+10,ty[i],TFT_YELLOW);lcd.drawLine(tx[i],ty[i]-10,tx[i],ty[i]+10,TFT_YELLOW);while(!touch.touched())delay(10);long sx=0,sy=0;int n=0;for(int k=0;k<20;k++){if(touch.touched()){auto p=touch.getPoint();sx+=p.x;sy+=p.y;n++;}delay(10);}if(!n){i--;continue;}rx[i]=float(sx)/n;ry[i]=float(sy)/n;waitRelease();}float u=rx[1]-rx[0],v=ry[1]-ry[0],w=rx[2]-rx[0],z=ry[2]-ry[0],d=u*z-w*v;if(fabs(d)<10000){calibrate();return;}ax=270*z/d;bx=-270*w/d;ay=-170*v/d;by=170*u/d;cx=25-ax*rx[0]-bx*ry[0];cy=45-ay*rx[0]-by*ry[0];prefs.putFloat("ax",ax);prefs.putFloat("bx",bx);prefs.putFloat("cx",cx);prefs.putFloat("ay",ay);prefs.putFloat("by",by);prefs.putFloat("cy",cy);prefs.putBool("cal",true);held=false;menu();}
void tap(int x,int y){if(page&&x>=248&&y<32){menu();return;}
if(page==0){if(y>=34&&y<210&&x>=8&&x<312){int col=x>=164;int row=(y-34)/44;if((y-34)%44<38)startGame(1+row*2+col);}return;}
if(ended){startGame(page);return;}
if(page==1){if(x<65||x>=251||y<35||y>=209)return;int i=((y-35)/58)*3+(x-65)/62;if(cells[i])return;cells[i]=1;if(!winner()){int choice=-1;for(int who=2;who>=1&&choice<0;who--)for(int k=0;k<9;k++)if(!cells[k]){cells[k]=who;if(winner()==who)choice=k;cells[k]=0;if(choice>=0)break;}if(choice<0&&!cells[4])choice=4;if(choice<0){int free[9],n=0;for(int k=0;k<9;k++)if(!cells[k])free[n++]=k;choice=free[random(n)];}cells[choice]=2;}drawTT();}
if(page==2){if(second>=0||y<35||y>=203||x<8||x>=320)return;int i=((y-35)/56)*4+(x-8)/78;if(i>=12||matched[i]||i==first)return;if(first<0)first=i;else{second=i;attempts++;deadline=millis()+700;}drawMemory();}
if(page==3){ended=true;header("Reflexes");text(ready?String(millis()-started)+" ms":"Trop tot !",80,90,pink);button("Toucher pour rejouer",25,145,270,55);}
if(page==4){int d;if(abs(x-160)>abs(y-120)*2)d=x>160?0:2;else d=y>120?1:3;if(d!=(dir+2)%4)nextDir=d;}
if(page==8&&x>=70&&x<250&&y>=36&&y<208){int i=(y-36)/43*4+(x-70)/45,h=0;while(tiles[h])h++;if(abs(i%4-h%4)+abs(i/4-h/4)==1){std::swap(tiles[i],tiles[h]);score++;ended=true;for(int k=0;k<15;k++)if(tiles[k]!=k+1)ended=false;dirty=true;}}
}
void tick(uint32_t now){if(ended)return;
if(page==4&&now-lastStep>=140){lastStep=now;dir=nextDir;int x=sx[0]+(dir==0)-(dir==2),y=sy[0]+(dir==1)-(dir==3);bool eat=x==foodX&&y==foodY;ended=x<0||x>=20||y<0||y>=10;for(int i=0;i<length-(eat?0:1);i++)if(sx[i]==x&&sy[i]==y)ended=true;if(!ended){if(eat){length++;score++;}for(int i=length-1;i>0;i--){sx[i]=sx[i-1];sy[i]=sy[i-1];}sx[0]=x;sy[0]=y;if(length>=160)ended=true;else if(eat)food();}dirty=true;}
if(page>=5&&page<=7&&now-lastStep>=20){lastStep+=20;dirty=true;
if(page==5||page==6){ballX+=vx;ballY+=vy;if(ballY<5){ballY=5;vy=fabs(vy);}if(page==5){enemy+=constrain(int(ballY)-enemy,-2,2);if(ballY>160){ballY=160;vy=-fabs(vy);}if(ballX<18&&ballX>3&&vx<0&&abs(ballY-enemy)<27){vx=fabs(vx);ballX=18;}if(ballX>301&&ballX<316&&vx>0&&abs(ballY-paddle)<27){vx=-fabs(vx)-0.08;ballX=301;score++;}if(ballX<0){score+=3;ballX=160;vx=2.8;}if(ballX>320)ended=true;}
else{if(ballX<5){ballX=5;vx=fabs(vx);}if(ballX>315){ballX=315;vx=-fabs(vx);}if(vy>0&&ballY>=158&&ballY<173&&abs(ballX-paddle)<29){vy=-fabs(vy);vx=(ballX-paddle)/10.0;ballY=158;}for(int i=0;i<24;i++)if(bricks[i]&&ballX>=4+(i%8)*39-3&&ballX<=39+(i%8)*39+3&&ballY>=8+(i/8)*15-4&&ballY<=19+(i/8)*15+4){bricks[i]=false;vy=-vy;score++;break;}if(ballY>177||score==24)ended=true;}}
if(page==7)for(int i=0;i<5;i++){obstacleY[i]+=2+min(score/10,4);if(obstacleY[i]>174){obstacleY[i]=-random(30,90);obstacleX[i]=random(300);score++;}if(obstacleY[i]+16>150&&obstacleY[i]<166&&obstacleX[i]<paddle+10&&obstacleX[i]+20>paddle-10)ended=true;}}
}
void setup(){Serial.begin(115200);pinMode(21,OUTPUT);digitalWrite(21,LOW);lcd.init();lcd.setRotation(1);lcd.fillScreen(bg);digitalWrite(21,HIGH);SPI.begin(25,39,32,33);touch.begin();touch.setRotation(1);prefs.begin("cydgames",false);randomSeed(esp_random());canvas.setColorDepth(16);buffered=canvas.createSprite(320,204)!=nullptr;if(!prefs.getBool("cal",false))calibrate();else{ax=prefs.getFloat("ax");bx=prefs.getFloat("bx");cx=prefs.getFloat("cx");ay=prefs.getFloat("ay");by=prefs.getFloat("by");cy=prefs.getFloat("cy");menu();}}
void loop(){uint32_t now=millis();bool down=touch.touched();static uint32_t holdStart=0;if(down){auto p=touch.getPoint();int x=constrain(int(ax*p.x+bx*p.y+cx),0,319),y=constrain(int(ay*p.x+by*p.y+cy),0,239);if(!held){held=true;holdStart=now;tap(x,y);}if(!ended&&y>=36){if(page==5){int n=constrain(y-36,23,143);if(paddle!=n){paddle=n;dirty=true;}}if(page==6||page==7){int n=constrain(x,page==6?25:10,page==6?295:310);if(n!=paddle){paddle=n;dirty=true;}}}if(page==0&&y>=214&&now-holdStart>3000)calibrate();}else held=false;
if(page==2&&second>=0&&int32_t(now-deadline)>=0){if(cards[first]==cards[second]){matched[first]=matched[second]=true;pairs++;}first=second=-1;drawMemory();}
if(page==3&&!ended&&!ready&&int32_t(now-deadline)>=0){ready=true;button("TOUCHE !",15,65,290,110,pink);started=millis();}
// Fixed 20 ms simulation steps: rendering time does not slow the game.
if(page>=5&&page<=7){
  if(now-lastStep>100)lastStep=now-100;
  for(int n=0;n<5&&now-lastStep>=20&&!ended;n++)tick(now);
}else tick(now);
if(page>=4&&dirty&&now-lastFrame>=16){lastFrame=now;render();}delay(1);}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik