Partage
  • Partager sur Facebook
  • Partager sur Twitter

Aide Jeu Pygame

Mouvement de personnage

    13 février 2016 à 15:48:10

    Bonjour, je suis en train de créer mon tout premier programme avec pygame  et je suis confronté à un problème

    Mon programme consiste générer aléatoirement deux personnages (perso et perso1) qui bougent dans la fenêtre aléatoirement grâce à une boucle.  Le but est de les toucher à la souris . Quand l'un est touché , il s'arrête mais l'autre doit continuer son mouvement. Je suis confonté à ce problème: quand j'arrive à toucher à un des personnages, il s'arrête comme prévu mais l'autre au lieu de se déplacer se dupliquer en laissant l'image de lui au mouvement précédent! J'ajoute que ces deux personnages sont chargés à partir d'une image.

    Voici le code:

    import pygame
    from pygame.locals import *
    import random
    import time

    pygame.init()
    fenetre = pygame.display.set_mode((640,480))

    x = random.randint(0,520)
    y = random.randint(0,370)
    a = random.randint(0,520)
    b = random.randint(0,370)


    fond = pygame.image.load("background.jpg").convert()
    fenetre.blit(fond, (0,0))
                                     

    perso = pygame.image.load("perso.png").convert_alpha()
    fenetre.blit(perso, (x, y))

    perso1 = pygame.image.load("perso.png").convert_alpha()
    fenetre.blit(perso1, (a, b))





    continuer = True

    while continuer:         #boucle infinie
        persovivant = True
        persovivant1 = True

        x = random.randint(0,520)
        y = random.randint(0,370)
        a = random.randint(0,520)
        b = random.randint(0,370)

        for event in pygame.event.get():  
            if event.type == QUIT:
                continuer = False
       
            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                if x <= event.pos[0] <= (x+100) and y <=event.pos[1] <= (y+100):                            
                    persovivant = False
                if a <= event.pos[0] <= (a+100) and b <=event.pos[1] <= (b+100):                            
                    persovivant1 = False
            if persovivant == False:
                while persovivant1  == True:
                    a = random.randint(0,520)
                    b = random.randint(0,370)
                    fenetre.blit(perso1, (a, b))
                    pygame.display.flip()
                    time.sleep(1.5)
                    for event in pygame.event.get():  
                        if event.type == MOUSEBUTTONDOWN and event.button == 1:
                            if a <= event.pos[0] <= (a+100) and b <=event.pos[1] <= (b+100):                            
                                persovivant1 = False   
                continuer = False

            if persovivant1 == False:
                while persovivant  == True:
                    x = random.randint(0,520)
                    y = random.randint(0,370)
                    fenetre.blit(perso, (x, y))
                    pygame.display.flip()
                    time.sleep(1.5)
                    for event in pygame.event.get():  
                        if event.type == MOUSEBUTTONDOWN and event.button == 1:
                            if x <= event.pos[0] <= (x+100) and y <=event.pos[1] <= (y+100):                            
                                persovivant = False
                continuer = False
       
               
               
           
               
                           
        fenetre.blit(fond, (0,0))
        fenetre.blit(perso, (x, y))
        fenetre.blit(perso1, (a, b))
        pygame.display.flip()
        time.sleep(1.5)



           
               
       

    • Partager sur Facebook
    • Partager sur Twitter
      14 février 2016 à 11:44:42

      Mets ton code entre balises STP , merci
      • Partager sur Facebook
      • Partager sur Twitter
      http://sinclair.recreatedzxspectrum.com/index.php
        16 février 2016 à 19:33:05

        import pygame
        from pygame.locals import *
        import random
        import time
        
        pygame.init()
        fenetre = pygame.display.set_mode((640,480))
        
        x = random.randint(0,520)
        y = random.randint(0,370)
        a = random.randint(0,520)
        b = random.randint(0,370)
        
        
        fond = pygame.image.load("background.jpg").convert()
        fenetre.blit(fond, (0,0))
                                         
        
        perso = pygame.image.load("perso.png").convert_alpha()
        fenetre.blit(perso, (x, y))
        
        perso1 = pygame.image.load("perso.png").convert_alpha()
        fenetre.blit(perso1, (a, b))
        
        
        
        
        
        continuer = True
        
        while continuer:         
            persovivant = True
            persovivant1 = True
        
            x = random.randint(0,520)
            y = random.randint(0,370)
            a = random.randint(0,520)
            b = random.randint(0,370)
        
            for event in pygame.event.get():  
                if event.type == QUIT:
                    continuer = False
           
                if event.type == MOUSEBUTTONDOWN znd event.button == 1
                    if x <= event.pos[0] <= (x+100) and y <=event.pos[1] <= (y+100):                            
                        persovivant = False
                    if a <= event.pos[0] <= (a+100) and b <=event.pos[1] <= (b+100):                            
                        persovivant1 = False
                if persovivant == False:
                    while persovivant1  == True:
                        a = random.randint(0,520)
                        b = random.randint(0,370)
                        fenetre.blit(perso1, (a, b))
                        pygame.display.flip()
                        time.sleep(1.5)
                        for event in pygame.event.get():  
                            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                                if a <= event.pos[0] <= (a+100) and b <=event.pos[1] <= (b+100):                            
                                    persovivant1 = False   
                    continuer = False
        
                if persovivant1 == False:
                    while persovivant  == True:
                        x = random.randint(0,520)
                        y = random.randint(0,370)
                        fenetre.blit(perso, (x, y))
                        pygame.display.flip()
                        time.sleep(1.5)
                        for event in pygame.event.get():  
                            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                                if x <= event.pos[0] <= (x+100) and y <=event.pos[1] <= (y+100):                            
                                    persovivant = False
                    continuer = False
           
                   
                   
               
                   
                               
            fenetre.blit(fond, (0,0))
            fenetre.blit(perso, (x, y))
            fenetre.blit(perso1, (a, b))
            pygame.display.flip()
            time.sleep(1.5)
        
        • Partager sur Facebook
        • Partager sur Twitter
          16 février 2016 à 20:49:52

          J'ai changé mon code! Il semblerait que le problème venait de l'abscence de 1 ou 2 "fenetre.blit(fond, (0,0))", j'en ai profité pour rajouter des images: à leur mort: les images des personnages sont remplacés par des images de leur cadavre. Mais maitenant le programem semble avoir des lenteurs et des bugs comme des cadavres qui se déplacent.... , pouvez vous vérifier s'il y a des erreurs s'il vous plaît:

          import pygame
          from pygame.locals import *
          import random
          import time
          
          pygame.init()
          fenetre = pygame.display.set_mode((640,480))
          
          
          fond = pygame.image.load("background.jpg").convert()
          fenetre.blit(fond, (0,0))
                                           
          
          perso = pygame.image.load("perso.png").convert_alpha()
          
          perso1 = pygame.image.load("perso.png").convert_alpha()
          
          mort = pygame.image.load("mort.png").convert_alpha()
          
          mort2 = pygame.image.load("mort.png").convert_alpha()
          
          
          pygame.display.flip()
          
          continuer = True
          bcl =  True
          persovivant = True
          persovivant1 = True
              
          while continuer:
              if bcl == True:
                      fenetre.blit(fond, (0,0))
                      x = random.randint(0,520)
                      y = random.randint(0,370)
                      fenetre.blit(perso, (x, y))
                      a = random.randint(0,520)
                      b = random.randint(0,370)
                      fenetre.blit(perso1, (a, b))
                      pygame.display.flip()
                      time.sleep(1.5)
          
              for event in pygame.event.get():  
                  if event.type == QUIT:
                      bcl = False
                      continuer = False
                      
             
                  if event.type == MOUSEBUTTONDOWN and event.button == 1:
                      bcl = False
                      fenetre.blit(fond, (0,0))
                      x = random.randint(0,520)
                      y = random.randint(0,370)
                      fenetre.blit(perso, (x, y))
                      a = random.randint(0,520)
                      b = random.randint(0,370)
                      fenetre.blit(perso1, (a, b))
                      pygame.display.flip()
                      time.sleep(1.5)
                      if x <= event.pos[0] <= (x+100) and y <=event.pos[1] <= (y+100):
                          persovivant = False #pour éviter que le programme re entre dans cette boucle après en avoir terminer avec l'autre, juste au cas où
                          if persovivant1 == True: #pour éviter que le programme re entre dans cette boucle après en avoir terminer avec l'autre, juste au cas où
                              while continuer == True:                   
                                  for event in pygame.event.get():  
                                      if event.type == MOUSEBUTTONDOWN and event.button == 1:
                                          fenetre.blit(fond, (0,0))
                                          fenetre.blit(mort, (x, y))
                                          a = random.randint(0,520)
                                          b = random.randint(0,370)
                                          fenetre.blit(perso1, (a, b))
                                          pygame.display.flip()
                                          time.sleep(1.5)
                                          if a <= event.pos[0] <= (a+100) and b <=event.pos[1] <= (b+100):
                                             continuer = False
                                      
                              
                          
                      if a <= event.pos[0] <= (a+100) and b <=event.pos[1] <= (b+100):
                          persovivant1 = False #pour éviter que le programme re entre dans cette boucle après en avoir terminer avec l'autre, juste au cas où
                          if persovivant == True:   #pour éviter que le programme re entre dans cette boucle après en avoir terminer avec l'autre, juste au cas où
                              while continuer == True:                    
                                  for event in pygame.event.get():  
                                      if event.type == MOUSEBUTTONDOWN and event.button == 1:
                                          fenetre.blit(fond, (0,0))
                                          fenetre.blit(mort, (a, b))
                                          x = random.randint(0,520)
                                          y = random.randint(0,370)
                                          fenetre.blit(perso, (x, y))
                                          pygame.display.flip()
                                          time.sleep(1.5)
                                          if x <= event.pos[0] <= (x+100) and y <=event.pos[1] <= (y+100):
                                              continuer = False
          
          
          fenetre.blit(fond, (0,0))
          fenetre.blit(mort, (x, y))
          fenetre.blit(mort2, (a, b))
          pygame.display.flip()


          • Partager sur Facebook
          • Partager sur Twitter
            18 février 2016 à 15:35:19

            bonjour je viens d'essayer ton programme . Chez moi que tu cliques ou pas tes personnages s'affichent aléatoirement en continue. Je vais essayer de t'aider sachant que suis pas un expert ...

            Pour essayer d'y voir clair dans ton code tu blittes un peu partout dans ton code.....ligne 35 38 53 56 94 95 96 65 66 83 84 ....bref je m'y repere pas ...faudrait une fonction tres certainement

            Ensuite pour chaque sprite ou image que tu vas utiliser (je me doute que tu veux peut etre utiliser n images....) je penserais a creer une structure de données pour chaque sprites . on aura besoin :

            -d'une image a charger par exemple

            -de ses coordonnees pour blitter

            -une variable qui me dira si elle est touchée ou pas par mon curseur de souris, qui me servira a recalculer ou pas de nouvelles coordonnees aleatoires

            Dans un premier temps je te propose d'utiliser peut etre un dictionnaire pour tes sprites qui contiendrait ce qu'il faut pour gerer tes sprites.

            Ensuite pour organiser ton code de fabriquer des fonctions.

            Question : au niveau de ton apprentissage (si tu as lu le cours du site) tu en ai ou ?

            @+

            • Partager sur Facebook
            • Partager sur Twitter
            http://sinclair.recreatedzxspectrum.com/index.php
              18 février 2016 à 18:19:38

              tu peux me mettre tes images STP

              • Partager sur Facebook
              • Partager sur Twitter
              http://sinclair.recreatedzxspectrum.com/index.php

              Aide Jeu 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