Browse Source

Transférer les fichiers vers ''

Salut les quoicoubébous UWUWUWUWUUW
master
DELPUI Loïc 11 months ago
parent
commit
05a361eb5b
  1. 255
      projet bataille.py

255
projet bataille.py

@ -1,107 +1,150 @@
from tkinter import * from tkinter import *
from File import * from File import *
from random import shuffle from random import shuffle
from time import sleep from tkinter import messagebox
import sqlite3
class Carte:
def __init__(self, valeur, couleur): class Carte:
self.valeur = valeur def __init__(self, valeur, couleur):
self.couleur = couleur self.valeur = valeur
def compare(self, other): self.couleur = couleur
#Les valeurs revoyées ne respectent pas les consignes pour des raisons d'optimisation et d'allègement du programme def compare(self, other):
if self.valeur > other.valeur: #Les valeurs revoyées ne respectent pas les consignes pour des raisons d'optimisation et d'allègement du programme
return 0 if self.valeur > other.valeur:
elif self.valeur < other.valeur: return 1
return 1 elif self.valeur < other.valeur:
return 2 return 0
return 2
class Jeux:
def __init__ (self, nbr_cartes): class Jeux:
self.nb = nbr_cartes def __init__ (self, nbr_cartes):
if self.nb != 32 and self.nb != 52: self.nb = nbr_cartes
raise ValueError("Choisissez 32 ou 52 cartes") self.paquet = []
self.paquet = [] for i in couleurs:
for i in couleurs: if self.nb == 52:
if self.nb == 52: for j in numeros:
for j in numeros: self.paquet.append(Carte(j, i))
self.paquet.append(Carte(j, i)) else:
else: for j in numeros[5:]:
for j in numeros[5:]: self.paquet.append(Carte(j, i))
self.paquet.append(Carte(j, i)) shuffle(self.paquet)
shuffle(self.paquet)
def distribue(self):
def distribue(self): while len(self.paquet) > 0:
while len(self.paquet) > 0: for player in players:
for player in players: player.enfiler(self.paquet.pop(0))
player.enfiler(self.paquet.pop(0))
def click(event):
def click(event): global status, cartes_tour
def afficher():
for i in range(2): def afficher():
canvas.itemconfig(player_cartecolor[i], fill = "#fff") for i in range(2):
canvas.itemconfig(player_carte[i], text = cartes_tour[-i-1].valeur + cartes_tour[-i-1].couleur) canvas.itemconfig(player_cartecolor[i], fill = "#fff")
canvas.itemconfig(player_left[i], text = "Left : " + str(players[i].taille())) canvas.itemconfig(player_carte[i], text = cartes_tour[-i-1].valeur + cartes_tour[-i-1].couleur)
if cartes_tour[i].couleur in ["", ""]: canvas.itemconfig(player_left[i], text = "Left : " + str(players[i].taille()))
canvas.itemconfig(player_carte[i], fill = "#f00") if cartes_tour[i].couleur in ["", ""]:
else: canvas.itemconfig(player_carte[i], fill = "#f00")
canvas.itemconfig(player_carte[i], fill = "#000") else:
canvas.itemconfig(player_carte[i], fill = "#000")
def premiere_carte():
window.after(3000, retourne_carte) def retourne_carte():
for rep in range(2):
def retourne_carte(): cartes_tour.extend([players[0].defiler(), players[1].defiler()])
for rep in range(2): for i in range(2):
cartes_tour.extend([players[0].defiler(), players[1].defiler()]) canvas.itemconfig(player_cartecolor[i], fill = "#f77")
for i in range(2): canvas.itemconfig(player_carte[i], text = "")
canvas.itemconfig(player_cartecolor[i], fill = "#f77") canvas.itemconfig(player_left[i], text = "Left : " + str(players[i].taille()))
canvas.itemconfig(player_carte[i], text = "")
canvas.itemconfig(player_left[i], text = "Left : " + str(players[i].taille())) if players[0].taille() == 0:
window.after(3000, prochaine_carte) messagebox.showinfo("Le joueur 2 a gagné", "LE JOUEUR 2 A GAGNÉ !")
exit()
def prochaine_carte(): if players[1].taille() == 0:
for rep in range(2): messagebox.showinfo("Le joueur 1 a gagné", "LE JOUEUR 1 A GAGNÉ !")
cartes_tour.extend([players[0].defiler(), players[1].defiler()]) exit()
afficher()
x, y = event.x, event.y
x, y = event.x, event.y if x > 50 and x < 150 and y > 100 and y < 250:
if x > 50 and x < 150 and y > 100 and y < 250: if status == 1:
cartes_tour = [] cartes_tour = []
cartes_tour.extend([players[0].defiler(), players[1].defiler()]) cartes_tour.extend([players[0].defiler(), players[1].defiler()])
winner = cartes_tour[-2].compare(cartes_tour[-1]) winner = cartes_tour[-2].compare(cartes_tour[-1])
while winner == 2: if winner == 2:
afficher() status = 2
premiere_carte() else:
winner = cartes_tour[-2].compare(cartes_tour[-1]) for carte in cartes_tour:
for carte in cartes_tour: players[winner].enfiler(carte)
players[winner].enfiler(carte) afficher()
afficher() elif status == 2:
cartes_tour.extend([players[0].defiler(), players[1].defiler()])
couleurs = ["", "", "", ""] retourne_carte()
numeros = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "R", "S"] status = 3
#J=Jack else:
#Q=Queen cartes_tour.extend([players[0].defiler(), players[1].defiler()])
#R=Roi winner = cartes_tour[-2].compare(cartes_tour[-1])
#S=aS if winner == 2:
status = 2
jeu = Jeux(52) else:
status = 1
players = [File_chaine(), File_chaine()] for carte in cartes_tour:
players[winner].enfiler(carte)
jeu.distribue() afficher()
Per1 = ("Joueur 1", str(players[0].taille()),)
window = Tk() c.execute("INSERT INTO Cartes VALUES (?, ?)", Per1)
window.title("Bataille") Per2 = ("Joueur 2", str(players[1].taille()),)
window.geometry(f"700x400+300+200") c.execute("INSERT INTO Cartes VALUES (?, ?)", Per2)
c.execute("SELECT * FROM Cartes")
canvas = Canvas(window, width = 650, height = 300, background = "#070") conn.commit()
canvas.place(x = 25, y = 50)
couleurs = ["", "", "", ""]
canvas.create_rectangle(50, 100, 150, 250, fill = "#f77") numeros = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "R", "S"]
canvas.create_rectangle(500, 100, 600, 250, fill = "#f77") #J=Jack
player_cartecolor = [canvas.create_rectangle(200, 75, 300, 225, fill = "#fff"), canvas.create_rectangle(350, 75, 450, 225, fill = "#fff")] #Q=Queen
player_left = [canvas.create_text(100, 175, text = "Left : " + str(players[0].taille())), canvas.create_text(550, 175, text = "Left : " + str(players[1].taille()))] #R=Roi
player_carte = [canvas.create_text(250, 150, text = "", font = ("Courrier New", 30, "bold")), canvas.create_text(400, 150, text = "", font = ("Courrier New", 30, "bold"))] #S=aS
canvas.bind("<Button 1>", click) 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() window.mainloop()
Loading…
Cancel
Save