Advertisement
Guest User

PYTHON MORPION 1.2

a guest
Dec 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. from tkinter import*
  2. from time import*
  3.  
  4. col=0
  5. lig=0
  6. tour= 0
  7. win=0
  8. e=0
  9.  
  10. LS=[0,1]
  11. LV=[[0,0,0],
  12. [0,0,0],
  13. [0,0,0]
  14. ]
  15.  
  16. #Affichage de l'action du joueur
  17. def cliquer(event):
  18. global col,lig,tour,LV,win
  19. abs=event.x
  20. ord=event.y
  21. col=abs//200
  22. lig=ord//200
  23. #Alterance de jeu
  24. if tour==0 :
  25. if LV[lig][col]==0:
  26. #Joueur 1 avec X
  27. texte=Can.create_text(col*200+100,lig*200+100,text="X",font="Helvetica 120 bold",fill='blue')
  28. LV[lig][col]=2
  29. print(LV,tour)
  30.  
  31. if tour==1:
  32. if LV[lig][col]==0:
  33. #Joueur 2 avec O
  34. texte=Can.create_text(col*200+100,lig*200+100,text="O", font="Helvetica 120 bold",fill='red')
  35. LV[lig][col]=1
  36. print(LV,tour)
  37. if tour==0:
  38. tour=1
  39. elif tour==1:
  40. tour=0
  41.  
  42. for i in range(0,3):
  43. if(LV[i][0]==LV[i][1]==LV[i][2]==1):
  44. Can.create_text(300, 175, text = 'Les ronds ont gagnés !', font = 'Arial 20 bold')
  45. Can.unbind('<Button-1>')
  46. win=1
  47. # Lignes
  48. for i in range(0,3):
  49. if(LV[0][i]==LV[1][i]==LV[2][i]==1):
  50. Can.create_text(300, 175, text = 'Les ronds ont gagnés !', font = 'Arial 20 bold')
  51. Can.unbind('<Button-1>')
  52. win=1
  53.  
  54. # Diagonales
  55. if(LV[0][0]==LV[1][1]==LV[2][2]==1):
  56. Can.create_text(300, 175, text = 'Les ronds ont gagnés !', font = 'Arial 20 bold')
  57. Can.unbind('<Button-1>')
  58.  
  59. win=1
  60.  
  61. if(LV[0][2]==LV[1][1]==LV[2][0]==1):
  62. Can.create_text(300, 175, text = 'Les ronds ont gagnés !', font = 'Arial 20 bold')
  63. Can.unbind('<Button-1>')
  64. win=1
  65.  
  66.  
  67. # Croix
  68. # Colonne
  69. for i in range(0,3):
  70. if(LV[i][0]==LV[i][1]==LV[i][2]==2):
  71. Can.create_text(300, 175, text = 'Les croix ont gagnés !', font = 'Arial 20 bold')
  72. Can.unbind('<Button-1>')
  73. win=1
  74.  
  75.  
  76. #lignes
  77. for i in range(0,3):
  78. if(LV[0][i]==LV[1][i]==LV[2][i]==2):
  79. Can.create_text(300, 175, text = 'Les croix ont gagnés !', font = 'Arial 20 bold')
  80. Can.unbind('<Button-1>')
  81. win=1
  82.  
  83. #diagonales
  84. if(LV[0][0]==LV[1][1]==LV[2][2]==2):
  85. Can.create_text(300, 175, text = 'Les croix ont gagnés !', font = 'Arial 20 bold')
  86. Can.unbind('<Button-1>')
  87. win=1
  88.  
  89. if(LV[0][2]==LV[1][1]==LV[2][0]==2):
  90. Can.create_text(300, 175, text = 'Les croix ont gagnés !', font = 'Arial 20 bold')
  91. Can.unbind('<Button-1>')
  92. win=1
  93.  
  94. egalite = 9
  95. #vide
  96. for i in range(0,3):
  97. for j in range(0,3):
  98. if(LV[i][j]!=0):
  99. egalite -= 1
  100. print(egalite,win)
  101.  
  102. if egalite == 0 and win==0:
  103. Can.create_text(300, 175, text = 'Egalité !', font = 'Arial 20 bold')
  104. Can.unbind('<Button-1>')
  105.  
  106.  
  107.  
  108.  
  109. #restart
  110. def restart():
  111. global tour,LV,col,lig,Can
  112. tour=0
  113. LV=[[0,0,0],
  114. [0,0,0],
  115. [0,0,0]
  116. ]
  117. lig=0
  118. col=0
  119. Can.destroy()
  120. Can =Canvas(f1,width=600,height=600, bg='white')
  121. Can.place(x=800,y=750)
  122. Can.create_line(200,0,200,600, fill='black')
  123. Can.create_line(400,0,400,600, fill='black')
  124. Can.create_line(0,200,600,200, fill='black')
  125. Can.create_line(0,400,600,400, fill='black')
  126. Can.pack()
  127. Can.bind('<Button-1>',cliquer)
  128.  
  129.  
  130.  
  131.  
  132. #Affichage grille
  133. def Jouer():
  134. global b1,b2,Menu,b4,Can
  135.  
  136.  
  137. b3.pack_forget()
  138. Menu.pack_forget()
  139. Can.place(x=800,y=750)
  140.  
  141. #Bouton retour
  142. b4=Button(f1,text='Retour',command=retour)
  143. b4.pack(side=TOP)
  144.  
  145. Can.create_line(200,0,200,600, fill='black')
  146. Can.create_line(400,0,400,600, fill='black')
  147. Can.create_line(0,200,600,200, fill='black')
  148. Can.create_line(0,400,600,400, fill='black')
  149. Can.pack()
  150. Can.bind('<Button-1>',cliquer)
  151.  
  152. #Bouton restart
  153. b2=Button(f1,text='Restart',command=restart)
  154. b2.pack(side=BOTTOM)
  155.  
  156. #Bouton quitter
  157. b1=Button(f1,text='Quitter',command=f1.destroy)
  158. b1.pack(side=BOTTOM)
  159.  
  160.  
  161. #Destruction du menu
  162. b3.pack_forget()
  163. Menu.pack_forget()
  164.  
  165.  
  166. #Retour
  167. def retour():
  168. global e,LS,b1,b2,b3,b4,Menu
  169.  
  170. if e==0 :
  171. Can.forget()
  172. b1.forget()
  173. b2.forget()
  174. b4.forget()
  175. Menu=Label(f1,width=50,height=0,text=' Bienvenue sur le meilleur MORPION du monde ',font = 'Revue 15 bold',bg='white')
  176. Menu.pack(padx=100,pady=100)
  177. e=1
  178. b3=Button(f1,width=75,height=100,text='JOUER',command=Jouer)
  179. b3.pack(padx=200,pady=200)
  180. b4.forget()
  181. e=0
  182.  
  183.  
  184. #fenetre principale
  185. f1=Tk()
  186. f1.geometry('700x700+400+150')
  187. f1['bg']='black'
  188. Can = Canvas(f1,width=600,height=600, bg='white')
  189. Can.place(x=800,y=750)
  190.  
  191.  
  192. Menu=Label(f1,width=50,height=0,text=' Bienvenue sur le meilleur MORPION du monde ',font = 'Revue 15 bold',bg='white')
  193. Menu.pack(padx=100,pady=100)
  194.  
  195.  
  196. #Bouton jouer
  197. b3=Button(f1,width=75,height=100,text='JOUER',command=Jouer)
  198. b3.pack(padx=200,pady=200)
  199.  
  200.  
  201.  
  202.  
  203. f1.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement