Skip to content

Commit

Permalink
Update snakegame to pass pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
masenf committed Nov 11, 2024
1 parent cd12801 commit f1641a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion snakegame/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
reflex>=0.6.0
reflex>=0.6.5a1
25 changes: 18 additions & 7 deletions snakegame/snakegame/snakegame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from typing import Dict

import reflex as rx
from reflex.event import EventType, key_event
from reflex.constants.colors import Color
from reflex.event import EventSpec
from reflex.utils.imports import ImportDict

N = 19 # There is a N*N grid for ground of snake
GRID_EMPTY = 0
Expand Down Expand Up @@ -43,7 +45,7 @@ class Colors(rx.State):
# Why is this not just a global? Because we index into the dict with state
# vars in an rx.foreach, so this dict needs to be accessible in the compiled
# frontend.
c: dict[int, str] = {
c: dict[int, Color] = {
GRID_EMPTY: rx.color("gray", 5),
GRID_SNAKE: rx.color("grass", 9),
GRID_FOOD: rx.color("blue", 9),
Expand All @@ -65,6 +67,7 @@ class State(rx.State):
running: bool = False
_n_tasks: int = 0

@rx.event
def play(self):
"""Start / resume the game."""
if not self.running:
Expand All @@ -74,10 +77,12 @@ def play(self):
self.running = True
return State.loop

@rx.event
def pause(self):
"""Signal the game to pause."""
self.running = False

@rx.event
def flip_switch(self, start):
"""Toggle whether the game is running or paused."""
if start:
Expand All @@ -93,7 +98,7 @@ def _last_move(self):
"""Returns the last queued direction the snake head should move in."""
return self.moves[-1] if self.moves else self.dir

@rx.background
@rx.event(background=True)
async def loop(self):
"""The main game loop, implemented as a singleton background task.
Expand Down Expand Up @@ -147,26 +152,31 @@ async def loop(self):
# Decrement task counter, since we're about to return
self._n_tasks -= 1

@rx.event
def arrow_up(self):
"""Queue a move up."""
if self._last_move() != HEAD_D:
self.moves.append(HEAD_U)

@rx.event
def arrow_left(self):
"""Queue a move left."""
if self._last_move() != HEAD_R:
self.moves.append(HEAD_L)

@rx.event
def arrow_right(self):
"""Queue a move right."""
if self._last_move() != HEAD_L:
self.moves.append(HEAD_R)

@rx.event
def arrow_down(self):
"""Queue a move down."""
if self._last_move() != HEAD_U:
self.moves.append(HEAD_D)

@rx.event
def arrow_rel_left(self):
"""Queue a move left relative to the current direction."""
last_move = self._last_move()
Expand All @@ -179,6 +189,7 @@ def arrow_rel_left(self):
elif last_move == HEAD_R:
self.arrow_up()

@rx.event
def arrow_rel_right(self):
"""Queue a move right relative to the current direction."""
last_move = self._last_move()
Expand All @@ -202,9 +213,9 @@ class GlobalKeyWatcher(rx.Fragment):
"""

# List of keys to trigger on
key_map: Dict[str, EventType[key_event]] = {}
key_map: Dict[str, EventSpec] = {}

def add_imports(self) -> dict[str, str]:
def add_imports(self) -> ImportDict:
return {"react": "useEffect"}

def add_hooks(self) -> list[str | rx.Var[str]]:
Expand All @@ -228,9 +239,9 @@ def add_hooks(self) -> list[str | rx.Var[str]]:
""",
]

def render(self) -> str:
def render(self) -> dict:
# This component has no visual element.
return ""
return {}


def colored_box(grid_square_type: int):
Expand Down

0 comments on commit f1641a0

Please sign in to comment.