diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2022-11-02 08:50:05 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2022-11-02 08:50:05 +0100 |
| commit | 85a180809dd9046feb0b64ae38b8a436379f98eb (patch) | |
| tree | bf6644515f5f9c7f976bf0011a48a799cc0e18ee /snake.py | |
| parent | 59ed0cdf7326854f97b6f90c6afcbe20c480e400 (diff) | |
Minor changes
Diffstat (limited to 'snake.py')
| -rwxr-xr-x | snake.py | 101 |
1 files changed, 67 insertions, 34 deletions
@@ -7,7 +7,7 @@ class Snake: Programmable Game of Snake written in PyGame """ - def __init__(self, startat=(0,0), margin=80,length=4,grid_width=30,grid_height=30, grid_pts=30,fps=180): + def __init__(self, startat=(0,0), margin=80,length=4,grid_width=30,grid_height=30, grid_pts=30,fps=15): # Init attributes self.grid_width=grid_width self.grid_height=grid_height @@ -17,11 +17,16 @@ class Snake: self.attempt=0 self.fps=fps self.startat=startat + self.last_score=-1 # Setup pygame pygame.init() self.font=pygame.font.SysFont(pygame.font.get_default_font(), int(self.margin/2)) self.font_small=pygame.font.SysFont(pygame.font.get_default_font(), int(self.margin/2.5)) self.screen=pygame.display.set_mode((grid_width*grid_pts,grid_height*grid_pts+margin)) + self.clock = pygame.time.Clock() + # Start game + self.new_game() + self.draw() def new_game(self): """ @@ -117,32 +122,73 @@ class Snake: return(True) return(False) + def draw(self): + self.screen.fill((0,0,0)) + self.draw_snake() + self.draw_apple() + self.draw_infos() + pygame.display.flip() - def run(self, event_handler=None): + def play(self,direction): """ - Play a game (one attempt) + Play using wall clock directions (12=up, 3=right, 6=down and 9=left) + """ + # Play + self.direction=direction + self.move() + # Return code + code=0 + if self.apple==self.snake[0]: + self.snake.append(self.snake[len(self.snake)-1]) + self.new_apple() + self.score+=1 + code=1 + elif self.has_loose(): + self.last_score=self.score + self.new_game() + code=-1 + # Refresh screen + self.draw() + self.clock.tick(self.fps) + return(code) + + def play2(self,direction): + """ + Play using ascii directions + """ + if direction.lower()=="up": + return(self.play(self,12)) + elif direction.lower()=="right": + return(self.play(self,3)) + elif direction.lower()=="down": + return(self.play(self,6)) + elif direction.lower()=="left": + return(self.play(self,9)) + + def play3(self,direction): + """ + Play using 0123 directions (0=right, 1=down, 3=left and 4=up) + """ + if direction == 0: + return(self.play(3)) + elif direction == 1: + return(self.play(6)) + elif direction == 2: + return(self.play(9)) + elif direction == 3: + return(self.play(12)) + + def play_with_keyboard(self): + """ + Play a game using keyboard """ - clock = pygame.time.Clock() - ignore_has_loose=True - self.new_game() - 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() - self.draw_apple() - 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 # Check inputs for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() - elif event_handler==None and event.type == pygame.KEYDOWN: + elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT and self.direction != 3: self.direction=9 break @@ -155,19 +201,6 @@ class Snake: elif event.key == pygame.K_DOWN and self.direction != 12: self.direction=6 break - # Check if an event handler is available - if event_handler!=None: - code=event_handler(self,last_event) - if code < 0: - break - 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 - last_event=1 - pygame.display.flip() - clock.tick(self.fps) - return(self.score) + + if self.play(self.direction) <0: + break |
