forked from menault/TD3_DEV51_Qualite_Algo
ajout pour exo3
This commit is contained in:
70
pendu.py
70
pendu.py
@@ -1,39 +1,55 @@
|
|||||||
|
"""
|
||||||
|
Importation des bibliothèques :
|
||||||
|
- tkinter : bibliothèque graphique
|
||||||
|
- random : permet de tirer au hasard un mot
|
||||||
|
- string et unicodedata : manipuler les mots
|
||||||
|
"""
|
||||||
from tkinter import *
|
from tkinter import *
|
||||||
from tkinter import ttk
|
from tkinter import ttk
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
|
||||||
|
"""
|
||||||
|
Initialisation de la fenêtre (titre, taille)
|
||||||
|
"""
|
||||||
root = Tk()
|
root = Tk()
|
||||||
root.title("Trouve le mot ou tu crève !")
|
root.title("Trouve le mot ou tu crèves !")
|
||||||
screen_width = root.winfo_screenwidth()
|
screen_width = root.winfo_screenwidth()
|
||||||
screen_height = root.winfo_screenheight()
|
screen_height = root.winfo_screenheight()
|
||||||
root.geometry(f"{screen_width}x{screen_height}")
|
root.geometry(f"{screen_width}x{screen_height}")
|
||||||
|
|
||||||
|
"""
|
||||||
|
Variable pour le nombre de tentatives échouées
|
||||||
|
"""
|
||||||
numberErrors = 0
|
numberErrors = 0
|
||||||
|
|
||||||
|
|
||||||
def game():
|
"""
|
||||||
global numberErrors
|
Texte pour l'affichage du jeu
|
||||||
titre = ttk.Label(root, text="Jeu du Pendu", font=("Arial", 24))
|
"""
|
||||||
titre.pack(side="top", fill="x", pady=20)
|
titre = ttk.Label(root, text="Jeu du Pendu", font=("Arial", 24))
|
||||||
titre.config(anchor="center")
|
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 = ttk.Label(root, text=f"Nombre d'erreur : {numberErrors}", font=("Arial", 24))
|
score.pack(side="bottom", fill="x", pady=20)
|
||||||
score.pack(side="bottom", fill="x", pady=20)
|
score.config(anchor="center")
|
||||||
score.config(anchor="center")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Selectionne un mot au hasard dans le fichier mots.txt.
|
||||||
|
"""
|
||||||
def SelectAWord():
|
def SelectAWord():
|
||||||
with open("mots.txt", 'r', encoding='utf-8') as f:
|
with open("mots.txt", 'r', encoding='utf-8') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
words = content.split()
|
words = content.split()
|
||||||
return random.choice(words)
|
return random.choice(words)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Cette fonction permet de créer une case dans le pack parent donné en paramètre. Cela correspond à une lettre.
|
||||||
|
"""
|
||||||
def create_case(parent):
|
def create_case(parent):
|
||||||
case = Frame(parent, width=40, height=40)
|
case = Frame(parent, width=40, height=40)
|
||||||
case.pack_propagate(False)
|
case.pack_propagate(False)
|
||||||
@@ -46,12 +62,17 @@ def create_case(parent):
|
|||||||
label = Label(case, text="", font=("Arial", 20))
|
label = Label(case, text="", font=("Arial", 20))
|
||||||
label.place(relx=0.5, rely=0.5, anchor=CENTER)
|
label.place(relx=0.5, rely=0.5, anchor=CENTER)
|
||||||
|
|
||||||
return label # retourne le Label et non le Frame
|
return label
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Cette fonction est activé lors d'un clique sur une lettre. Elle prend en paramètre la lettre et le bouton selectionné.
|
||||||
|
Cette fonction permet de déterminer aussi quand le jeu est terminé ou non en mettant à jour le nombre d'erreur ou le mot.
|
||||||
|
"""
|
||||||
def letterClick(letter, button):
|
def letterClick(letter, button):
|
||||||
global numberErrors
|
global numberErrors
|
||||||
global cases_labels
|
global cases_labels
|
||||||
button.config(state=DISABLED) # désactive le bouton cliqué
|
button.config(state=DISABLED)
|
||||||
indices = [i for i, l in enumerate(wordSelect) if l == letter]
|
indices = [i for i, l in enumerate(wordSelect) if l == letter]
|
||||||
if indices:
|
if indices:
|
||||||
for i in indices:
|
for i in indices:
|
||||||
@@ -65,6 +86,9 @@ def letterClick(letter, button):
|
|||||||
if numberErrors > 6:
|
if numberErrors > 6:
|
||||||
endGame(False)
|
endGame(False)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Cette fonction supprime l'affichage du jeu pour afficher la fin de jeu.
|
||||||
|
"""
|
||||||
def endGame(victoire):
|
def endGame(victoire):
|
||||||
global wordSelect
|
global wordSelect
|
||||||
for widget in root.winfo_children():
|
for widget in root.winfo_children():
|
||||||
@@ -78,15 +102,18 @@ def endGame(victoire):
|
|||||||
lbl.pack(expand=True)
|
lbl.pack(expand=True)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Normalise le mot qui a été tirer au sort pour retirer les accents.
|
||||||
|
"""
|
||||||
wordSelect = SelectAWord().upper()
|
wordSelect = SelectAWord().upper()
|
||||||
print(wordSelect)
|
|
||||||
wordNormalize = unicodedata.normalize('NFKD', wordSelect)
|
wordNormalize = unicodedata.normalize('NFKD', wordSelect)
|
||||||
wordNormalize = ''.join(c for c in wordNormalize if not unicodedata.combining(c))
|
wordNormalize = ''.join(c for c in wordNormalize if not unicodedata.combining(c))
|
||||||
wordSelect = wordNormalize
|
wordSelect = wordNormalize
|
||||||
|
|
||||||
|
"""
|
||||||
|
Contient les cases du mot (chaque lettre)
|
||||||
|
"""
|
||||||
cases_labels = []
|
cases_labels = []
|
||||||
print(wordNormalize)
|
|
||||||
|
|
||||||
|
|
||||||
frame_cases = Frame(root)
|
frame_cases = Frame(root)
|
||||||
frame_cases.pack(pady=20)
|
frame_cases.pack(pady=20)
|
||||||
for _ in wordSelect:
|
for _ in wordSelect:
|
||||||
@@ -94,6 +121,11 @@ for _ in wordSelect:
|
|||||||
cases_labels.append(lbl)
|
cases_labels.append(lbl)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Via l'import string, ici on récupère l'alphabet et on crée les cases correspondantes pour que le joueur puisse sélectionner
|
||||||
|
une lettres
|
||||||
|
"""
|
||||||
frame_buttons = Frame(root)
|
frame_buttons = Frame(root)
|
||||||
frame_buttons.pack(pady=20)
|
frame_buttons.pack(pady=20)
|
||||||
for letter in string.ascii_uppercase:
|
for letter in string.ascii_uppercase:
|
||||||
|
Reference in New Issue
Block a user