nieskończony feature

This commit is contained in:
s464859 2022-04-07 18:27:35 +02:00
parent 1c7b3f2840
commit 039f36fda3
4 changed files with 37 additions and 14 deletions

View File

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

View File

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

View File

@ -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)
@ -15,6 +18,11 @@ class Window:
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
self.height = height
@ -31,3 +39,17 @@ class Window:
if self.icon_path != "":
icon = pygame.image.load(self.icon_path)
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()

13
main.py
View File

@ -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)
@ -61,10 +62,6 @@ def main():
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()