Firefly Flap · Round 1.28" touch watch
Game 1 - Firefly Flap (original tap-to-fly game) Tap to flap. Guide the glowing firefly through the gaps in the bamboo. Teaches: a game loop in an lv.timer, gravity, collision, game states, high score file.
You’ll learn: tap to fly, gravity, scrolling gaps
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 1 - Firefly Flap (original tap-to-fly game)
# Tap to flap. Guide the glowing firefly through the gaps in the bamboo.
# Teaches: a game loop in an lv.timer, gravity, collision, game states, high score file.
import math
import random
import lvgl as lv
import display
display.init() # start the display and touch (does nothing if already on)
TEXT = 0xE3EFE6
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(0x061A24), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
GRAVITY = 0.42
FLAP = -5.6
SPEED = 2.4
GAP = 78
PIPE_W = 30
HS_FILE = "/firefly_hs.txt"
# --- scenery: night sky gradient + moon -------------------------------------
scr.set_style_bg_grad_color(lv.color_hex(0x0E3B2C), 0)
scr.set_style_bg_grad_dir(lv.GRAD_DIR.VER, 0)
moon = dot(scr, 26, 0xF5F1D0, 50, -70)
moon.set_style_shadow_width(25, 0)
moon.set_style_shadow_color(lv.color_hex(0xF5F1D0), 0)
moon.set_style_shadow_opa(lv.OPA._50, 0)
def bamboo():
o = lv.obj(scr)
o.remove_style_all()
o.set_size(PIPE_W, 200)
o.set_style_bg_opa(lv.OPA.COVER, 0)
o.set_style_bg_color(lv.color_hex(0x3FA34D), 0)
o.set_style_bg_grad_color(lv.color_hex(0x1F6B2E), 0)
o.set_style_bg_grad_dir(lv.GRAD_DIR.HOR, 0)
o.set_style_radius(6, 0)
o.set_style_border_color(lv.color_hex(0x9BE36D), 0)
o.set_style_border_width(2, 0)
o.remove_flag(lv.obj.FLAG.CLICKABLE)
return o
pipes = [{"top": bamboo(), "bot": bamboo(), "x": 0.0, "gap_y": 0, "scored": False} for _ in range(2)]
# --- the firefly -----------------------------------------------------------------
fly = dot(scr, 16, 0xFFF36B, -50, 0)
fly.set_style_shadow_width(22, 0)
fly.set_style_shadow_color(lv.color_hex(0xFFE600), 0)
fly.set_style_shadow_spread(2, 0)
wing = dot(scr, 9, 0xD8FFF5, -54, -7)
wing.set_style_opa(180, 0)
score_lbl = label(scr, "0", 0xFFFFFF, 16, y=-92, zoom=1.6)
msg = label(scr, "", 0xFFFFFF, 14, y=40)
msg.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
try:
with open(HS_FILE) as f:
high = int(f.read())
except (OSError, ValueError):
high = 0
g = {"state": "ready", "y": 0.0, "vy": 0.0, "score": 0, "t": 0}
def place(p, x):
p["x"] = x
p["gap_y"] = random.randint(-45, 45) # lv.rand() takes unsigned numbers only
p["scored"] = False
def reset():
g.update(state="ready", y=0.0, vy=0.0, score=0, t=0)
place(pipes[0], 140)
place(pipes[1], 140 + 135)
score_lbl.set_text("0")
msg.set_text("Firefly Flap\ntap to start\nbest %d" % high)
draw()
def draw():
fly.align(lv.ALIGN.CENTER, -50, int(g["y"]))
flap_up = (g["t"] // 4) % 2 == 0 and g["state"] != "over"
wing.align(lv.ALIGN.CENTER, -54, int(g["y"]) - (9 if flap_up else 5))
for p in pipes:
x = int(p["x"])
gy = p["gap_y"]
p["top"].set_pos(120 + x - PIPE_W // 2, gy - GAP // 2 - 200 + 120)
p["bot"].set_pos(120 + x - PIPE_W // 2, gy + GAP // 2 + 120)
def hits(p):
if abs(p["x"] - (-50)) > PIPE_W // 2 + 7:
return False
return not (p["gap_y"] - GAP // 2 + 7 < g["y"] < p["gap_y"] + GAP // 2 - 7)
def game_over():
global high
g["state"] = "over"
fly.set_style_bg_color(lv.color_hex(0xFF6B6B), 0)
if g["score"] > high:
high = g["score"]
try:
with open(HS_FILE, "w") as f:
f.write(str(high))
except OSError:
pass
msg.set_text("Game over %d\nbest %d\ntap to retry" % (g["score"], high))
def tick(_):
g["t"] += 1
if g["state"] == "ready":
g["y"] = math.sin(g["t"] / 8) * 6 # gentle hover
elif g["state"] == "play":
g["vy"] += GRAVITY
g["y"] += g["vy"]
for p in pipes:
p["x"] -= SPEED
if p["x"] < -135:
other = pipes[1] if p is pipes[0] else pipes[0]
place(p, other["x"] + 135)
if not p["scored"] and p["x"] < -50:
p["scored"] = True
g["score"] += 1
score_lbl.set_text(str(g["score"]))
if hits(p):
game_over()
# the round screen edge is the "ground" and "sky"
if (g["y"] * g["y"] + 50 * 50) ** 0.5 > 112:
game_over()
elif g["state"] == "over":
if g["y"] < 112:
g["vy"] += GRAVITY
g["y"] = min(112, g["y"] + g["vy"])
draw()
def tap(e):
if g["state"] == "ready":
g["state"] = "play"
msg.set_text("")
g["vy"] = FLAP
elif g["state"] == "play":
g["vy"] = FLAP
else:
fly.set_style_bg_color(lv.color_hex(0xFFF36B), 0)
reset()
scr.add_event_cb(tap, lv.EVENT.PRESSED, None)
reset()
tick_timer = lv.timer_create(tick, 30, None)
print("Firefly Flap: tap the watch to fly.")
Comments