Fireworks · Round 1.28" touch watch
Showcase 4 - Fireworks: tap anywhere to launch a burst of particles with gravity and fade
You’ll learn: particle pool, gravity, fade
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 4 - Fireworks: tap anywhere to launch a burst of particles with gravity and fade
import math
import random
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
TEXT = 0xE3EFE6
MUTED = 0x8EA999
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(0x02030A), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
COLORS = [0xFF6B6B, 0xF2B84B, 0x46E07C, 0x6CB8FF, 0xB593FF, 0xFF4FA3, 0xFFFFFF]
POOL = 48
particles = []
for i in range(POOL):
d = dot(scr, 5, 0xFFFFFF)
d.add_flag(lv.obj.FLAG.HIDDEN)
particles.append({"o": d, "x": 0.0, "y": 0.0, "vx": 0.0, "vy": 0.0, "life": 0})
hint = label(scr, "tap to launch", MUTED, 12, y=96)
next_i = [0]
def burst(cx, cy):
col = lv.color_hex(COLORS[lv.rand(0, len(COLORS) - 1)])
for k in range(16):
p = particles[next_i[0] % POOL]
next_i[0] += 1
a = k * math.pi / 8 + lv.rand(0, 30) / 100
sp = 2.5 + lv.rand(0, 25) / 10
p.update(x=cx, y=cy, vx=math.cos(a) * sp, vy=math.sin(a) * sp, life=40)
p["o"].set_style_bg_color(col, 0)
p["o"].remove_flag(lv.obj.FLAG.HIDDEN)
def frame(_):
for p in particles:
if p["life"] <= 0:
continue
p["life"] -= 1
p["vy"] += 0.12 # gravity
p["vx"] *= 0.97
p["x"] += p["vx"]
p["y"] += p["vy"]
o = p["o"]
o.align(lv.ALIGN.CENTER, int(p["x"]), int(p["y"]))
o.set_style_opa(p["life"] * 6, 0)
if p["life"] == 0:
o.add_flag(lv.obj.FLAG.HIDDEN)
def tap(e):
pt = lv.point_t()
lv.indev_active().get_point(pt)
burst(pt.x - 120, pt.y - 120)
hint.add_flag(lv.obj.FLAG.HIDDEN)
auto = [0]
def autoplay(_):
auto[0] += 1
burst(random.randint(-60, 60), random.randint(-60, 20)) # random.randint: works with negative numbers
scr.add_event_cb(tap, lv.EVENT.CLICKED, None)
frame_timer = lv.timer_create(frame, 33, None)
autoplay_timer = lv.timer_create(autoplay, 1400, None)
Comments