forked from menault/TD3_DEV51_Qualite_Algo
ajout jeu du pendu pour exo3
This commit is contained in:
110
pendu.py
Normal file
110
pendu.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
from tkinter import *
|
||||||
|
from tkinter import ttk
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
root = Tk()
|
||||||
|
root.title("Trouve le mot ou tu crève !")
|
||||||
|
screen_width = root.winfo_screenwidth()
|
||||||
|
screen_height = root.winfo_screenheight()
|
||||||
|
root.geometry(f"{screen_width}x{screen_height}")
|
||||||
|
|
||||||
|
numberErrors = 0
|
||||||
|
|
||||||
|
|
||||||
|
def game():
|
||||||
|
global numberErrors
|
||||||
|
titre = ttk.Label(root, text="Jeu du Pendu", font=("Arial", 24))
|
||||||
|
titre.pack(side="top", fill="x", pady=20)
|
||||||
|
titre.config(anchor="center")
|
||||||
|
|
||||||
|
|
||||||
|
score = ttk.Label(root, text=f"Nombre d'erreur : {numberErrors}", font=("Arial", 24))
|
||||||
|
score.pack(side="bottom", fill="x", pady=20)
|
||||||
|
score.config(anchor="center")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def SelectAWord():
|
||||||
|
with open("mots.txt", 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
words = content.split()
|
||||||
|
return random.choice(words)
|
||||||
|
|
||||||
|
def create_case(parent):
|
||||||
|
case = Frame(parent, width=40, height=40)
|
||||||
|
case.pack_propagate(False)
|
||||||
|
case.pack(side=LEFT, padx=2)
|
||||||
|
|
||||||
|
canvas = Canvas(case, width=40, height=40, highlightthickness=0)
|
||||||
|
canvas.pack(fill=BOTH, expand=True)
|
||||||
|
canvas.create_line(0, 39, 40, 39, fill="black", width=2)
|
||||||
|
|
||||||
|
label = Label(case, text="", font=("Arial", 20))
|
||||||
|
label.place(relx=0.5, rely=0.5, anchor=CENTER)
|
||||||
|
|
||||||
|
return label # retourne le Label et non le Frame
|
||||||
|
|
||||||
|
def letterClick(letter, button):
|
||||||
|
global numberErrors
|
||||||
|
global cases_labels
|
||||||
|
button.config(state=DISABLED) # désactive le bouton cliqué
|
||||||
|
indices = [i for i, l in enumerate(wordSelect) if l == letter]
|
||||||
|
if indices:
|
||||||
|
for i in indices:
|
||||||
|
cases_labels[i].config(text=letter)
|
||||||
|
allFound = all(lbl.cget("text") != "" for lbl in cases_labels)
|
||||||
|
if allFound:
|
||||||
|
endGame(True)
|
||||||
|
else:
|
||||||
|
numberErrors=numberErrors+1
|
||||||
|
score.config(text=f"Nombre d'erreur : {numberErrors}")
|
||||||
|
if numberErrors > 6:
|
||||||
|
endGame(False)
|
||||||
|
|
||||||
|
def endGame(victoire):
|
||||||
|
global wordSelect
|
||||||
|
for widget in root.winfo_children():
|
||||||
|
widget.destroy()
|
||||||
|
msg = wordSelect
|
||||||
|
if victoire:
|
||||||
|
texte = f"{msg}\n \n GAGNE !"
|
||||||
|
else:
|
||||||
|
texte = f"{msg}\n \nPERDU !"
|
||||||
|
lbl = Label(root, text=texte, font=("Arial", 48), justify="center")
|
||||||
|
lbl.pack(expand=True)
|
||||||
|
|
||||||
|
|
||||||
|
wordSelect = SelectAWord().upper()
|
||||||
|
print(wordSelect)
|
||||||
|
wordNormalize = unicodedata.normalize('NFKD', wordSelect)
|
||||||
|
wordNormalize = ''.join(c for c in wordNormalize if not unicodedata.combining(c))
|
||||||
|
wordSelect = wordNormalize
|
||||||
|
cases_labels = []
|
||||||
|
print(wordNormalize)
|
||||||
|
|
||||||
|
|
||||||
|
frame_cases = Frame(root)
|
||||||
|
frame_cases.pack(pady=20)
|
||||||
|
for _ in wordSelect:
|
||||||
|
lbl = create_case(frame_cases)
|
||||||
|
cases_labels.append(lbl)
|
||||||
|
|
||||||
|
|
||||||
|
frame_buttons = Frame(root)
|
||||||
|
frame_buttons.pack(pady=20)
|
||||||
|
for letter in string.ascii_uppercase:
|
||||||
|
btn = Button(frame_buttons, text=letter, width=3, height=1)
|
||||||
|
btn.pack(side=LEFT, padx=2)
|
||||||
|
btn.config(command=lambda l=letter, b=btn: letterClick(l, b))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
root.mainloop()
|
Reference in New Issue
Block a user