from File import File_chaine import tkinter as tk from PIL import Image, ImageTk import sqlite3 import random class Carte: def __init__(self, couleur, valeur, img_path): self.valeur = valeur self.couleur = couleur image = Image.open(img_path) self.image = ImageTk.PhotoImage((image)) def compare(self, other): if self.valeur > other.valeur: return 1 elif self.valeur < other.valeur: return -1 return 0 def __str__(self) -> str: return str(self.valeur) + ' de ' + self.couleur class Jeux: def __init__(self, nb) -> None: self.paquet = [] self.nombre_carte = nb if self.nombre_carte != 32 and self.nombre_carte != 52: raise ValueError("Saissisez 32 ou 52 cartes") # Création du paquet de carte en fonction du nombre de carte conn = sqlite3.connect('bdd.db') c = conn.cursor() if self.nombre_carte == 32: c.execute('SELECT * FROM Cartes WHERE belote=True') else: c.execute('SELECT * FROM Carte') data = c.fetchall() for i, carte in enumerate(data): self.paquet.append(Carte(data[i][0], data[i][1], data[i][3])) def distribute(self) -> File_chaine: joueur = [File_chaine(), File_chaine()] while len(self.paquet) > 0: for i in range(2): random_number = random.randint(0, len(self.paquet)-1) joueur[i].enfiler(self.paquet.pop(random_number)) return joueur def jeu(self): self.j1, self.j2 = self.distribute() while not (self.j1.est_vide() or self.j2.est_vide()): self.bataille() if self.j1.est_vide(): # self.j1 a perdu donc self.j2 gagne toutes les cartes print(self.j1, self.j2) else: print(self.j1, self.j2) def bataille(self, buffer_draw=[File_chaine(), File_chaine()]): # print('buffer', buffer_draw[0], buffer_draw[1], sep='\n') j1_active_carte, j2_active_carte = self.j1.tete(), self.j2.tete() if j1_active_carte.compare(j2_active_carte) == 0 : try: # envers buffer_draw[0].enfiler(self.j1.defiler()) # carte du dessus buffer_draw[0].enfiler(self.j1.defiler()) # carte a l'envers buffer_draw[1].enfiler(self.j2.defiler()) # carte du dessus buffer_draw[1].enfiler(self.j2.defiler()) # carte a l'envers # print('next_buff' ,buffer_draw[0], buffer_draw[1]) self.bataille(buffer_draw) except IndexError: pass elif j1_active_carte.compare(j2_active_carte) == 1 : while not buffer_draw[0].est_vide(): self.j1.enfiler(buffer_draw[0].defiler()) self.j1.enfiler(buffer_draw[1].defiler()) self.j1.enfiler(self.j2.defiler()) elif j1_active_carte.compare(j2_active_carte) == -1 :#self.j1.tete().compare(self.j2.tete()) == -1 : while not buffer_draw[0].est_vide(): self.j2.enfiler(buffer_draw[0].defiler()) self.j2.enfiler(buffer_draw[1].defiler()) self.j2.enfiler(self.j1.defiler()) class App: def __init__(self) -> None: self.win = tk.Tk() self.jeu = Jeux(32) self.jeu.jeu() self.images = [] # self.display_card(self.jeu.paquet[12]) self.win.mainloop() def display_card(self, carte :Carte) -> None: self.images.append(tk.Label(image=carte.image)) self.images[-1].pack() gui = App()