Tkinter

Modérateur : Michel Quercia

Répondre
Gandel W
Messages : 14
Enregistré le : sam. déc. 08, 2018 12:57 pm
Classe : MP

Tkinter

Message par Gandel W » ven. mai 17, 2019 3:20 pm

Bonjour,
En informatique, je dois réaliser un programme qui simule une évacuation d'avion dont une animation sera réalisé sous Tkinter. Pour mener a bien mon animation, je dois renouveler ma fonction qui représente la position des personnes dans l'avion à un instant t. Comment faire en sorte que Tkinter m’affiche graphiquement et toute les seconde où en est l'évacuation (donc le résultat de ma fonction toutes les secondes) permettant ainsi d’observer toutes les étapes de l'évacuation du début à la fin ?
Merci d'avance :)

Gandel W
Messages : 14
Enregistré le : sam. déc. 08, 2018 12:57 pm
Classe : MP

Re: Tkinter

Message par Gandel W » sam. mai 18, 2019 11:29 am

Je vous envoie mon code ( l'animation reste fixe je pense que ca vient de la fonction after qui est incorrecte )


def evacuation(L):
T=['red','light green','light blue','yellow','orange','pink']
derniers(L)
avancecolonne1(L)
avancecol1ar(L)
avancemilieu1(L)
avancemilieu2(L)
for j in range(len(L)):
for k in range(len(L[0])):
if L[j][k]==1:
m=randint(0,len(T)-1)
Terrain.create_oval((k*30)+5,(j*30)+5,(k+1)*30-5,(j+1)*30-5,width=2,fill=T[m])
Terrain.after(100,evacuation,L)
return(L)


def repet(L):
T=['red','light green','light blue','yellow','orange','pink','brown',]
S=[0]*36
decal(L)
n=0
Q=[]
R=[]
for i in range(len(L)):
Terrain.create_rectangle(60,(i*30),90,(i+1)*30,fill="white")
Terrain.create_rectangle(180,(i*30),210,(i+1)*30,fill="white")
for j in range(len(L[0])):
Terrain.create_rectangle(j*30,540,(j+1)*30,570,fill="white")
for j in range(len(L)):
for k in range(len(L[0])):
if L[j][k]==1:
m=randint(0,len(T)-1)
Terrain.create_oval((k*30)+5,(j*30)+5,(k+1)*30-5,(j+1)*30-5,width=2,fill=T[m])
for i in range(36):
Q.append(L[2])
R.append(L[6])
while Q!=S or R!=S:
evacuation(L)
print(L)
n=n+1
Q=[]
R=[]
for r in range(36):
Q.append(L[r][2])
R.append(L[r][6])
L[18][1]=0
L[18][7]=0
return(L,n+1)
print(repet(Avion))

Terrain.mainloop()

Avatar du membre
LeToucan
Messages : 14
Enregistré le : mer. mai 15, 2019 9:31 pm
Classe : Terminale

Re: Tkinter

Message par LeToucan » sam. mai 18, 2019 12:05 pm

Il manque quelques fonctions et variables (c'est quoi avancecolonne1, Terrain, L, etc ?) et je n'arrive pas à deviner l'indentation, pour remédier à cela tu pourrais joindre le fichier entier (je viens d'arriver ici donc pas sûr, mais je crois qu'on peut joindre des fichiers sur le forum).

En essayant de deviner, peut-être es-tu coincé dans une espèce de récursion infinie: je crois que quand tu appelles .after(), tu relances la fonction evacuation sans avoir modifié L. Sinon, Tkinter possède des méthodes (.move()) qui permettent de bouger des objets (au lieu de recréer les ovales à chaque fois, ce qui me semble être ce que tu es en train de faire).
TS-SVT-OIB (2018-2019) -> MPSI (2019-2020) -> ?
"If at first you don't succeed ... you fail." - Glados

Gandel W
Messages : 14
Enregistré le : sam. déc. 08, 2018 12:57 pm
Classe : MP

Re: Tkinter

Message par Gandel W » sam. mai 18, 2019 6:32 pm

Le voici : ( Si vous voulez m'envoyer du code, utilisez lorsque vous faites "répondre", l'onglet "code")

Code : Tout sélectionner

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 16:43:28 2019

@author: miste
"""
from tkinter import *
from random import randint #Nombre des sièges :32 Milieu:16
import numpy as np 
import time
Avion=np.array([[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[0.,0,0,0,0,0,0,0,0],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[4,0,0,1,1,1,0,0,4],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[0.,0,0,0,0,0,0,0,0],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1],[1.,1,0,1,1,1,0,1,1]])
print(Avion)

fenetre=Tk()
Terrain=Canvas(fenetre,height=1170,width=270,background="light grey")
Terrain.pack()
carreau=[[Terrain.create_rectangle(i*30,j*30,(i+1)*30,(j+1)*30)
                                        for i in range(9)] for j in range(37)]

Terrain.bind('<ButtonRelease>')

Coord=Label(fenetre)
Coord.pack(pady='10px')

def decal(L):
    L[18][1]=0
    L[18][2]=0
    L[18][3]=0    
    L[18][4]=0
    L[18][5]=0    #Les gens vont commencer a sortir de leur sièges
    for i in range (len(L)):
          if L[i][0]==1:
              if randint(1,2)==1:
                L[i][2]=L[i][1]
                L[i][1]=L[i][0]
                L[i][0]=0
              else :
                L[i][2]=L[i][3]
                L[i][3]=L[i][4]
                L[i][4]=0
    for i in range (len(L)):
        if L[i][8]==1:
            if randint(1,2)==1:
                L[i][6]=L[i][7]
                L[i][7]=L[i][8]
                L[i][8]=0
            else :
                L[i][6]=L[i][5]
                L[i][5]=L[i][4] 
                L[i][4]=0
    return(L)   
print(decal(Avion))
     
def avancecolonne1(L):
    from random import randint 
    L[0][2]=0
    L[0][6]=0
    for i in range(9):
                 if (L[i][1]==0 and L[i][3]!=0) and L[i+1][2]!=0:
                       if randint(1,5)==1:
                            L[i][2]=L[i][3]
                            L[i][3]=L[i][4]
                            L[i][4]=0
                            return None
                       else : 
                           L[i][2]=L[i+1][2]
                           L[i+1][2]=0
                 elif (L[i][1]!=0 and L[i][3]==0) and L[i+1][2]!=0:
                       if randint(1,5)==2:
                           L[i][2]=L[i][1]
                           L[i][1]=L[i][0]
                           L[i][0]=0
                           return None
                       else :
                           L[i][2]=L[i+1][2]
                           L[i+1][2]=0
                 elif (L[i][1]==0 and L[i][3]==0) and L[i+1][2]!=0:
                       L[i][2]=L[i+1][2]
                       L[i+1][2]=0
                 elif (L[i][1]!=0 and L[i][3]!=0) and L[i+1][2]!=0:
                     if randint(1,5)==2:
                        L[i][2]=L[i][1]
                        L[i][1]=L[i][0]
                        L[i][0]=0
                        return None
                     elif randint(1,5)==3:
                       L[i][2]=L[i][3]
                       L[i][3]=L[i][4]
                       L[i][4]=0
                       return None
                     else:
                       L[i][2]=L[i+1][2]
                       L[i+1][2]=0
                 elif (L[i][1]==0 and L[i][3]!=0) and L[i+1][2]==0:
                            L[i][2]=L[i][3]
                            L[i][3]=L[i][4]
                            L[i][4]=0
                 elif (L[i][1]!=0 and L[i][3]==0) and L[i+1][2]==0:
                           L[i][2]=L[i][1]
                           L[i][1]=L[i][0]
                           L[i][0]=0
                 elif (L[i][1]!=0 and L[i][3]!=0) and L[i+1][2]==0:
                     if randint(1,2)==2:
                        L[i][2]=L[i][3]
                        L[i][3]=L[i][4]
                        L[i][4]=0
                     else:
                       L[i][2]=L[i][1]
                       L[i][1]=L[i][0]
                       L[i][0]=0
    for i in range(9):            
                 if  (L[i][5]==0 and L[i][7]!=0) and L[i+1][6]!=0:
                       if randint(1,5)==1:
                            L[i][6]=L[i][7]
                            L[i][7]=L[i][8]
                            L[i][8]=0
                            return None
                       else : 
                           L[i][6]=L[i+1][6]
                           L[i+1][6]=0
                 elif (L[i][5]!=0 and L[i][7]==0) and L[i+1][6]!=0:
                       if randint(1,5)==2:
                           L[i][6]=L[i][5]
                           L[i][5]=L[i][4]
                           L[i][4]=0
                           return None
                       else :
                           L[i][6]=L[i+1][6]
                           L[i+1][6]=0
                 elif (L[i][5]==0 and L[i][7]==0) and L[i+1][6]!=0:
                      L[i][6]=L[i+1][6]
                      L[i+1][6]=0
                 elif (L[i][5]!=0 and L[i][7]!=0) and L[i+1][6]!=0:
                     if randint(1,5)==2:
                        L[i][6]=L[i][7]
                        L[i][7]=L[i][8]
                        L[i][8]=0
                        return None
                     elif randint(1,5)==3:
                       L[i][6]=L[i][5]
                       L[i][5]=L[i][4]
                       L[i][4]=0
                       return None
                     else:
                       L[i][6]=L[i+1][6]
                       L[i+1][6]=0
                 elif (L[i][5]==0 and L[i][7]!=0) and L[i+1][6]==0:
                            L[i][6]=L[i][7]
                            L[i][7]=L[i][8]
                            L[i][8]=0
                 elif (L[i][5]!=0 and L[i][7]==0) and L[i+1][6]==0:
                           L[i][6]=L[i][5]
                           L[i][5]=L[i][4]
                           L[i][4]=0
                 elif (L[i][5]!=0 and L[i][7]!=0) and L[i+1][6]==0:
                     if randint(1,2)==2:
                        L[i][6]=L[i][5]
                        L[i][5]=L[i][4]
                        L[i][4]=0
                     else:
                       L[i][6]=L[i][7]
                       L[i][7]=L[i][8]
                       L[i][8]=0
    return(L)

def derniers(L):
   if L[9][2]==0:
        if randint(1,2)==1:
            L[9][2]=L[9][1]
            L[9][1]=L[9][0]
            L[9][0]=0
        else : 
            L[9][2]=L[9][3]
            L[9][3]=L[9][4]
            L[9][4]=0
   if L[11][2]==0:
        if randint(1,2)==1:
            L[11][2]=L[11][1]
            L[11][1]=L[11][0]
            L[11][0]=0
        else : 
            L[11][2]=L[11][3]
            L[11][3]=L[11][4]
            L[11][4]=0
   if L[11][6]==0:
        if randint(1,2)==1:
            L[11][6]=L[11][5]
            L[11][5]=L[11][4]
            L[11][4]=0
        else : 
            L[11][6]=L[11][7]
            L[11][7]=L[11][8]
            L[11][8]=0
   if L[9][6]==0:
        if randint(1,2)==1:
            L[9][6]=L[9][5]
            L[9][5]=L[9][4]
            L[9][4]=0
        else : 
            L[9][6]=L[9][7]
            L[9][7]=L[9][8]
            L[9][8]=0
   if L[-10][2]==0:
        if randint(1,2)==1:
            L[-10][2]=L[-10][1]
            L[-10][1]=L[-10][0]
            L[-10][0]=0
        else : 
            L[-10][2]=L[-10][3]
            L[-10][3]=L[-10][4]
            L[-10][4]=0
   if L[-10][6]==0:
        if randint(1,2)==1:
            L[-10][6]=L[-10][7]
            L[-10][7]=L[-10][8]
            L[-10][8]=0
        else : 
            L[-10][6]=L[-10][5]
            L[-10][5]=L[-10][4]
            L[-10][4]=0
   if L[-12][2]==0:
        if randint(1,2)==1:
            L[-12][2]=L[-12][1]
            L[-12][1]=L[-12][0]
            L[-12][0]=0
        else : 
            L[-12][2]=L[-12][3]
            L[-12][3]=L[-12][4]
            L[-12][4]=0
   if L[-12][6]==0:
        if randint(1,2)==1:
            L[-12][6]=L[-12][5]
            L[-12][5]=L[-12][4]
            L[-12][4]=0
        else : 
            L[-12][6]=L[-12][7]
            L[-12][7]=L[-12][8]
            L[-12][8]=0
   return(L)
              
def avancecol1ar(L):
    from random import randint
    L[36][2]=0
    L[36][6]=0
    for i in range(1,10):
                 if (L[-i][1]==0 and L[-i][3]!=0) and L[-(i+1)][2]!=0:
                       if randint(1,5)==1:
                            L[-i][2]=L[-i][3]
                            L[-i][3]=L[-i][4]
                            L[-i][4]=0
                            return None
                       else : 
                           L[-i][2]=L[-(i+1)][2]
                           L[-(i+1)][2]=0
                 elif (L[-i][1]!=0 and L[-i][3]==0) and L[-(i+1)][2]!=0:
                       if randint(1,5)==2:
                           L[-i][2]=L[-i][1]
                           L[-i][1]=L[-i][0]
                           L[-i][0]=0
                           return None
                       else :
                           L[-i][2]=L[-(i+1)][2]
                           L[-(i+1)][2]=0
                 elif (L[-i][1]==0 and L[-i][3]==0) and L[-(i+1)][2]!=0:
                     L[-i][2]=L[-(i+1)][2]
                     L[-(i+1)][2]=0
                 elif (L[-i][1]!=0 and L[-i][3]!=0) and L[-(i+1)][2]!=0:
                     if randint(1,5)==2:
                        L[-i][2]=L[-i][1]
                        L[-i][1]=L[-i][0]
                        L[-i][0]=0
                        return None
                     elif randint(1,5)==3:
                       L[-i][2]=L[-i][3]
                       L[-i][3]=L[-i][4]
                       L[-i][4]=0
                       return None
                     else:
                       L[-i][2]=L[-(i+1)][2]
                       L[-(i+1)][2]=0
                 elif (L[-i][1]==0 and L[-i][3]!=0) and L[-(i+1)][2]==0:
                            L[-i][2]=L[-i][3]
                            L[-i][3]=L[-i][4]
                            L[-i][4]=0
                 elif (L[-i][1]!=0 and L[-i][3]==0) and L[i+1][2]==0:
                           L[-i][2]=L[-i][1]
                           L[-i][1]=L[-i][0]
                           L[-i][0]=0
                 elif (L[-i][1]!=0 and L[-i][3]!=0) and L[-(i+1)][2]==0:
                     if randint(1,2)==2:
                        L[-i][2]=L[-i][3]
                        L[-i][3]=L[-i][4]
                        L[-i][4]=0
                     else:
                       L[i][2]=L[i][1]
                       L[i][1]=L[i][0]
                       L[i][0]=0
    for i in range(1,10):
                 if (L[-i][5]==0 and L[-i][7]!=0) and L[-(i+1)][6]!=0:
                       if randint(1,5)==1:
                            L[-i][6]=L[-i][7]
                            L[-i][7]=L[-i][8]
                            L[-i][8]=0
                            return None
                       else : 
                           L[-i][6]=L[-(i+1)][6]
                           L[-(i+1)][6]=0
                 elif (L[-i][5]!=0 and L[-i][7]==0) and L[-(i+1)][6]!=0:
                       if randint(1,5)==2:
                           L[-i][6]=L[-i][5]
                           L[-i][5]=L[-i][4]
                           L[-i][4]=0
                           return None
                       else :
                           L[-i][6]=L[-(i+1)][6]
                           L[-(i+1)][6]=0
                 elif (L[-i][5]==0 and L[-i][7]==0) and L[-(i+1)][6]!=0:
                     L[-i][6]=L[-(i+1)][6]
                     L[-(i+1)][6]=0
                 elif (L[-i][5]!=0 and L[-i][7]!=0) and L[-(i+1)][6]!=0:
                     if randint(1,5)==2:
                        L[-i][6]=L[-i][7]
                        L[-i][7]=L[-i][8]
                        L[-i][8]=0
                        return None
                     elif randint(1,5)==3:
                       L[-i][6]=L[-i][5]
                       L[-i][5]=L[-i][4]
                       L[-i][4]=0
                       return None
                     else:
                       L[-i][6]=L[-(i+1)][6]
                       L[-(i+1)][6]=0
                 elif (L[-i][5]==0 and L[-i][7]!=0) and L[-(i+1)][6]==0:
                            L[-i][6]=L[-i][7]
                            L[-i][7]=L[-i][8]
                            L[-i][8]=0
                 elif (L[-i][5]!=0 and L[-i][7]==0) and L[-(i+1)][6]==0:
                           L[-i][6]=L[-i][5]
                           L[-i][5]=L[-i][4]
                           L[-i][4]=0
                 elif (L[-i][5]!=0 and L[-i][7]!=0) and L[-(i+1)][6]==0:
                     if randint(1,2)==2:
                        L[-i][6]=L[-i][5]
                        L[-i][5]=L[-i][4]
                        L[-i][4]=0
                     else:
                       L[-i][6]=L[-i][7]
                       L[-i][7]=L[-i][8]
                       L[-i][8]=0
                 
    return L

def avancemilieu1(L):
    L[18][1]=0  
    from random import randint   
    if randint (1,2)==1:
            L[18][1]=L[18][2]
            L[18][2]=L[17][2]
            L[17][2]=0
            for i in range(20,26):
              if (L[-i][1]==0 and L[-i][3]!=0) and L[-(i+1)][2]!=0:
                       if randint(1,5)==1:
                            L[-i][2]=L[-i][3]
                            L[-i][3]=L[-i][4]
                            L[-i][4]=0
                            return None
                       else : 
                           L[-i][2]=L[-(i+1)][2]
                           L[-(i+1)][2]=0
              elif (L[-i][1]!=0 and L[-i][3]==0) and L[-(i+1)][2]!=0:
                       if randint(1,5)==2:
                           L[-i][2]=L[-i][1]
                           L[-i][1]=L[-i][0]
                           L[-i][0]=0
                           return None
                       else :
                           L[-i][2]=L[-(i+1)][2]
                           L[-(i+1)][2]=0
              elif (L[-i][1]==0 and L[-i][3]==0) and L[-(i+1)][2]!=0:
                     L[-i][2]=L[-(i+1)][2]
                     L[-(i+1)][2]=0
              elif (L[-i][1]!=0 and L[-i][3]!=0) and L[-(i+1)][2]!=0:
                     if randint(1,5)==2:
                        L[-i][2]=L[-i][1]
                        L[-i][1]=L[-i][0]
                        L[-i][0]=0
                        return None
                     elif randint(1,5)==3:
                       L[-i][2]=L[-i][3]
                       L[-i][3]=L[-i][4]
                       L[-i][4]=0
                       return None
                     else:
                       L[-i][2]=L[-(i+1)][2]
                       L[-(i+1)][2]=0
              elif (L[-i][1]==0 and L[-i][3]!=0) and L[-(i+1)][2]==0:
                            L[-i][2]=L[-i][3]
                            L[-i][3]=L[-i][4]
                            L[-i][4]=0
              elif (L[-i][1]!=0 and L[-i][3]==0) and L[i+1][2]==0:
                           L[-i][2]=L[-i][1]
                           L[-i][1]=L[-i][0]
                           L[-i][0]=0
              elif (L[-i][1]!=0 and L[-i][3]!=0) and L[-(i+1)][2]==0:
                     if randint(1,2)==2:
                        L[-i][2]=L[-i][3]
                        L[-i][3]=L[-i][4]
                        L[-i][4]=0
                     else:
                       L[i][2]=L[i][1]
                       L[i][1]=L[i][0]
                       L[i][0]=0
    else :
              L[18][1]=L[18][2]
              L[18][2]=L[19][2]
              L[19][2]=0
              for i in range(19,25):
                 if (L[i][1]==0 and L[i][3]!=0) and L[i+1][2]!=0:
                       if randint(1,5)==1:
                            L[i][2]=L[i][3]
                            L[i][3]=L[i][4]
                            L[i][4]=0
                            return None
                       else : 
                           L[i][2]=L[i+1][2]
                           L[i+1][2]=0
                 elif (L[i][1]!=0 and L[i][3]==0) and L[i+1][2]!=0:
                       if randint(1,5)==2:
                           L[i][2]=L[i][1]
                           L[i][1]=L[i][0]
                           L[i][0]=0
                           return None
                       else :
                           L[i][2]=L[i+1][2]
                           L[i+1][2]=0
                 elif (L[i][1]==0 and L[i][3]==0) and L[i+1][2]!=0:
                     L[i][2]=L[i+1][2]
                     L[i+1][2]=0
                 elif (L[i][1]!=0 and L[i][3]!=0) and L[i+1][2]!=0:
                     if randint(1,5)==2:
                        L[i][2]=L[i][1]
                        L[i][1]=L[i][0]
                        L[i][0]=0
                        return None
                     elif randint(1,5)==3:
                       L[i][2]=L[i][3]
                       L[i][3]=L[i][4]
                       L[i][4]=0
                       return None
                     else:
                       L[i][2]=L[i+1][2]
                       L[i+1][2]=0
                 elif (L[i][1]==0 and L[i][3]!=0) and L[i+1][2]==0:
                            L[i][2]=L[i][3]
                            L[i][3]=L[i][4]
                            L[i][4]=0
                 elif (L[i][1]!=0 and L[i][3]==0) and L[i+1][2]==0:
                           L[i][2]=L[i][1]
                           L[i][1]=L[i][0]
                           L[i][0]=0
                 elif (L[i][1]!=0 and L[i][3]!=0) and L[i+1][2]==0:
                     if randint(1,2)==2:
                        L[i][2]=L[i][3]
                        L[i][3]=L[i][4]
                        L[i][4]=0
                     else:
                       L[i][2]=L[i][1]
                       L[i][1]=L[i][0]
                       L[i][0]=0
    return(L)
    
def avancemilieu2(L):
    L[18][7]=0  
    from random import randint   
    if randint (1,2)==1:
            L[18][7]=L[18][6]
            L[17][6]=L[17][6]
            L[17][6]=0
            for i in range(20,26):
              if (L[-i][5]==0 and L[-i][7]!=0) and L[-(i+1)][6]!=0:
                       if randint(1,5)==1:
                            L[-i][6]=L[-i][7]
                            L[-i][7]=L[-i][8]
                            L[-i][8]=0
                            return None
                       else : 
                           L[-i][6]=L[-(i+1)][6]
                           L[-(i+1)][6]=0
              elif (L[-i][5]!=0 and L[-i][7]==0) and L[-(i+1)][6]!=0:
                       if randint(1,5)==2:
                           L[-i][6]=L[-i][5]
                           L[-i][5]=L[-i][4]
                           L[-i][4]=0
                           return None
                       else :
                           L[-i][6]=L[-(i+1)][6]
                           L[-(i+1)][6]=0
              elif (L[-i][5]==0 and L[-i][7]==0) and L[-(i+1)][6]!=0:
                     L[-i][6]=L[-(i+1)][6]
                     L[-(i+1)][6]=0
              elif (L[-i][5]!=0 and L[-i][7]!=0) and L[-(i+1)][6]!=0:
                     if randint(1,5)==2:
                        L[-i][6]=L[-i][5]
                        L[-i][5]=L[-i][4]
                        L[-i][4]=0
                        return None
                     elif randint(1,5)==3:
                       L[-i][6]=L[-i][7]
                       L[-i][7]=L[-i][8]
                       L[-i][8]=0
                       return None
                     else:
                       L[-i][6]=L[-(i+1)][6]
                       L[-(i+1)][6]=0
              elif (L[-i][5]==0 and L[-i][7]!=0) and L[-(i+1)][6]==0:
                            L[-i][6]=L[-i][7]
                            L[-i][7]=L[-i][8]
                            L[-i][8]=0
              elif (L[-i][5]!=0 and L[-i][7]==0) and L[-(i+1)][6]==0:
                           L[-i][6]=L[-i][5]
                           L[-i][5]=L[-i][4]
                           L[-i][4]=0
              elif (L[-i][5]!=0 and L[-i][7]!=0) and L[-(i+1)][6]==0:
                     if randint(1,2)==2:
                        L[-i][6]=L[-i][5]
                        L[-i][5]=L[-i][4]
                        L[-i][4]=0
                     else:
                       L[-i][6]=L[-i][7]
                       L[-i][7]=L[-i][8]
                       L[-i][8]=0
    else :
              L[18][7]=L[18][6]
              L[18][6]=L[19][6]
              L[19][6]=0
              for i in range(19,25):
                 if (L[i][5]==0 and L[i][7]!=0) and L[i+1][6]!=0:
                       if randint(1,5)==1:
                            L[i][6]=L[i][7]
                            L[i][7]=L[i][8]
                            L[i][8]=0
                            return None
                       else : 
                           L[i][6]=L[i+1][6]
                           L[i+1][6]=0
                 elif (L[i][5]!=0 and L[i][7]==0) and L[i+1][6]!=0:
                       if randint(1,5)==2:
                           L[i][6]=L[i][5]
                           L[i][5]=L[i][4]
                           L[i][4]=0
                           return None
                       else :
                           L[i][6]=L[i+1][6]
                           L[i+1][6]=0
                 elif (L[i][5]==0 and L[i][7]==0) and L[i+1][6]!=0:
                     L[i][6]=L[i+1][6]
                     L[i+1][6]=0
                 elif (L[i][5]!=0 and L[i][7]!=0) and L[i+1][6]!=0:
                     if randint(1,5)==2:
                        L[i][6]=L[i][5]
                        L[i][5]=L[i][4]
                        L[i][4]=0
                        return None
                     elif randint(1,5)==3:
                       L[i][6]=L[i][7]
                       L[i][7]=L[i][8]
                       L[i][8]=0
                       return None
                     else:
                       L[i][6]=L[i+1][6]
                       L[i+1][6]=0
                 elif (L[i][5]==0 and L[i][7]!=0) and L[i+1][6]==0:
                            L[i][6]=L[i][7]
                            L[i][7]=L[i][8]
                            L[i][8]=0
                 elif (L[i][5]!=0 and L[i][7]==0) and L[i+1][6]==0:
                           L[i][6]=L[i][5]
                           L[i][5]=L[i][4]
                           L[i][4]=0
                 elif (L[i][5]!=0 and L[i][7]!=0) and L[i+1][6]==0:
                     if randint(1,2)==2:
                        L[i][6]=L[i][5]
                        L[i][5]=L[i][4]
                        L[i][4]=0
                     else:
                       L[i][6]=L[i][7]
                       L[i][7]=L[i][8]
                       L[i][8]=0
                 
    return(L)


def evacuation(L):
     T=['red','light green','light blue','yellow','orange','pink']
     derniers(L)
     avancecolonne1(L)
     avancecol1ar(L)
     avancemilieu1(L)
     avancemilieu2(L)
     for j in range(len(L)):
               for k in range(len(L[0])):
                   if L[j][k]==1:
                      m=randint(0,len(T)-1)
                      Terrain.create_oval((k*30)+5,(j*30)+5,(k+1)*30-5,(j+1)*30-5,width=2,fill=T[m])
     Terrain.after(100,evacuation,L)
     return(L)

                   
def repet(L):
    T=['red','light green','light blue','yellow','orange','pink','brown',]
    S=[0]*36
    decal(L)
    n=0
    Q=[]
    R=[]
    for i in range(len(L)):
        Terrain.create_rectangle(60,(i*30),90,(i+1)*30,fill="white")
        Terrain.create_rectangle(180,(i*30),210,(i+1)*30,fill="white")
    for j in range(len(L[0])):
        Terrain.create_rectangle(j*30,540,(j+1)*30,570,fill="white")
    for j in range(len(L)):
               for k in range(len(L[0])):
                   if L[j][k]==1:
                      m=randint(0,len(T)-1)
                      Terrain.create_oval((k*30)+5,(j*30)+5,(k+1)*30-5,(j+1)*30-5,width=2,fill=T[m])
    for i in range(36):
        Q.append(L[i][2])
        R.append(L[i][6])
    while Q!=S or R!=S:
           evacuation(L)
           print(L)
           n=n+1
           Q=[]
           R=[]
           for r in range(36):             
              Q.append(L[r][2])
              R.append(L[r][6])             
    L[18][1]=0
    L[18][7]=0  
    return(L,n+1)
print(repet(Avion))

Terrain.mainloop() 


Avatar du membre
LeToucan
Messages : 14
Enregistré le : mer. mai 15, 2019 9:31 pm
Classe : Terminale

Re: Tkinter

Message par LeToucan » sam. mai 18, 2019 8:35 pm

Voici ce que j'ai changé pour obtenir l'animation seconde par seconde:
  • Tout d'abord, les fonctions de mouvement (avance*, derniers, etc.) modifiaient en leur sein la liste L, cependant elles ne retournaient que parfois cette valeur, c'est à dire que les modifications n'étaient jamais reflétées sur la variable globale L. Pour régler ça, il suffit de remplaçer tous les "return None" par des "return L".
  • Ensuite pour utiliser cette nouvelle valeur, il ne faut pas faire:

    Code : Tout sélectionner

    derniers(L)
    avancecolonne1(L)
    avancecol1ar(L)
    avancemilieu1(L)
    avancemilieu2(L)
    mais

    Code : Tout sélectionner

    L = avancemilieu1(avancemilieu1(avancecol1ar(avancecolonne1(derniers(L)))))
    Ce qui assigne à L la nouvelle valeur, produit de ces 5 transformations.
  • Maintenant que L évolue, ne sachant pas comment utiliser .after() pour animer j'ai juste appelé Terrain.update() après avoir spawné les ellipses (suivi d'un délai de 1s)
  • Enfin je me suis rendu compte qu'il fallait effacer les anciennes ellipses, donc je les ai mises dans une liste (passagers) et je les efface toutes avant de les redessiner dans les nouvelles positions.
Code final:

Code : Tout sélectionner

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 16:43:28 2019

@author: miste
"""
from tkinter import *
from random import randint  # Nombre des sièges :32 Milieu:16
import numpy as np
import time

Avion = np.array([[1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [0., 0, 0, 0, 0, 0, 0, 0, 0], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [4, 0, 0, 1, 1, 1, 0, 0, 4], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [0., 0, 0, 0, 0, 0, 0, 0, 0],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1], [1., 1, 0, 1, 1, 1, 0, 1, 1],
                  [1., 1, 0, 1, 1, 1, 0, 1, 1]])
print(Avion)

fenetre = Tk()
Terrain = Canvas(fenetre, height=1170, width=270, background="light grey")
Terrain.pack()
carreau = [[Terrain.create_rectangle(i * 30, j * 30, (i + 1) * 30, (j + 1) * 30)
            for i in range(9)] for j in range(37)]

Terrain.bind('<ButtonRelease>')
passagers = []
Coord = Label(fenetre)
Coord.pack(pady='10px')


def decal(L):
    L[18][1] = 0
    L[18][2] = 0
    L[18][3] = 0
    L[18][4] = 0
    L[18][5] = 0  # Les gens vont commencer a sortir de leur sièges
    for i in range(len(L)):
        if L[i][0] == 1:
            if randint(1, 2) == 1:
                L[i][2] = L[i][1]
                L[i][1] = L[i][0]
                L[i][0] = 0
            else:
                L[i][2] = L[i][3]
                L[i][3] = L[i][4]
                L[i][4] = 0
    for i in range(len(L)):
        if L[i][8] == 1:
            if randint(1, 2) == 1:
                L[i][6] = L[i][7]
                L[i][7] = L[i][8]
                L[i][8] = 0
            else:
                L[i][6] = L[i][5]
                L[i][5] = L[i][4]
                L[i][4] = 0
    return L


print(decal(Avion))


def avancecolonne1(L):
    L[0][2] = 0
    L[0][6] = 0
    for i in range(9):
        if (L[i][1] == 0 and L[i][3] != 0) and L[i + 1][2] != 0:
            if randint(1, 5) == 1:
                L[i][2] = L[i][3]
                L[i][3] = L[i][4]
                L[i][4] = 0
                return L
            else:
                L[i][2] = L[i + 1][2]
                L[i + 1][2] = 0
        elif (L[i][1] != 0 and L[i][3] == 0) and L[i + 1][2] != 0:
            if randint(1, 5) == 2:
                L[i][2] = L[i][1]
                L[i][1] = L[i][0]
                L[i][0] = 0
                return L
            else:
                L[i][2] = L[i + 1][2]
                L[i + 1][2] = 0
        elif (L[i][1] == 0 and L[i][3] == 0) and L[i + 1][2] != 0:
            L[i][2] = L[i + 1][2]
            L[i + 1][2] = 0
        elif (L[i][1] != 0 and L[i][3] != 0) and L[i + 1][2] != 0:
            if randint(1, 5) == 2:
                L[i][2] = L[i][1]
                L[i][1] = L[i][0]
                L[i][0] = 0
                return L
            elif randint(1, 5) == 3:
                L[i][2] = L[i][3]
                L[i][3] = L[i][4]
                L[i][4] = 0
                return L
            else:
                L[i][2] = L[i + 1][2]
                L[i + 1][2] = 0
        elif (L[i][1] == 0 and L[i][3] != 0) and L[i + 1][2] == 0:
            L[i][2] = L[i][3]
            L[i][3] = L[i][4]
            L[i][4] = 0
        elif (L[i][1] != 0 and L[i][3] == 0) and L[i + 1][2] == 0:
            L[i][2] = L[i][1]
            L[i][1] = L[i][0]
            L[i][0] = 0
        elif (L[i][1] != 0 and L[i][3] != 0) and L[i + 1][2] == 0:
            if randint(1, 2) == 2:
                L[i][2] = L[i][3]
                L[i][3] = L[i][4]
                L[i][4] = 0
            else:
                L[i][2] = L[i][1]
                L[i][1] = L[i][0]
                L[i][0] = 0
    for i in range(9):
        if (L[i][5] == 0 and L[i][7] != 0) and L[i + 1][6] != 0:
            if randint(1, 5) == 1:
                L[i][6] = L[i][7]
                L[i][7] = L[i][8]
                L[i][8] = 0
                return L
            else:
                L[i][6] = L[i + 1][6]
                L[i + 1][6] = 0
        elif (L[i][5] != 0 and L[i][7] == 0) and L[i + 1][6] != 0:
            if randint(1, 5) == 2:
                L[i][6] = L[i][5]
                L[i][5] = L[i][4]
                L[i][4] = 0
                return L
            else:
                L[i][6] = L[i + 1][6]
                L[i + 1][6] = 0
        elif (L[i][5] == 0 and L[i][7] == 0) and L[i + 1][6] != 0:
            L[i][6] = L[i + 1][6]
            L[i + 1][6] = 0
        elif (L[i][5] != 0 and L[i][7] != 0) and L[i + 1][6] != 0:
            if randint(1, 5) == 2:
                L[i][6] = L[i][7]
                L[i][7] = L[i][8]
                L[i][8] = 0
                return L
            elif randint(1, 5) == 3:
                L[i][6] = L[i][5]
                L[i][5] = L[i][4]
                L[i][4] = 0
                return L
            else:
                L[i][6] = L[i + 1][6]
                L[i + 1][6] = 0
        elif (L[i][5] == 0 and L[i][7] != 0) and L[i + 1][6] == 0:
            L[i][6] = L[i][7]
            L[i][7] = L[i][8]
            L[i][8] = 0
        elif (L[i][5] != 0 and L[i][7] == 0) and L[i + 1][6] == 0:
            L[i][6] = L[i][5]
            L[i][5] = L[i][4]
            L[i][4] = 0
        elif (L[i][5] != 0 and L[i][7] != 0) and L[i + 1][6] == 0:
            if randint(1, 2) == 2:
                L[i][6] = L[i][5]
                L[i][5] = L[i][4]
                L[i][4] = 0
            else:
                L[i][6] = L[i][7]
                L[i][7] = L[i][8]
                L[i][8] = 0
    return L


def derniers(L):
    if L[9][2] == 0:
        if randint(1, 2) == 1:
            L[9][2] = L[9][1]
            L[9][1] = L[9][0]
            L[9][0] = 0
        else:
            L[9][2] = L[9][3]
            L[9][3] = L[9][4]
            L[9][4] = 0
    if L[11][2] == 0:
        if randint(1, 2) == 1:
            L[11][2] = L[11][1]
            L[11][1] = L[11][0]
            L[11][0] = 0
        else:
            L[11][2] = L[11][3]
            L[11][3] = L[11][4]
            L[11][4] = 0
    if L[11][6] == 0:
        if randint(1, 2) == 1:
            L[11][6] = L[11][5]
            L[11][5] = L[11][4]
            L[11][4] = 0
        else:
            L[11][6] = L[11][7]
            L[11][7] = L[11][8]
            L[11][8] = 0
    if L[9][6] == 0:
        if randint(1, 2) == 1:
            L[9][6] = L[9][5]
            L[9][5] = L[9][4]
            L[9][4] = 0
        else:
            L[9][6] = L[9][7]
            L[9][7] = L[9][8]
            L[9][8] = 0
    if L[-10][2] == 0:
        if randint(1, 2) == 1:
            L[-10][2] = L[-10][1]
            L[-10][1] = L[-10][0]
            L[-10][0] = 0
        else:
            L[-10][2] = L[-10][3]
            L[-10][3] = L[-10][4]
            L[-10][4] = 0
    if L[-10][6] == 0:
        if randint(1, 2) == 1:
            L[-10][6] = L[-10][7]
            L[-10][7] = L[-10][8]
            L[-10][8] = 0
        else:
            L[-10][6] = L[-10][5]
            L[-10][5] = L[-10][4]
            L[-10][4] = 0
    if L[-12][2] == 0:
        if randint(1, 2) == 1:
            L[-12][2] = L[-12][1]
            L[-12][1] = L[-12][0]
            L[-12][0] = 0
        else:
            L[-12][2] = L[-12][3]
            L[-12][3] = L[-12][4]
            L[-12][4] = 0
    if L[-12][6] == 0:
        if randint(1, 2) == 1:
            L[-12][6] = L[-12][5]
            L[-12][5] = L[-12][4]
            L[-12][4] = 0
        else:
            L[-12][6] = L[-12][7]
            L[-12][7] = L[-12][8]
            L[-12][8] = 0
    return L


def avancecol1ar(L):
    from random import randint
    L[36][2] = 0
    L[36][6] = 0
    for i in range(1, 10):
        if (L[-i][1] == 0 and L[-i][3] != 0) and L[-(i + 1)][2] != 0:
            if randint(1, 5) == 1:
                L[-i][2] = L[-i][3]
                L[-i][3] = L[-i][4]
                L[-i][4] = 0
                return L
            else:
                L[-i][2] = L[-(i + 1)][2]
                L[-(i + 1)][2] = 0
        elif (L[-i][1] != 0 and L[-i][3] == 0) and L[-(i + 1)][2] != 0:
            if randint(1, 5) == 2:
                L[-i][2] = L[-i][1]
                L[-i][1] = L[-i][0]
                L[-i][0] = 0
                return L
            else:
                L[-i][2] = L[-(i + 1)][2]
                L[-(i + 1)][2] = 0
        elif (L[-i][1] == 0 and L[-i][3] == 0) and L[-(i + 1)][2] != 0:
            L[-i][2] = L[-(i + 1)][2]
            L[-(i + 1)][2] = 0
        elif (L[-i][1] != 0 and L[-i][3] != 0) and L[-(i + 1)][2] != 0:
            if randint(1, 5) == 2:
                L[-i][2] = L[-i][1]
                L[-i][1] = L[-i][0]
                L[-i][0] = 0
                return L
            elif randint(1, 5) == 3:
                L[-i][2] = L[-i][3]
                L[-i][3] = L[-i][4]
                L[-i][4] = 0
                return L
            else:
                L[-i][2] = L[-(i + 1)][2]
                L[-(i + 1)][2] = 0
        elif (L[-i][1] == 0 and L[-i][3] != 0) and L[-(i + 1)][2] == 0:
            L[-i][2] = L[-i][3]
            L[-i][3] = L[-i][4]
            L[-i][4] = 0
        elif (L[-i][1] != 0 and L[-i][3] == 0) and L[i + 1][2] == 0:
            L[-i][2] = L[-i][1]
            L[-i][1] = L[-i][0]
            L[-i][0] = 0
        elif (L[-i][1] != 0 and L[-i][3] != 0) and L[-(i + 1)][2] == 0:
            if randint(1, 2) == 2:
                L[-i][2] = L[-i][3]
                L[-i][3] = L[-i][4]
                L[-i][4] = 0
            else:
                L[i][2] = L[i][1]
                L[i][1] = L[i][0]
                L[i][0] = 0
    for i in range(1, 10):
        if (L[-i][5] == 0 and L[-i][7] != 0) and L[-(i + 1)][6] != 0:
            if randint(1, 5) == 1:
                L[-i][6] = L[-i][7]
                L[-i][7] = L[-i][8]
                L[-i][8] = 0
                return L
            else:
                L[-i][6] = L[-(i + 1)][6]
                L[-(i + 1)][6] = 0
        elif (L[-i][5] != 0 and L[-i][7] == 0) and L[-(i + 1)][6] != 0:
            if randint(1, 5) == 2:
                L[-i][6] = L[-i][5]
                L[-i][5] = L[-i][4]
                L[-i][4] = 0
                return L
            else:
                L[-i][6] = L[-(i + 1)][6]
                L[-(i + 1)][6] = 0
        elif (L[-i][5] == 0 and L[-i][7] == 0) and L[-(i + 1)][6] != 0:
            L[-i][6] = L[-(i + 1)][6]
            L[-(i + 1)][6] = 0
        elif (L[-i][5] != 0 and L[-i][7] != 0) and L[-(i + 1)][6] != 0:
            if randint(1, 5) == 2:
                L[-i][6] = L[-i][7]
                L[-i][7] = L[-i][8]
                L[-i][8] = 0
                return L
            elif randint(1, 5) == 3:
                L[-i][6] = L[-i][5]
                L[-i][5] = L[-i][4]
                L[-i][4] = 0
                return L
            else:
                L[-i][6] = L[-(i + 1)][6]
                L[-(i + 1)][6] = 0
        elif (L[-i][5] == 0 and L[-i][7] != 0) and L[-(i + 1)][6] == 0:
            L[-i][6] = L[-i][7]
            L[-i][7] = L[-i][8]
            L[-i][8] = 0
        elif (L[-i][5] != 0 and L[-i][7] == 0) and L[-(i + 1)][6] == 0:
            L[-i][6] = L[-i][5]
            L[-i][5] = L[-i][4]
            L[-i][4] = 0
        elif (L[-i][5] != 0 and L[-i][7] != 0) and L[-(i + 1)][6] == 0:
            if randint(1, 2) == 2:
                L[-i][6] = L[-i][5]
                L[-i][5] = L[-i][4]
                L[-i][4] = 0
            else:
                L[-i][6] = L[-i][7]
                L[-i][7] = L[-i][8]
                L[-i][8] = 0

    return L


def avancemilieu1(L):
    L[18][1] = 0
    from random import randint
    if randint(1, 2) == 1:
        L[18][1] = L[18][2]
        L[18][2] = L[17][2]
        L[17][2] = 0
        for i in range(20, 26):
            if (L[-i][1] == 0 and L[-i][3] != 0) and L[-(i + 1)][2] != 0:
                if randint(1, 5) == 1:
                    L[-i][2] = L[-i][3]
                    L[-i][3] = L[-i][4]
                    L[-i][4] = 0
                    return L
                else:
                    L[-i][2] = L[-(i + 1)][2]
                    L[-(i + 1)][2] = 0
            elif (L[-i][1] != 0 and L[-i][3] == 0) and L[-(i + 1)][2] != 0:
                if randint(1, 5) == 2:
                    L[-i][2] = L[-i][1]
                    L[-i][1] = L[-i][0]
                    L[-i][0] = 0
                    return L
                else:
                    L[-i][2] = L[-(i + 1)][2]
                    L[-(i + 1)][2] = 0
            elif (L[-i][1] == 0 and L[-i][3] == 0) and L[-(i + 1)][2] != 0:
                L[-i][2] = L[-(i + 1)][2]
                L[-(i + 1)][2] = 0
            elif (L[-i][1] != 0 and L[-i][3] != 0) and L[-(i + 1)][2] != 0:
                if randint(1, 5) == 2:
                    L[-i][2] = L[-i][1]
                    L[-i][1] = L[-i][0]
                    L[-i][0] = 0
                    return L
                elif randint(1, 5) == 3:
                    L[-i][2] = L[-i][3]
                    L[-i][3] = L[-i][4]
                    L[-i][4] = 0
                    return L
                else:
                    L[-i][2] = L[-(i + 1)][2]
                    L[-(i + 1)][2] = 0
            elif (L[-i][1] == 0 and L[-i][3] != 0) and L[-(i + 1)][2] == 0:
                L[-i][2] = L[-i][3]
                L[-i][3] = L[-i][4]
                L[-i][4] = 0
            elif (L[-i][1] != 0 and L[-i][3] == 0) and L[i + 1][2] == 0:
                L[-i][2] = L[-i][1]
                L[-i][1] = L[-i][0]
                L[-i][0] = 0
            elif (L[-i][1] != 0 and L[-i][3] != 0) and L[-(i + 1)][2] == 0:
                if randint(1, 2) == 2:
                    L[-i][2] = L[-i][3]
                    L[-i][3] = L[-i][4]
                    L[-i][4] = 0
                else:
                    L[i][2] = L[i][1]
                    L[i][1] = L[i][0]
                    L[i][0] = 0
    else:
        L[18][1] = L[18][2]
        L[18][2] = L[19][2]
        L[19][2] = 0
        for i in range(19, 25):
            if (L[i][1] == 0 and L[i][3] != 0) and L[i + 1][2] != 0:
                if randint(1, 5) == 1:
                    L[i][2] = L[i][3]
                    L[i][3] = L[i][4]
                    L[i][4] = 0
                    return L
                else:
                    L[i][2] = L[i + 1][2]
                    L[i + 1][2] = 0
            elif (L[i][1] != 0 and L[i][3] == 0) and L[i + 1][2] != 0:
                if randint(1, 5) == 2:
                    L[i][2] = L[i][1]
                    L[i][1] = L[i][0]
                    L[i][0] = 0
                    return L
                else:
                    L[i][2] = L[i + 1][2]
                    L[i + 1][2] = 0
            elif (L[i][1] == 0 and L[i][3] == 0) and L[i + 1][2] != 0:
                L[i][2] = L[i + 1][2]
                L[i + 1][2] = 0
            elif (L[i][1] != 0 and L[i][3] != 0) and L[i + 1][2] != 0:
                if randint(1, 5) == 2:
                    L[i][2] = L[i][1]
                    L[i][1] = L[i][0]
                    L[i][0] = 0
                    return L
                elif randint(1, 5) == 3:
                    L[i][2] = L[i][3]
                    L[i][3] = L[i][4]
                    L[i][4] = 0
                    return L
                else:
                    L[i][2] = L[i + 1][2]
                    L[i + 1][2] = 0
            elif (L[i][1] == 0 and L[i][3] != 0) and L[i + 1][2] == 0:
                L[i][2] = L[i][3]
                L[i][3] = L[i][4]
                L[i][4] = 0
            elif (L[i][1] != 0 and L[i][3] == 0) and L[i + 1][2] == 0:
                L[i][2] = L[i][1]
                L[i][1] = L[i][0]
                L[i][0] = 0
            elif (L[i][1] != 0 and L[i][3] != 0) and L[i + 1][2] == 0:
                if randint(1, 2) == 2:
                    L[i][2] = L[i][3]
                    L[i][3] = L[i][4]
                    L[i][4] = 0
                else:
                    L[i][2] = L[i][1]
                    L[i][1] = L[i][0]
                    L[i][0] = 0
    return L


def avancemilieu2(L):
    L[18][7] = 0
    from random import randint
    if randint(1, 2) == 1:
        L[18][7] = L[18][6]
        L[17][6] = L[17][6]
        L[17][6] = 0
        for i in range(20, 26):
            if (L[-i][5] == 0 and L[-i][7] != 0) and L[-(i + 1)][6] != 0:
                if randint(1, 5) == 1:
                    L[-i][6] = L[-i][7]
                    L[-i][7] = L[-i][8]
                    L[-i][8] = 0
                    return L
                else:
                    L[-i][6] = L[-(i + 1)][6]
                    L[-(i + 1)][6] = 0
            elif (L[-i][5] != 0 and L[-i][7] == 0) and L[-(i + 1)][6] != 0:
                if randint(1, 5) == 2:
                    L[-i][6] = L[-i][5]
                    L[-i][5] = L[-i][4]
                    L[-i][4] = 0
                    return L
                else:
                    L[-i][6] = L[-(i + 1)][6]
                    L[-(i + 1)][6] = 0
            elif (L[-i][5] == 0 and L[-i][7] == 0) and L[-(i + 1)][6] != 0:
                L[-i][6] = L[-(i + 1)][6]
                L[-(i + 1)][6] = 0
            elif (L[-i][5] != 0 and L[-i][7] != 0) and L[-(i + 1)][6] != 0:
                if randint(1, 5) == 2:
                    L[-i][6] = L[-i][5]
                    L[-i][5] = L[-i][4]
                    L[-i][4] = 0
                    return L
                elif randint(1, 5) == 3:
                    L[-i][6] = L[-i][7]
                    L[-i][7] = L[-i][8]
                    L[-i][8] = 0
                    return L
                else:
                    L[-i][6] = L[-(i + 1)][6]
                    L[-(i + 1)][6] = 0
            elif (L[-i][5] == 0 and L[-i][7] != 0) and L[-(i + 1)][6] == 0:
                L[-i][6] = L[-i][7]
                L[-i][7] = L[-i][8]
                L[-i][8] = 0
            elif (L[-i][5] != 0 and L[-i][7] == 0) and L[-(i + 1)][6] == 0:
                L[-i][6] = L[-i][5]
                L[-i][5] = L[-i][4]
                L[-i][4] = 0
            elif (L[-i][5] != 0 and L[-i][7] != 0) and L[-(i + 1)][6] == 0:
                if randint(1, 2) == 2:
                    L[-i][6] = L[-i][5]
                    L[-i][5] = L[-i][4]
                    L[-i][4] = 0
                else:
                    L[-i][6] = L[-i][7]
                    L[-i][7] = L[-i][8]
                    L[-i][8] = 0
    else:
        L[18][7] = L[18][6]
        L[18][6] = L[19][6]
        L[19][6] = 0
        for i in range(19, 25):
            if (L[i][5] == 0 and L[i][7] != 0) and L[i + 1][6] != 0:
                if randint(1, 5) == 1:
                    L[i][6] = L[i][7]
                    L[i][7] = L[i][8]
                    L[i][8] = 0
                    return L
                else:
                    L[i][6] = L[i + 1][6]
                    L[i + 1][6] = 0
            elif (L[i][5] != 0 and L[i][7] == 0) and L[i + 1][6] != 0:
                if randint(1, 5) == 2:
                    L[i][6] = L[i][5]
                    L[i][5] = L[i][4]
                    L[i][4] = 0
                    return L
                else:
                    L[i][6] = L[i + 1][6]
                    L[i + 1][6] = 0
            elif (L[i][5] == 0 and L[i][7] == 0) and L[i + 1][6] != 0:
                L[i][6] = L[i + 1][6]
                L[i + 1][6] = 0
            elif (L[i][5] != 0 and L[i][7] != 0) and L[i + 1][6] != 0:
                if randint(1, 5) == 2:
                    L[i][6] = L[i][5]
                    L[i][5] = L[i][4]
                    L[i][4] = 0
                    return L
                elif randint(1, 5) == 3:
                    L[i][6] = L[i][7]
                    L[i][7] = L[i][8]
                    L[i][8] = 0
                    return L
                else:
                    L[i][6] = L[i + 1][6]
                    L[i + 1][6] = 0
            elif (L[i][5] == 0 and L[i][7] != 0) and L[i + 1][6] == 0:
                L[i][6] = L[i][7]
                L[i][7] = L[i][8]
                L[i][8] = 0
            elif (L[i][5] != 0 and L[i][7] == 0) and L[i + 1][6] == 0:
                L[i][6] = L[i][5]
                L[i][5] = L[i][4]
                L[i][4] = 0
            elif (L[i][5] != 0 and L[i][7] != 0) and L[i + 1][6] == 0:
                if randint(1, 2) == 2:
                    L[i][6] = L[i][5]
                    L[i][5] = L[i][4]
                    L[i][4] = 0
                else:
                    L[i][6] = L[i][7]
                    L[i][7] = L[i][8]
                    L[i][8] = 0

    return (L)


def evacuation(L):
    T = ['red', 'light green', 'light blue', 'yellow', 'orange', 'pink']
    # derniers(L)
    # avancecolonne1(L)
    # avancecol1ar(L)
    # avancemilieu1(L)
    # avancemilieu2(L)
    old_L = L.copy()
    L = avancemilieu1(avancemilieu1(avancecol1ar(avancecolonne1(derniers(L)))))
    print(np.array_equal(L, old_L))
    global passagers
    print(len(passagers))
    # effacer les ellipses
    for p in passagers:
        Terrain.delete(p)
    passagers = []
    for j in range(len(L)):
        for k in range(len(L[0])):
            if L[j][k] == 1:
                m = randint(0, len(T) - 1)
                passagers.append(Terrain.create_oval((k * 30) + 5, (j * 30) + 5, (k + 1) * 30 - 5, (j + 1) * 30 - 5, width=2, fill=T[m]))
    print("Evacuation")
    # Terrain.after(100, evacuation, L)
    return L


def repet(L):
    T = ['red', 'light green', 'light blue', 'yellow', 'orange', 'pink', 'brown', ]
    S = [0] * 36
    decal(L)
    n = 0
    Q = []
    R = []
    for i in range(len(L)):
        Terrain.create_rectangle(60, (i * 30), 90, (i + 1) * 30, fill="white")
        Terrain.create_rectangle(180, (i * 30), 210, (i + 1) * 30, fill="white")
    for j in range(len(L[0])):
        Terrain.create_rectangle(j * 30, 540, (j + 1) * 30, 570, fill="white")
    for j in range(len(L)):
        for k in range(len(L[0])):
            if L[j][k] == 1:
                m = randint(0, len(T) - 1)
                passagers.append(Terrain.create_oval((k * 30) + 5, (j * 30) + 5, (k + 1) * 30 - 5, (j + 1) * 30 - 5, width=2, fill=T[m]))
    for i in range(36):
        Q.append(L[i][2])
        R.append(L[i][6])
    while Q != S or R != S:
        L = evacuation(L)
        Terrain.update()
        time.sleep(1)
        print(L)
        n = n + 1
        Q = []
        R = []
        for r in range(36):
            Q.append(L[r][2])
            R.append(L[r][6])
    L[18][1] = 0
    L[18][7] = 0
    return (L, n + 1)


print(repet(Avion))

Terrain.mainloop() 
TS-SVT-OIB (2018-2019) -> MPSI (2019-2020) -> ?
"If at first you don't succeed ... you fail." - Glados

Gandel W
Messages : 14
Enregistré le : sam. déc. 08, 2018 12:57 pm
Classe : MP

Re: Tkinter

Message par Gandel W » sam. mai 18, 2019 8:49 pm

Merci beaucoup !
J'ai juste une autre question : Comment faire pour que les ronds gardent leurs couleurs du début à la fin ? Je crois aussi qu'il y a un problème au départ parce que la première étape devrait etre lorsque tout le monde est assis avant l'execution de la fonction "decal" qui correspond lorsque les premières personnes sortent de leurs sièges mais ce n'est pas la cas et les ronds sont aléatoirement répartis.

Avatar du membre
LeToucan
Messages : 14
Enregistré le : mer. mai 15, 2019 9:31 pm
Classe : Terminale

Re: Tkinter

Message par LeToucan » dim. mai 19, 2019 11:22 am

Pour la couleur des cercles, le moyen le plus simple je pense est de représenter les passagers non pas par des 1 dans Avion, mais par un chiffre correspondant a leur couleur mais il faut modifier un peu ton algo pour qu'il garde ces chiffres en mémoire.

Pour le départ, c'est parce que decal(Avion) modifie tout de même l'objet Avion (car tu modifies son argument directement). Donc print(decal(Avion)), décalé(L) par exemple modifient la liste avant le premier render. De plus il ne faut pas oublier d'ajouter un petit délai pour que la phase initiale soit visible
TS-SVT-OIB (2018-2019) -> MPSI (2019-2020) -> ?
"If at first you don't succeed ... you fail." - Glados

Gandel W
Messages : 14
Enregistré le : sam. déc. 08, 2018 12:57 pm
Classe : MP

Re: Tkinter

Message par Gandel W » dim. mai 19, 2019 12:05 pm

Excuse moi mais j'ai pas très bien compris pour la fonction decal()^^

Avatar du membre
LeToucan
Messages : 14
Enregistré le : mer. mai 15, 2019 9:31 pm
Classe : Terminale

Re: Tkinter

Message par LeToucan » dim. mai 19, 2019 3:31 pm

Ligne 68, il y a un "print(decal(Avion))" que tu as mis j'imagine pour tester la fonction. Or le problème est que ta fonction decal (comme les autres tranformations) modifient leur argument, c'est à dire qu'elle sont de la forme:

Code : Tout sélectionner

def fonction(L):
    L[1] = 0
Ici, l'argument L est modifié directement, et le problème de ça c'est qu'en python ça va aussi modifier l'élément à l'extérieur, que tu as passé comme argument (je ne sais pas comment rendre ça clair, donc voilà un exemple:)

Code : Tout sélectionner

l = [1, 2, 3]
def truc(A):
  A[1] = 0  # modification de l'argument
  return A

print(truc(l))  # [1, 0, 3]
print(l)        # [1, 0, 3]
Tout ça pour dire que quand tu run ton print ligne 68, il va modifier directement la variable Avion, et donc quand plus tard tu dessines tes cercles pour ce que tu pense être l'instant t = 0, les passagers auront déjà bougé. De même, ligne 643, tu as un decal() qui j'imagine est là pour mettre en marche le mouvement des passagers, mais il est avant la boucle qui génère les ellipses, donc on ne verra jamais la position initiale. Il faut donc enlever ces deux appels, et après avoir dessiné les ronds pour la première fois, updater le terrain et enfin décaler (dans la fonction repet):

Code : Tout sélectionner

    for j in range(len(L)):
        for k in range(len(L[0])):
            if L[j][k] == 1:
                m = randint(0, len(T) - 1)
                passagers.append(Terrain.create_oval((k * 30) + 5, (j * 30) + 5, (k + 1) * 30 - 5, (j + 1) * 30 - 5, width=2, fill=T[m]))
    Terrain.update()            
    time.sleep(1)            
    L = decal(L)
Pour éviter d'avoir des bugs comme ça, j'essaie souvent le plus possible d'écrire des fonctions qui n'ont pas "d'effets secondaires", c'est à dire qui ne modifient pas directement leurs arguments. Le problème c'est qu'en python c'est un peu plus compliqué à faire, il faudrait faire une copie de la liste au début de chaque fonction, et modifier celle ci au sein de la fonction.
TS-SVT-OIB (2018-2019) -> MPSI (2019-2020) -> ?
"If at first you don't succeed ... you fail." - Glados

Gandel W
Messages : 14
Enregistré le : sam. déc. 08, 2018 12:57 pm
Classe : MP

Re: Tkinter

Message par Gandel W » mar. mai 21, 2019 12:23 pm

Merci beaucoup pour ton aide !

Répondre

Qui est en ligne

Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 3 invités