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)
|
return(False)
|
||||||
|
|
||||||
|
|
||||||
def event_handler(game):
|
def event_handler(game,event):
|
||||||
h=game.snake[0]
|
h=game.snake[0]
|
||||||
left=(h[0]-1,h[1])
|
left=(h[0]-1,h[1])
|
||||||
right=(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()
|
clock = pygame.time.Clock()
|
||||||
ignore_has_loose=True
|
ignore_has_loose=True
|
||||||
self.new_game()
|
self.new_game()
|
||||||
|
event=0 # 0 is nothing, 1 is eat an apple and -1 loose
|
||||||
while True:
|
while True:
|
||||||
self.screen.fill((0,0,0))
|
self.screen.fill((0,0,0))
|
||||||
self.draw_snake()
|
self.draw_snake()
|
||||||
|
@ -131,6 +132,7 @@ class Snake:
|
||||||
self.draw_infos()
|
self.draw_infos()
|
||||||
# Check for loose
|
# Check for loose
|
||||||
if not(ignore_has_loose) and self.has_loose():
|
if not(ignore_has_loose) and self.has_loose():
|
||||||
|
event_handler(self,-1)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
ignore_has_loose=False
|
ignore_has_loose=False
|
||||||
|
@ -154,14 +156,26 @@ class Snake:
|
||||||
break
|
break
|
||||||
# Check if an event handler is available
|
# Check if an event handler is available
|
||||||
if event_handler!=None:
|
if event_handler!=None:
|
||||||
event_handler(self)
|
event_handler(self,event)
|
||||||
|
event=0
|
||||||
self.move()
|
self.move()
|
||||||
# Check for eating apple
|
# Check for eating apple
|
||||||
if self.apple==self.snake[0]:
|
if self.apple==self.snake[0]:
|
||||||
self.snake.append(self.snake[len(self.snake)-1])
|
self.snake.append(self.snake[len(self.snake)-1])
|
||||||
self.new_apple()
|
self.new_apple()
|
||||||
self.score+=1
|
self.score+=1
|
||||||
|
event=1
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(self.fps)
|
clock.tick(self.fps)
|
||||||
return(self.score)
|
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