Advertisement
csmine

TIPE

Apr 1st, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 19.42 KB | None | 0 0
  1. from math import *
  2. from tkinter import *
  3. from copy import deepcopy
  4. from time import *
  5.  
  6. class maze:
  7.  
  8.     # Comment définir entièrement une case
  9.     def __init__( self,coords,parent=[] ):
  10.         self.coords=coords
  11.  
  12.     def __repr__(self):
  13.         return("||"+str(self.coords[0])+","+str(self.coords[1])+"||")
  14.  
  15.  
  16.  
  17.  
  18.     # G est la distance par rapport au point de départ
  19.     # On peut dire que c'est la distance par rapport au parent + 1
  20.  
  21.  
  22.     # Distance par rapport à l'arrivée
  23.     # approximation h = x² + y², x et y les droites pour arriver à l'arrivée
  24.  
  25.     def h(self,fin,ret=1):
  26.         x = abs(self.coords[0] - fin.coords[0])
  27.         y = abs(self.coords[1] - fin.coords[1])
  28.  
  29.         self.h=x**2 + y**2
  30.  
  31.         if ret==1:
  32.             return(self.h)
  33.  
  34.  
  35.     # f = g + h, approximation de la distance pour atteindre l'arrivée pour une case donnée
  36.  
  37.     # Actualise directement self.f,self.g,self.h pour pouvoir les manipuler sans calcul
  38.     def fh(self,debut,fin):
  39.         x = abs(self.coords[0] - fin.coords[0])
  40.         y = abs(self.coords[1] - fin.coords[1])
  41.         self.h=x**2 + y**2
  42.  
  43.         self.f = self.g + self.h
  44.  
  45.  
  46.  
  47.  
  48.  
  49. # Chercher l'élément avec le plus petit "F" d'une liste
  50. # Renvoie l'indice de l'élément
  51.  
  52. # A noter que les F ne sont pas re-calculés, comme l'openlist est censé être calculée déjà
  53. # Possibilité de trier la liste directement selon le critère de F, à voir.
  54. def lowest_F(liste,fin):
  55.     min=liste[0]
  56.     i=0
  57.  
  58.     for k in range(0,len(liste)):
  59.         if min.f > liste[k].f:
  60.             min=liste[k]
  61.             i=k
  62.     return(i)
  63.  
  64. # Identique à lowest_F, mais en booléen pour tester si element est le plus petit G
  65. def is_lowest_G(element,liste,fin):
  66.  
  67.     for k in range(0,len(liste)):
  68.         if element > liste[k].g:
  69.             return(False)
  70.     return(True)
  71.  
  72.  
  73. # (*) A ajouter : ne pas faire de "diagonale" si il y a trop d'obstacles autour
  74. def qui_adjacents(x,y,couple_x1y1,couple_x2y2,obstacles):
  75.     liste=[]
  76.     petitX=min(couple_x1y1[0],couple_x2y2[0])
  77.     grandX=max(couple_x1y1[0],couple_x2y2[0])
  78.     petitY=min(couple_x1y1[1],couple_x2y2[1])
  79.     grandY=max(couple_x1y1[1],couple_x2y2[1])
  80.  
  81.     if petitX<=(x+1)<=grandX :
  82.         a1=maze([x+1,y])
  83.         liste.append(a1)
  84.     if petitX<=(x+1)<=grandX and petitY<=(y+1)<=grandY:
  85.         if ([x+1,y] not in obstacles) and ([x,y+1] not in obstacles):
  86.             a2=maze([x+1,y+1])
  87.             liste.append(a2)
  88.     if petitX<=(x+1)<=grandX and petitY<=(y-1)<=grandY :
  89.         if ([x,y-1] not in obstacles) and ([x+1,y] not in obstacles):
  90.             a3=maze([x+1,y-1])
  91.             liste.append(a3)
  92.     if petitX<=(x-1)<=grandX :
  93.         a4=maze([x-1,y])
  94.         liste.append(a4)
  95.     if petitX<=(x-1)<=grandX and petitY<=(y+1)<=grandY :
  96.         if ([x,y+1] not in obstacles) and ([x-1,y] not in obstacles):
  97.             a5=maze([x-1,y+1])
  98.             liste.append(a5)
  99.     if petitX<=(x-1)<=grandX and petitY<=(y-1)<=grandY :
  100.         if ([x,y-1] not in obstacles) and ([x-1,y] not in obstacles):
  101.             a6=maze([x-1,y-1])
  102.             liste.append(a6)
  103.     if petitY<=(y+1)<=grandY :
  104.         a7=maze([x,y+1])
  105.         liste.append(a7)
  106.     if petitY<=(y-1)<=grandY :
  107.         a8=maze([x,y-1])
  108.         liste.append(a8)
  109.     return(liste)
  110.  
  111. # Initialise l'algorithme A*
  112.  
  113. # Contient la boucle principale de l'algo
  114. def start_algo(debut_coords,fin_coords,couple_x1y1,couple_x2y2,obstacles=[],retour=""):
  115.     a=0
  116.    
  117.     fin=maze(fin_coords)
  118.    
  119.     openlist=[]
  120.     openlist_coords=[]
  121.    
  122.     closedlist=[]
  123.     closedlist_coords= deepcopy(obstacles)
  124.  
  125.     debut=maze(debut_coords)
  126.     debut.g=0
  127.     debut.h(fin,0)
  128.     debut.f=debut.h
  129.  
  130.     openlist.append(debut)
  131.     openlist_coords.append(debut.coords)
  132.    
  133.     Liste_Fin=[]
  134.     Liste_Fin_F=[]
  135.    
  136.     while a!=1:
  137.        
  138.         # cherche le plus petit F de openlist, l'enlève de openlist, et le met dans closedlist
  139.         indice=lowest_F(openlist,fin)
  140.         current=openlist[indice]
  141.         del openlist[indice]
  142.         del openlist_coords[indice]
  143.        
  144.         closedlist.append(current)
  145.         closedlist_coords.append(current.coords)
  146.  
  147.  
  148.         [x,y]=current.coords
  149.  
  150.  
  151.         # les 8 carrés autour
  152.         adjacents=qui_adjacents(x,y,couple_x1y1,couple_x2y2,obstacles)
  153.  
  154.        
  155.         for square in adjacents:
  156.             # S'il n'est pas dans la closedlist, continue
  157.             if square.coords not in closedlist_coords:
  158.                 # S'il n'est pas dans l'openlist, l'ajoute, défini son parent, et calcule fgh
  159.                 if square.coords not in openlist_coords:
  160.                     square.g=current.g + int(copysign(1,current.coords[0]-square.coords[0] + current.coords[1] - square.coords[1]))
  161.                     #square.g = current.g + 1
  162.                     square.h=square.h(fin,1)
  163.                     square.f=square.g+square.h
  164.                    
  165.                     openlist.append(square)
  166.                     openlist_coords.append(square.coords)
  167.                     square.parent = current
  168.                     # On définit son G comme le G du parent +1, longueur du chemin de "square" à "debut"
  169.                    
  170.                    
  171.                 # Sinon, on regarde si son chemin est meilleur (proche du départ).
  172.                 else:
  173.                     # Si son chemin est meilleur, défini son parent et calcule son f,g,h
  174.                     try:
  175.                         if is_lowest_G(square.g,openlist,fin):
  176.                             square.parent=current
  177.                     except:
  178.                         square.g=current.g + int(copysign(1,current.coords[0]-square.coords[0] + current.coords[1] - square.coords[1]))
  179.                         if is_lowest_G(square.g,openlist,fin):
  180.                             square.parent=current
  181.  
  182.         # Conditions d'arrêts
  183.         if fin.coords in closedlist_coords:
  184.             a=1
  185.             Liste_Fin_F=[closedlist[-1].f]
  186.             Liste_Fin=[closedlist[-1].coords]
  187.             bidule=closedlist[-1]
  188.             while debut.coords not in Liste_Fin:
  189.                 Liste_Fin.append( (bidule.parent).coords)
  190.                 Liste_Fin_F.append( (bidule.parent).f )
  191.                
  192.                 bidule=bidule.parent
  193.                
  194.             Liste_Fin.reverse()
  195.             Liste_Fin_F.reverse()
  196.             if retour=="liste/liste_f":
  197.                 return(Liste_Fin,Liste_Fin_F)
  198.             elif retour=="liste_f":
  199.                 return(Liste_Fin_F)
  200.             elif retour=="chemin_opti":
  201.                 portion_f=portions_effe(Liste_Fin,Liste_Fin_F)
  202.                 return(refaire_chemin(portion_f,obstacles,couple_x1y1,couple_x2y2)[0])
  203.             else:
  204.                 return(Liste_Fin)
  205.            
  206.         elif openlist==[] or openlist_coords==[]:
  207.             a=1
  208.             return("error")
  209.  
  210.  
  211.  
  212.  
  213.  
  214. def labbi(coord1,coord2):
  215.     xmin=min(coord1[0],coord2[0])
  216.     xmax=max(coord1[0],coord2[0])
  217.     ymin=min(coord1[1],coord2[1])
  218.     ymax=max(coord1[1],coord2[1])
  219.  
  220.     L=[]
  221.     for i in range (xmin,xmax+1):
  222.         for j in range(ymin,ymax+1):
  223.             L+=[[i,j]]
  224.     L=sorted(L, key=lambda x: x[0])
  225.     return(L)
  226.  
  227. def remove_doublon(nnliste):
  228.     enlever=[]
  229.     liste=nnliste[:]
  230.     for i in range(0,len(liste)):
  231.         if liste[i] in liste[(i+1):]:
  232.             enlever.append(liste[i])
  233.     for elem in enlever:
  234.         liste.remove(elem)
  235.     return(liste)
  236.  
  237.  
  238.  
  239. def creer_mur(coord01,coord02,fat=0):
  240.     L=[coord01]
  241.     coord1=coord01[:]
  242.     coord2=coord02[:]
  243.  
  244.     compteur_tour=0
  245.  
  246.     while True:
  247.         #deltaX = X2 - X1
  248.         #deltaY = Y2 - Y1
  249.         deltaX=coord2[0] - coord1[0]
  250.         deltaY=coord2[1] - coord1[1]
  251.  
  252.         # Le cas deltaX==0 et deltaY==0 est pris en compte ici, ça veut dire coord1 = coord2
  253.         # C'est pour éviter avec la méthode pair/impair de ne pas finir la boucle, on va arriver à un milieu
  254.         if coord02 in L or (deltaX==0 and deltaY==0):
  255.             if coord02 not in L:
  256.                 L.append(coord02)
  257.             L=remove_doublon(L)
  258.             L=sorted(L, key=lambda x: x[0])
  259.             #L.sort()
  260.             if fat==0:
  261.                 return(L)
  262.             else:
  263.                 break
  264.  
  265.         # Si les 2 se trouvent au même X, on remonte/descend
  266.         # copysign(1,deltaY) renvoie 1 si deltaY>0, et -1 si deltaY<0
  267.         elif deltaX==0:
  268.             coord1 = (coord1[0],coord1[1]+int(copysign(1,deltaY)))
  269.             L.append(list(coord1))
  270.  
  271.         # Si les 2 se trouvent au même Y, on va à droite/gauche
  272.         elif deltaY==0:
  273.             coord1 = (coord1[0]+int(copysign(1,deltaX)),coord1[1])
  274.             L.append(list(coord1))
  275.  
  276.         else:
  277.         # Les tours pairs on ajoute "coord1 +/- 1", impairs on ajoute "coord2 +/- 1"
  278.             if compteur_tour%2==0:
  279.                 coord1 = (coord1[0] + int(copysign(1,deltaX)),coord1[1] + int(copysign(1,deltaY)))
  280.                 compteur_tour+=1
  281.                 L.append(list(coord1))
  282.  
  283.             else:
  284.                 coord2 = (coord2[0] + int(copysign(1,-deltaX)),coord2[1] + int(copysign(1,-deltaY)))
  285.                 compteur_tour+=1
  286.                 L.append(list(coord2))
  287.     if fat!=0:
  288.        
  289.         for i in range(0,len(L)):
  290.             L.append( list( (L[i][0] + 1, L[i][1] + 1) ) )
  291.             L.append( list( (L[i][0] + 1, L[i][1]) ) )
  292.             L.append( list( (L[i][0] , L[i][1] + 1) ) )
  293.            
  294.            
  295.            
  296.             L.append( list( (L[i][0] - 1, L[i][1] - 1) ) )
  297.             L.append( list( (L[i][0] - 1, L[i][1] ) ) )
  298.             L.append( list( (L[i][0] , L[i][1] - 1) ) )
  299.            
  300.            
  301.             L.append( list( (L[i][0] - 1, L[i][1] +1) ) )
  302.             L.append( list( (L[i][0] + 1, L[i][1] - 1) ) )
  303.            
  304.            
  305.            
  306.            
  307.         L=remove_doublon(L)
  308.         L=sorted(L, key=lambda x: x[0])
  309.         return(L)
  310.  
  311. # Fonction qui détecte les portions où F augmente (dernier "checkpoint" -> dernier "f baisse")
  312. # Il renvoie les portions à changer !
  313. def portions_effe(Liste_Coords,Liste_F):
  314.     Liste_de_listes = []
  315.     Liste_first_indice = []
  316.     i=0
  317.     while i!=(len(Liste_F)-1):
  318.         first_indice=i
  319.         while Liste_F[i+1]>Liste_F[i] :
  320.             i+=1
  321.         # Si l'indice n'a pas changé, pas de changements (on stocke les coordonnées pour les ajouter plus tard)
  322.         if first_indice==i :
  323.             Liste_first_indice.append( first_indice )
  324.         # Si l'indice a bien changé, on rajoute les coordonnées "stockées avant" puis les dernières, ceci constitue une portion, on recommence...
  325.         else:
  326.             L= Liste_first_indice + [k for k in range(first_indice,i + 1)]
  327.             Liste_de_listes.append( L )
  328.             Liste_first_indice=[]
  329.         i+=1
  330.     Liste_de_listes.append((Liste_first_indice)+[len(Liste_F)-1])
  331.    
  332.     final=[]
  333.     for liste in Liste_de_listes :
  334.         final.append( [ Liste_Coords[liste[0]],Liste_Coords[liste[-1]]] )
  335.    
  336.     return(final)
  337.  
  338. def refaire_chemin(portion_f,obstacles,limite1,limite2):
  339.     new_chemin=[]
  340.     new_chemin_F=[]
  341.     for couples_coords in portion_f:
  342.         (liste_chemin,liste_chemin_f)=start_algo(couples_coords[0],couples_coords[1],limite1,limite2,obstacles,"liste/liste_f")
  343.         new_chemin+=liste_chemin
  344.         new_chemin_F+=liste_chemin_f
  345.     return(new_chemin,new_chemin_F)
  346.        
  347.  
  348. # Creation de la fenêtre
  349. def start_window(coord1,coord2):
  350.     global grid_window, mainCanvas
  351.    
  352.     grid_window = Toplevel()
  353.     grid_window.title("Pathfinding - A* Algorithm")
  354.     grid_window.aspect(1,1,1,1)
  355.     grid_window.resizable(False, False)
  356.     mainCanvas=Canvas(grid_window,bg="lightgray")
  357.     mainCanvas.pack(fill=BOTH,expand=1)
  358.    
  359.     setup(coord1,coord2)
  360.    
  361.     grid_window.mainloop()
  362.    
  363.    
  364.  
  365.  
  366.  
  367. # Variables à modifier selon les coordonnées de départ
  368.  
  369.  
  370.  
  371.  
  372. def colorier_liste(liste_coords,coord1,coord2,col_arg="green",debut_coords=[],fin_coords=[]):
  373.    
  374.     for coord0 in liste_coords:
  375.         if coord0 != debut_coords and coord0 != fin_coords:
  376.             window_coord=[(coord0[0] + abs(coord1[0]))*pas , (coord0[1] + abs(coord1[1]))*pas]
  377.             mainCanvas.create_rectangle(window_coord[0],window_coord[1],window_coord[0]+pas,window_coord[1]+pas,fill=col_arg,tag="carre")
  378.     mainCanvas.update()
  379.  
  380. def colorier_case(coord0,coord1,coord2,col_arg="green"):
  381.     #mainCanvas.delete("carre")
  382.     window_coord=[(coord0[0] + abs(coord1[0]))*pas , (coord0[1] + abs(coord1[1]))*pas]
  383.    
  384.     mainCanvas.create_rectangle(window_coord[0],window_coord[1],window_coord[0]+pas,window_coord[1]+pas,fill=col_arg,tag="carre")
  385.     mainCanvas.update()
  386.  
  387. # Clique droit pour remettre la bonne taille de fenêtre
  388. # (*) La fonction qui détermine le pas est minable pour rentrer dans "tout écran"
  389. def setup(coord1,coord2):
  390.     global largeur_bc,hauteur_bc,pas
  391.     largeur_bc = abs(coord2[0] - coord1[0]) + 1
  392.     hauteur_bc = abs(coord2[1] - coord1[1]) + 1
  393.    
  394.    
  395.     # Le pas doit dépendre des hauteurs/largeurs pour rentrer dans l'écran.
  396.     pas=int(600/((hauteur_bc+largeur_bc)/2))
  397.    
  398.     # La taille de la fenêtre, REEL * PAS
  399.     grid_window.geometry('%dx%d' % (largeur_bc*pas, hauteur_bc*pas))
  400.    
  401.     create_grid()
  402.    
  403.  
  404.  
  405.  
  406.  
  407. def create_grid():
  408.     mainCanvas.delete("ligne")
  409.    
  410.     # On doit pouvoir générer le bon nombre de carré (en ligne, et en colonne)
  411.     # Là, c'est bon !
  412.  
  413.  
  414.     # Génération de lignes sur toute la hauteur (haut en bas), avec un pas proportionnel aux coordonnées
  415.     for i in range(0,hauteur_bc*pas,pas):
  416.         mainCanvas.create_line([(0,i), (largeur_bc*pas,i)], tag="ligne")
  417.    
  418.    
  419.     # Génération de lignes sur toute la largeur (gauce à droite), avec un pas proportionnel aux coordonnées
  420.     for i in range(0,largeur_bc*pas,pas):
  421.         mainCanvas.create_line([(i,0), (i,hauteur_bc*pas)], tag="ligne")
  422.    
  423.  
  424. def BoutonConfirmer():
  425.     global coord1,coord2
  426.     coord1_0=int(coord1_0var.get())
  427.     coord1_1=int(coord1_1var.get())
  428.     coord2_0=int(coord2_0var.get())
  429.     coord2_1=int(coord2_1var.get())
  430.     coord1=[coord1_0,coord1_1]
  431.     coord2=[coord2_0,coord2_1]
  432.    
  433.     start_window([coord1_0,coord1_1],[coord2_0,coord2_1])
  434.  
  435. def BoutonResoudre():
  436.     mainCanvas.delete("carre")
  437.    
  438.     c1_0=int(c1_0var.get())
  439.     c1_1=int(c1_1var.get())
  440.     c2_0=int(c2_0var.get())
  441.     c2_1=int(c2_1var.get())
  442.    
  443.     c1=[c1_0,c1_1]
  444.     c2=[c2_0,c2_1]
  445.    
  446.     obs1_0=int(obs1_0var.get())
  447.     obs1_1=int(obs1_1var.get())
  448.     obs2_0=int(obs2_0var.get())
  449.     obs2_1=int(obs2_1var.get())
  450.     obs1=[obs1_0,obs1_1]
  451.     obs2=[obs2_0,obs2_1]
  452.    
  453.     obstacl = creer_mur(obs1,obs2,1)
  454.    
  455.     colorier_liste(obstacl,coord1,coord2,"red")
  456.     colorier_case(c1,coord1,coord2,"green")
  457.     colorier_case(c2,coord1,coord2,"orange")
  458.    
  459.     chemin_opti=start_algo(c1,c2,coord1,coord2,obstacl,"chemin_opti")
  460.    
  461.     colorier_liste(chemin_opti,coord1,coord2,"yellow",chemin_opti[0],chemin_opti[-1])
  462.  
  463. Niveau=Tk()
  464.  
  465. Niveau.title("Sélection des coordonnées")
  466. Niveau.resizable(width=False,height=False)
  467.  
  468. canvasniveau=Canvas(Niveau,bg="lightgray")
  469. canvasniveau.grid(row=0,column=0,rowspan=10,columnspan=10)
  470.  
  471. # Limite 1 coord x
  472. coord1_0var=IntVar()
  473. coord1_0var.set("-10")
  474. ChampCoord1_0=Entry(canvasniveau,textvariable=coord1_0var,font="Constantia 18",width=10)
  475. ChampCoord1_0.grid(row=0,column=0,padx=30,pady=30)
  476.  
  477. # Limite 1 y
  478. coord1_1var=IntVar()
  479. coord1_1var.set("-10")
  480. ChampCoord1_1=Entry(canvasniveau,textvariable=coord1_1var,font="Constantia 18",width=10)
  481. ChampCoord1_1.grid(row=0,column=1,padx=30,pady=30)
  482.  
  483. # Limite 2 x
  484. coord2_0var=IntVar()
  485. coord2_0var.set("10")
  486. ChampCoord2_0=Entry(canvasniveau,textvariable=coord2_0var,font="Constantia 18",width=10,bg="gray")
  487. ChampCoord2_0.grid(row=2,column=0,padx=30,pady=30)
  488.  
  489. # Limite 2 y
  490. coord2_1var=IntVar()
  491. coord2_1var.set("10")
  492. ChampCoord2_1=Entry(canvasniveau,textvariable=coord2_1var,font="Constantia 18",width=10,bg="gray")
  493. ChampCoord2_1.grid(row=2,column=1,padx=30,pady=30)
  494.  
  495. # Bouton confirmer
  496. Confirm=Button(canvasniveau,text="Confirmer",font="Constantia 15",justify="center",overrelief="groove",activeforeground="blue",activebackground="white",bg="white",command=BoutonConfirmer)
  497. Confirm.grid(row=3,column=0,columnspan=2,padx=10,pady=10)
  498.  
  499. # Bouton résoudre
  500. Resoudre=Button(canvasniveau,text="Résoudre",font="Constantia 15",justify="center",overrelief="groove",activeforeground="blue",activebackground="white",bg="white",command=BoutonResoudre)
  501. Resoudre.grid(row=6,column=0,rowspan=2,columnspan=2,padx=10,pady=10)
  502.  
  503.  
  504. # Début coord x
  505. c1_0var=IntVar()
  506. #c1_0var.set("début 0")
  507. c1_0var.set("-10")
  508. ChampCoord1_0=Entry(canvasniveau,textvariable=c1_0var,font="Constantia 18",width=10,bg="green")
  509. ChampCoord1_0.grid(row=4,column=0,padx=30,pady=30)
  510.  
  511. # Début coord y
  512. c1_1var=IntVar()
  513. #c1_1var.set("début 1")
  514. c1_1var.set("-10")
  515. ChampCoord1_1=Entry(canvasniveau,textvariable=c1_1var,font="Constantia 18",width=10,bg="green")
  516. ChampCoord1_1.grid(row=4,column=1,padx=30,pady=30)
  517.  
  518. # Fin coord x
  519. c2_0var=IntVar()
  520. #c2_0var.set("fin 0")
  521. c2_0var.set("10")
  522. ChampCoord2_0=Entry(canvasniveau,textvariable=c2_0var,font="Constantia 18",width=10,bg="orange")
  523. ChampCoord2_0.grid(row=6,column=0,padx=30,pady=30)
  524.  
  525. # Fin coord y
  526. c2_1var=IntVar()
  527. #c2_1var.set("fin 1")
  528. c2_1var.set("-10")
  529. ChampC2_1=Entry(canvasniveau,textvariable=c2_1var,font="Constantia 18",width=10,bg="orange")
  530. ChampC2_1.grid(row=6,column=1,padx=30,pady=30)
  531.  
  532. # Obstacle "creer_mur"
  533. obs1_0var=IntVar()
  534. #obs1_0var.set("obsdeb 0")
  535. obs1_0var.set("2")
  536. ChampCoord1_0=Entry(canvasniveau,textvariable=obs1_0var,font="Constantia 18",width=10,bg="IndianRed1")
  537. ChampCoord1_0.grid(row=7,column=0,padx=30,pady=30)
  538.  
  539. obs1_1var=IntVar()
  540. #obs1_1var.set("obsdeb 1")
  541. obs1_1var.set("-10")
  542.  
  543.  
  544. Champobs1_1=Entry(canvasniveau,textvariable=obs1_1var,font="Constantia 18",width=10,bg="IndianRed1")
  545. Champobs1_1.grid(row=7,column=1,padx=30,pady=30)
  546.  
  547. obs2_0var=IntVar()
  548. #obs2_0var.set("obsfin 0")
  549. obs2_0var.set("2")
  550.  
  551. Champobs2_0=Entry(canvasniveau,textvariable=obs2_0var,font="Constantia 18",width=10,bg="IndianRed4")
  552. Champobs2_0.grid(row=8,column=0,padx=30,pady=30)
  553.  
  554. obs2_1var=IntVar()
  555. #obs2_1var.set("obsfin 1")
  556. obs2_1var.set("0")
  557.  
  558. Champobs2_1=Entry(canvasniveau,textvariable=obs2_1var,font="Constantia 18",width=10,bg="IndianRed4")
  559. Champobs2_1.grid(row=8,column=1,padx=30,pady=30)
  560.  
  561.  
  562. def test_obstacle_bas_fonction(coord1,coord2):
  563.     xmin,ymin=coord1[0],coord1[1]
  564.     xmax,ymax=coord2[0],coord2[1]
  565.     L=labbi(coord1,coord2)
  566.     for k in range (xmin,xmax+1):
  567.         for t in range(ymin,ymax+1):
  568.             if k%2==0 or t%2==0:
  569.                 L.remove([k,t])
  570.     return(L)
  571.  
  572. truc_chelou_lol = creer_mur([1,1],[1,7]) + creer_mur([1,7],[8,7])+creer_mur([8,7],[8,1])
  573.  
  574.  
  575.  
  576. Niveau.mainloop()
  577.  
  578. ##tracer courbe bézier
  579.  
  580. #fonction qui regarde sur deux coordonnés s'ils sont adjacents ou pas, renvoie un boléen
  581. def est_adjacent(x,y):
  582.     if x[0] == y[0] + 1 or x[0] == y [0] -1 or x[0]==y[0]:
  583.         if x[1] == y[1] + 1 or x[1] == y[1] -1 or x[1]==y[1]:
  584.             return(True)
  585.         else:
  586.             return(False)
  587.     else:
  588.         return(False)
  589.     return(False)
  590.  
  591. #focntion qui détecte les portions qui touchent les obstacles
  592.  
  593. def portion_adjacente(Listcoord,obstacle):
  594.     L=[]
  595.     for i in range(0,len(Listcoord)):
  596.         I=[]
  597.         for j in range(0,len(obstacle)):
  598.             if est_adjacent(Listcoord[i], obstacle[j]):
  599.                 a=Listcoord[i]
  600.                 if a not in I:
  601.                     I.append(a)
  602.         L+=I
  603.     return(L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement