Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-Liste de mots-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
- liste_mots = [ #On crée une liste dans lequel le jeu va piocher un mot que l'utilisateur va devoir trouver
- "armoire",
- "boucle",
- "buisson",
- "bureau",
- "chaise",
- "carton",
- "couteau",
- "fichier",
- "garage",
- "glace",
- "journal",
- "kiwi",
- "lampe",
- "liste",
- "montagne",
- "remise",
- "sandale",
- "taxi",
- "vampire",
- "volant",
- ]
- #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-Importations-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
- from tkinter import * #On importe les modules Tkinter pour l'interface graphique ainsi que la fonction "choice"
- from random import choice #qui va permettre de choisir un mot au hasard dans la liste
- #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_Fonctions_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
- def debut(mot_full): #Cette définition permet d'afficher le mot avec des tirés. Le mot sera donc affiché comme
- global mot_cache, chance #ceci " _ _ _ _ _ "
- chance = 0
- mot_cache = "_" * len(mot_full)
- disp_mot(mot_cache)
- def disp_mot(mot): #Cette définition permet d'afficher le mot avec la lettre
- global mot_disp #que l'utilisateur a choisie, par exemple, "E _ O _ E"
- mot_clean = " "
- i = 0
- while i < len(mot):
- mot_clean = mot_clean + mot[i] + " "
- i += 1
- canvas.delete(mot_disp)
- mot_disp = canvas.create_text(250, 60, text=mot_clean, fill="white", font="Consolas 30 bold")
- def ajouter_lettre(lettre, mot_full): #Cette définition permet de récupérer la lettre choisie par l'utilisateur ainsi
- global mot_cache, chance,img_pendu #que d'actualiser le nombre de chances restantes, et renvoie cette lettre à la définition
- mot_partiel = "" #permettant d'afficher le mot
- ajouter_lettre = False #En actualisant le nombres de chances, on actualise aussi l'image du Pendu
- for i, char in enumerate(mot_full): #On va aussi désactiver le bouton pressé par l'utilisateur
- if char == lettre:
- mot_partiel = mot_partiel + lettre
- ajouter_lettre = True
- else:
- mot_partiel = mot_partiel + mot_cache[i]
- mot_cache = mot_partiel
- disp_mot(mot_cache)
- bouton[ord(lettre) - 65].config(text=" ", command="")
- if mot_cache == mot_full:
- img_pendu = PhotoImage(file='Pendug.png')
- canvas.create_image(250,350,image=img_pendu)
- for i in range(26):
- bouton[ord(chr(i+65)) - 65].config(text=" ", command="")
- if ajouter_lettre == False:
- chance += 1
- canvas.delete(img_pendu)
- img_pendu = PhotoImage(file='Pendu'+str(chance)+'.png')
- canvas.create_image(100,300,image=img_pendu)
- if chance == 7:
- disp_mot(mot_full)
- for i in range(26):
- bouton[ord(chr(i+65)) - 65].config(text=" ", command="")
- #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-Interface Graphique-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
- fenetre = Tk()
- fenetre.resizable(width=False, height=False)
- canvas = Canvas(fenetre, bg='dark grey', height=500, width=500)
- canvas.pack(side=BOTTOM)
- mot_full = choice(liste_mots).upper()
- bouton = [""] * 26
- for i in range(26):
- 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))
- bouton[i].pack(side=LEFT)
- mot_disp = canvas.create_text(200, 60, text="", fill="white")
- img_pendu = PhotoImage(file='Pendu1.png')
- debut(mot_full)
- fenetre.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement