Advertisement
Guest User

Le pendu

a guest
May 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-Liste de mots-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  2.  
  3. liste_mots = [  #On crée une liste dans lequel le jeu va piocher un mot que l'utilisateur va devoir trouver
  4.     "armoire",
  5.     "boucle",
  6.     "buisson",
  7.     "bureau",
  8.     "chaise",
  9.     "carton",
  10.     "couteau",
  11.     "fichier",
  12.     "garage",
  13.     "glace",
  14.     "journal",
  15.     "kiwi",
  16.     "lampe",
  17.     "liste",
  18.     "montagne",
  19.     "remise",
  20.     "sandale",
  21.     "taxi",
  22.     "vampire",
  23.     "volant",
  24. ]
  25.  
  26. #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-Importations-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  27.  
  28. from tkinter import *              #On importe les modules Tkinter pour l'interface graphique ainsi que la fonction "choice"
  29. from random import choice          #qui va permettre de choisir un mot au hasard dans la liste
  30.  
  31. #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_Fonctions_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  32.  
  33. def debut(mot_full):                         #Cette définition permet d'afficher le mot avec des tirés. Le mot sera donc affiché comme
  34.     global mot_cache, chance                 #ceci " _ _ _ _ _ "
  35.     chance = 0
  36.     mot_cache = "_" * len(mot_full)
  37.     disp_mot(mot_cache)
  38.  
  39.  
  40. def disp_mot(mot):                           #Cette définition permet d'afficher le mot avec la lettre
  41.     global mot_disp                          #que l'utilisateur a choisie, par exemple, "E _ O _ E"
  42.     mot_clean = " "
  43.     i = 0
  44.     while i < len(mot):
  45.         mot_clean = mot_clean + mot[i] + " "
  46.         i += 1
  47.     canvas.delete(mot_disp)
  48.     mot_disp = canvas.create_text(250, 60, text=mot_clean, fill="white", font="Consolas 30 bold")
  49.  
  50.  
  51. def ajouter_lettre(lettre, mot_full):         #Cette définition permet de récupérer la lettre choisie par l'utilisateur ainsi
  52.     global mot_cache, chance,img_pendu        #que d'actualiser le nombre de chances restantes, et renvoie cette lettre à la définition
  53.     mot_partiel = ""                          #permettant d'afficher le mot
  54.     ajouter_lettre = False                    #En actualisant le nombres de chances, on actualise aussi l'image du Pendu
  55.     for i, char in enumerate(mot_full):       #On va aussi désactiver le bouton pressé par l'utilisateur
  56.         if char == lettre:
  57.             mot_partiel = mot_partiel + lettre
  58.             ajouter_lettre = True
  59.         else:
  60.             mot_partiel = mot_partiel + mot_cache[i]
  61.  
  62.     mot_cache = mot_partiel
  63.     disp_mot(mot_cache)
  64.     bouton[ord(lettre) - 65].config(text=" ", command="")
  65.    
  66.     if mot_cache == mot_full:
  67.             img_pendu = PhotoImage(file='Pendug.png')
  68.             canvas.create_image(250,350,image=img_pendu)
  69.             for i in range(26):
  70.                 bouton[ord(chr(i+65)) - 65].config(text=" ", command="")
  71.  
  72.     if ajouter_lettre == False:
  73.         chance += 1
  74.         canvas.delete(img_pendu)
  75.         img_pendu = PhotoImage(file='Pendu'+str(chance)+'.png')
  76.         canvas.create_image(100,300,image=img_pendu)
  77.  
  78.         if chance == 7:
  79.             disp_mot(mot_full)
  80.             for i in range(26):
  81.                 bouton[ord(chr(i+65)) - 65].config(text=" ", command="")
  82.  
  83.  
  84. #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-Interface Graphique-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  85.  
  86. fenetre = Tk()
  87. fenetre.resizable(width=False, height=False)
  88.  
  89. canvas = Canvas(fenetre, bg='dark grey', height=500, width=500)
  90. canvas.pack(side=BOTTOM)
  91.  
  92. mot_full = choice(liste_mots).upper()
  93.  
  94. bouton = [""] * 26                                                  
  95. for i in range(26):                                              
  96.     bouton[i] = Button(fenetre,text=chr(i+65),font='Consolas 12',bg='light blue',command=lambda x = i + 65: ajouter_lettre(chr(x), mot_full))
  97.     bouton[i].pack(side=LEFT)
  98.  
  99. mot_disp = canvas.create_text(200, 60, text="", fill="white")
  100. img_pendu = PhotoImage(file='Pendu1.png')
  101.  
  102. debut(mot_full)
  103.  
  104. fenetre.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement