@@ -44,6 +44,8 @@ class Direction(enum.Enum):
44
44
def GameLoop (grid_size , block_scale , set_game_state ):
45
45
# we `use_ref` here to capture the latest direction press without any delay
46
46
direction = idom .hooks .use_ref (Direction .ArrowRight .value )
47
+ # capture the last direction of travel that was rendered
48
+ last_direction = direction .current
47
49
48
50
snake , set_snake = idom .hooks .use_state ([(grid_size // 2 - 1 , grid_size // 2 - 1 )])
49
51
food , set_food = use_snake_food (grid_size , snake )
@@ -52,11 +54,11 @@ def GameLoop(grid_size, block_scale, set_game_state):
52
54
grid_events = grid ["eventHandlers" ] = idom .Events ()
53
55
54
56
@grid_events .on ("KeyDown" , prevent_default = True )
55
- async def on_direction_change (event ):
57
+ def on_direction_change (event ):
56
58
if hasattr (Direction , event ["key" ]):
57
59
maybe_new_direction = Direction [event ["key" ]].value
58
60
direction_vector_sum = tuple (
59
- map (sum , zip (direction . current , maybe_new_direction ))
61
+ map (sum , zip (last_direction , maybe_new_direction ))
60
62
)
61
63
if direction_vector_sum != (0 , 0 ):
62
64
direction .current = maybe_new_direction
0 commit comments