Orbit Pong · Round 1.28" touch watch
Game 4 - Orbit Pong: your paddle slides around the rim of the round screen. Drag your finger around the edge (or tilt) to move it; keep the ball inside. Teaches: angles with atan2, reflecting a vector off a circle, difficulty ramp.
You’ll learn: paddle on the rim, reflection
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 4 - Orbit Pong: your paddle slides around the rim of the round screen.
# Drag your finger around the edge (or tilt) to move it; keep the ball inside.
# Teaches: angles with atan2, reflecting a vector off a circle, difficulty ramp.
import math
import lvgl as lv
import display
import bb_imu
display.init() # start the display and touch (does nothing if already on)
LINE = 0x24452F
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
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
def high_score(name, score=None):
"""Read (score=None) or raise the best score saved in a file on the board."""
path = "/hs_%s.txt" % name
try:
with open(path) as f:
best = int(f.read())
except (OSError, ValueError):
best = 0
if score is not None and score > best:
best = score
try:
with open(path, "w") as f:
f.write(str(best))
except OSError:
pass
return best
imu = bb_imu.IMU() # QMI8658 motion sensor, on the touch I2C bus
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(0x0B0716), 0)
scr.remove_flag(lv.obj.FLAG.SCROLLABLE)
R = 108 # rim radius where the paddle lives
PADDLE_DEG = 40
rim = ring(scr, 2 * R + 10, 2, 0x2A2050, 100)
paddle = lv.arc(scr)
paddle.set_size(2 * R + 10, 2 * R + 10)
paddle.center()
paddle.remove_style(None, lv.PART.KNOB)
paddle.remove_flag(lv.obj.FLAG.CLICKABLE)
paddle.set_bg_angles(0, 0)
paddle.set_style_arc_width(0, lv.PART.MAIN)
paddle.set_style_arc_width(10, lv.PART.INDICATOR)
paddle.set_style_arc_rounded(True, lv.PART.INDICATOR)
paddle.set_style_arc_color(lv.color_hex(0xFF4FA3), lv.PART.INDICATOR)
ball = dot(scr, 14, 0xFFFFFF)
ball.set_style_shadow_width(16, 0)
ball.set_style_shadow_color(lv.color_hex(0xFF4FA3), 0)
score_lbl = label(scr, "0", 0xFFFFFF, 16, zoom=2)
score_lbl.set_style_opa(90, 0)
msg = label(scr, "", 0xFFFFFF, 14, y=40)
g = {}
def reset(e=None):
a = math.radians(lv.rand(0, 359))
g.update(x=0.0, y=0.0, vx=math.cos(a) * 2.2, vy=math.sin(a) * 2.2, paddle=90.0, score=0, over=False)
score_lbl.set_text("0")
msg.set_text("")
def set_paddle(deg):
g["paddle"] = deg % 360
# lv.arc angles: 0 = 3 o'clock, clockwise
paddle.set_angles(int(g["paddle"] - PADDLE_DEG / 2) % 360, int(g["paddle"] + PADDLE_DEG / 2) % 360)
def follow_finger(e):
p = lv.point_t()
lv.indev_active().get_point(p)
set_paddle(math.degrees(math.atan2(p.y - 120, p.x - 120)))
def tick(_):
if g["over"]:
return
g["x"] += g["vx"]
g["y"] += g["vy"]
r = math.sqrt(g["x"] ** 2 + g["y"] ** 2)
if r > R - 12:
ang = math.degrees(math.atan2(g["y"], g["x"])) % 360
diff = (ang - g["paddle"] + 180) % 360 - 180
if abs(diff) <= PADDLE_DEG / 2 + 4:
nx, ny = g["x"] / r, g["y"] / r
dot = g["vx"] * nx + g["vy"] * ny
g["vx"] -= 2 * dot * nx
g["vy"] -= 2 * dot * ny
# add spin depending on where it hit, and speed up a little
a = math.atan2(g["vy"], g["vx"]) + math.radians(diff) * 0.5
sp = min(5.5, math.sqrt(g["vx"] ** 2 + g["vy"] ** 2) * 1.06)
g["vx"], g["vy"] = math.cos(a) * sp, math.sin(a) * sp
g["x"], g["y"] = nx * (R - 13), ny * (R - 13)
g["score"] += 1
score_lbl.set_text(str(g["score"]))
elif r > R + 10:
g["over"] = True
best = high_score("orbitpong", g["score"])
msg.set_text("missed! best %d\ntap to replay" % best)
ball.align(lv.ALIGN.CENTER, int(g["x"]), int(g["y"]))
def tilt_control(_):
if g.get("touching"):
return
ax, ay, _z = imu.accel()
if ax * ax + ay * ay > 0.04:
set_paddle(math.degrees(math.atan2(ay, -ax)))
scr.add_event_cb(follow_finger, lv.EVENT.PRESSING, None)
scr.add_event_cb(lambda e: g["over"] and reset(), lv.EVENT.CLICKED, None)
reset()
set_paddle(90)
tick_timer = lv.timer_create(tick, 25, None)
tilt_control_timer = lv.timer_create(tilt_control, 60, None)
print("Drag around the edge (or tilt) to move the paddle.")
Comments