diff options
Diffstat (limited to 'snake.py')
| -rwxr-xr-x | snake.py | 16 |
1 files changed, 8 insertions, 8 deletions
@@ -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) |
