From 1302fdaaf6195690f3d90cb413f57d594dd9652f Mon Sep 17 00:00:00 2001 From: Michal Zmudzinski Date: Tue, 15 Jun 2021 20:24:06 +0200 Subject: [PATCH] multiple trash on map --- __pycache__/bfs.cpython-39.pyc | Bin 1705 -> 1667 bytes __pycache__/trash.cpython-39.pyc | Bin 792 -> 972 bytes bfs.py | 2 +- main.py | 30 ++++++++++++++++++------------ trash.py | 9 +++++++-- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/__pycache__/bfs.cpython-39.pyc b/__pycache__/bfs.cpython-39.pyc index 1ae82be8fa22bd6c9347f239e966bb365a453e79..dec76453cc880a53db624a1614b9520f879c2614 100644 GIT binary patch delta 105 zcmZ3<+swZS;5A$&*0EGY_;s5{u diff --git a/__pycache__/trash.cpython-39.pyc b/__pycache__/trash.cpython-39.pyc index 8a69a0f55321850ac6420fe1c6a3d0ad4ecfbef6..5ad737ce33b924991dd2aa7b7491084af2ccf7a4 100644 GIT binary patch delta 348 zcmXv}yH3ME5WHPKW5p>_P^3U0(HIbX0ZM^{QlK=Ua$_u?B|8C|+?_#+EK$G@L~dy4 zkl<4&D5(7hQ1A`xIWW@f&fV_l=AP^iXAt|o#|Vc%@0ZWG9;p32I;AWG&O+qC@sWiF zH@HdN>K@ zZc5qOSy{^NEvRLX}D` delta 174 zcmX@ZK7)-fk(ZZ?0SMaf9!dDkIFYYJNe0MEWr$)-VTfW%VN79aVTfW*VNPKI(kv;g z!3>&g6W6?wVg_k~0uY-Gh@FAB*k$qtMti10hsi$~Z6%qD*nwhEoFzr2$=UG*`Ng-` zGV)7{Q;R2uF=>j3F;?-06eSjC=;fuBgJk_QStn~U>rY<9EIhe@nTvx3sF{P2gLQHX Gvj+fE@F;8m diff --git a/bfs.py b/bfs.py index 18f77d4..444a5a5 100644 --- a/bfs.py +++ b/bfs.py @@ -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 diff --git a/main.py b/main.py index 33908bc..931d0e3 100644 --- a/main.py +++ b/main.py @@ -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) diff --git a/trash.py b/trash.py index 9ac4c9a..66d604d 100644 --- a/trash.py +++ b/trash.py @@ -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