summaryrefslogtreecommitdiff
path: root/snake.py
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-11-01 20:21:34 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-11-01 20:21:34 +0100
commit3b35b6866d4abbc4eb446ab8a4a06c305305325b (patch)
tree1ed6cf75e68ab2c093f927a4f465d097bc1801b1 /snake.py
parent4d4d5b1481f366f70f5fd4a97e3afbed3b9a28dd (diff)
Minor changes
Diffstat (limited to 'snake.py')
-rwxr-xr-xsnake.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/snake.py b/snake.py
index b2c8226..cfaa9b8 100755
--- a/snake.py
+++ b/snake.py
@@ -7,7 +7,7 @@ class Snake:
Programmable Game of Snake written in PyGame
"""
- def __init__(self, margin=80,length=4,grid_width=30,grid_height=30, grid_pts=30,fps=12):
+ def __init__(self, margin=80,length=4,grid_width=30,grid_height=30, grid_pts=30,fps=80):
# Init attributes
self.grid_width=grid_width
self.grid_height=grid_height
@@ -26,7 +26,7 @@ class Snake:
"""
Reset game state
"""
- self.snake=[(5,5)]*self.default_length
+ self.snake=[(0,0)]*self.default_length
self.direction=3 # Like clock (12=up, 3=right, 6=bottom, 9=left)
self.new_apple()
self.score=0
@@ -57,8 +57,8 @@ class Snake:
Create a new apple
"""
self.apple=(random.randint(0,self.grid_width-1),random.randint(0,self.grid_height-1))
- while self.apple in self.snake:
- self.apple=(random.randint(0,self.grid_width),random.randint(0,self.grid_height))
+ if self.apple in self.snake:
+ self.new_apple()
def move(self):
"""
@@ -124,7 +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
+ last_event=0 # 0 is nothing, 1 is eat an apple and -1 loose
while True:
self.screen.fill((0,0,0))
self.draw_snake()
@@ -156,15 +156,15 @@ class Snake:
break
# Check if an event handler is available
if event_handler!=None:
- event_handler(self,event)
- event=0
+ event_handler(self,last_event)
+ last_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
+ last_event=1
pygame.display.flip()
clock.tick(self.fps)
return(self.score)