Skip to content

Commit

Permalink
more refactoring of example
Browse files Browse the repository at this point in the history
  • Loading branch information
davethepunkyone committed Oct 4, 2024
1 parent d58ab04 commit e16d289
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions examples/snake_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ class Direction(Enum):
LEFT = 4


def determine_snake_direction(event_key: int, snake_direction: Direction) -> Direction:
if event_key == pygame.K_UP and \
snake_direction != Direction.DOWN:
return Direction.UP
elif event_key == pygame.K_RIGHT and \
snake_direction != Direction.LEFT:
return Direction.RIGHT
elif event_key == pygame.K_DOWN and \
snake_direction != Direction.UP:
return Direction.DOWN
elif event_key == pygame.K_LEFT and \
snake_direction != Direction.RIGHT:
return Direction.LEFT

return snake_direction


def determine_snake_position(snake_direction: Direction, snake_head_top: int, snake_head_left: int) -> tuple:
if snake_direction == Direction.UP:
snake_head_top -= 1
Expand All @@ -38,7 +55,7 @@ def determine_snake_position(snake_direction: Direction, snake_head_top: int, sn
snake_head_left -= 1
if snake_head_left == -1:
snake_head_left = 9

return snake_head_top, snake_head_left


Expand Down Expand Up @@ -85,18 +102,8 @@ def start_snake_example() -> None:
if event.key == pygame.K_ESCAPE:
running = False
break
if event.key == pygame.K_UP and \
snake_direction != Direction.DOWN:
snake_direction = Direction.UP
elif event.key == pygame.K_RIGHT and \
snake_direction != Direction.LEFT:
snake_direction = Direction.RIGHT
elif event.key == pygame.K_DOWN and \
snake_direction != Direction.UP:
snake_direction = Direction.DOWN
elif event.key == pygame.K_LEFT and \
snake_direction != Direction.RIGHT:
snake_direction = Direction.LEFT

snake_direction = determine_snake_direction(event.key, snake_direction)

# Draw border
border = shape_factory.Rect(0, 0, grid.width_gap(0, 10),
Expand Down

0 comments on commit e16d289

Please sign in to comment.