multiple trash on map

This commit is contained in:
Michal Zmudzinski 2021-06-15 20:24:06 +02:00
parent 66dfb94548
commit 1302fdaaf6
5 changed files with 26 additions and 15 deletions

Binary file not shown.

Binary file not shown.

2
bfs.py
View File

@ -11,7 +11,7 @@ class Node:
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, Node): if isinstance(other, Node):
return self.pos[0] == other.pos[0] and self.pos[1] == other.pos[1] and self.direction == other.direction return self.pos == other.pos and self.direction == other.direction
return False return False

30
main.py
View File

@ -12,7 +12,7 @@ from bfs import bfs
pygame.init() pygame.init()
def game_keys(truck, trash, houses, auto=False): def game_keys(truck, multi_trash, houses, auto=False):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if auto: if auto:
@ -22,8 +22,10 @@ def game_keys(truck, trash, houses, auto=False):
break break
truck.move() truck.move()
print('') print('')
if truck.pos == trash.pos: for tindex, trash in enumerate(multi_trash):
trash.new_pos(truck.pos, houses) if truck.pos == trash.pos:
multi_trash.pop(tindex)
break break
elif (event.key == pygame.K_a or event.key == pygame.K_LEFT): elif (event.key == pygame.K_a or event.key == pygame.K_LEFT):
print('') print('')
@ -37,11 +39,12 @@ def game_keys(truck, trash, houses, auto=False):
break break
def update_images(gameDisplay, truck, trash, houses): def update_images(gameDisplay, truck, multi_trash, houses):
gameDisplay.fill(gray) gameDisplay.fill(gray)
for house in houses: for house in houses:
gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos) gameDisplay.blit(pygame.image.load('./img/house.png'), house.pos)
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos) for trash in multi_trash:
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
gameDisplay.blit(truck.image, truck.pos) gameDisplay.blit(truck.image, truck.pos)
pygame.display.update() pygame.display.update()
@ -58,11 +61,13 @@ def game_loop():
gameExit = False gameExit = False
truck = Truck(game_w, game_h, grid_size) truck = Truck(game_w, game_h, grid_size)
trash = Trash(game_w, game_h, grid_size)
houses = create_houses(grid_size) houses = create_houses(grid_size)
trash.pos = [(game_w // 2)-160, (game_h // 2)] multi_trash = []
# trash.new_pos(truck.pos, houses) for _ in range(7):
trash = Trash(game_w, game_h, grid_size)
trash.new_pos(truck.pos, houses, multi_trash)
multi_trash.append(trash)
while not gameExit: while not gameExit:
for event in pygame.event.get(): for event in pygame.event.get():
@ -74,6 +79,7 @@ def game_loop():
pygame.quit() pygame.quit()
quit() quit()
if (event.key == pygame.K_b): if (event.key == pygame.K_b):
trash = multi_trash[0]
actions = bfs(truck.pos, truck.dir_control, actions = bfs(truck.pos, truck.dir_control,
trash.pos, houses) trash.pos, houses)
if not actions: if not actions:
@ -86,12 +92,12 @@ def game_loop():
pygame.event.post(pygame.event.Event( pygame.event.post(pygame.event.Event(
pygame.KEYDOWN, {'key': action})) pygame.KEYDOWN, {'key': action}))
game_keys(truck, trash, houses, True) game_keys(truck, multi_trash, houses, True)
update_images(gameDisplay, truck, trash, houses) update_images(gameDisplay, truck, multi_trash, houses)
else: else:
pygame.event.post(event) pygame.event.post(event)
game_keys(truck, trash, houses) game_keys(truck, multi_trash, houses)
update_images(gameDisplay, truck, trash, houses) update_images(gameDisplay, truck, multi_trash, houses)
clock.tick(60) clock.tick(60)

View File

@ -8,9 +8,14 @@ class Trash:
self.grid_h = grid_h self.grid_h = grid_h
self.size = grid_size self.size = grid_size
def new_pos(self, truck_pos, houses): def new_pos(self, truck_pos, houses, multi):
while True: while True:
self.pos = [random.randrange(0, self.grid_w, self.size), self.pos = [random.randrange(0, self.grid_w, self.size),
random.randrange(0, self.grid_h, self.size)] random.randrange(0, self.grid_h, self.size)]
if self.pos != truck_pos and not is_house(self.pos, houses): if self.pos != truck_pos and not is_house(self.pos, houses) and self not in multi:
break break
def __eq__(self, other):
if isinstance(self, Trash):
return self.pos == other.pos
return False