Mode placement bille fonctionnel, bugs à corriger

This commit is contained in:
neox 2023-12-30 11:52:34 +01:00
parent 1b5fbc9e8c
commit 40c2714da8
No known key found for this signature in database
GPG Key ID: 2974E1D5F25DFCC8
2 changed files with 150 additions and 77 deletions

82
game.py
View File

@ -55,7 +55,9 @@ def calcul_coord(x,y):
calcule une nouvelle coordonnée calcule une nouvelle coordonnée
@params void @params void
@return une nouvelle position @return une nouvelle position
""" """
#print("COORD : x={}, y={}, res = {}".format(x, y, y * DIM + x))
return y * DIM + x return y * DIM + x
@ -65,13 +67,17 @@ class Plateau:
""" """
constructor constructor
""" """
self.trous = [ [False] * DIM * DIM ] * 2 # Espace 7 × 7, deux fois pour
# vertical et horizontal # Espace 7 × 7, deux fois pour
# vertical et horizontal
self.trous = [ [False] * DIM * DIM, [False] * DIM * DIM ]
self.billes = [] # liste de tuples (x,y,joueur) self.billes = [] # liste de tuples (x,y,joueur)
self.tirettes = [ [None] * DIM ] * 2 # Espace 7 , deux fois pour
# Espace 7 , deux fois pour # vertical et horizontal
# vertical et horizontal self.tirettes = [ [None] * DIM, [None] * DIM ]
self.générer_tirettes() self.générer_tirettes()
def générer_tirettes(self): def générer_tirettes(self):
@ -83,17 +89,50 @@ class Plateau:
""" """
for orientation in range(2): # vertical et horizontal for orientation in range(2): # vertical et horizontal
for i in range(DIM): for i in range(DIM):
self.tirettes[orientation][i] = \ tirette = Tirette(i+(orientation*DIM), self, orientation=orientation)
Tirette(i+(orientation*DIM), self) for trou in tirette.trous:
self.trous[orientation][tirette.calcul_pos(trou)] = True
self.tirettes[orientation][i] = tirette
print("Liste trous communs")
liste=[]
for i in range(DIM):
for j in range(DIM):
if self.trous[HORIZONTAL][i + j * DIM] and \
self.trous[VERTICAL][i + j * DIM]:
print("({}, {})".format(i,j))
liste.append((i,j))
print("Liste trous partiels horizontaux")
for i in range(DIM):
for j in range(DIM):
if self.trous[HORIZONTAL][i + j * DIM] and (i,j) not in liste:
print("({}, {})".format(i,j))
print("Liste trous partiels verticaux")
for i in range(DIM):
for j in range(DIM):
if self.trous[VERTICAL][i + j * DIM] and (i,j) not in liste:
print("({}, {})".format(i,j))
def est_ce_un_trou(self,x,y): def est_ce_un_trou(self,x,y):
""" """
la methode renvoie True si c'est un Trou , False sinon la methode renvoie True si c'est un Trou , False sinon
@params x l'abscisse et y l'ordonnée @params x l'abscisse et y l'ordonnée
@return boolean @return VERTICAL, si VERTICAL a un trou
HORIZONTAL, si HORIZONTAL a un trou
2, si trou aligné
0, si aucun trou
""" """
return self.trous[0][calcul_coord(x,y)] \ if self.trous[VERTICAL][calcul_coord(x,y)] and \
and self.trous[0][calcul_coord(x,y)] self.trous[HORIZONTAL][calcul_coord(x,y)]:
return 2
elif self.trous[VERTICAL][calcul_coord(x,y)]:
return VERTICAL
elif self.trous[HORIZONTAL][calcul_coord(x,y)]:
return HORIZONTAL
else:
return 0
def mise_à_jour(self): def mise_à_jour(self):
@ -104,7 +143,7 @@ class Plateau:
@return void @return void
""" """
for bille in self.billes: for bille in self.billes:
if self.est_ce_un_trou(bille[0], bille[1]): if self.est_ce_un_trou(bille[0], bille[1]) == 0:
self.billes.pop(bille) self.billes.pop(bille)
@ -114,6 +153,7 @@ class Tirette:
Constructor Constructor
""" """
self.numéro = numéro # définit son placement dans l'espace self.numéro = numéro # définit son placement dans l'espace
#print("Tirette n°{}".format(self.numéro))
self.plateau = plateau # lien vers le plateau de jeu self.plateau = plateau # lien vers le plateau de jeu
@ -123,17 +163,19 @@ class Tirette:
# du point 0 de la tirette (bas ou gauche selon # du point 0 de la tirette (bas ou gauche selon
# orientation) # orientation)
# position initiale à 0
self.position = 0
# Si rien fourni, générer 50% de trous # Si rien fourni, générer 50% de trous
if len(self.trous) == 0: if len(self.trous) == 0:
for i in range(0,DIM): for i in range(DIM):
tirage = random.random() * 100 tirage = random.random() * 100
if (tirage < 50): if (tirage < 50):
self.trous.append(i) self.trous.append(i)
# position initiale à 0 print("Tirette n°{} a les trous : {}".format(self.numéro, self.trous))
self.position = 0
def calcul_pos(self, offset): def calcul_pos(self, offset):
""" """
@ -144,8 +186,10 @@ class Tirette:
if self.orientation == VERTICAL: if self.orientation == VERTICAL:
return calcul_coord(self.numéro, offset - self.position) return calcul_coord(self.numéro, offset - self.position)
elif self.orientation == HORIZONTAL:
return calcul_coord(offset - self.position, self.numéro - DIM)
else: else:
return calcul_coord(offset - self.position, self.numéro) raise()
def déplacer(self, nouvelle_position): def déplacer(self, nouvelle_position):
""" """
@ -161,11 +205,11 @@ class Tirette:
# Nettoie l'ancienne position du trou si dans le plateau # Nettoie l'ancienne position du trou si dans le plateau
if trou - self.position > 0: if trou - self.position > 0:
self.plateau.trous[self.orientation][calcul_pos(trou)] = False self.plateau.trous[self.orientation][self.calcul_pos(trou)] = False
# Met à jour la position du trou dans l'espace si dans le plateau # Met à jour la position du trou dans l'espace si dans le plateau
if trou - nouvelle_position > 0: if trou - nouvelle_position > 0:
self.plateau.trous[self.orientation][calcul_pos(trou + self.plateau.trous[self.orientation][self.calcul_pos(trou +
(nouvelle_position - ancienne_position))] = True (nouvelle_position - ancienne_position))] = True
# Mise à jour de la position de la tirette # Mise à jour de la position de la tirette
@ -185,7 +229,7 @@ class Joueur:
@return boolean @return boolean
""" """
if not self.plateau.est_ce_un_trou(x,y): if not self.plateau.est_ce_un_trou(x,y) == 2:
if not (x,y,self) in self.plateau.billes: if not (x,y,self) in self.plateau.billes:
self.plateau.billes.append((x,y,self)) self.plateau.billes.append((x,y,self))
return True return True

