Starfield warp · Round 1.28" touch watch
Showcase 3 - Warp starfield: stars fly out of the centre and speed up. Tap = hyperdrive.
You’ll learn: particles, perspective, tap boost
Board: Waveshare ESP32-S3-Touch-LCD-1.28 (ESP32-S3R2). Part of the kit’s Showcase and benchmarks. Needs the board’s LVGL firmware: flash it from the notebook’s Flash button, then press Open in Full IDE.
# Showcase 3 - Warp starfield: stars fly out of the centre and speed up. Tap = hyperdrive.
import math
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
TEXT = 0xE3EFE6
BLUE = 0x6CB8FF
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 dot(parent, size, color, x=0, y=0, align=lv.ALIGN.CENTER):
"""A filled circle: a plain lv.obj with every theme style removed."""
o = lv.obj(parent)
o.remove_style_all()
size = max(1, int(size))
o.set_size(size, size)
o.set_style_radius(lv.RADIUS_CIRCLE, 0)
o.set_style_bg_color(lv.color_hex(color), 0)
o.set_style_bg_opa(lv.OPA.COVER, 0)
o.remove_flag(lv.obj.FLAG.CLICKABLE)
o.align(align, int(x), int(y))
return o
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(0x000000), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
N = 45
stars = []
for i in range(N):
d = dot(scr, 3, 0xFFFFFF)
stars.append([d, lv.rand(0, 359), lv.rand(1, 110) * 1.0, 0.02 + lv.rand(0, 30) / 1000])
boost = [1.0]
speed_lbl = label(scr, "", BLUE, 12, y=98)
def frame(_):
b = boost[0]
for s in stars:
d, ang, r, v = s
r += r * v * b + 0.4 * b
if r > 125:
r = lv.rand(1, 15)
s[1] = ang = lv.rand(0, 359)
s[2] = r
a = math.radians(ang)
d.align(lv.ALIGN.CENTER, int(math.cos(a) * r), int(math.sin(a) * r))
size = 1 + int(r / 30)
d.set_size(size, size)
d.set_style_opa(min(255, int(r * 3)), 0)
if b > 1:
boost[0] = max(1.0, b * 0.97)
speed_lbl.set_text("warp %.1f" % boost[0])
def hyper(e):
boost[0] = 6.0
scr.add_event_cb(hyper, lv.EVENT.CLICKED, None)
frame_timer = lv.timer_create(frame, 33, None)
print("Tap the screen for a hyperdrive burst.")
Comments