From 4f113282d7a709790854f8194b6c9c77f2f164b3 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Tue, 1 Nov 2022 15:09:08 +0100 Subject: [PATCH] Minor changes --- qlearning.py | 2 +- snake.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/qlearning.py b/qlearning.py index 797e2a9..5cb3d8a 100755 --- a/qlearning.py +++ b/qlearning.py @@ -39,7 +39,7 @@ def isWall(h,game): return(False) -def event_handler(game): +def event_handler(game,event): h=game.snake[0] left=(h[0]-1,h[1]) right=(h[0]+1,h[1]) diff --git a/snake.py b/snake.py index e5db997..0597808 100755 --- a/snake.py +++ b/snake.py @@ -124,6 +124,7 @@ class Snake: clock = pygame.time.Clock() ignore_has_loose=True self.new_game() + event=0 # 0 is nothing, 1 is eat an apple and -1 loose while True: self.screen.fill((0,0,0)) self.draw_snake() @@ -131,6 +132,7 @@ class Snake: self.draw_infos() # Check for loose if not(ignore_has_loose) and self.has_loose(): + event_handler(self,-1) break else: ignore_has_loose=False @@ -154,14 +156,26 @@ class Snake: break # Check if an event handler is available if event_handler!=None: - event_handler(self) + event_handler(self,event) + event=0 self.move() # Check for eating apple if self.apple==self.snake[0]: self.snake.append(self.snake[len(self.snake)-1]) self.new_apple() self.score+=1 + event=1 pygame.display.flip() clock.tick(self.fps) return(self.score) + +game=Snake() + +def event_handler(game): + if game.snake[0][0]==10: + game.direction=6 + +for i in range(0,10): + score=game.run() + print("Game ended with "+str(score)) \ No newline at end of file