from tkinter import* from random import* class application: def __init__(self): self.flag=0 self.f=Tk() self.can=Canvas(self.f, height=600, width=200) self.matrice=[] for y in range(30): ligne=[] for x in range(10): ligne.append(0) self.matrice.append(ligne) self.matrix=[] for y in range(30): ligne=[] for x in range(10): ligne.append(0) self.matrix.append(ligne) self.fig1=(0, 5, 1, 5, 2, 5, 3, 5) self.fig2=(0, 5, 1, 5, 1, 6, 2, 6) self.fig3=(0, 6, 1, 6, 1, 5, 2, 5) self.fig4=(0, 5, 0, 6, 1, 5,1, 6) self.fig5=(0, 5, 0, 6, 1, 6,2, 6) self.fig6=(0, 5, 0, 6, 1, 5, 2, 5) self.listfig=(self.fig1, self.fig2,self.fig3, self.fig4, self.fig5,self.fig6) self.listecar=[] for y in range(30): ligne=[] for x in range(10): c=self.can.create_rectangle(x*20, y*20,(x+1)*20, (y+1)*20, fill="white", outline="white") ligne.append(c) self.listecar.append(ligne) def go(self): self.flag=0 x=randrange(5) self.a=self.listfig[0] self.newmatrice(self.a) self.descente() def descente(self): for y in range(30): for x in range(10): if (self.matrice[y][x]==1): self.matrix[y+1][x]=1 elif(self.matrice[y][x]==2): self.matrix[y][x]=2 else: pass for a in range(30): for b in range(10): self.matrice[a][b]=self.matrix[a][b] self.matrix[a][b]=0 self.teststop() self.dessin() if(self.flag==0): self.can.after(500, self.descente) def teststop(self): for a in range(30): for b in range(10): if(self.matrice[a][b]==1): if (self.matrice[29][b]==1 or self.matrice[a+1][b]==2): self.flag=1 for m in range(30): for n in range(10): if(self.matrice[m][n]==1): self.matrice[m][n]=2 self.go() def newmatrice(self, a): for x in range(4): for y in range(4): xx=self.a[2*x] yy=self.a[2*x+1] self.matrice[xx][yy]=1 self.dessin() def dessin(self): for x in range(30): for y in range(10): if (self.matrice[x][y]==1 or self.matrice[x][y]==2): self.can.itemconfigure(self.listecar[x][y], fill="green", outline="green") elif(self.matrice[x][y]==0): self.can.itemconfigure(self.listecar[x][y], fill="white", outline="white") app=application() app.go() app.can.pack()