class Area(object): ''' Terrain du jeux ''' def __init__(self, namePlayer1, namePlayer2): ''' Constructor @param namePlayer1: Nom du joueur 1 @param namePlayer2: Nom du joueur 2 ''' self.tour = 1 self.pioche = Pioche() deckPlayer1 = Deck("deck1", self.pioche.getDecks("deck1")) deckPlayer2 = Deck("deck1", self.pioche.getDecks("deck2")) self.player1 = Player(1, namePlayer1, deckPlayer1) self.player2 = Player(2, namePlayer2, deckPlayer2) def inputAction(self, message): ''' Demande de saisie utilisateur @param message: prompt @return: int ''' ID = -1 try: ID = input(message + "[int]# ") ID = int(ID) except EOFError: print("Saisie incorrecte EOFError=") ID = -1 except NameError: print("Saisie incorrecte NameError") ID = -1 except ValueError: print("Saisie incorrecte ValueError") ID = -1 return ID def gameLoop(self): ''' Boucle du jeux ''' stop = False while stop == False: if self.player1.health <= 0: print(self.player1.name + " a perdu et " + self.player2.name + " a GAGNER !!!!") stop = True if self.player2.health <= 0: print(self.player2.name + " a perdu et " + self.player1.name + " a GAGNER !!!!") stop = True self.playerTurn_v2(self.player1, self.player2) self.playerTurn_v2(self.player2, self.player1) self.tour = self.tour + 1 self.player1.nextTour(self.tour) self.player2.nextTour(self.tour) print("Tous le monde Pioche une carte") try: self.player1.piocheCarte() except GameException as e: print(self.player1.name + " " + str(e)) try: self.player2.piocheCarte() except GameException as e: print(self.player2.name + " " + str(e)) def gameLoopNetworkHost(self, host, conn): ''' Boucle du jeux en réseau pour l'host ''' stop = False while stop == False: if self.player1.health <= 0: print(self.player1.name + " a perdu et " + self.player2.name + " a GAGNER !!!!") stop = True if self.player2.health <= 0: print(self.player2.name + " a perdu et " + self.player1.name + " a GAGNER !!!!") stop = True self.playerTurn_v2(self.player1, self.player2) self = host.run(self, conn) self.tour = self.tour + 1 self.player1.nextTour(self.tour) self.player2.nextTour(self.tour) print("Tous le monde Pioche une carte") try: self.player1.piocheCarte() except GameException as e: print(self.player1.name + " " + str(e)) try: self.player2.piocheCarte() except GameException as e: print(self.player2.name + " " + str(e)) host.close(conn) def gameLoopNetworkClient(self): self.playerTurn_v2(self.player2, self.player1) return self def playerTurn_v2(self, player, ennemy): ''' Les interaction du joueur pendant le tour @param player: Joueur @param ennemy: Joueur adverse ''' validator = True while validator: print(self) try: ID_attack = random.randint(1,len(self.player.hand)) if player.isCarteServant(ID_attack): # Si c'est une carte d'invocation des serviteur player.invoke(ID_attack) validator = self.verifActionJoueur(player) else: #Si ce n'est pas une carte sort ID_target = random.randint(1,len(self.enemy.fields)) player.war(ID_attack, ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) def playerTurn(self, player, ennemy): ''' Action du joueur @param player: Joueur @param ennemy: Joueur adverse ''' validator = True while validator: print(self.toString()) ID = random.randint(1,len(self.player.hand)) elif self.isPlayer(ID): # Si le joueur a choisie lui meme if int(player.mana) >= 2: ID_target = random.randint(1,len(self.enemy.fields)) player.fight(ID_target, ennemy) validator = self.verifActionJoueur(player) else: print(player.name + " : Je n'ai pas suffisamment de mana") elif self.isSpell(ID): # Si le joueur choisie un sort try: ID_target = random.randint(1,len(self.enemy.fields)) player.useCarteSpell(ID, ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) elif player.isCarteServant(ID): # Si le joueur a choisie d'invoquer un serviteur try: player.invoke(ID) validator = self.verifActionJoueur(player) except GameException as e: print(e)
class GameScene(Scene): ''' Une scene pour le chargement des données ''' def __init__(self, l): ''' Constructeur de la class LoadScene ''' self.width = pygame.display.get_surface().get_width() self.height = pygame.display.get_surface().get_height() self.loader = l self.action = 0 self.actionCallBack = False self.nt = False self.tour = 1 self.pioche = Pioche() deckPlayer1 = Deck("deck1", self.pioche.getDecks("deck1")) deckPlayer2 = Deck("deck2", self.pioche.getDecks("deck2")) self.player1 = Player(1, "Egor", deckPlayer1) self.player2 = Player(2, "Quentin", deckPlayer2) self.lastAnimCard = None l.clearAnimation() l.addAnimationByPath('bg', '../img/background.jpg') l.addAnimationByPath('showCard', '../img/card/spellCard.jpg', self.width / 2 - 75, self.height / 2 - 105, level=4) l.addAnimationByPath('pioche1', '../img/card/backSpellCard.jpg', level=1) l.addAnimationByPath('pioche2', '../img/card/backSpellCard.jpg', level=1) l.addAnimationByPath('passerTourJoueur', '../img/passer.png', self.width / 2 - 140, self.height / 2 - 50, level=1) self.btnPasser = Button('passerTourJoueur', '../img/passer.png', '../img/passer_select.png', self.partieSolo) self.btnPasser.loadButton(self.width / 2 - 140, self.height / 2 - 50, 280, 50, self.width, self.height) selection = l.getAnimation('pioche1') if len(selection) > 0: selection[0].newPos(self.width - 200, self.height / 2 - 105 - 110, 75, 105, 0, None) selection = l.getAnimation('pioche2') if len(selection) > 0: selection[0].newPos(self.width - 200, self.height / 2 + 105, 75, 105, 0, None) self.levelMaxPlayer = 3 self.levelMaxEnemy = 3 self.cardPlayer = [] for i in range(5, 8): self.cardPlayer.append(CardInfo(l, self.player1.deck.getCarte(), i)) self.cardEnemy = [] for i in range(5, 8): self.cardEnemy.append(CardInfo(l, self.player2.deck.getCarte(), i)) self.resizeWindow(l, self.width, self.height) self.ReturnScene = self self.changeText(l, "INFO") def asyncAnimDeckPlayerFinal(self, anim): if not (self.lastAnimCard == None): self.actionCallBack = False self.lastAnimCard = None def asyncAnimDeckPlayer(self, anim): if not (self.lastAnimCard == None): self.lastAnimCard.setResize(self.width - 5 - (80 * len(self.cardPlayer)), self.height - 115, 75, 105, 40, self.asyncAnimDeckPlayerFinal) def animDeckPlayer(self): self.levelMaxPlayer += 1 card = CardInfo(self.loader, self.player1.deck.getCarte(), self.levelMaxPlayer) card.setResize(self.width - 163 , self.height / 2 + 152, 0, 0, 0, None) card.setResize(self.width - 200, self.height / 2 + 105, 75, 105, 20, self.asyncAnimDeckPlayer) self.cardPlayer.append(card) self.lastAnimCard = card def asyncAnimDeckEnemyFinal(self, anim): if not (self.lastAnimCard == None): self.actionCallBack = False self.lastAnimCard = None def asyncAnimDeckEnemy(self, anim): if not (self.lastAnimCard == None): self.lastAnimCard.setResize(-70 + (80 * len(self.cardPlayer)), 10, 75, 105, 40, self.asyncAnimDeckEnemyFinal) def animDeckEnemy(self): self.levelMaxPlayer += 1 card = CardInfo(self.loader, self.player2.deck.getCarte(), self.levelMaxPlayer) card.setResize(self.width - 163 , self.height / 2 - 53 - 110, 0, 0, 0, None) card.setResize(self.width - 200, self.height / 2 - 105 - 110, 75, 105, 20, self.asyncAnimDeckEnemy) self.cardEnemy.append(card) self.lastAnimCard = card def asyncChangeText(self, anim): anim.newPos((self.width / 2), (self.height / 2), 0, 0, 50, None) self.actionCallBack = False def changeText(self, l, message): texts = l.getFont('mainTitle') if len(texts) > 0: sprite = Sprite(texts[0].render(message, 1, (255, 255, 0))) animation = Animation(sprite) animation.newPos((self.width / 2 - sprite.w / 2), (self.height / 2 - sprite.h / 2) + 60, sprite.w, sprite.h, 0) animation.newPos((self.width / 2 - sprite.w / 2), (self.height / 2 - sprite.h / 2) + 60, sprite.w, sprite.h, 50, self.asyncChangeText) l.removeAnimation('info') l.addAnimation('info', animation) def partieSolo(self, e): self.nt = True def statusText(self, l): texts = l.getFont('mainTitle') # PLayer 1 ( Down ) if len(texts) > 0: sprite = Sprite(texts[0].render("VIE: " + str(self.player1.health) + "/30", 1, (255, 255, 0))) spriteBis = Sprite(texts[0].render("MANA: " + str(self.player1.mana) + "/10", 1, (255, 255, 0))) spriteTer = Sprite(texts[0].render("" + str(self.player1.name) + "", 1, (255, 255, 0))) animation = Animation(sprite) animationBis = Animation(spriteBis) animationTer = Animation(spriteTer) animation.newPos(self.width - (sprite.w / 2) - 10, (self.height / 2 - sprite.h / 2) - 10 - (sprite.h / 2), sprite.w / 2, sprite.h / 2, 0) animationBis.newPos(self.width - (sprite.w / 2) - 10, (self.height / 2 - sprite.h / 2), sprite.w / 2, sprite.h / 2, 0) animationTer.newPos(self.width - (sprite.w / 2) - 10, (self.height / 2 - sprite.h / 2) - (10 - (sprite.h / 2) * 2), sprite.w / 2, sprite.h / 2, 0) l.removeAnimation('infoVieP1') l.addAnimation('infoVieP1', animation, level=5) l.removeAnimation('infoManaP1') l.addAnimation('infoManaP1', animationBis, level=5) l.removeAnimation('infoNameP1') l.addAnimation('infoNameP1', animationTer, level=5) # PLayer 1 ( UP ) if len(texts) > 0: sprite = Sprite(texts[0].render("VIE: " + str(self.player2.health) + "/30", 1, (255, 255, 0))) spriteBis = Sprite(texts[0].render("MANA: " + str(self.player2.mana) + "/10", 1, (255, 255, 0))) spriteTer = Sprite(texts[0].render("" + str(self.player2.name) + "", 1, (255, 255, 0))) animation = Animation(sprite) animationBis = Animation(spriteBis) animationTer = Animation(spriteTer) animation.newPos(10, (self.height / 2 - sprite.h / 2) - 10 - (sprite.h / 2), sprite.w / 2, sprite.h / 2, 0) animationBis.newPos(10, (self.height / 2 - sprite.h / 2), sprite.w / 2, sprite.h / 2, 0) animationTer.newPos(10, (self.height / 2 - sprite.h / 2) - (10 - (sprite.h / 2) * 2), sprite.w / 2, sprite.h / 2, 0) l.removeAnimation('infoVieP2') l.addAnimation('infoVieP2', animation, level=5) l.removeAnimation('infoManaP2') l.addAnimation('infoManaP2', animationBis, level=5) l.removeAnimation('infoNameP2') l.addAnimation('infoNameP2', animationTer, level=5) selection = l.getAnimation('pioche1') if len(selection) > 0: selection[0].newPos(self.width - 200, self.height / 2 - 105 - 110, 75, 105, 0, None) selection = l.getAnimation('pioche2') if len(selection) > 0: selection[0].newPos(self.width - 200, self.height / 2 + 105, 75, 105, 0, None) def quitterCallBack(self, nom): self.ReturnScene = None def retourCallBack(self, nom): from scene.MainScene import MainScene self.ReturnScene = MainScene(self.loader) def updateMain(self, e, l): x = 10 y = 10 for cardUpdateEnemy in self.cardEnemy: if self.lastAnimCard != cardUpdateEnemy: cardUpdateEnemy.update(e, l) cardUpdateEnemy.updateResize(self.width, self.height) cardUpdateEnemy.setPosition(x, y, 0) x += cardUpdateEnemy.width + 5 x = self.width - 85 y = self.height - 115 for cardUpdatePlayer in self.cardPlayer: if self.lastAnimCard != cardUpdatePlayer: cardUpdatePlayer.update(e, l) cardUpdatePlayer.updateResize(self.width, self.height) cardUpdatePlayer.setPosition(x, y, 0) x -= cardUpdatePlayer.width + 5 self.statusText(l) def update(self, e, l): ''' @param e: le gestionnaire d'événement @param l: le gestionnaire d'image @return: Scene à renvoyer ''' self.btnPasser.update(e, l) if (e.keyboard[pygame.K_ESCAPE]): e.keyboard[pygame.K_ESCAPE] = False self.retourCallBack('') self.loader.clearFont() if self.action == 0: #Get name and random begin print("Choix du nom ...") print("Choix du joueur commençant ...") self.changeText(l, "" + self.player1.name + " à vous !") self.actionCallBack = True self.action = 1 elif self.action == 1: if self.actionCallBack == False: self.animDeckPlayer() self.action = 2 self.actionCallBack = True elif self.action == 2: if self.actionCallBack == False: self.action = 10 elif self.action == 10: # Joueur 1 sur joueur 2 if e.keyboard[pygame.K_v] or self.nt: self.nt = False e.keyboard[pygame.K_v] = False self.changeText(l, "" + self.player2.name + " à vous !") self.action = 11 self.actionCallBack = True elif self.action == 11: if self.actionCallBack == False: self.animDeckEnemy() self.action = 12 self.actionCallBack = True elif self.action == 12: if self.actionCallBack == False: self.action = 20 elif self.action == 20: # Joueur 2 sur joueur 1 if e.keyboard[pygame.K_v] or self.nt: self.nt = False e.keyboard[pygame.K_v] = False self.changeText(l, "" + self.player1.name + " à vous !") self.actionCallBack = True self.action = 30 elif self.action == 30: # reset self.tour += 1 print("Tour passé : " + str(self.tour)) self.player1.nextTour(self.tour) self.player2.nextTour(self.tour) self.changeText(l, "" + self.player1.name + " à vous !") self.actionCallBack = True self.action = 1 elif self.action == 40: # Fin de partie pass elif self.action == 50: self.retourCallBack('') self.updateMain(e, l) if (e.quit): e.quit = True self.retourCallBack('') self.loader.clearFont() return self.ReturnScene def resizeWindow(self, l, w, h): self.width = w self.height = h backgrounds = l.getAnimation('bg') if len(backgrounds) > 0: backgrounds[0].newPos(0, 0, self.width, self.height, 0) self.btnPasser.resizeWindow(l, w, h) animation = l.getAnimation('title') if len(animation) > 0: animation[0].newPos((self.width / 2 - animation[0].sprite.w / 2), 30, animation[0].sprite.w, animation[0].sprite.h, 0) self.updateMain(None, l)
class Area(object): ''' Terrain du jeux ''' def __init__(self, namePlayer1, namePlayer2): ''' Constructor @param namePlayer1: Nom du joueur 1 @param namePlayer2: Nom du joueur 2 ''' self.tour = 1 self.pioche = Pioche() deckPlayer1 = Deck("deck1", self.pioche.getDecks("deck1")) deckPlayer2 = Deck("deck1", self.pioche.getDecks("deck2")) self.player1 = Player(1, namePlayer1, deckPlayer1) self.player2 = Player(2, namePlayer2, deckPlayer2) def inputAction(self, message): ''' Demande de saisie utilisateur @param message: prompt @return: int ''' ID = -1 try: ID = input(message + "[int]# ") ID = int(ID) except EOFError: print("Saisie incorrecte EOFError=") ID = -1 except NameError: print("Saisie incorrecte NameError") ID = -1 except ValueError: print("Saisie incorrecte ValueError") ID = -1 return ID def gameLoop(self): ''' Boucle du jeux ''' stop = False while stop == False: if self.player1.health <= 0: print(self.player1.name + " a perdu et " + self.player2.name + " a GAGNER !!!!") stop = True if self.player2.health <= 0: print(self.player2.name + " a perdu et " + self.player1.name + " a GAGNER !!!!") stop = True self.playerTurn_v2(self.player1, self.player2) #self.playerTurnIA(self.player2, self.player1) self.playerTurnIA(self.player2, self.player1) self.tour = self.tour + 1 self.player1.nextTour(self.tour) self.player2.nextTour(self.tour) print("Tous le monde Pioche une carte") try: self.player1.piocheCarte() except GameException as e: print(self.player1.name + " " + str(e)) try: self.player2.piocheCarte() except GameException as e: print(self.player2.name + " " + str(e)) def gameLoopNetworkHost(self, host, conn): ''' Boucle du jeux en réseau pour l'host ''' stop = False while stop == False: if self.player1.health <= 0: print(self.player1.name + " a perdu et " + self.player2.name + " a GAGNER !!!!") stop = True if self.player2.health <= 0: print(self.player2.name + " a perdu et " + self.player1.name + " a GAGNER !!!!") stop = True self.playerTurn_v2(self.player1, self.player2) self = host.run(self, conn) self.tour = self.tour + 1 self.player1.nextTour(self.tour) self.player2.nextTour(self.tour) print("Tous le monde Pioche une carte") try: self.player1.piocheCarte() except GameException as e: print(self.player1.name + " " + str(e)) try: self.player2.piocheCarte() except GameException as e: print(self.player2.name + " " + str(e)) host.close(conn) def gameLoopNetworkClient(self): self.playerTurn_v2(self.player2, self.player1) return self def playerTurn_v2(self, player, ennemy): ''' Les interaction du joueur pendant le tour @param player: Joueur @param ennemy: Joueur adverse ''' validator = True while validator: self.player1.enterrerServiteurs() self.player2.enterrerServiteurs() print(self) try: ID_attack = self.inputAction("<" + player.name + "> Id carte ou serviteur [passer=0]") if int(ID_attack) == 0: # Si le joueur souhaite passer son tour print(player.name + " passe son tour") validator = False elif int(ID_attack) < 0: print("Saisie incorrecte") else: if player.isCarteServant(ID_attack): # Si c'est une carte d'invocation des serviteur player.invoke(ID_attack) validator = self.verifActionJoueur(player) else: #Si ce n'est pas une carte sort ID_target = self.inputAction(player.name + " : entrer de la cible [annuler=0]") if int(ID_target) == 0: pass else: player.war(ID_attack, ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) def choiseTargetIA(self, ennemy): ''' Choisie le cible a attaquer ''' ID_Target = 2 if len(ennemy.fields) > 0: for ID_serv in ennemy.fields: if ennemy.fields[ID_serv].camouflage != True: ID_Target = ID_serv return ID_Target def playerTurnIA(self, player, ennemy): ''' Action du joueur @param player: Joueur @param ennemy: Joueur adverse ''' self.player1.enterrerServiteurs() self.player2.enterrerServiteurs() if len(player.fields): print("Attaques des serviteur") for ID_serv in player.fields: if player.fields[ID_serv].action == True: ID_Target = self.choiseTargetIA(ennemy) player.fields[ID_serv].fight(ID_Target, ennemy) print("Attaques du joueur") ID_Carte = 0 #go = False for carte in player.hand: if player.hand[carte].cost <= player.mana and player.isCarteServant(carte): print("IA choisie " + carte) ID_Carte = carte break if ID_Carte != 0: print("IA utilise " + carte) player.invoke(carte) ID_Carte = 0 ID_Target = 0 for carte in player.hand: if player.hand[carte].cost <= player.mana: #print(player.hand[carte]) if player.isSpell(carte): ID_Target = self.choiseTargetIA(ennemy) ID_Carte = carte if ID_Target != 0: player.useCarteSpell(ID_Carte, ID_Target, ennemy) ''' ID = self.inputAction("<" + player.name + "> Id carte ou serviteur [passer=0]") if int(ID) == 0: #Passer le tour validator = False elif self.isPlayer(ID): # Si le joueur a choisie lui meme if int(player.mana) >= 2: ID_target = self.inputAction(player.name + " : entrer de la cible [passer=0]") player.fight(ID_target, ennemy) validator = self.verifActionJoueur(player) else: print(player.name + " : Je n'ai pas suffisamment de mana") elif self.isSpell(ID): # Si le joueur choisie un sort try: ID_target = self.inputAction(player.name + " : entrer de la cible [passer=0]") player.useCarteSpell(ID, ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) elif player.isCarteServant(ID): # Si le joueur a choisie d'invoquer un serviteur try: player.invoke(ID) validator = self.verifActionJoueur(player) except GameException as e: print(e) elif self.isServant(ID): try: ID_target = self.inputAction(player.name + " : entrer de la cible (0 pour passer) ") servant = player.getServiteur(ID) servant.fight(ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) else: print("Saisie incorrecte ()") self.player1.enterrerServiteurs() self.player2.enterrerServiteurs() ''' def playerTurn(self, player, ennemy): ''' Action du joueur @param player: Joueur @param ennemy: Joueur adverse ''' validator = True while validator: print(self.toString()) ID = self.inputAction("<" + player.name + "> Id carte ou serviteur [passer=0]") if int(ID) == 0: #Passer le tour validator = False elif self.isPlayer(ID): # Si le joueur a choisie lui meme if int(player.mana) >= 2: ID_target = self.inputAction(player.name + " : entrer de la cible [passer=0]") player.fight(ID_target, ennemy) validator = self.verifActionJoueur(player) else: print(player.name + " : Je n'ai pas suffisamment de mana") elif self.isSpell(ID): # Si le joueur choisie un sort try: ID_target = self.inputAction(player.name + " : entrer de la cible [passer=0]") player.useCarteSpell(ID, ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) elif player.isCarteServant(ID): # Si le joueur a choisie d'invoquer un serviteur try: player.invoke(ID) validator = self.verifActionJoueur(player) except GameException as e: print(e) elif self.isServant(ID): try: ID_target = self.inputAction(player.name + " : entrer de la cible (0 pour passer) ") servant = player.getServiteur(ID) servant.fight(ID_target, ennemy) validator = self.verifActionJoueur(player) except GameException as e: print(e) else: print("Saisie incorrecte ()") self.player1.enterrerServiteurs() self.player2.enterrerServiteurs() def verifActionJoueur(self, joueur): ''' Verifie si l'utilisateur peut encore faire des actions @param joueur: Le joueur ''' flag = False if int(joueur.mana) > 0: # Si le joueur n'a plus de mana if joueur.action == True: flag = True for key in joueur.fields: if joueur.fields[key].action == True: flag = True return flag def toString(self): txt = "\n\n@@@@@@@@@@@@@@@ --------------- TOUR " + str(self.tour) + " --------------- @@@@@@@@@@@@@@@\n" txt += "ID=" + str(self.player1) txt += "ID=" + str(self.player2) return txt + "\n" def toString_v2(self): txt = "\n\n@@@@@@@@@@@@@@@ --------------- TOUR " + str(self.tour) + " --------------- @@@@@@@@@@@@@@@\n" txt += "ID=" + str(self.player1) txt += "ID=" + str(self.player2.ID) + ":" + str(self.player2.name) + ":[" + str(self.player2.mana) + "pm, " + str(self.player2.health) + "pv, a=" + str(self.player2.action) + "] " txt += " Fields :\n" for key in self.player2.fields: txt += "\t" + str(self.player2.fields[key]) + "\n" return txt + "}\n" def __str__(self): return self.toString_v2() def setPlayer1(self, player1): self.player1 = player1 def setPlayer2(self, player2): self.player2 = player2 def getPlayer1(self): return self.player1 def getPlayer2(self): return self.player2 def getTour(self): return self.tour