Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions snake_game_human.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ def _place_food(self):
def play_step(self):
# 1. collect user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.direction = Direction.LEFT
if self.direction!=direction.RIGHT:
self.direction = direction.LEFT
elif event.key == pygame.K_RIGHT:
self.direction = Direction.RIGHT
if self.direction!=direction.LEFT:
self.direction = direction.RIGHT
elif event.key == pygame.K_UP:
self.direction = Direction.UP
if self.direction!=direction.DOWN:
self.direction = direction.UP
elif event.key == pygame.K_DOWN:
self.direction = Direction.DOWN
if self.direction!=direction.UP:
self.direction = direction.DOWN

# 2. move
self._move(self.direction) # update the head
Expand Down Expand Up @@ -144,4 +145,4 @@ def _move(self, direction):
print('Final Score', score)


pygame.quit()
pygame.quit()