multiple trash on map
This commit is contained in:
parent
66dfb94548
commit
1302fdaaf6
Binary file not shown.
Binary file not shown.
2
bfs.py
2
bfs.py
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
26
main.py
26
main.py
@ -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('↑')
|
||||||
|
for tindex, trash in enumerate(multi_trash):
|
||||||
if truck.pos == trash.pos:
|
if truck.pos == trash.pos:
|
||||||
trash.new_pos(truck.pos, houses)
|
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,10 +39,11 @@ 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)
|
||||||
|
for trash in multi_trash:
|
||||||
gameDisplay.blit(pygame.image.load('./img/trash.png'), trash.pos)
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
9
trash.py
9
trash.py
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user