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 118 119 120 121 122 123 124 125 126 127
| from tkinter import *
jeton = {"R": 1, "J": 5}
grilleDeJeu = [[0 for i in range(7)] for i in range (6)]
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, pady=10)
lumiereRouge.grid(column=1, row=0, pady=10)
def victoire(vertical, horizontal, grille):
print(vertical, horizontal, grille)
# Lignes
for x in range(4):
if grille[vertical][x] == grille[vertical][x + 1] == grille[vertical][x + 2] == grille[vertical][x + 3] == 1:
print ("L'équipe rouge gagne cette manche")
if grille[vertical][x] == grille[vertical][x + 1] == grille[vertical][x + 2] == grille[vertical][x + 3] == 5:
print("L'équipe Jaune gagne cette manche")
# Colonnes
for y in range(3):
if grille[y][horizontal] == grille[y+1][horizontal] == grille[y+2][horizontal] == grille[y+3][horizontal] == 1:
print("L'équipe rouge gagne cette manche")
if grille[y][horizontal] == grille[y+1][horizontal] == grille[y+2][horizontal] == grille[y+3][horizontal] == 5:
print("L'équipe Jaune gagne cette manche")
# Diagonales
for x in range(3):
for y in range(4):
if grille[x][y] == grille[x+1][y+1] == grille[x+2][y+2] == grille[x+3][y+3] == grille[vertical][horizontal]:
print("Diagonale1")
for x in range(3):
for y in range(4):
if grille[x][y] == grille[x-1][y+1] == grille[x-2][y+2] == grille[x-3][y+3] == grille[vertical][horizontal]:
print("Diagonale2")
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
# Test s il y a un gagnant
victoire(vertical, horizontal, grilleDeJeu)
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)
# Passe au joueur suivant
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)
for i in range(7) :
Button(boiteBouton, text=" |O| ", font=("Courrier", 20), bg='orange', fg='white', command=lambda i=i: ajouterJeton(i, EQUIPE, jeton, grilleDeJeu)).grid(row=0, column=i+1, sticky=S)
boiteBouton.pack()
boiteGrille.pack()
fenetre.mainloop() |