
How it works
This is a complete game of Pong written in MicroPython, running on the official MicroPython v1.28.0 image on an ESP32-C6. The 1.8-inch ST7735 TFT is 160 × 128 pixels, and the script drives it over hardware SPI at 20 MHz: SPI(1) with SCK on GPIO6 and MOSI on GPIO7, CS on GPIO5, DC on GPIO3 and RST on GPIO4. The joystick's vertical axis goes to the ADC on GPIO1, and its push button goes to GPIO0 with a 10 kΩ pull-up to 3V3.
The script clears the screen once and from then on redraws only what changes. Paddles repaint the rows they gain or lose, and the ball updates its old and new areas together when they overlap. The script repairs any net dashes it paints over. A typical frame sends a few hundred bytes. You steer the left paddle, at up to 120 pixels per second; the right paddle is the computer, which tracks the ball more slowly when the ball moves away. The ball serves at 70 pixels per second horizontally, with a separate vertical speed. Paddle hits raise its horizontal speed up to 145 pixels per second and change its angle according to where it strikes. The first player to 5 points wins.
What to watch
The screen shows PRESS BUTTON, and Serial prints PONG ready. Press the joystick cap to serve; Serial prints serve!, a score line after each point, and the winner at the end. Press the button again for a rematch.
What's on the bench
- ESP32-C6
- Joystick
- Resistor
- TFT 1.8″ 160×128 (ST7735)
How it's wired
- ESP32-C6 · 3v3→TFT 1.8″ 160×128 (ST7735) · vcc
- ESP32-C6 · gnd2→TFT 1.8″ 160×128 (ST7735) · gnd
- ESP32-C6 · g7→TFT 1.8″ 160×128 (ST7735) · din
- ESP32-C6 · g6→TFT 1.8″ 160×128 (ST7735) · clk
- ESP32-C6 · g5→TFT 1.8″ 160×128 (ST7735) · cs
- ESP32-C6 · g3→TFT 1.8″ 160×128 (ST7735) · dc
- ESP32-C6 · g4→TFT 1.8″ 160×128 (ST7735) · rst
- ESP32-C6 · 3v3→Joystick · vcc
- ESP32-C6 · gnd2→Joystick · gnd
- ESP32-C6 · g1→Joystick · vry
- ESP32-C6 · g0→Joystick · sw
- ESP32-C6 · 3v3→Resistor · a
- Resistor · b→Joystick · sw
The code
This MicroPython script runs on the emulated board every boot; edit it in Code.
# PONG - move the joystick to steer the left paddle; first to 5 wins. # Runs on the C6 hardware SPI (sck=6, mosi=7 - the SPI(1) defaults), # so the full 40 KB framebuffer streams at bus speed. Rendering is # still dirty-rect - paddles repaint only the rows they gained and # lost (the body itself never blinks), the ball draws its new cell # before erasing the old one, points erase a couple of rects instead # of clearing the screen, and only net dashes actually painted over # get redrawn - a typical frame is a few hundred bytes of SPI, the # way a tight game loop treats any serial panel. from machine import Pin, SPI, ADC import framebuf, time W, H = 160, 128 TOP = 14 # scoreboard strip; playfield is below spi = SPI(1, baudrate=20000000) dc = Pin(3, Pin.OUT); cs = Pin(5, Pin.OUT, value=1); rst = Pin(4, Pin.OUT) joy = ADC(Pin(1)); joy.atten(ADC.ATTN_11DB) btn = Pin(0, Pin.IN) def wr(c, d=None): cs(0); dc(0); spi.write(bytes([c])) if d is not None: dc(1); spi.write(bytes(d)) cs(1) rst(0); time.sleep_ms(20); rst(1); time.sleep_ms(50) wr(0x01); time.sleep_ms(120) # SWRESET wr(0x11); time.sleep_ms(120) # SLPOUT wr(0x3A, [0x05]) # COLMOD 16-bit RGB565 wr(0x36, [0x00]) # MADCTL: 160x128 window wr(0x29) # DISPON def rgb(r, g, b): # byte-swapped RGB565 (the panel wire is big-endian, framebuf little). v = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3) return ((v & 0xFF) << 8) | (v >> 8) INK = rgb(240, 240, 240) DIM = rgb(90, 100, 120) BALL_C = rgb(120, 255, 150) def blit(x, y, w, h, buf): wr(0x2A, [0, x, 0, x + w - 1]) wr(0x2B, [0, y, 0, y + h - 1]) cs(0); dc(0); spi.write(bytes([0x2C])); dc(1); spi.write(buf); cs(1) def solid(w, h, color): b = bytearray(w * h * 2) if color: hi = color & 0xFF; lo = color >> 8 for i in range(0, len(b), 2): b[i] = hi; b[i + 1] = lo return b PW, PH, BS = 4, 24, 5 ball_spr = solid(BS, BS, BALL_C) ball_gap = solid(BS, BS, 0) pad_white = solid(PW, PH, INK) pad_black = solid(PW, PH, 0) dash_spr = solid(2, 5, DIM) BALU = bytearray(9 * 9 * 2) # ball union scratch (overlapping moves) sb = bytearray(64 * 10 * 2) sfb = framebuf.FrameBuffer(sb, 64, 10, framebuf.RGB565) def net(): for y in range(TOP + 2, H - 6, 14): blit(79, y, 2, 5, dash_spr) def net_repair(x, y, w, h): # Redraw only the dashes a repaint actually painted over. if x + w <= 79 or x >= 81: return for dy in range(TOP + 2, H - 6, 14): if dy < y + h and dy + 5 > y: blit(79, dy, 2, 5, dash_spr) def pad_repair(x, y, w, h): # Same idea for the paddles: the ball rect can overlap a paddle at # impact, so its black erase would bite a hole in the body. Re-blit # the rows the repaint touched (whole-width - the sprite is solid). for px, ptop in ((6, py), (150, cy)): if x + w <= px or x >= px + PW: continue y0 = y if y > ptop else ptop y1 = min(y + h, ptop + PH) if y0 < y1: blit(px, y0, PW, y1 - y0, memoryview(pad_white)[: PW * (y1 - y0) * 2]) def score_draw(a, b): sfb.fill(0) sfb.text('%d - %d' % (a, b), 8, 1, INK) blit(48, 2, 64, 10, sb) last_msg = None def msg(text, color): global last_msg mw = len(text) * 8 + 4 mb = bytearray(mw * 12 * 2) mfb = framebuf.FrameBuffer(mb, mw, 12, framebuf.RGB565) mfb.fill(0) mfb.text(text, 2, 2, color) blit((W - mw) // 2, 60, mw, 12, mb) last_msg = ((W - mw) // 2, 60, mw, 12) def msg_clear(): global last_msg if last_msg: x, y, mw, mh = last_msg blit(x, y, mw, mh, solid(mw, mh, 0)) net_repair(x, y, mw, mh) pad_repair(x, y, mw, mh) last_msg = None def pad_move(x, old, new): # Repaint only the delta rows: the paddle body never leaves the # screen, so this cannot flicker, and a small move costs ~100 bytes. d = new - old if d == 0: return if d >= PH or -d >= PH: blit(x, new, PW, PH, pad_white) blit(x, old, PW, PH, pad_black) return if d > 0: blit(x, old + PH, PW, d, memoryview(pad_white)[: PW * d * 2]) blit(x, old, PW, d, memoryview(pad_black)[: PW * d * 2]) else: blit(x, new, PW, -d, memoryview(pad_white)[: PW * -d * 2]) blit(x, new + PH, PW, -d, memoryview(pad_black)[: PW * -d * 2]) def ball_move(ox, oy, nx, ny): if ox >= 0 and abs(nx - ox) < BS and abs(ny - oy) < BS: # Overlapping cells: one atomic union blit. x0 = min(ox, nx); y0 = min(oy, ny) w = abs(nx - ox) + BS; h = abs(ny - oy) + BS mv = memoryview(BALU)[: w * h * 2] fbb = framebuf.FrameBuffer(mv, w, h, framebuf.RGB565) fbb.fill(0) fbb.fill_rect(nx - x0, ny - y0, BS, BS, BALL_C) blit(x0, y0, w, h, mv) net_repair(x0, y0, w, h) pad_repair(x0, y0, w, h) return blit(nx, ny, BS, BS, ball_spr) # draw first: the ball never vanishes if ox >= 0: blit(ox, oy, BS, BS, ball_gap) net_repair(ox, oy, BS, BS) pad_repair(ox, oy, BS, BS) def ball_erase(ox, oy): if ox >= 0: blit(ox, oy, BS, BS, ball_gap) net_repair(ox, oy, BS, BS) pad_repair(ox, oy, BS, BS) # --- game state: positions in px, ball fixed-point x16, speeds in px/s --- py = cy = (TOP + H - PH) // 2 score_p = score_c = 0 serves = 0 WIN_AT = 5 def serve(direction): global bx, by, vx, vy, speed, serves serves += 1 bx = (W // 2 - BS // 2) * 16 by = ((TOP + H) // 2) * 16 speed = 70 vx = speed * direction vy = ((serves * 23) % 91) - 45 # px/s, varies rally to rally if -10 < vy < 10: vy = 20 print('PONG ready - joystick = paddle, press its button to serve') # The ONE full-frame stream: real panels power up with random RAM, so a # real driver always clears once. Everything after this is dirty rects. row = solid(W, 8, 0) for y in range(0, H, 8): blit(0, y, W, 8, row) net() score_draw(0, 0) msg('PRESS BUTTON', INK) while btn.value() == 1: time.sleep_ms(30) msg_clear() serve(1) state = 'play' print('serve!') opy, ocy = py, cy obx, oby = -1, -1 pad_move(6, py - PH, py) pad_move(150, cy - PH, cy) last = time.ticks_ms() while True: if state == 'over': if btn.value() == 0: score_p = score_c = 0 msg_clear() ball_erase(obx, oby) obx = oby = -1 score_draw(0, 0) serve(1) state = 'play' last = time.ticks_ms() print('rematch!') time.sleep_ms(50) continue now = time.ticks_ms() dt = time.ticks_diff(now, last) last = now if dt > 250: dt = 250 # joystick Y -> paddle target, rate-limited to 120 px/s jv = joy.read_u16() target = TOP + ((65535 - jv) * (H - TOP - PH)) // 65535 lim = 120 * dt // 1000 dpy = target - py if dpy > lim: dpy = lim if dpy < -lim: dpy = -lim py += dpy # CPU: capped tracking - slower when the ball is moving away want = by // 16 - PH // 2 if want < TOP: want = TOP if want > H - PH: want = H - PH climit = (60 if vx > 0 else 45) * dt // 1000 dcy = want - cy if dcy > climit: dcy = climit if dcy < -climit: dcy = -climit cy += dcy # ball physics (px/s scaled by dt) bx += vx * dt * 16 // 1000 by += vy * dt * 16 // 1000 bpx = bx // 16; bpy = by // 16 # clamp the drawn cell too: a blit rect hanging past the panel edge # gets its window clamped and the pixel counter wraps, smearing ball # pixels where the later erase cannot reach them if bpy <= TOP: by = TOP * 16; bpy = TOP; vy = -vy if bpy >= H - BS: by = (H - BS) * 16; bpy = H - BS; vy = -vy if vx < 0 and 6 <= bpx <= 6 + PW and bpy + BS >= py and bpy <= py + PH: vy += (bpy + BS // 2 - (py + PH // 2)) * 3 # deflect off the face speed = speed + 15 if speed < 140 else speed vx = speed if vx > 0 and 150 - BS <= bpx <= 150 + PW and bpy + BS >= cy and bpy <= cy + PH: vy += (bpy + BS // 2 - (cy + PH // 2)) * 3 speed = speed + 15 if speed < 140 else speed vx = -speed if vy > 100: vy = 100 if vy < -100: vy = -100 scored = 0 if bpx < 0: score_c += 1; scored = -1 elif bpx > W - BS: score_p += 1; scored = 1 if scored: print('score YOU %d - %d CPU' % (score_p, score_c)) score_draw(score_p, score_c) # No screen clear between points - just take the dead ball off. ball_erase(obx, oby) obx = oby = -1 if score_p >= WIN_AT or score_c >= WIN_AT: won = score_p >= WIN_AT msg('YOU WIN!' if won else 'CPU WINS', BALL_C if won else rgb(255, 120, 120)) print('game over -', 'you win!' if won else 'cpu wins', '- press the button for a rematch') state = 'over' continue serve(-scored) time.sleep_ms(250) last = time.ticks_ms() continue # dirty-rect repaints if opy != py: pad_move(6, opy, py) opy = py if ocy != cy: pad_move(150, ocy, cy) ocy = cy if obx != bpx or oby != bpy: ball_move(obx, oby, bpx, bpy) obx, oby = bpx, bpy time.sleep_ms(15)
Try this
- With Select active, drag the joystick cap up and down to move your paddle, or select the joystick, click the canvas and use the arrow keys or W and S, with Space for the button.
- In Code, change WIN_AT = 5 to WIN_AT = 3 and press Run on board for a shorter game.
- In Code, lower the 60 in the computer's tracking line (climit) to make the computer easier to beat, then press Run on board.



