From 40c2714da80d1dea4554a8e474d60b5776754066 Mon Sep 17 00:00:00 2001 From: neox Date: Sat, 30 Dec 2023 11:52:34 +0100 Subject: [PATCH] =?UTF-8?q?Mode=20placement=20bille=20fonctionnel,=20bugs?= =?UTF-8?q?=20=C3=A0=20corriger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.py | 82 +++++++++++++++++++++++-------- piege.py | 145 +++++++++++++++++++++++++++++++++---------------------- 2 files changed, 150 insertions(+), 77 deletions(-) diff --git a/game.py b/game.py index d2a00df..1bd28d0 100644 --- a/game.py +++ b/game.py @@ -55,7 +55,9 @@ def calcul_coord(x,y): calcule une nouvelle coordonnée @params void @return une nouvelle position - """ + """ + + #print("COORD : x={}, y={}, res = {}".format(x, y, y * DIM + x)) return y * DIM + x @@ -65,13 +67,17 @@ class Plateau: """ 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.tirettes = [ [None] * DIM ] * 2 - # Espace 7 , deux fois pour - # vertical et horizontal + # Espace 7 , deux fois pour + # vertical et horizontal + self.tirettes = [ [None] * DIM, [None] * DIM ] + self.générer_tirettes() def générer_tirettes(self): @@ -83,17 +89,50 @@ class Plateau: """ for orientation in range(2): # vertical et horizontal for i in range(DIM): - self.tirettes[orientation][i] = \ - Tirette(i+(orientation*DIM), self) + tirette = Tirette(i+(orientation*DIM), self, orientation=orientation) + 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): """ la methode renvoie True si c'est un Trou , False sinon @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)] \ - and self.trous[0][calcul_coord(x,y)] + if self.trous[VERTICAL][calcul_coord(x,y)] and \ + 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): @@ -104,7 +143,7 @@ class Plateau: @return void """ 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) @@ -114,6 +153,7 @@ class Tirette: Constructor """ 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 @@ -123,17 +163,19 @@ class Tirette: # du point 0 de la tirette (bas ou gauche selon # orientation) + # position initiale à 0 + self.position = 0 + # Si rien fourni, générer 50% de trous if len(self.trous) == 0: - for i in range(0,DIM): + for i in range(DIM): tirage = random.random() * 100 if (tirage < 50): self.trous.append(i) - # position initiale à 0 - self.position = 0 + print("Tirette n°{} a les trous : {}".format(self.numéro, self.trous)) def calcul_pos(self, offset): """ @@ -144,8 +186,10 @@ class Tirette: if self.orientation == VERTICAL: return calcul_coord(self.numéro, offset - self.position) + elif self.orientation == HORIZONTAL: + return calcul_coord(offset - self.position, self.numéro - DIM) else: - return calcul_coord(offset - self.position, self.numéro) + raise() def déplacer(self, nouvelle_position): """ @@ -161,11 +205,11 @@ class Tirette: # Nettoie l'ancienne position du trou si dans le plateau 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 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 # Mise à jour de la position de la tirette @@ -185,7 +229,7 @@ class Joueur: @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: self.plateau.billes.append((x,y,self)) return True diff --git a/piege.py b/piege.py index d426018..e775858 100755 --- a/piege.py +++ b/piege.py @@ -14,24 +14,17 @@ import game ## VARIABLES DE CONFIGURATION LARGEUR_FENETRE = 900 -HAUTEUR_FENETRE = 1000 +HAUTEUR_FENETRE = 900 +OFFSET_X = 230 +OFFSET_Y = 100 -DIM = 7 -VERTICAL = 0 -HORIZONTAL = 1 +DIM = game.DIM +VERTICAL = game.VERTICAL +HORIZONTAL = game.HORIZONTAL -def phase1(joueur,joueur2,plateau): - compteur=0 - while(True): - 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 +PLACEMENT=0 +TIRETTES=1 +FIN=2 def run_game(joueur1, joueur2, plateau): """ @@ -41,6 +34,15 @@ def run_game(joueur1, joueur2, plateau): @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.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/20, "Pièges", @@ -48,12 +50,6 @@ def run_game(joueur1, joueur2, plateau): taille=30, ancrage='center') - fltk.texte(LARGEUR_FENETRE/2, HAUTEUR_FENETRE/8, - "Tapez Echap pour quitter le jeu", - couleur="green", - taille=10, - ancrage='center') - modeles = { "tirette_horiz":"sprites/tirette.png", "barre_horiz":"sprites/barre.png", @@ -64,17 +60,39 @@ def run_game(joueur1, joueur2, plateau): "tour_de_plateau":"sprites/bois3.png", "case1":"sprites/bois1.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 - while True: + while gamephase != FIN: fltk.mise_a_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 # Tirettes verticales @@ -131,8 +149,6 @@ def run_game(joueur1, joueur2, plateau): # Choix du type de sprite selon position if (i+j) % 2 == 0: type_modele = "case1" - #elif trou: - # type_modele = trou else: type_modele = "case2" @@ -144,11 +160,18 @@ def run_game(joueur1, joueur2, plateau): hauteur=60, tag="{},{}".format(i,j)) - #XXX TROUS - if plateau.est_ce_un_trou(i,j): + potentiel_trou = 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, OFFSET_Y + 20 + (j+1)*62, - modeles["trou"], + modele_trou, ancrage = "center", largeur=60, hauteur=60, @@ -157,8 +180,8 @@ def run_game(joueur1, joueur2, plateau): # Billes for bille in plateau.billes: if bille[2] is joueur1: - fltk.image( OFFSET_X + 2 + (i+1)*62, - OFFSET_Y + 20 + (j+1)*62, + fltk.image( OFFSET_X + 2 + (bille[0]+1)*62, + OFFSET_Y + 20 + (bille[1]+1)*62, modeles["bille1"], ancrage = "center", largeur=30, @@ -166,8 +189,8 @@ def run_game(joueur1, joueur2, plateau): tag="{},{}".format(i,j)) if bille[2] is joueur2: - fltk.image( OFFSET_X + 2 + (i+1)*62, - OFFSET_Y + 20 + (j+1)*62, + fltk.image( OFFSET_X + 2 + (bille[0]+1)*62, + OFFSET_Y + 20 + (bille[1]+1)*62, modeles["bille2"], ancrage = "center", largeur=30, @@ -181,49 +204,55 @@ def run_game(joueur1, joueur2, plateau): event = fltk.attend_ev() if "Quitte" in fltk.type_ev(event): - for i in range(6): - for j in range(6): + for i in range(DIM): + for j in range(DIM): fltk.efface("{},{}".format(i,j)) fltk.ferme_fenetre() - return None + return if "Touche" in fltk.type_ev(event) and "Escape" in fltk.touche(event): - for i in range(6): - for j in range(6): + for i in range(DIM): + for j in range(DIM): fltk.efface("{},{}".format(i,j)) fltk.ferme_fenetre() - fltk.ferme_fenetre() - return None + return if "ClicGauche" in fltk.type_ev(event): # XXX à améliorer x = fltk.abscisse(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(" case {},{}".format(i,j)) - #clic_sur_case(i, j) - ## XXX Gérer évènement de clic + if gamephase == PLACEMENT: + i = int( (x - OFFSET_X - 2 + 62/2)/62 - 1) + j = int( (y - OFFSET_Y - 20 + 62/2)/62 - 1) + + if 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 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) - joueur2 = game.Joueur(plateau) - - run_game(joueur1, joueur2, plateau) - - #XXX vérifier condition de sortie + joueur1 = game.Joueur(plateau) + joueur2 = game.Joueur(plateau) + run_game(joueur1, joueur2, plateau) return 0 main()