From 6e8ee9d5aea28227b47f10780edf1f08191cb7d4 Mon Sep 17 00:00:00 2001 From: Idriss Chaouch <62848804+CppProgrammer23@users.noreply.github.com> Date: Tue, 22 Dec 2020 08:59:24 +0100 Subject: [PATCH 1/2] still in game even if we clicked on the opposite key to the direction --- snake_game_human.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/snake_game_human.py b/snake_game_human.py index f51e9c0..11051c5 100644 --- a/snake_game_human.py +++ b/snake_game_human.py @@ -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.Dir!=Dir.RIGHT: + self.Dir = Dir.LEFT elif event.key == pygame.K_RIGHT: - self.direction = Direction.RIGHT + if self.Dir!=Dir.LEFT: + self.Dir = Dir.RIGHT elif event.key == pygame.K_UP: - self.direction = Direction.UP + if self.Dir!=Dir.DOWN: + self.Dir = Dir.UP elif event.key == pygame.K_DOWN: - self.direction = Direction.DOWN + if self.Dir!=Dir.UP: + self.Dir = Dir.DOWN # 2. move self._move(self.direction) # update the head @@ -144,4 +145,4 @@ def _move(self, direction): print('Final Score', score) - pygame.quit() \ No newline at end of file + pygame.quit() From 8b8eb7a1496b7f8472072b6fbfcdde5f0fa2fe64 Mon Sep 17 00:00:00 2001 From: Idriss Chaouch <62848804+CppProgrammer23@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:00:48 +0100 Subject: [PATCH 2/2] Update snake_game_human.py --- snake_game_human.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/snake_game_human.py b/snake_game_human.py index 11051c5..119a58f 100644 --- a/snake_game_human.py +++ b/snake_game_human.py @@ -59,17 +59,17 @@ def play_step(self): for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: - if self.Dir!=Dir.RIGHT: - self.Dir = Dir.LEFT + if self.direction!=direction.RIGHT: + self.direction = direction.LEFT elif event.key == pygame.K_RIGHT: - if self.Dir!=Dir.LEFT: - self.Dir = Dir.RIGHT + if self.direction!=direction.LEFT: + self.direction = direction.RIGHT elif event.key == pygame.K_UP: - if self.Dir!=Dir.DOWN: - self.Dir = Dir.UP + if self.direction!=direction.DOWN: + self.direction = direction.UP elif event.key == pygame.K_DOWN: - if self.Dir!=Dir.UP: - self.Dir = Dir.DOWN + if self.direction!=direction.UP: + self.direction = direction.DOWN # 2. move self._move(self.direction) # update the head