From 039f36fda30bb7aa00bbfe3aacd7a0e2cdf61d11 Mon Sep 17 00:00:00 2001 From: s464859 <@> Date: Thu, 7 Apr 2022 18:27:35 +0200 Subject: [PATCH] =?UTF-8?q?niesko=C5=84czony=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classes/ai.py | 2 +- classes/bfs.py | 12 ++++++++---- classes/system.py | 24 +++++++++++++++++++++++- main.py | 13 +++++-------- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/classes/ai.py b/classes/ai.py index a7d34c9..41c5ca7 100644 --- a/classes/ai.py +++ b/classes/ai.py @@ -94,5 +94,5 @@ class AI: goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y] print(goal_state) - find_path = bfs.BFS(self.saper) + find_path = bfs.BFS(self.saper, self.window) self.the_way = find_path.graphsearch([], [], bfs.BFS.successor, goal_state) \ No newline at end of file diff --git a/classes/bfs.py b/classes/bfs.py index 3c3482b..9e70610 100644 --- a/classes/bfs.py +++ b/classes/bfs.py @@ -1,15 +1,16 @@ import heapq # dla utrzymania fringe -from classes import node, minesweeper +from classes import node, minesweeper, system import time class BFS: - + window: system.Window agent: minesweeper.Minesweeper node: node.Node - def __init__(self, agent): + def __init__(self, agent, window): self.agent = agent + self.window = window def successor(self, current_position): new_nodes = [] @@ -39,6 +40,7 @@ class BFS: # goaltest = test spewnienia celu def graphsearch(self, fringe, explored, succ, goaltest): + self.window.pause(True) positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy final_action_list = [] @@ -78,6 +80,7 @@ class BFS: tmp_node = tmp_node[1].get_parent() final_action_list.reverse() print(final_action_list) + self.window.pause(False) return final_action_list @@ -92,7 +95,8 @@ class BFS: #print([node_[0], node_[1]]) x = node.Node(tmp_node, None, [node_[0], node_[1]]) # action heapq.heappush(fringe, (counter, x)) - # time.sleep(0.5) + self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[0], node_[1]]) + time.sleep(0.01) diff --git a/classes/system.py b/classes/system.py index 52b67db..d6784a8 100644 --- a/classes/system.py +++ b/classes/system.py @@ -7,6 +7,9 @@ class Window: title:str icon_path:str paused:bool + pause_menu:None + + search: pygame.Surface def __init__(self, width:int=640, height:int=480, title="", icon_path=""): self.set_resolution(width,height) @@ -14,6 +17,11 @@ class Window: self.set_icon(icon_path) self.mount() self.paused=False + + self.pause_menu = pygame.Surface((width, height)) + self.pause_menu.set_alpha(128) + self.pause_menu.fill((0,0,0)) + self.search = pygame.Surface((width, height), flags=pygame.SRCALPHA) def set_resolution(self, width:int, height:int): self.width = width @@ -30,4 +38,18 @@ class Window: pygame.display.set_caption(self.title) if self.icon_path != "": icon = pygame.image.load(self.icon_path) - pygame.display.set_icon(icon) \ No newline at end of file + pygame.display.set_icon(icon) + + def pause(self, paused:bool): + self.paused=paused + if self.paused: + self.window.blit(self.pause_menu,(0,0)) + pygame.display.update() + self.search.fill(pygame.Color(0,0,0,0)) + + def draw_search(self, pos1:list=[0,0], pos2:list=[0,0], tile_size:int=64): + self.search.fill(pygame.Color(0,0,0,0)) + pos1 = [pos1[0]*tile_size, pos1[1]*tile_size] + pos2 = [pos2[0]*tile_size, pos2[1]*tile_size] + pygame.draw.line(self.search, pygame.Color(255,0,0), pos1, pos2, 1) + pygame.display.update() \ No newline at end of file diff --git a/main.py b/main.py index c9c829c..427bef3 100644 --- a/main.py +++ b/main.py @@ -24,10 +24,6 @@ def main(): #utworzenie okna do gry window = system.Window(TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y, "Intelligent Minesweeper", "icon.png") - #utworzenie ekranu pauzy - pause_menu = pygame.Surface((TILE_SIZE*TILES_X, TILE_SIZE*TILES_Y)) - pause_menu.set_alpha(128) - pause_menu.fill((0,0,0)) #utworzenie objektu mapy, wygenerowanie jej i narysowanie na ekranie map = minesweeper.Map(window, TILE_SIZE, TILES_X, TILES_Y) @@ -37,6 +33,11 @@ def main(): #utworzenie sapera saper = minesweeper.Minesweeper(0,0, TILE_SIZE) + #pierwszy render + map.draw_tiles() + map.draw_objects() + saper.draw(window.window, 0.1) + pygame.display.update() #utworzenie objektu klasy AI AI = ai.AI(window, map, saper) @@ -60,10 +61,6 @@ def main(): map.draw_tiles() map.draw_objects() saper.draw(window.window, delta) - - #pauza - if window.paused: - window.window.blit(pause_menu, (0,0)) #odświeżenie ekranu pygame.display.update()