Skip to content

Commit 7ad9a2f

Browse files
committed
examples/main.py: More pedantic tidyup.
1 parent 117feed commit 7ad9a2f

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

examples/main.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
psram.mkramfs()
1212

1313
try:
14-
os.stat("/ramfs/launch.txt")[0] & 0x4000 == 0
15-
f = open("/ramfs/launch.txt", "r")
16-
result = f.readline()
17-
f.close()
14+
with open("/ramfs/launch.txt", "r") as f:
15+
result = f.readline()
1816
except OSError:
19-
result = False
17+
result = ""
2018

21-
if result and result.endswith(".py"):
19+
if result.endswith(".py"):
2220
os.remove("/ramfs/launch.txt")
2321
__import__(result[:-3])
2422

@@ -29,9 +27,11 @@
2927
presto = Presto(ambient_light=False)
3028

3129
display = presto.display
30+
3231
WIDTH, HEIGHT = display.get_bounds()
33-
CY = HEIGHT // 2
34-
CX = WIDTH // 2
32+
33+
CENTER_Y = HEIGHT // 2
34+
CENTER_X = WIDTH // 2
3535

3636
OFFSET_X = 0
3737
OFFSET_Y = -30
@@ -40,7 +40,6 @@
4040
RADIUS_Y = 30
4141

4242
# Couple of colours for use later
43-
WHITE = display.create_pen(255, 255, 255)
4443
BLACK = display.create_pen(0, 0, 0)
4544
BACKGROUND = display.create_pen(255, 250, 240)
4645

@@ -56,6 +55,20 @@
5655
vector.set_font_align(HALIGN_CENTER)
5756
t = Transform()
5857

58+
# Touch tracking and menu movement
59+
touch_start_x = 0
60+
touch_start_time = None
61+
tap = False
62+
63+
move_angle = 0
64+
move = 0
65+
friction = 0.98
66+
67+
# Rounded corners
68+
rounded_corners = Polygon()
69+
rounded_corners.rectangle(0, 0, WIDTH, HEIGHT)
70+
rounded_corners.rectangle(0, 0, WIDTH, HEIGHT, (10, 10, 10, 10))
71+
5972

6073
class Application:
6174
DEFAULT_ICON = [
@@ -129,6 +142,7 @@ def __init__(self, w, h, file):
129142

130143
if line.startswith("# NAME "):
131144
self.name = line[7:]
145+
132146
if line.startswith("# DESC "):
133147
self.description = line[7:]
134148

@@ -137,12 +151,10 @@ def __init__(self, w, h, file):
137151
self.file = file
138152

139153
# Background
140-
self.i = Polygon()
141-
self.i.rectangle(0 - w / 2, 0 - h / 2, w, h, (10, 10, 10, 10))
154+
self.bg = Polygon().rectangle(0 - w / 2, 0 - h / 2, w, h, (10, 10, 10, 10))
142155

143156
# Outline
144-
self.ol = Polygon()
145-
self.ol.rectangle(0 - w / 2, 0 - h / 2, w, h, (10, 10, 10, 10), stroke=2)
157+
self.ol = Polygon().rectangle(0 - w / 2, 0 - h / 2, w, h, (10, 10, 10, 10), stroke=2)
146158

147159
# Transform
148160
self.t = Transform()
@@ -193,14 +205,14 @@ def update(self, move_angle):
193205
# Translate to our final display offset
194206
self.t.translate(OFFSET_X, OFFSET_Y)
195207
# Translate back to screen space, moving origin 0, 0 to our X and Y
196-
self.t.translate(CX + self.x, CY + self.y)
208+
self.t.translate(CENTER_X + self.x, CENTER_Y + self.y)
197209
# Scale the icon around origin 0, 0
198210
self.t.scale(self.scale, self.scale)
199211

200212
def draw(self, selected=False):
201213
display.set_pen(self.color_bg)
202214
vector.set_transform(self.t)
203-
vector.draw(self.i)
215+
vector.draw(self.bg)
204216
display.set_pen(self.color_fg)
205217
self.t.translate(0, 2)
206218
vector.draw(self.icon)
@@ -231,8 +243,8 @@ def bounds(self):
231243
y = -h // 2
232244

233245
return (
234-
int(x + self.x + CX + OFFSET_X),
235-
int(y + self.y + CY + OFFSET_Y),
246+
int(x + self.x + CENTER_X + OFFSET_X),
247+
int(y + self.y + CENTER_Y + OFFSET_Y),
236248
int(w),
237249
int(h),
238250
)
@@ -254,19 +266,6 @@ def launch(self):
254266
Application(60, 60, file) for file in os.listdir()
255267
if file.endswith(".py") and file not in ("main.py", "secrets.py")]
256268

257-
touch_start_x = 0
258-
touch_start_time = None
259-
tap = False
260-
261-
move_angle = 0
262-
move = 0
263-
friction = 0.98
264-
265-
# Rounded corners
266-
bg = Polygon()
267-
bg.rectangle(0, 0, WIDTH, HEIGHT)
268-
bg.rectangle(0, 0, WIDTH, HEIGHT, (10, 10, 10, 10))
269-
270269
# Take a local reference to touch for a tiny performance boost
271270
touch = presto.touch
272271

@@ -280,7 +279,7 @@ def launch(self):
280279

281280
# Draw rounded corners in black
282281
display.set_pen(BLACK)
283-
vector.draw(bg)
282+
vector.draw(rounded_corners)
284283

285284
touch.poll()
286285

0 commit comments

Comments
 (0)