1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| from tkinter import *
jeton = {"R": 1, "J": 5}
grilleDeJeu = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
]
def equipe(couleur):
# Affiche la couleur de l'equipe selectionnée et attribut la variable EQUIPE
global EQUIPE
lumiereJaune = Canvas(lumiere, width=80, height=30, bg='#625957', highlightthickness=0)
lumiereRouge = Canvas(lumiere, width=80, height=30, bg='#625957', highlightthickness=0)
if couleur == "J":
lumiereJaune.create_rectangle(0, 0, 80, 30, fill='red')
lumiereRouge.create_rectangle(0, 0, 80, 30, fill='#625957')
EQUIPE = "R"
if couleur =="R":
lumiereRouge.create_rectangle(0, 0, 80, 50, fill='yellow')
lumiereJaune.create_rectangle(0, 0, 80, 50, fill='#625957')
EQUIPE = "J"
lumiereJaune.grid(column=0, row=0)
lumiereRouge.grid(column=1, row=0)
def ajouterJeton(colonne, couleur, jeton, grilleDeJeu):
global EQUIPE
hauteur = 100
largeur = 150
sortie = False
# Fonction qui place le jeton dans la matrice
jetonPlace = False
for vertical in reversed(range(0, 6)):
for horizontal in range(0, 7):
nom = "nom" + str(vertical) + str(horizontal)
nom = Canvas(boiteGrille, width=largeur, height=hauteur, bg='#272AC4', highlightthickness=0)
if colonne == horizontal:
if 0 == grilleDeJeu[vertical][horizontal] and jetonPlace == False:
grilleDeJeu[vertical][horizontal] = jeton[couleur]
jetonPlace = True
sortie = True
if jetonPlace == False and grilleDeJeu[vertical][horizontal] != 0:
sortie = False
if grilleDeJeu[vertical][horizontal] == 0:
nom.create_oval(150 / 2 - 35, 15, 150 / 2 + 35, 85, fill='white', outline='black', width=3)
nom.grid(row=vertical, column=horizontal, sticky=N)
if grilleDeJeu[vertical][horizontal] == 5:
nom.create_oval(150 / 2 - 35, 15, 150 / 2 + 35, 85, fill='yellow', outline='black', width=3)
nom.grid(row=vertical, column=horizontal, sticky=N)
if grilleDeJeu[vertical][horizontal] == 1:
nom.create_oval(150 / 2 - 35, 15, 150 / 2 + 35, 85, fill='red', outline='black', width=3)
nom.grid(row=vertical, column=horizontal, sticky=N)
equipe(EQUIPE)
return sortie
# Initialisation de la fenetre
fenetre = Tk()
fenetre.title("Puissance 4 Python")
fenetre.geometry("1080x825")
fenetre.minsize(480, 360)
fenetre.config(background='#625957')
titre = Label(fenetre, text="Bienvenue sur notre puissance 4 python !!", font=("Courrier", 30), bg='#625957', fg='white')
titre.pack()
# Bouton d'equipe
lumiere = Frame(fenetre, bg ='#625957')
equipe("J") # Equipe par default qui commence
lumiere.pack()
# Ajout de la boite contenant les boutons et les fleches
boiteBouton = Frame(fenetre, bg='#625957')
boiteGrille = Frame(fenetre, bg='#625957')
hauteur = 50
largeur = 150
# Affichage de la grille vide
for vertical in range(0, 6):
for horizontal in range(0, 7):
vide = Canvas(boiteGrille, width=150, height=100, bg='#272AC4', highlightthickness=0)
vide.create_oval(150 / 2 - 35, 15, 150 / 2 + 35, 85, fill='white', outline='black', width=3)
vide.grid(row=vertical, column=horizontal, sticky=N)
# Affichage des fleches et des boutons
for i in range(1, 8):
nomFleche = "fleche" + str(i)
nomFleche = Canvas(boiteBouton, width=largeur, height=hauteur, bg='#625957', highlightthickness=0)
nomFleche.create_line(150/2, 10, 150/2, 40, width=20, arrow='last', fill='silver')
nomFleche.grid(row=1, column=0+i, sticky=N)
bouton1 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(0, EQUIPE, jeton, grilleDeJeu))
bouton1.grid(row=0, column=1, sticky=S)
bouton2 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(1, EQUIPE, jeton, grilleDeJeu))
bouton2.grid(row=0, column=2, sticky=S)
bouton3 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(2, EQUIPE, jeton, grilleDeJeu))
bouton3.grid(row=0, column=3, sticky=S)
bouton4 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(3, EQUIPE, jeton, grilleDeJeu))
bouton4.grid(row=0, column=4, sticky=S)
bouton5 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(4, EQUIPE, jeton, grilleDeJeu))
bouton5.grid(row=0, column=5, sticky=S)
bouton6 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(5, EQUIPE, jeton, grilleDeJeu))
bouton6.grid(row=0, column=6, sticky=S)
bouton7 = Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda: ajouterJeton(6, EQUIPE, jeton, grilleDeJeu))
bouton7.grid(row=0, column=7, sticky=S)
boiteBouton.pack()
boiteGrille.pack()
fenetre.mainloop() |