def drawPath(self): if self.slow: self.current_state = Map.getCell(self.new_p) Map.setCell(self.new_p, Map.ORANGE_CELL) if self.slow: Map.setCell(self.new_p, self.current_state) if self.move_forward: if Map.getCell(self.new_p) == Map.EMPTY: Map.setCell(self.new_p, Map.SOLUTION_PATH) if self.old_p not in (Map.start_pos, Map.goal_pos): Map.setCell(self.old_p, Map.SOLUTION_PATH) elif Map.getCell(self.old_p) == Map.SOLUTION_PATH or Map.getCell( self.new_p) == Map.SOLUTION_PATH: if Map.getCell(self.new_p) not in (Map.START, Map.GOAL): Map.setCell(self.new_p, Map.VISITED_PATH) if self.old_p not in (Map.start_pos, Map.goal_pos): Map.setCell(self.old_p, Map.VISITED_PATH)
def drawCell(cls, p1: Position, p2: Position) -> None: """ Affiche une ligne de plusieurs cellules de même couleur INPUT : p1 : Position, position de la première cellule p2 : Position, position de la dernière cellule EXCEPTIONS : ValueError : si l'état de la cellule est inconnu ValueError : si une cellule est en fait un mur """ etat = Map.getCell(p1) if etat != Map.EMPTY: try: color_in = cls.color_scheme[etat] except KeyError as e: raise ValueError("Couleur inconnue : " + str(etat)) from e if etat == Map.WALL: raise ValueError("Il y a un mur sur une case (!)") color_out = color_in p1 = p1 * cls.cell_size p2 = p2 + (1, 1) p2 = p2 * cls.cell_size # On dessine un rectange allant de p1 à p2 cls.grille.create_rectangle(p1.x + 1, p1.y + 1, p2.x, p2.y, outline=color_out, fill=color_in)
def draw(cls) -> None: """ Procédure pour dessiner à l'écran. """ t1 = time.time() # Repère temporel pour savoir la durée d'un affichage if not cls.is_running: # Si la fenêtre est fermée on arrête return cls.grille.delete(Tk.ALL) # On réinitialise la grille ######################################################################## # On dessine les cellules # On va parcourir la map ligne par ligne # pour chercher les cellules adjacentes de même couleur # pour ne dessiner qu'un seul rectange plutot que plusieurs carrés. # tkinter supporte mal plusieurs centaines de formes à afficher for y in range(Map.height): first_p = Position((0, y)) old_p = Position(first_p) for x in range(Map.width): new_p = Position((x, y)) if Map.getCell(first_p) == Map.getCell(new_p): pass else: cls.drawCell(first_p, old_p) first_p = Position(new_p) old_p = Position(new_p) cls.drawCell(first_p, old_p) ######################################################################## # On dessine les murs # Murs verticaux for x in range(Map.width - 1): first_p = Position((x + 0.5, 0)) old_p = Position(first_p) for y in range(Map.height): new_p = Position((x + 0.5, y)) if Map.getWall(first_p) == Map.getWall(new_p): pass else: cls.drawWall(first_p, old_p, vertical=True) first_p = Position(new_p) old_p = Position(new_p) cls.drawWall(first_p, old_p, vertical=True) # Murs horizontaux for y in range(Map.height - 1): first_p = Position((0, y + 0.5)) old_p = Position(first_p) for x in range(Map.width): new_p = Position((x, y + 0.5)) if Map.getWall(first_p) == Map.getWall(new_p): pass else: cls.drawWall(first_p, old_p, horizontal=True) first_p = Position(new_p) old_p = Position(new_p) cls.drawWall(first_p, old_p, horizontal=True) ######################################################################## # On dessine les 4 murs extérieurs # Mur vertical gauche cls.grille.create_line(3, 0, 3, Map.height * cls.cell_size, fill=Display.BLACK, width=cls.WALL_WIDTH) # Mur vertical droit cls.grille.create_line(cls.width, 0, cls.width, Map.height * cls.cell_size, fill=Display.BLACK, width=cls.WALL_WIDTH) # Mur horizontal en haut cls.grille.create_line(0, 3, Map.width * cls.cell_size, 3, fill=Display.BLACK, width=cls.WALL_WIDTH) # Mur horizontal en bas cls.grille.create_line(0, Map.height * cls.cell_size, Map.width * cls.cell_size, Map.height * cls.cell_size, fill=Display.BLACK, width=cls.WALL_WIDTH) cls.frame_count += 1 cls.updateTitle() delta_t = time.time() - t1 # Calcul du temps mis cls.draw_time = delta_t cls.window.after(1, cls.beforeDraw)