Skip to content

Commit

Permalink
python: Migrate snake to pygame
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <dhs@frame.work>
  • Loading branch information
JohnAZoidberg committed Nov 22, 2024
1 parent ddffb07 commit 3eb7575
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 116 deletions.
6 changes: 5 additions & 1 deletion python/inputmodule/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def run_gui(devices):
# Games tab
games_frame = ttk.LabelFrame(tab_games, text="Games", style="TLabelframe")
games_frame.pack(fill="x", padx=10, pady=5)
ttk.Button(games_frame, text="Snake", command=lambda: perform_action(devices, 'game_snake'), style="TButton").pack(side="left", padx=5, pady=5)
ttk.Button(games_frame, text="Ledris", command=lambda: perform_action(devices, 'game_ledris'), style="TButton").pack(side="left", padx=5, pady=5)

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

snake.main_devices(devices)

root.mainloop()

def perform_action(devices, action):
action_map = {
"game_ledris": ledris.main_devices
"game_snake": snake.main_devices,
"game_ledris": ledris.main_devices,
}
if action in action_map:
threading.Thread(target=action_map[action], args=(devices,), daemon=True).start(),
Expand Down
115 changes: 0 additions & 115 deletions python/inputmodule/gui/games/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,6 @@
ARG_2LEFT = 5
ARG_2RIGHT = 6

# Variables
direction = None
body = []


def opposite_direction(direction):
if direction == keys.RIGHT:
return keys.LEFT
elif direction == keys.LEFT:
return keys.RIGHT
elif direction == keys.UP:
return keys.DOWN
elif direction == keys.DOWN:
return keys.UP
return direction


def snake_keyscan():
global direction
global body

while True:
current_dir = direction
key = getkey()
if key in [keys.RIGHT, keys.UP, keys.LEFT, keys.DOWN]:
# Don't allow accidental suicide if we have a body
if key == opposite_direction(current_dir) and body:
continue
direction = key


def snake_embedded_keyscan(dev):
while True:
Expand All @@ -76,18 +46,6 @@ def snake_embedded_keyscan(dev):
send_command(dev, CommandVals.GameControl, [key_arg])


def game_over(dev):
global body
while True:
show_string(dev, "GAME ")
time.sleep(0.75)
show_string(dev, "OVER!")
time.sleep(0.75)
score = len(body)
show_string(dev, f"{score:>3} P")
time.sleep(0.75)


def pong_embedded(dev):
# Start game
send_command(dev, CommandVals.StartGame, [Game.Pong])
Expand Down Expand Up @@ -124,79 +82,6 @@ def snake_embedded(dev):
snake_embedded_keyscan(dev)


def snake(dev):
global direction
global body
head = (0, 0)
direction = keys.DOWN
food = (0, 0)
while food == head:
food = (random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1))

# Setting
WRAP = False

thread = threading.Thread(target=snake_keyscan, args=(), daemon=True)
thread.start()

prev = datetime.now()
while True:
now = datetime.now()
delta = (now - prev) / timedelta(milliseconds=1)

if delta > 200:
prev = now
else:
continue

# Update position
(x, y) = head
oldhead = head
if direction == keys.RIGHT:
head = (x + 1, y)
elif direction == keys.LEFT:
head = (x - 1, y)
elif direction == keys.UP:
head = (x, y - 1)
elif direction == keys.DOWN:
head = (x, y + 1)

# Detect edge condition
(x, y) = head
if head in body:
return game_over(dev)
elif x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
if WRAP:
if x >= WIDTH:
x = 0
elif x < 0:
x = WIDTH - 1
elif y >= HEIGHT:
y = 0
elif y < 0:
y = HEIGHT - 1
head = (x, y)
else:
return game_over(dev)
elif head == food:
body.insert(0, oldhead)
while food == head:
food = (random.randint(0, WIDTH - 1),
random.randint(0, HEIGHT - 1))
elif body:
body.pop()
body.insert(0, oldhead)

# Draw on screen
matrix = [[0 for _ in range(HEIGHT)] for _ in range(WIDTH)]
matrix[x][y] = 1
matrix[food[0]][food[1]] = 1
for bodypart in body:
(x, y) = bodypart
matrix[x][y] = 1
render_matrix(dev, matrix)


def wpm_demo(dev):
"""Capture keypresses and calculate the WPM of the last 10 seconds
TODO: I'm not sure my calculation is right."""
Expand Down
Loading

0 comments on commit 3eb7575

Please sign in to comment.