You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.4 KiB

from tkinter import *
from File import *
from random import shuffle
from tkinter import messagebox
import sqlite3
class Carte:
def __init__(self, valeur, couleur):
self.valeur = valeur
self.couleur = couleur
def compare(self, other):
#Les valeurs revoyées ne respectent pas les consignes pour des raisons d'optimisation et d'allègement du programme
if self.valeur > other.valeur:
return 1
elif self.valeur < other.valeur:
return 0
return 2
class Jeux:
def __init__ (self, nbr_cartes):
self.nb = nbr_cartes
self.paquet = []
for i in couleurs:
if self.nb == 52:
for j in numeros:
self.paquet.append(Carte(j, i))
else:
for j in numeros[5:]:
self.paquet.append(Carte(j, i))
shuffle(self.paquet)
def distribue(self):
while len(self.paquet) > 0:
for player in players:
player.enfiler(self.paquet.pop(0))
def click(event):
global status, cartes_tour
def afficher():
for i in range(2):
canvas.itemconfig(player_cartecolor[i], fill = "#fff")
canvas.itemconfig(player_carte[i], text = cartes_tour[-i-1].valeur + cartes_tour[-i-1].couleur)
canvas.itemconfig(player_left[i], text = "Left : " + str(players[i].taille()))
if cartes_tour[-i-1].couleur in ["", ""]:
canvas.itemconfig(player_carte[i], fill = "#f00")
else:
canvas.itemconfig(player_carte[i], fill = "#000")
def retourne_carte():
for i in range(2):
canvas.itemconfig(player_cartecolor[i], fill = "#f77")
canvas.itemconfig(player_carte[i], text = "")
canvas.itemconfig(player_left[i], text = "Left : " + str(players[i].taille()))
if players[0].taille() == 0:
messagebox.showinfo("Le joueur 2 a gagné", "LE JOUEUR 2 A GAGNÉ !")
exit()
if players[1].taille() == 0:
messagebox.showinfo("Le joueur 1 a gagné", "LE JOUEUR 1 A GAGNÉ !")
exit()
x, y = event.x, event.y
if x > 50 and x < 150 and y > 100 and y < 250:
if status == 1:
cartes_tour = []
cartes_tour.extend([players[0].defiler(), players[1].defiler()])
winner = cartes_tour[-2].compare(cartes_tour[-1])
if winner == 2:
status = 2
else:
for carte in cartes_tour:
players[winner].enfiler(carte)
afficher()
elif status == 2:
cartes_tour.extend([players[0].defiler(), players[1].defiler()])
retourne_carte()
status = 3
else:
cartes_tour.extend([players[0].defiler(), players[1].defiler()])
winner = cartes_tour[-2].compare(cartes_tour[-1])
if winner == 2:
status = 2
else:
status = 1
for carte in cartes_tour:
players[winner].enfiler(carte)
afficher()
Per1 = ("Joueur 1", str(players[0].taille()),)
c.execute("INSERT INTO Cartes VALUES (?, ?)", Per1)
Per2 = ("Joueur 2", str(players[1].taille()),)
c.execute("INSERT INTO Cartes VALUES (?, ?)", Per2)
c.execute("SELECT * FROM Cartes")
conn.commit()
couleurs = ["", "", "", ""]
numeros = ["2", "3", "4", "5", "6", "7", "8", "9", "D", "J", "Q", "R", "S"]
#D=Dix
#J=Jack
#Q=Queen
#R=Roi
#S=aS
def starting(nb_cartes):
global jeu
jeu = Jeux(nb_cartes)
start.destroy()
start = Tk()
start_title = Label(start, text = "La Bataille", font = ("Courrier New", 30, "bold"))
start_title.pack()
button_32 = Button(start, text = "Jeu de 32 cartes", font = ("Courrier New", 20, "bold"), command = lambda: starting(32))
button_32.pack()
button_52 = Button(start, text = "Jeu de 52 cartes", font = ("Courrier New", 20, "bold"), command = lambda: starting(52))
button_52.pack()
start.mainloop()
players = [File_chaine(), File_chaine()]
conn = sqlite3.connect("Bataille.db")
c = conn.cursor()
c.execute('''CREATE TABLE if not exists Cartes(Joueur VARCHAR(255), NombreCartes INT)''')
Per1 = ("Joueur 1", str(players[0].taille()),)
c.execute("INSERT INTO Cartes VALUES (?, ?)", Per1)
Per2 = ("Joueur 2", str(players[1].taille()),)
c.execute("INSERT INTO Cartes VALUES (?, ?)", Per2)
c.execute("SELECT * FROM Cartes")
conn.commit()
jeu.distribue()
window = Tk()
window.title("Bataille")
window.geometry(f"700x400+300+200")
canvas = Canvas(window, width = 650, height = 300, background = "#070")
canvas.place(x = 25, y = 50)
canvas.create_rectangle(50, 100, 150, 250, fill = "#f77")
canvas.create_rectangle(500, 100, 600, 250, fill = "#f77")
player_cartecolor = [canvas.create_rectangle(200, 75, 300, 225, fill = "#fff"), canvas.create_rectangle(350, 75, 450, 225, fill = "#fff")]
player_left = [canvas.create_text(100, 175, text = "Left : " + str(players[0].taille())), canvas.create_text(550, 175, text = "Left : " + str(players[1].taille()))]
player_carte = [canvas.create_text(250, 150, text = "", font = ("Courrier New", 30, "bold")), canvas.create_text(400, 150, text = "", font = ("Courrier New", 30, "bold"))]
status = 1
canvas.bind("<Button 1>", click)
window.mainloop()