import pygame from time import sleep from colors import gray from house import create_houses from truck import Truck from trash import Trash from bfs import bfs pygame.init() def game_keys(truck, trash, houses, auto=False): for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if auto: sleep(.2) if (event.key == pygame.K_w or event.key == pygame.K_UP): if truck.test_crash(houses): break truck.move() print('↑') if truck.pos == trash.pos: trash.new_pos(truck.pos, houses) break elif (event.key == pygame.K_a or event.key == pygame.K_LEFT): print('←') truck.rotate(-1) truck.rotate_image(90) break elif (event.key == pygame.K_d or event.key == pygame.K_RIGHT): print('→') truck.rotate(1) truck.rotate_image(-90) break def update_images(gameDisplay, truck, trash, houses): gameDisplay.fill(gray) for house in houses: gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos) gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos) gameDisplay.blit(truck.image, truck.pos) pygame.display.update() def game_loop(): game_w = 1280 # 32 game_h = 640 # 16 grid_size = 40 gameDisplay = pygame.display.set_mode((game_w, game_h)) pygame.display.set_caption('Garbage truck') clock = pygame.time.Clock() gameExit = False truck = Truck(game_w, game_h, grid_size) trash = Trash(game_w, game_h, grid_size) houses = create_houses(grid_size) trash.pos = [(game_w // 2)-160, (game_h // 2)] # trash.new_pos(truck.pos, houses) while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() quit() if (event.key == pygame.K_b): actions = bfs(truck.pos, truck.dir_control, trash.pos, houses) if not actions: print('Path couldn\'t be found') break print('##################################################') while actions: action = actions.pop() print(action) pygame.event.post(pygame.event.Event( pygame.KEYDOWN, {'key': action})) game_keys(truck, trash, houses, True) update_images(gameDisplay, truck, trash, houses) else: pygame.event.post(event) game_keys(truck, trash, houses) update_images(gameDisplay, truck, trash, houses) clock.tick(60) if __name__ == '__main__': game_loop() pygame.quit()