Minor changes
This commit is contained in:
parent
a417a3054c
commit
4f113282d7
2 changed files with 16 additions and 2 deletions
|
@ -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])
|
||||
|
|
16
snake.py
16
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))
|
Loading…
Add table
Reference in a new issue