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

30
main.py
View File

@ -12,7 +12,7 @@ from bfs import bfs
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():
if event.type == pygame.KEYDOWN:
if auto:
@ -22,8 +22,10 @@ def game_keys(truck, trash, houses, auto=False):
break
truck.move()
print('')
if truck.pos == trash.pos:
trash.new_pos(truck.pos, houses)
for tindex, trash in enumerate(multi_trash):
if truck.pos == trash.pos:
multi_trash.pop(tindex)
break
elif (event.key == pygame.K_a or event.key == pygame.K_LEFT):
print('')
@ -37,11 +39,12 @@ def game_keys(truck, trash, houses, auto=False):
break
def update_images(gameDisplay, truck, trash, houses):
def update_images(gameDisplay, truck, multi_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)
for trash in multi_trash:
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
gameDisplay.blit(truck.image, truck.pos)
pygame.display.update()
@ -58,11 +61,13 @@ def game_loop():
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)
multi_trash = []
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:
for event in pygame.event.get():
@ -74,6 +79,7 @@ def game_loop():
pygame.quit()
quit()
if (event.key == pygame.K_b):
trash = multi_trash[0]
actions = bfs(truck.pos, truck.dir_control,
trash.pos, houses)
if not actions:
@ -86,12 +92,12 @@ def game_loop():
pygame.event.post(pygame.event.Event(
pygame.KEYDOWN, {'key': action}))
game_keys(truck, trash, houses, True)
update_images(gameDisplay, truck, trash, houses)
game_keys(truck, multi_trash, houses, True)
update_images(gameDisplay, truck, multi_trash, houses)
else:
pygame.event.post(event)
game_keys(truck, trash, houses)
update_images(gameDisplay, truck, trash, houses)
game_keys(truck, multi_trash, houses)
update_images(gameDisplay, truck, multi_trash, houses)
clock.tick(60)

View File

@ -8,9 +8,14 @@ class Trash:
self.grid_h = grid_h
self.size = grid_size
def new_pos(self, truck_pos, houses):
def new_pos(self, truck_pos, houses, multi):
while True:
self.pos = [random.randrange(0, self.grid_w, 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
def __eq__(self, other):
if isinstance(self, Trash):
return self.pos == other.pos
return False