1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| from tkinter import *
import random
def creation():
global pieces
n=random.randint(0,6)
xc,yc=pieces[n][0][0],pieces[n][0][1]
x1,x2,x3,x4,x5,x6,x7,x8=pieces[n][1][0],pieces[n][2][0],pieces[n][3][0],pieces[n][4][0],pieces[n][5][0],pieces[n][6][0],pieces[n][7][0],pieces[n][8][0]
y1,y2,y3,y4,y5,y6,y7,y8=pieces[n][1][1],pieces[n][2][1],pieces[n][3][1],pieces[n][4][1],pieces[n][5][1],pieces[n][6][1],pieces[n][7][1],pieces[n][8][1]
square1=can.create_rectangle(x1,y1,x2,y2,fill='blue')
square2=can.create_rectangle(x3,y3,x4,y4,fill='blue')
square3=can.create_rectangle(x5,y5,x6,y6,fill='blue')
square4=can.create_rectangle(x7,y7,x8,y8,fill='blue')
def avance(gd,bh):
global x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8
x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8=x1+gd,y1+bh,x2+gd,y2+bh,x3+gd,y3+bh,x4+gd,y4+bh,x5+gd,y5+bh,x6+gd,y6+bh,x7+gd,y7+bh,x8+gd,y8+bh
can.coords(square1,x1,y1,x2,y2)
can.coords(square2,x3,y3,x4,y4)
can.coords(square3,x5,y5,x6,y6)
can.coords(square4,x7,y7,x8,y8)
def droite(event):
global pieces,x1,x2,x3,x4,x5,x6,x7,x8
if x1<600 and x2<600 and x3<600 and x4<600 and x5<600 and x6<600 and x7<600 and x8<600 :
avance(25,0)
def gauche(event):
global pieces,x1,x2,x3,x4,x5,x6,x7,x8
if x1>0 and x2>0 and x3>0 and x4>0 and x5>0 and x6>0 and x7>0 and x8>0 :
avance(-25,0)
def descendre(event):
global pieces
avance(0,25)
def rotation(event):
global pieces,xc,yc,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8
x1,y1=xc+y1-yc,yc-x1+xc
x2,y2=xc+y2-yc,yc-x2+xc
x3,y3=xc+y3-yc,yc-x3+xc
x4,y4=xc+y4-yc,yc-x4+xc
x5,y5=xc+y5-yc,yc-x5+xc
x6,y6=xc+y6-yc,yc-x6+xc
x7,y7=xc+y7-yc,yc-x7+xc
x8,y8=xc+y8-yc,yc-x8+xc
can.coords(square1,x1,y1,x2,y2)
can.coords(square2,x3,y3,x4,y4)
can.coords(square3,x5,y5,x6,y6)
can.coords(square4,x7,y7,x8,y8)
fen=Tk()
fen.title("Tétris")
can=Canvas(fen,width=250,height=600,bg="white")
can.pack()
pieces=[[[125,25],[100,0],[125,25],[100,25],[125,50],[125,25],[150,50],[125,0],[150,25]], #pièce C
[[125,25],[100,50],[125,75],[100,25],[125,50],[125,25],[150,50],[125,0],[150,25]], #pièce Z
[[125,25],[100,0],[125,25],[100,25],[125,50],[125,25],[150,50],[125,50],[150,75]], #pièce S
[[125,25],[100,50],[125,75],[100,25],[125,50],[100,0],[125,25],[125,0],[150,25]], #pièce J
[[125,25],[100,0],[125,25],[125,0],[150,25],[125,25],[150,50],[125,50],[150,75]], #pièce L
[[125,25],[100,25],[125,50],[125,25],[150,50],[125,0],[150,25],[150,25],[175,50]], #pièce T
[[125,0],[75,0],[100,25],[100,0],[125,25],[125,0],[150,25],[150,0],[175,25]]] #pièce I
fen.bind("<Right>",droite) #détection de présion de la flèche directionelle droite
fen.bind("<Left>",gauche) #détection de présion de la flèche directionelle gauche
fen.bind("<Down>",descendre)#détection de présion de le flèche directionelle bas
fen.bind("<Up>",rotation)#fonction de rotation des pièces
fen.mainloop() |
Partager