def winner(self): self.draw_path(self.path, red) # draws player path self.draw_path(self.maze.route, blue) # draws optimal path # Winner text at center of the screen font = pygame.font.SysFont("monospace", 60) win = font.render("WINNER!", 1, yellow) win_pos = win.get_rect() win_pos.centerx = self.maze.screen.get_rect().centerx win_pos.centery = self.maze.screen.get_rect().centery # display "winner" message for 4 seconds and returns to level selection menu self.maze.screen.blit(win, win_pos) pygame.display.update() t1 = time.clock() while True: t2 = time.clock() if t2 - t1 > 0.4: # 4 second delay break try: self.clock.tick( 60) # game can't run faster than 60 frames per second for event in pygame.event.get(): is_quit_event( event) # exit gracefully if the user requests except: sys.exit()
def move_player(self, x, y): try: while True: self.clock.tick( 60) # game can't run faster than 60 frames per second for event in pygame.event.get(): is_quit_event( event) # exit gracefully if the user requests if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: # moves up if self.maze.screen.get_at( (x, y - wall_width))[:3] != dark_blue: self.maze.screen.fill( pink, (x, y, self.maze.hall_width, self.maze.hall_width)) y -= self.maze.hall_width + wall_width self.path.append((x, y)) if event.key == pygame.K_DOWN: # moves down if self.maze.screen.get_at( (x, y + self.maze.hall_width))[:3] != dark_blue: self.maze.screen.fill( pink, (x, y, self.maze.hall_width, self.maze.hall_width)) y += self.maze.hall_width + wall_width self.path.append((x, y)) if event.key == pygame.K_RIGHT: # moves right if self.maze.screen.get_at( (x + self.maze.hall_width, y))[:3] != dark_blue: self.maze.screen.fill( pink, (x, y, self.maze.hall_width, self.maze.hall_width)) x += self.maze.hall_width + wall_width self.path.append((x, y)) if event.key == pygame.K_LEFT: # moves left if self.maze.screen.get_at( (x - wall_width, y))[:3] != dark_blue: self.maze.screen.fill( pink, (x, y, self.maze.hall_width, self.maze.hall_width)) x -= self.maze.hall_width + wall_width self.path.append((x, y)) self.maze.screen.blit(self.player, (x, y)) pygame.display.update() # display player to new postion if x == self.maze.end[0] and y == self.maze.end[1]: self.winner() # player has reached the goal! break except: sys.exit()
def move_player(self, x, y): try: while True: self.clock.tick(60) # game can't run faster than 60 frames per second for event in pygame.event.get(): is_quit_event(event) # exit gracefully if the user requests if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: # moves up if self.maze.screen.get_at((x, y - wall_width))[:3] != dark_blue: self.maze.screen.fill(pink, (x, y, self.maze.hall_width, self.maze.hall_width)) y -= self.maze.hall_width + wall_width self.path.append((x,y)) if event.key == pygame.K_DOWN: # moves down if self.maze.screen.get_at((x, y + self.maze.hall_width))[:3] != dark_blue: self.maze.screen.fill(pink, (x, y, self.maze.hall_width, self.maze.hall_width)) y += self.maze.hall_width + wall_width self.path.append((x,y)) if event.key == pygame.K_RIGHT: # moves right if self.maze.screen.get_at((x + self.maze.hall_width, y))[:3] != dark_blue: self.maze.screen.fill(pink,(x, y, self.maze.hall_width, self.maze.hall_width)) x += self.maze.hall_width + wall_width self.path.append((x,y)) if event.key == pygame.K_LEFT: # moves left if self.maze.screen.get_at((x - wall_width, y))[:3] != dark_blue: self.maze.screen.fill(pink,(x, y, self.maze.hall_width, self.maze.hall_width)) x -= self.maze.hall_width + wall_width self.path.append((x,y)) self.maze.screen.blit(self.player, (x, y)) pygame.display.update() # display player to new postion if x == self.maze.end[0] and y == self.maze.end[1]: self.winner() # player has reached the goal! break except: sys.exit()
def winner(self): self.draw_path(self.path, red) # draws player path self.draw_path(self.maze.route, blue) # draws optimal path # Winner text at center of the screen font = pygame.font.SysFont("monospace", 60) win = font.render("WINNER!", 1, yellow) win_pos = win.get_rect() win_pos.centerx = self.maze.screen.get_rect().centerx win_pos.centery = self.maze.screen.get_rect().centery # display "winner" message for 4 seconds and returns to level selection menu self.maze.screen.blit(win, win_pos) pygame.display.update() t1 = time.clock() while True: t2 = time.clock() if t2 - t1 > 0.4: # 4 second delay break try: self.clock.tick(60) # game can't run faster than 60 frames per second for event in pygame.event.get(): is_quit_event(event) # exit gracefully if the user requests except: sys.exit()