Skip to content

Commit 3eb7575

Browse files
committed
python: Migrate snake to pygame
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent ddffb07 commit 3eb7575

File tree

3 files changed

+259
-116
lines changed

3 files changed

+259
-116
lines changed

Diff for: python/inputmodule/gui/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def run_gui(devices):
109109
# Games tab
110110
games_frame = ttk.LabelFrame(tab_games, text="Games", style="TLabelframe")
111111
games_frame.pack(fill="x", padx=10, pady=5)
112+
ttk.Button(games_frame, text="Snake", command=lambda: perform_action(devices, 'game_snake'), style="TButton").pack(side="left", padx=5, pady=5)
112113
ttk.Button(games_frame, text="Ledris", command=lambda: perform_action(devices, 'game_ledris'), style="TButton").pack(side="left", padx=5, pady=5)
113114

114115
# Countdown Timer
@@ -168,11 +169,14 @@ def run_gui(devices):
168169
for text, action in control_buttons.items():
169170
ttk.Button(device_control_frame, text=text, command=lambda a=action: perform_action(devices, a), style="TButton").pack(side="left", padx=5, pady=5)
170171

172+
snake.main_devices(devices)
173+
171174
root.mainloop()
172175

173176
def perform_action(devices, action):
174177
action_map = {
175-
"game_ledris": ledris.main_devices
178+
"game_snake": snake.main_devices,
179+
"game_ledris": ledris.main_devices,
176180
}
177181
if action in action_map:
178182
threading.Thread(target=action_map[action], args=(devices,), daemon=True).start(),

Diff for: python/inputmodule/gui/games/__init__.py

-115
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,6 @@
2626
ARG_2LEFT = 5
2727
ARG_2RIGHT = 6
2828

29-
# Variables
30-
direction = None
31-
body = []
32-
33-
34-
def opposite_direction(direction):
35-
if direction == keys.RIGHT:
36-
return keys.LEFT
37-
elif direction == keys.LEFT:
38-
return keys.RIGHT
39-
elif direction == keys.UP:
40-
return keys.DOWN
41-
elif direction == keys.DOWN:
42-
return keys.UP
43-
return direction
44-
45-
46-
def snake_keyscan():
47-
global direction
48-
global body
49-
50-
while True:
51-
current_dir = direction
52-
key = getkey()
53-
if key in [keys.RIGHT, keys.UP, keys.LEFT, keys.DOWN]:
54-
# Don't allow accidental suicide if we have a body
55-
if key == opposite_direction(current_dir) and body:
56-
continue
57-
direction = key
58-
5929

6030
def snake_embedded_keyscan(dev):
6131
while True:
@@ -76,18 +46,6 @@ def snake_embedded_keyscan(dev):
7646
send_command(dev, CommandVals.GameControl, [key_arg])
7747

7848

79-
def game_over(dev):
80-
global body
81-
while True:
82-
show_string(dev, "GAME ")
83-
time.sleep(0.75)
84-
show_string(dev, "OVER!")
85-
time.sleep(0.75)
86-
score = len(body)
87-
show_string(dev, f"{score:>3} P")
88-
time.sleep(0.75)
89-
90-
9149
def pong_embedded(dev):
9250
# Start game
9351
send_command(dev, CommandVals.StartGame, [Game.Pong])
@@ -124,79 +82,6 @@ def snake_embedded(dev):
12482
snake_embedded_keyscan(dev)
12583

12684

127-
def snake(dev):
128-
global direction
129-
global body
130-
head = (0, 0)
131-
direction = keys.DOWN
132-
food = (0, 0)
133-
while food == head:
134-
food = (random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1))
135-
136-
# Setting
137-
WRAP = False
138-
139-
thread = threading.Thread(target=snake_keyscan, args=(), daemon=True)
140-
thread.start()
141-
142-
prev = datetime.now()
143-
while True:
144-
now = datetime.now()
145-
delta = (now - prev) / timedelta(milliseconds=1)
146-
147-
if delta > 200:
148-
prev = now
149-
else:
150-
continue
151-
152-
# Update position
153-
(x, y) = head
154-
oldhead = head
155-
if direction == keys.RIGHT:
156-
head = (x + 1, y)
157-
elif direction == keys.LEFT:
158-
head = (x - 1, y)
159-
elif direction == keys.UP:
160-
head = (x, y - 1)
161-
elif direction == keys.DOWN:
162-
head = (x, y + 1)
163-
164-
# Detect edge condition
165-
(x, y) = head
166-
if head in body:
167-
return game_over(dev)
168-
elif x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
169-
if WRAP:
170-
if x >= WIDTH:
171-
x = 0
172-
elif x < 0:
173-
x = WIDTH - 1
174-
elif y >= HEIGHT:
175-
y = 0
176-
elif y < 0:
177-
y = HEIGHT - 1
178-
head = (x, y)
179-
else:
180-
return game_over(dev)
181-
elif head == food:
182-
body.insert(0, oldhead)
183-
while food == head:
184-
food = (random.randint(0, WIDTH - 1),
185-
random.randint(0, HEIGHT - 1))
186-
elif body:
187-
body.pop()
188-
body.insert(0, oldhead)
189-
190-
# Draw on screen
191-
matrix = [[0 for _ in range(HEIGHT)] for _ in range(WIDTH)]
192-
matrix[x][y] = 1
193-
matrix[food[0]][food[1]] = 1
194-
for bodypart in body:
195-
(x, y) = bodypart
196-
matrix[x][y] = 1
197-
render_matrix(dev, matrix)
198-
199-
20085
def wpm_demo(dev):
20186
"""Capture keypresses and calculate the WPM of the last 10 seconds
20287
TODO: I'm not sure my calculation is right."""

0 commit comments

Comments
 (0)