Partage
  • Partager sur Facebook
  • Partager sur Twitter

erreur python (socket, class et pygame )

Sujet résolu
    6 août 2023 à 12:43:30

    import socket
    import pygame
    import json
    import pickle
    pygame.init()
    
    
    class Serveur:
        def __init__(self, game):
            self.game = game
            self.serveur_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.serveur_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.fichier_json = "assets/stats.json"
            self.client_socket = None
            self.continuer = True
            self.connected = False
            self.BLU = (40, 120, 230)
            self.GREEN = (40, 230, 120)
            self.BLANC = (255, 255, 255)
            self.message = ""
            self.font = pygame.font.SysFont("Arial", 30)
            self.identifianto = self.font.render(self.message, True, self.GREEN)
            self.screen = pygame.display.set_mode((0, 0))
            self.adresse = None
            self.sauvegare_complete = []
            self.att = 0
            self.deff = 0
            self.Vmax = 0
            self.manamax = 0
            self.speed = 0
            self.a = 0
            self.level = 0
            self.diviser1 = 0
            self.multiplier1 = 0
            self.v = 0
            self.mana = 0
            self.vitessea = 0
            self.manamaxa = 0
            self.levela = 0
            self.Vmaxa = 0
            self.aa = 0
            self.atta = 0
            self.deffa = 0
            self.diviser1a = 0
            self.multiplier1a = 0
            self.Va = 0
            self.manaa = 0
            self.comp1_button = pygame.image.load("assets/fireball.png")
            self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
            self.comp1_button_rect = self.comp1_button.get_rect()
            self.comp1_button_rect.x = self.screen.get_width() * 0.850 + 100
            self.comp1_button_rect.y = 100
            self.hitting = pygame.image.load("assets/punch.jpg")
            self.hitting = pygame.transform.scale(self.hitting, (100, 100))
            self.hitting_rect = self.hitting.get_rect()
            self.hitting_rect.x = self.screen.get_width() * 0.850 + 100
            self.hitting_rect.y = 0
            self.image = pygame.image.load("assets/mage.png")
            self.imagea = pygame.image.load("assets/mage.png")
            self.turn_text = self.font.render("", True, (200, 200, 200))
            self.sending = None
            self.winner = None
            self.invua = 0
    
        def boucle(self):
            while self.continuer:
                self.affichage()
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        self.continuer = False
    
        def affichage(self):
            background = pygame.image.load("assets/wall.png")
            font = pygame.font.SysFont("arial", 30)
            self.screen.blit(background, (0, 0))
            self.screen.blit(self.hitting, self.hitting_rect)
            self.screen.blit(self.comp1_button, self.comp1_button_rect)
            self.screen.blit(self.image, (self.screen.get_width() * 0.584, self.screen.get_height() * 0.351))
            self.screen.blit(self.adresse, (500, 500))
            if self.connected:
                self.screen.blit(self.imagea, (self.screen.get_width() * 0.380, self.screen.get_height() * 0.351))
                lifep = font.render(str(self.v) + "/" + str(self.Vmax), True, (200, 200, 200))
                manap = font.render(str(self.mana) + "/" + str(self.manamax), True, (200, 200, 200))
                lifem = font.render(str(self.Va) + "/" + str(self.Vmaxa), True, (200, 200, 200))
                manam = font.render(str(self.manaa) + "/" + str(self.manamaxa), True, (200, 200, 200))
                pygame.draw.rect(self.screen, (0, 0, 0), [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260, 200, 20])
                pygame.draw.rect(self.screen, (110, 210, 50), [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260, 200 * self.v / self.Vmax, 20])
                pygame.draw.rect(self.screen, (0, 0, 0), [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260 + 20, 200, 20])
                pygame.draw.rect(self.screen, (110, 50, 210), [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260 + 20, 200 * self.mana / self.manamax, 20])
                pygame.draw.rect(self.screen, (0, 0, 0), [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260, 200, 20])
                pygame.draw.rect(self.screen, (110, 210, 50), [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260, 200 * self.Va / self.Vmaxa, 20])
                pygame.draw.rect(self.screen, (0, 0, 0), [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260 + 20, 200, 20])
                pygame.draw.rect(self.screen, (110, 50, 210), [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260 + 20, 200 * self.mana / self.manamax, 20])
                self.screen.blit(lifep, (self.screen.get_width() * 0.584, self.screen.get_height() * 0.221))
                self.screen.blit(manap, (self.screen.get_width() * 0.584, self.screen.get_height() * 0.312))
                self.screen.blit(lifem, (self.screen.get_width() * 0.366, self.screen.get_height() * 0.221))
                self.screen.blit(manam, (self.screen.get_width() * 0.366, self.screen.get_height() * 0.312))
                self.screen.blit(self.turn_text, (self.screen.get_width() // 2, 5))
    
            pygame.display.flip()
    
        def recup(self):
            with open(self.fichier_json, "r") as f:
                stats = json.load(f)
            identifiant = self.game.pun.identifiant
            self.att = stats[identifiant]["attaque"]
            self.deff = stats[identifiant]["defense"]
            self.Vmax = stats[identifiant]["viemax"]
            self.manamax = stats[identifiant]["manamax"]
            self.speed = stats[identifiant]["vitesse"]
            self.a = stats[identifiant]["a"]
            self.level = stats[identifiant]["level"]
            self.diviser1 = stats[identifiant]["diviser1"]
            self.multiplier1 = stats[identifiant]["multiplier1"]
            self.v = self.Vmax
            self.mana = self.manamax
            if self.a == 0:
                self.image = pygame.image.load("assets/paladin.png")
                self.comp1_button = pygame.image.load("assets/slash.png")
                self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
            if self.a == 1:
                self.image = pygame.image.load("assets/assassin.png")
                self.comp1_button = pygame.image.load("assets/dagger.png")
                self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
            if self.a == 3:
                self.image = pygame.image.load("assets/wolf.png")
                self.image = pygame.transform.scale(self.image, (self.image.get_width() // 3, self.image.get_height() // 3))
                self.comp1_button = pygame.image.load("assets/claws.png")
                self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
    
        def fight_ini(self):
            if self.level >= self.levela:
                if self.level * 0.8 > self.levela:
                    self.continuer = False
            if self.levela >= self.level:
                if self.levela * 0.8 > self.level:
                    self.continuer = False
            if self.speed >= self.vitessea:
                self.attacking()
            else:
                self.defending()
    
        def attacking(self):
            self.turn_text = self.font.render("TON TOUR", True, (200, 200, 200))
            self.affichage()
            attack = True
            while attack:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        if self.hitting_rect.collidepoint(event.pos):
                            attack = False
                            self.punch()
                        if self.comp1_button_rect.collidepoint(event.pos):
                            if self.mana >= self.game.fight.player.cost1:
                                attack = False
                                self.attack()
            if self.invua != 0:
                self.invua -= 1
            self.defending()
    
        def defending(self):
            self.turn_text = self.font.render("TOUR DE L'ADVERSAIRE", True, (200, 200, 200))
            self.affichage()
            data = self.client_socket.recv(4096)
            if data[0] == "buff, attaque":
                self.atta = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            elif data[0] == "buff, vitesse":
                self.vitessea = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            elif data[0] == "buff, invu":
                self.invua = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            elif data[0] == "soins":
                self.deffa = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            else:
                self.v = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
    
        def envoi(self):
            print(self.att, self.v, self.mana, self.deff, self.speed, self.Va)
            if self.game.fight.player.action == "buff, attaque":
                stats = [self.game.fight.player.action, self.att, self.v, self.mana, self.deff]
            elif self.game.fight.player.action == "buff, vitesse":
                stats = [self.game.fight.player.action, self.speed, self.v, self.mana, self.deff]
            elif self.game.fight.player.action == "buff, invu":
                stats = [self.game.fight.player.action, self.game.fight.player.invu, self.v, self.mana, self.deff]
            elif self.game.fight.player.action == "soins":
                stats = [self.game.fight.player.action, self.deff, self.v, self.mana, self.deff]
            else:
                stats = [self.game.fight.player.action, self.Va, self.v, self.mana, self.deff]
            envoie = pickle.dumps(stats)
            self.client_socket.send(envoie)
            print(stats)
    
        def socket(self):
            self.recup()
            adresse_serveur = ''
            hostname = socket.gethostname()
            adresse_ip = socket.gethostbyname(hostname)
            octets = adresse_ip.split('.')
            adresse_ip = octets[-1]
            self.adresse = self.font.render(adresse_ip, True, (200, 0, 0))
            self.affichage()
            port = 12345
            self.serveur_socket.bind((adresse_serveur, port))
            self.serveur_socket.listen(1)
            self.client_socket, adresse_client = self.serveur_socket.accept()
            Stata = self.client_socket.recv(4096)
            liste_recue = pickle.loads(Stata)
            self.vitessea = liste_recue[0]
            self.levela = liste_recue[1]
            self.aa = liste_recue[2]
            self.atta = liste_recue[3]
            self.deffa = liste_recue[4]
            self.Vmaxa = liste_recue[5]
            self.manamaxa = liste_recue[6]
            self.diviser1a = liste_recue[7]
            self.multiplier1a = liste_recue[8]
            self.Va = self.Vmaxa
            self.manaa = self.manamaxa
            if self.aa == 0:
                self.imagea = pygame.image.load("assets/paladin.png")
            if self.aa == 1:
                self.imagea = pygame.image.load("assets/assassin.png")
            if self.aa == 3:
                self.imagea = pygame.image.load("assets/wolf.png")
                self.imagea = pygame.transform.scale(self.imagea, (self.imagea.get_width() // 3, self.imagea.get_height() // 3))
            print(liste_recue)
            self.connected = True
            Stats = [self.speed, self.level, self.a, self.att, self.deff, self.Vmax, self.manamax, self.diviser1, self.multiplier1]
            envoie = pickle.dumps(Stats)
            self.client_socket.send(envoie)
            self.adresse = self.font.render("", True, (0, 0, 0))
            self.affichage()
            self.fight_ini()
    
        def attack(self):
            self.game.fight.player.comp1()
            self.game.fight.player.cexp1 += 1
            if self.game.fight.player.cexp1 >= self.game.fight.player.cmexp1:
                self.game.fight.player.cexp1 -= self.game.fight.player.cmexp1
                self.game.fight.player.clevel1 += 1
                self.game.fight.player.cmexp1 *= 2
            self.mana -= self.game.fight.player.cost1
            if self.invua == 0:
                self.Va -= round(self.game.fight.player.damages1 * (1 - self.deffa / 1000))
            self.envoi()
            if self.Va <= 0:
                self.winner = "Server"
    
        def attack2(self):
            self.game.fight.player.comp2()
            self.game.fight.player.cexp2 += 1
            if self.game.fight.player.cexp2 >= self.game.fight.player.cmexp2:
                self.game.fight.player.cexp2 -= self.game.fight.player.cmexp2
                self.game.fight.player.clevel2 += 1
                self.game.fight.player.cmexp2 *= 2
            self.mana -= self.game.fight.player.cost2
            self.envoi()
            if self.Va <= 0:
                self.winner = "Server"
    
        def punch(self):
            if self.invua == 0:
                self.Va -= round(self.att * (1 - self.deffa / 1000))
            self.game.fight.player.action = "attaque"
            self.envoi()
            if self.Va <= 0:
                self.winner = "Server"
    
    #client
    import socket
    import pygame
    import json
    import pickle
    pygame.init()
    
    
    class Client:
        def __init__(self, game):
            self.game = game
            self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.continuer = True
            self.fichier_json = "assets/stats.json"
            self.BLU = (40, 120, 230)
            self.GREEN = (40, 230, 120)
            self.BLANC = (255, 255, 255)
            self.message = ""
            self.font = pygame.font.SysFont("Comic Sans MS,Arial", 24)
            self.identifianto = self.font.render(self.message, True, self.GREEN)
            self.user_input_rect = self.identifianto.get_rect(center=(160, 120))
            self.text_input_rect = pygame.Rect(70, 5, 160, 20)
            self.text_input_active = False
            self.screen = pygame.display.set_mode((0, 0))
            self.prompt2 = self.font.render("Entrez le Mot de passe : ", True, self.BLU)
            self.prompt2_rect = self.prompt2.get_rect(center=(37, 10))
            self.adresse_serveur = ""
            self.screen1 = pygame.display.set_mode((0, 0))
            self.text_input_active1 = True
            self.text_input_rect1 = pygame.Rect(180, 5, 270, 20)
            self.cookie = self.font.render(self.adresse_serveur, True, self.GREEN)
            self.cookie1 = self.cookie.get_rect(center=(160, 120))
            self.cookie2 = True
            self.user_input_rect = self.cookie.get_rect(center=(183, 11))
            self.messages = []
            self.att = 0
            self.deff = 0
            self.Vmax = 0
            self.manamax = 0
            self.speed = 0
            self.a = 0
            self.level = 0
            self.diviser1 = 0
            self.multiplier1 = 0
            self.v = 0
            self.mana = 0
            self.vitessea = 0
            self.manamaxa = 0
            self.levela = 0
            self.Vmaxa = 0
            self.aa = 0
            self.atta = 0
            self.deffa = 0
            self.diviser1a = 0
            self.multiplier1a = 0
            self.manaa = 0
            self.Va = 0
            self.connected = False
            self.comp1_button = pygame.image.load("assets/fireball.png")
            self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
            self.comp1_button_rect = self.comp1_button.get_rect()
            self.comp1_button_rect.x = self.screen.get_width() * 0.850 + 100
            self.comp1_button_rect.y = 100
            self.hitting = pygame.image.load("assets/punch.jpg")
            self.hitting = pygame.transform.scale(self.hitting, (100, 100))
            self.hitting_rect = self.hitting.get_rect()
            self.hitting_rect.x = self.screen.get_width() * 0.850 + 100
            self.hitting_rect.y = 0
            self.image = pygame.image.load("assets/mage.png")
            self.imagea = pygame.image.load("assets/mage.png")
            self.turn_text = self.font.render("", True, (200, 200, 200))
            self.invua = 0
            self.winner = ""
    
        def quitterpage(self):
            self.cookie2 = False
    
        def attack(self):
            self.game.fight.player.comp1()
            self.game.fight.player.cexp1 += 1
            if self.game.fight.player.cexp1 >= self.game.fight.player.cmexp1:
                self.game.fight.player.cexp1 -= self.game.fight.player.cmexp1
                self.game.fight.player.clevel1 += 1
                self.game.fight.player.cmexp1 *= 2
            self.mana -= self.game.fight.player.cost1
            if self.invua == 0:
                self.Va -= round(self.game.fight.player.damages1 * (1 - self.deffa / 1000))
            self.envoi()
            if self.Va <= 0:
                self.winner = "Server"
    
        def attack2(self):
            self.game.fight.player.comp2()
            self.game.fight.player.cexp2 += 1
            if self.game.fight.player.cexp2 >= self.game.fight.player.cmexp2:
                self.game.fight.player.cexp2 -= self.game.fight.player.cmexp2
                self.game.fight.player.clevel2 += 1
                self.game.fight.player.cmexp2 *= 2
            self.mana -= self.game.fight.player.cost2
            self.envoi()
            if self.Va <= 0:
                self.winner = "Server"
    
        def punch(self):
            if self.invua == 0:
                self.Va -= round(self.att * (1 - self.deffa / 1000))
            self.game.fight.player.action = "attaque"
            self.envoi()
            if self.Va <= 0:
                self.winner = "Server"
    
        def boucle_1(self):
            self.affichage()
            while self.cookie2:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        self.cookie2 = False
                        break
                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        if self.text_input_rect1.collidepoint(event.pos):
                            self.text_input_active1 = True
                            self.affichage()
                        else:
                            self.text_input_active1 = False
                            self.affichage()
                    elif event.type == pygame.KEYDOWN:
                        if event.key in (pygame.K_RETURN, pygame.K_KP_ENTER):
                            self.affichage()
                            if self.text_input_active1:
                                self.adresse_serveur = self.adresse_serveur.strip()
                                self.cookie2 = False
                        elif self.text_input_active1:
                            if event.key == pygame.K_BACKSPACE:
                                self.adresse_serveur = self.adresse_serveur[:-1]
                                self.affichage()
                            else:
                                self.adresse_serveur += event.unicode
                                self.affichage()
    
        def affichage(self):
            self.screen1.fill(self.BLANC)
            pygame.draw.rect(self.screen1, self.GREEN if self.text_input_active1 else self.BLU, self.text_input_rect1, 2)
            self.cookie1 = self.font.render(self.adresse_serveur, True, self.GREEN if self.text_input_active1 else self.BLU)
            self.screen1.blit(self.cookie1, self.user_input_rect)
            self.screen1.blit(self.prompt2, self.prompt2_rect)
            pygame.display.flip()
    
        def affichage1(self):
            background = pygame.image.load("assets/wall.png")
            font = pygame.font.SysFont("arial", 30)
            self.screen.blit(background, (0, 0))
            self.screen.blit(self.hitting, self.hitting_rect)
            self.screen.blit(self.comp1_button, self.comp1_button_rect)
            self.screen.blit(self.image, (self.screen.get_width() * 0.584, self.screen.get_height() * 0.351))
            if self.connected:
                self.screen.blit(self.imagea, (self.screen.get_width() * 0.380, self.screen.get_height() * 0.351))
                lifep = font.render(str(self.v) + "/" + str(self.Vmax), True, (200, 200, 200))
                manap = font.render(str(self.mana) + "/" + str(self.manamax), True, (200, 200, 200))
                lifem = font.render(str(self.Va) + "/" + str(self.Vmaxa), True, (200, 200, 200))
                manam = font.render(str(self.manaa) + "/" + str(self.manamaxa), True, (200, 200, 200))
                pygame.draw.rect(self.screen, (0, 0, 0),
                                 [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260, 200, 20])
                pygame.draw.rect(self.screen, (110, 210, 50),
                                 [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260,
                                  200 * self.v / self.Vmax, 20])
                pygame.draw.rect(self.screen, (0, 0, 0),
                                 [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260 + 20, 200, 20])
                pygame.draw.rect(self.screen, (110, 50, 210),
                                 [self.screen.get_width() * 0.584, self.screen.get_height() * 0.260 + 20,
                                  200 * self.mana / self.manamax, 20])
                pygame.draw.rect(self.screen, (0, 0, 0),
                                 [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260, 200, 20])
                pygame.draw.rect(self.screen, (110, 210, 50),
                                 [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260,
                                  200 * self.Va / self.Vmaxa, 20])
                pygame.draw.rect(self.screen, (0, 0, 0),
                                 [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260 + 20, 200, 20])
                pygame.draw.rect(self.screen, (110, 50, 210),
                                 [self.screen.get_width() * 0.367, self.screen.get_height() * 0.260 + 20,
                                  200 * self.mana / self.manamax, 20])
                self.screen.blit(lifep, (self.screen.get_width() * 0.584, self.screen.get_height() * 0.221))
                self.screen.blit(manap, (self.screen.get_width() * 0.584, self.screen.get_height() * 0.312))
                self.screen.blit(lifem, (self.screen.get_width() * 0.366, self.screen.get_height() * 0.221))
                self.screen.blit(manam, (self.screen.get_width() * 0.366, self.screen.get_height() * 0.312))
                self.screen.blit(self.turn_text, (self.screen.get_width() // 2, 5))
            pygame.display.flip()
    
        def recup(self):
            with open(self.game.pun.fichier_json, "r") as f:
                stats = json.load(f)
                identifiant = self.game.pun.identifiant
            self.att = stats[identifiant]["attaque"]
            self.deff = stats[identifiant]["defense"]
            self.Vmax = stats[identifiant]["viemax"]
            self.manamax = stats[identifiant]["manamax"]
            self.speed = stats[identifiant]["vitesse"]
            self.a = stats[identifiant]["a"]
            self.level = stats[identifiant]["level"]
            self.diviser1 = stats[identifiant]["diviser1"]
            self.multiplier1 = stats[identifiant]["multiplier1"]
            self.v = self.Vmax
            self.mana = self.manamax
            if self.a == 0:
                self.image = pygame.image.load("assets/paladin.png")
                self.comp1_button = pygame.image.load("assets/slash.png")
                self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
            if self.a == 1:
                self.image = pygame.image.load("assets/assassin.png")
                self.comp1_button = pygame.image.load("assets/dagger.png")
                self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
            if self.a == 3:
                self.image = pygame.image.load("assets/wolf.png")
                self.image = pygame.transform.scale(self.image, (self.image.get_width() // 3, self.image.get_height() // 3))
                self.comp1_button = pygame.image.load("assets/claws.png")
                self.comp1_button = pygame.transform.scale(self.comp1_button, (100, 100))
    
        def fight_ini(self):
            if self.level >= self.levela:
                if self.level * 0.8 > self.levela:
                    self.continuer = False
            if self.levela >= self.level:
                if self.levela * 0.8 > self.level:
                    self.continuer = False
            if self.speed > self.vitessea:
                self.attacking()
            else:
                self.defending()
    
        def attacking(self):
            self.turn_text = self.font.render("TON TOUR", True, (200, 200, 200))
            self.affichage1()
            attack = True
            while attack:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        if self.hitting_rect.collidepoint(event.pos):
                            attack = False
                            self.punch()
                        if self.comp1_button_rect.collidepoint(event.pos):
                            if self.mana >= self.game.fight.player.cost1:
                                attack = False
                                self.attack()
            if self.invua != 0:
                self.invua -= 1
            self.defending()
    
        def defending(self):
            self.turn_text = self.font.render("TOUR DE L'ADVERSAIRE", True, (200, 200, 200))
            self.affichage1()
            data = self.client_socket.recv(4096)
            if data[0] == "buff, attaque":
                self.atta = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            elif data[0] == "buff, vitesse":
                self.vitessea = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            elif data[0] == "buff, invu":
                self.invua = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            elif data[0] == "soins":
                self.deffa = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            else:
                self.v = data[1]
                self.Va = data[2]
                self.manaa = data[3]
                self.deffa = data[4]
                self.attacking()
            print(data)
    
        def envoi(self):
            if self.game.fight.player.action == "buff, attaque":
                stats = [self.game.fight.player.action, self.att, self.v, self.mana, self.deff]
            elif self.game.fight.player.action == "buff, vitesse":
                stats = [self.game.fight.player.action, self.speed, self.v, self.mana, self.deff]
            elif self.game.fight.player.action == "buff, invu":
                stats = [self.game.fight.player.action, self.game.fight.player.invu, self.v, self.mana, self.deff]
            elif self.game.fight.player.action == "soins":
                stats = [self.game.fight.player.action, self.deff, self.v, self.mana, self.deff]
            else:
                stats = [self.game.fight.player.action, self.Va, self.v, self.mana, self.deff]
            print(stats)
            envoie = pickle.dumps(stats)
            self.client_socket.send(envoie)
    
        def socket1(self):
            self.recup()
            port = 12345
            self.boucle_1()
            self.client_socket.connect(("192.168.1." + self.adresse_serveur, port))
            Stats = [self.speed, self.level, self.a, self.att, self.deff, self.Vmax, self.manamax, self.diviser1, self.multiplier1]
            envoie = pickle.dumps(Stats)
            self.client_socket.send(envoie)
            Stata = self.client_socket.recv(4096)
            liste_recue = pickle.loads(Stata)
            self.vitessea = liste_recue[0]
            self.levela = liste_recue[1]
            self.aa = liste_recue[2]
            self.atta = liste_recue[3]
            self.deffa = liste_recue[4]
            self.Vmaxa = liste_recue[5]
            self.manamaxa = liste_recue[6]
            self.diviser1a = liste_recue[7]
            self.multiplier1a = liste_recue[8]
            self.manaa = self.manamaxa
            self.Va = self.Vmaxa
            print(liste_recue)
            if self.aa == 0:
                self.imagea = pygame.image.load("assets/paladin.png")
            if self.aa == 1:
                self.imagea = pygame.image.load("assets/assassin.png")
            if self.aa == 3:
                self.imagea = pygame.image.load("assets/wolf.png")
                self.imagea = pygame.transform.scale(self.imagea, (self.imagea.get_width() // 3, self.imagea.get_height() // 3))
            self.connected = True
            self.adresse_serveur = ""
            self.quitterpage()
            self.fight_ini()

    Bonjour, j'ai un problème sur mon programme et je n'en comprends pas l'origine. Quand je fais une action, la valeur réceptionnée n'est pas la bonne mais je ne sais pas pourquoi
    . Pourriez vous m'aider svp?
    • Partager sur Facebook
    • Partager sur Twitter
      6 août 2023 à 14:07:50

      et quelles sont les valeurs sensées être envoyées et réceptionnées ?
      • Partager sur Facebook
      • Partager sur Twitter

      Python c'est bon, mangez-en. 

        6 août 2023 à 14:56:48

        TCP permet d'échanger des suites d'octets pas des messages.

        La différence est que pour un message on sait où il commence, où il se termine. On pourra ajouter des contrôles pour assurer qu'il est intègre/complet avant de récupérer les données qui sont dedans.    

        Le guide pratique de programmation avec les sockets qui vient avec Python mentionne quelques techniques pour y arriver.

        • Partager sur Facebook
        • Partager sur Twitter
          6 août 2023 à 15:01:35

          donc les valeurs envoyée sont dans les recup() grace à un fichiers json des 2 fichiers et celles réceptionnées  dcp  sont les même mais stockées dans d'autres variables du à l'envoie de liste recu qui est liste. 

          • Partager sur Facebook
          • Partager sur Twitter
            6 août 2023 à 15:22:43

            Problème résolu, merci de vos aides. Mon collègue ne lit pas ses tutos jusqu'au bout ;-; Le problème venait de pickle.dumps, il fallait faire pickle.loads à la réception.
            • Partager sur Facebook
            • Partager sur Twitter

            erreur python (socket, class et pygame )

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown