This commit is contained in:
Michal Zmudzinski 2021-06-15 19:48:34 +02:00
parent 5289c48fe2
commit 40b2b178f3
9 changed files with 100 additions and 42 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

55
bfs.py
View File

@ -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

71
main.py
View File

@ -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)

16
text.py
View File

@ -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)