Reaction Tap · Round 1.28" touch watch
Game 6 - Reaction Tap: wait for green, then tap as fast as you can. Best of 5. Teaches: random delays, measuring milliseconds, simple statistics.
You’ll learn: timing, random delays
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Games. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Game 6 - Reaction Tap: wait for green, then tap as fast as you can. Best of 5.
# Teaches: random delays, measuring milliseconds, simple statistics.
import time
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
LINE = 0x24452F
TEXT = 0xE3EFE6
MUTED = 0x8EA999
ACCENT = 0x46E07C
FONTS = {12: lv.font_montserrat_12, 14: lv.font_montserrat_14, 16: lv.font_montserrat_16}
def label(parent, text="", color=TEXT, font=16, align=lv.ALIGN.CENTER, x=0, y=0, zoom=0):
"""lv.label with a colour and a Montserrat 12/14/16 font. zoom=2 draws it twice as big."""
lb = lv.label(parent)
lb.set_text(text)
lb.set_style_text_color(lv.color_hex(color), 0)
lb.set_style_text_font(FONTS.get(font, lv.font_montserrat_16), 0)
if zoom:
lb.set_style_transform_scale(int(256 * zoom), 0) # 256 = 100 %
lb.update_layout()
lb.set_style_transform_pivot_x(lb.get_width() // 2, 0)
lb.set_style_transform_pivot_y(lb.get_height() // 2, 0)
lb.align(align, int(x), int(y))
return lb
def ring(parent, size, width, color, value=100, bg_color=LINE):
"""An lv.arc used as a progress ring (no knob, not touchable)."""
a = lv.arc(parent)
a.set_size(size, size)
a.center()
a.set_rotation(270) # start at 12 o'clock
a.set_bg_angles(0, 360)
a.set_range(0, 100)
a.set_value(value)
a.remove_style(None, lv.PART.KNOB)
a.remove_flag(lv.obj.FLAG.CLICKABLE)
a.set_style_arc_width(width, lv.PART.MAIN)
a.set_style_arc_width(width, lv.PART.INDICATOR)
a.set_style_arc_color(lv.color_hex(bg_color), lv.PART.MAIN)
a.set_style_arc_color(lv.color_hex(color), lv.PART.INDICATOR)
return a
scr = lv.screen_active() # the screen LVGL is showing
scr.clean() # remove what the last program drew
scr.set_style_bg_color(lv.color_hex(0x1A0A0A), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
big = label(scr, "", 0xFFFFFF, 16, y=-10, zoom=2)
small = label(scr, "", MUTED, 12, y=40)
timer_ring = ring(scr, 220, 8, ACCENT, 0)
g = {"state": "intro", "go_at": 0, "results": [], "wait": 0}
def set_bg(col):
scr.set_style_bg_color(lv.color_hex(col), 0)
def intro():
g.update(state="intro", results=[])
set_bg(0x1A0A0A)
big.set_text("READY?")
small.set_text("tap to start - 5 rounds")
timer_ring.set_value(0)
def arm():
g["state"] = "wait"
g["wait"] = time.ticks_add(time.ticks_ms(), lv.rand(1200, 3500))
set_bg(0x3A0A0A)
big.set_text("wait...")
small.set_text("round %d of 5" % (len(g["results"]) + 1))
def tick(_):
if g["state"] == "wait" and time.ticks_diff(time.ticks_ms(), g["wait"]) >= 0:
g["state"] = "go"
g["go_at"] = time.ticks_ms()
set_bg(0x0B5A2A)
big.set_text("TAP!")
def tap(e):
s = g["state"]
if s in ("intro", "done"):
intro() if s == "done" else arm()
elif s == "wait":
g["state"] = "early"
set_bg(0x5A3A0A)
big.set_text("too soon")
small.set_text("tap to try again")
elif s == "early":
arm()
elif s == "go":
ms = time.ticks_diff(time.ticks_ms(), g["go_at"])
g["results"].append(ms)
timer_ring.set_value(len(g["results"]) * 20)
if len(g["results"]) == 5:
avg = sum(g["results"]) // 5
best = min(g["results"])
g["state"] = "done"
set_bg(0x0A1A2A)
big.set_text("%d ms" % avg)
small.set_text("average - best %d ms\ntap to restart" % best)
else:
g["state"] = "shown"
big.set_text("%d ms" % ms)
small.set_text("tap for next round")
elif s == "shown":
arm()
scr.add_event_cb(tap, lv.EVENT.PRESSED, None)
intro()
tick_timer = lv.timer_create(tick, 10, None)
print("Wait for green, then tap fast.")
Comments