145
piege.py
View File

@ -14,24 +14,17 @@ import game
## VARIABLES DE CONFIGURATION ## VARIABLES DE CONFIGURATION
LARGEUR_FENETRE = 900 LARGEUR_FENETRE = 900
HAUTEUR_FENETRE = 1000 HAUTEUR_FENETRE = 900
OFFSET_X = 230
OFFSET_Y = 100
DIM = 7 DIM = game.DIM
VERTICAL = 0 VERTICAL = game.VERTICAL
HORIZONTAL = 1 HORIZONTAL = game.HORIZONTAL
def phase1(joueur,joueur2,plateau): PLACEMENT=0
compteur=0 TIRETTES=1
while(True): FIN=2
print( "{}, c'est à vous de poser une bille ! ".format(i) +
"Entrez les coordonnées x et y séparées par une virgule:")
demande=input()
def clic_sur_case(i, j):
if joueur1.game.placer_bille(i,j)==True:
return True
elif joueur2.game.placer_bille(i,j)==True:
return True
def run_game(joueur1, joueur2, plateau): def run_game(joueur1, joueur2, plateau):
""" """
@ -41,6 +34,15 @@ def run_game(joueur1, joueur2, plateau):
@return void @return void
""" """
# A variable that allows to store/check the current phase during the game
gamephase = PLACEMENT
retry = False
# Current player (XXX, joueur1 commence toujours)
currentplayer = joueur1
nextplayer = joueur2
fltk.cree_fenetre(LARGEUR_FENETRE, HAUTEUR_FENETRE) fltk.cree_fenetre(LARGEUR_FENETRE, HAUTEUR_FENETRE)
fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/20, fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/20,
"Pièges", "Pièges",
@ -48,12 +50,6 @@ def run_game(joueur1, joueur2, plateau):
taille=30, taille=30,
ancrage='center') ancrage='center')
fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/8,
"Tapez Echap pour quitter le jeu",
couleur="green",
taille=10,
ancrage='center')
modeles = { modeles = {
"tirette_horiz":"sprites/tirette.png", "tirette_horiz":"sprites/tirette.png",
"barre_horiz":"sprites/barre.png", "barre_horiz":"sprites/barre.png",
@ -64,17 +60,39 @@ def run_game(joueur1, joueur2, plateau):
"tour_de_plateau":"sprites/bois3.png", "tour_de_plateau":"sprites/bois3.png",
"case1":"sprites/bois1.png", "case1":"sprites/bois1.png",
"case2":"sprites/bois2.png", "case2":"sprites/bois2.png",
"trou": "sprites/trou.png" "trou": "sprites/trou.png",
"trou_horiz": "sprites/trou_partiel1.png",
"trou_vert": "sprites/trou_partiel2.png"
} }
OFFSET_X = 230
OFFSET_Y = 230
# Boucle principale # Boucle principale
while True: while gamephase != FIN:
fltk.mise_a_jour() fltk.mise_a_jour()
plateau.mise_à_jour() plateau.mise_à_jour()
if not retry:
# inversion des rôles
currentplayer, nextplayer = nextplayer, currentplayer
if currentplayer is joueur1:
strjoueur = "joueur1"
elif currentplayer is joueur2:
strjoueur = "joueur2"
if gamephase == PLACEMENT:
strphase = "placement des billes"
elif gamephase == TIRETTES:
strphase = "manipulation des tirettes"
fltk.efface("joueur")
fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/10,
"Phase {} : au tour du {}".format(strphase, strjoueur),
couleur="green",
taille=10,
ancrage='center',
tag="joueur")
else:
retry = False
# Affichage plateau # Affichage plateau
# Tirettes verticales # Tirettes verticales
@ -131,8 +149,6 @@ def run_game(joueur1, joueur2, plateau):
# Choix du type de sprite selon position # Choix du type de sprite selon position
if (i+j) % 2 == 0: if (i+j) % 2 == 0:
type_modele = "case1" type_modele = "case1"
#elif trou:
# type_modele = trou
else: else:
type_modele = "case2" type_modele = "case2"
@ -144,11 +160,18 @@ def run_game(joueur1, joueur2, plateau):
hauteur=60, hauteur=60,
tag="{},{}".format(i,j)) tag="{},{}".format(i,j))
#XXX TROUS potentiel_trou = plateau.est_ce_un_trou(i,j)
if plateau.est_ce_un_trou(i,j): if potentiel_trou != 0:
if potentiel_trou == 2:
modele_trou = modeles["trou"]
elif potentiel_trou == HORIZONTAL:
modele_trou = modeles["trou_horiz"]
elif potentiel_trou == VERTICAL:
modele_trou = modeles["trou_vert"]
fltk.image( OFFSET_X + 2 + (i+1)*62, fltk.image( OFFSET_X + 2 + (i+1)*62,
OFFSET_Y + 20 + (j+1)*62, OFFSET_Y + 20 + (j+1)*62,
modeles["trou"], modele_trou,
ancrage = "center", ancrage = "center",
largeur=60, largeur=60,
hauteur=60, hauteur=60,
@ -157,8 +180,8 @@ def run_game(joueur1, joueur2, plateau):
# Billes # Billes
for bille in plateau.billes: for bille in plateau.billes:
if bille[2] is joueur1: if bille[2] is joueur1:
fltk.image( OFFSET_X + 2 + (i+1)*62, fltk.image( OFFSET_X + 2 + (bille[0]+1)*62,
OFFSET_Y + 20 + (j+1)*62, OFFSET_Y + 20 + (bille[1]+1)*62,
modeles["bille1"], modeles["bille1"],
ancrage = "center", ancrage = "center",
largeur=30, largeur=30,
@ -166,8 +189,8 @@ def run_game(joueur1, joueur2, plateau):
tag="{},{}".format(i,j)) tag="{},{}".format(i,j))
if bille[2] is joueur2: if bille[2] is joueur2:
fltk.image( OFFSET_X + 2 + (i+1)*62, fltk.image( OFFSET_X + 2 + (bille[0]+1)*62,
OFFSET_Y + 20 + (j+1)*62, OFFSET_Y + 20 + (bille[1]+1)*62,
modeles["bille2"], modeles["bille2"],
ancrage = "center", ancrage = "center",
largeur=30, largeur=30,
@ -181,49 +204,55 @@ def run_game(joueur1, joueur2, plateau):
event = fltk.attend_ev() event = fltk.attend_ev()
if "Quitte" in fltk.type_ev(event): if "Quitte" in fltk.type_ev(event):
for i in range(6): for i in range(DIM):
for j in range(6): for j in range(DIM):
fltk.efface("{},{}".format(i,j)) fltk.efface("{},{}".format(i,j))
fltk.ferme_fenetre() fltk.ferme_fenetre()
return None return
if "Touche" in fltk.type_ev(event) and "Escape" in fltk.touche(event): if "Touche" in fltk.type_ev(event) and "Escape" in fltk.touche(event):
for i in range(6): for i in range(DIM):
for j in range(6): for j in range(DIM):
fltk.efface("{},{}".format(i,j)) fltk.efface("{},{}".format(i,j))
fltk.ferme_fenetre() fltk.ferme_fenetre()
fltk.ferme_fenetre() return
return None
if "ClicGauche" in fltk.type_ev(event): if "ClicGauche" in fltk.type_ev(event):
# XXX à améliorer # XXX à améliorer
x = fltk.abscisse(event) x = fltk.abscisse(event)
y = fltk.ordonnee(event) y = fltk.ordonnee(event)
i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1)
j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1)
print("Clic sur coords ({},{})".format(x,y)) print("Clic sur coords ({},{})".format(x,y))
print(" case {},{}".format(i,j))
#clic_sur_case(i, j) if gamephase == PLACEMENT:
## XXX Gérer évènement de clic i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1)
j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1)
if i<DIM and j<DIM and i>=0 and j>=0:
print(" case {},{}".format(i,j))
retry = not currentplayer.placer_bille(i,j)
if len(plateau.billes) == 10:
gamephase = TIRETTES
elif gamephase == TIRETTES:
# XXX
i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1)
j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1)
print(" case {},{}".format(i,j))
## Fonction principale ## Fonction principale
def main(): def main():
while True: importlib.reload(fltk) # corrige un bug sérieux de fltk avec les images
importlib.reload(fltk) # corrige un bug sérieux de fltk avec les images
plateau = game.Plateau() plateau = game.Plateau()
joueur1 = game.Joueur(plateau) joueur1 = game.Joueur(plateau)
joueur2 = game.Joueur(plateau) joueur2 = game.Joueur(plateau)
run_game(joueur1, joueur2, plateau)
#XXX vérifier condition de sortie
run_game(joueur1, joueur2, plateau)
return 0 return 0
main() main()