diff --git a/__pycache__/bfs.cpython-39.pyc b/__pycache__/bfs.cpython-39.pyc new file mode 100644 index 0000000..b61c917 Binary files /dev/null and b/__pycache__/bfs.cpython-39.pyc differ diff --git a/__pycache__/colors.cpython-39.pyc b/__pycache__/colors.cpython-39.pyc new file mode 100644 index 0000000..7660b71 Binary files /dev/null and b/__pycache__/colors.cpython-39.pyc differ diff --git a/__pycache__/house.cpython-39.pyc b/__pycache__/house.cpython-39.pyc new file mode 100644 index 0000000..50f49c9 Binary files /dev/null and b/__pycache__/house.cpython-39.pyc differ diff --git a/__pycache__/text.cpython-39.pyc b/__pycache__/text.cpython-39.pyc new file mode 100644 index 0000000..3871121 Binary files /dev/null and b/__pycache__/text.cpython-39.pyc differ diff --git a/__pycache__/trash.cpython-39.pyc b/__pycache__/trash.cpython-39.pyc new file mode 100644 index 0000000..8a69a0f Binary files /dev/null and b/__pycache__/trash.cpython-39.pyc differ diff --git a/__pycache__/truck.cpython-39.pyc b/__pycache__/truck.cpython-39.pyc new file mode 100644 index 0000000..6dc5f95 Binary files /dev/null and b/__pycache__/truck.cpython-39.pyc differ diff --git a/bfs.py b/bfs.py index e69de29..3074435 100644 --- a/bfs.py +++ b/bfs.py @@ -0,0 +1,55 @@ +from house import is_house +import pygame + + +class Node: + def __init__(self, pos, direction, parent=None, action=None): + self.pos = pos + self.direction = direction + self.parent = parent + self.action = action + + +def successor(pos, direction, houses): + neighbours = [] + axis = 0 if direction in [0, 2] else 1 + move = 1 if direction in [0, 1] else -1 + + neighbours.append([pos, (direction - 1) % 4, pygame.K_a]) + neighbours.append([pos, (direction + 1) % 4, pygame.K_d]) + + if not axis: # x + new_pos = [pos[0] + (move * 40), pos[1]] + if not is_house(new_pos, houses): + neighbours.append([new_pos, direction, pygame.K_w]) + + else: # y + new_pos = [pos[0], pos[1] + (move * 40)] + if not is_house(new_pos, houses): + neighbours.append([new_pos, direction, pygame.K_w]) + return neighbours + + +def bfs(pos, direction, end_pos, houses): + visited = [] + queue = [] + actions = [] + queue.append(Node(pos, direction)) + + while queue: + curr_node = queue.pop(0) + print(curr_node.pos, curr_node.direction) + if not is_house(curr_node.pos, houses) and curr_node.pos == end_pos: + while curr_node.parent: + print(curr_node.pos, end_pos) + actions.append(curr_node.action) + curr_node = curr_node.parent + actions.append(curr_node.action) + return actions + + visited.append(curr_node) + for n_pos, n_direction, action in successor(curr_node.pos, curr_node.direction, houses): + neighbour_node = Node(n_pos, n_direction, curr_node, action) + if neighbour_node not in visited and neighbour_node not in queue: + queue.append(neighbour_node) + return actions diff --git a/main.py b/main.py index 23c1690..aa67b20 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import pygame +from time import sleep -from text import msg_display from colors import gray from house import create_houses from truck import Truck @@ -12,13 +12,37 @@ from bfs import bfs pygame.init() -def update_images(gameDisplay, truck, trash, houses, score): +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) - msg_display(gameDisplay, f'Trash collected: {score}') pygame.display.update() @@ -33,12 +57,12 @@ def game_loop(): gameExit = False - score = 0 truck = Truck(game_w, game_h, grid_size) trash = Trash(game_w, game_h, grid_size) houses = create_houses(grid_size) - trash.new_pos(truck.pos, houses) + trash.pos = [(game_w // 2)-160, (game_h // 2)] + # trash.new_pos(truck.pos, houses) while not gameExit: for event in pygame.event.get(): @@ -50,29 +74,24 @@ def game_loop(): pygame.quit() quit() if (event.key == pygame.K_b): - bfs() - break - if (event.key == pygame.K_w or event.key == pygame.K_UP): - if truck.test_crash(houses): + actions = bfs(truck.pos, truck.dir_control, + trash.pos, houses) + if not actions: + print('Path couldn\'t be found') break - truck.move() - print('↑') - if truck.pos == trash.pos: - score += 1 - 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 - update_images(gameDisplay, truck, trash, houses, score) + 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) diff --git a/text.py b/text.py deleted file mode 100644 index 2ad8549..0000000 --- a/text.py +++ /dev/null @@ -1,16 +0,0 @@ -from colors import white -import pygame - - -def text_objects(text, font): - textSurface = font.render(text, True, white) - return textSurface, textSurface.get_rect() - - -def msg_display(gameDisplay, msg): - msg_size = 35 - pos = (1100, 16) - largeText = pygame.font.Font('freesansbold.ttf', msg_size) - TestSurf, TextRect = text_objects(msg, largeText) - TextRect.center = (pos) - gameDisplay.blit(TestSurf, TextRect)