Isn Python bibliothèque Pygame

Fermé
MatthieuLem - Modifié le 26 mai 2019 à 00:43
 ghobi - 27 mai 2019 à 14:41
Bonjour j'ai un projet d'Isn a terminer (conception d'un jeu) celui ci est dailleur presque terminé or je rencontre un probleme auquel je n'arrive malheuresment pas a repondre dans la conception de mes plateforme python me dit que que ma list plateforme n'est pas attribué et je ne comprend pas. Si qq peut m'apporter son aide svp...
si vous avez besoin des images pour comprendre le bug je peux vous les envoyer par mail si besoin...

import pygame, sys
from pygame.locals import *
class Stickmanrouge(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.Run1DR=pygame.image.load("1RunDR.png").convert_alpha()
        self.Run2DR=pygame.image.load('2RunDR.png').convert_alpha()
        self.Run3DR=pygame.image.load('3RunDR.png').convert_alpha()
        self.Run4DR=pygame.image.load('4RunDR.png').convert_alpha()
        self.Run5DR=pygame.image.load('5RunDR.png').convert_alpha()
        self.Run6DR=pygame.image.load('6RunDR.png').convert_alpha()
        self.Run1GR=pygame.image.load('1RunGR.png').convert_alpha()
        self.Run2GR=pygame.image.load('2RunGR.png').convert_alpha()
        self.Run3GR=pygame.image.load('3RunGR.png').convert_alpha()
        self.Run4GR=pygame.image.load('4RunGR.png').convert_alpha()
        self.Run5GR=pygame.image.load('5RunGR.png').convert_alpha()
        self.Run6GR=pygame.image.load('6RunGR.png').convert_alpha()
        self.baseGR=pygame.image.load('deboutGR.png').convert_alpha()
        self.baseDR=pygame.image.load('deboutDR.png').convert_alpha()
        self.Jump1DR=pygame.image.load('1JumpDR.png').convert_alpha()
        self.Jump2DR=pygame.image.load('2JumpDR.png').convert_alpha()
        self.Jump1GR=pygame.image.load('1JumpGR.png').convert_alpha()
        self.Jump2GR=pygame.image.load('2JumpGR.png').convert_alpha()

        self.image = self.baseDR

        self.rect = self.image.get_rect()

# initialisation des caracteristique
        self.change_x =0
        self.change_y =0
        self.vie=4
        self.orientation=0
        self.anim=0
        self.tir=0
        self.score=0
        self.level = None





    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y
        self.anim+=1

        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:


            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right


        self.rect.y += self.change_y


        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:


            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            elif self.change_y < 0:
                self.rect.top = block.rect.bottom


            self.change_y = 0

        if self.rect.y<430:
            self.change_y+=0.5

        if self.rect.x<0:
            self.rect.x-=self.change_x

        if self.rect.x>1050:
            self.rect.x-=self.change_x

        if self.rect.y==430:
            self.saut=0

        if self.rect.y>430:
            self.rect.y=430
            self.change_y=0
            self.saut=0

        if self.orientation==0:
            if self.change_x==0 and self.change_y==0:
                self.image = self.baseGR
            elif self.change_y==0 :
                self.rect.y=455
                if 0<self.anim and self.anim<10:
                    self.image=self.Run1GR
                if 10<=self.anim and self.anim<20:
                    self.image=self.Run2GR
                if 20<=self.anim and self.anim<30:
                    self.image=self.Run3GR
                if 30<=self.anim and self.anim<40:
                    self.image=self.Run4GR
                if 40<=self.anim and self.anim<50:
                  self.image=self.Run5GR
                if 50<=self.anim and self.anim<60:
                   self.image=self.Run6GR
                if self.anim>=60:

                    self.anim=0
            else :
                if 0<self.anim and self.anim<40:
                    self.image=self.Jump1GR
                if 40<=self.anim and self.anim<80:
                    self.image=self.Jump2GR
##                if 80<=self.anim and self.anim<120:
##                    self.image=self.saut1GR
##                if 120<=self.anim and self.anim<160:
##                    self.image=self.saut2GR
##                if self.anim>=160:
                    self.anim=0


        if self.orientation==1:

            if self.change_x==0 and self.change_y==0:
                self.image = self.baseDR
            elif self.change_y==0 :
                self.rect.y=455
                if 0<self.anim and self.anim<10:
                    self.image=self.Run1DR
                if 10<=self.anim and self.anim<20:
                    self.image=self.Run2DR
                if 20<=self.anim and self.anim<30:
                    self.image=self.Run3DR
                if 30<=self.anim and self.anim<40:
                    self.image=self.Run4DR
                if 40<=self.anim and self.anim<50:
                  self.image=self.Run5DR
                if 50<=self.anim and self.anim<60:
                   self.image=self.Run6DR
                if self.anim>=60:
                    self.anim=0

                    self.anim=0

            else :
                if 0<self.anim and self.anim<40:
                    self.image=self.Jump1DR
                if 40<=self.anim and self.anim<80:
                    self.image=self.Jump2DR
##                if 80<=self.anim and self.anim<120:
##                    self.image=self.saut1DR
##                if 120<=self.anim and self.anim<160:
##                    self.image=self.saut2DR
##                if self.anim>=160:
                    self.anim=0





    def mvtgauche(self):
       """Mouvement gauche """
       self.change_x = -4
       self.change_y = 0
       self.orientation=0

    def mvtdroit(self):
        """Mouvement droite"""
        self.change_x = 4
        self.change_y = 0
        self.orientation=1

    def jump(self):
        if self.saut==0:
            self.saut=1
            self.change_y=-16
        if self.orientation==0:
            if self.change_y==0:
                self.image = self.baseGR
            else :
                if 0<self.anim and self.anim<-2:
                    self.image=self.Jump1GR
                if self.anim>=-2:
                    self.anim=0
        if self.orientation==1:
            if self.change_y==0:
                self.image = self.baseDB
            else :
                if 0<self.anim and self.anim<-2:
                    self.image=self.Jump1DR
                if self.anim>=-2:
                    self.anim=0


        # On detecte s'il y a une plate-forme en dessous
        self.rect.y += 1
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        self.rect.y -= 1

        # Si c'est le cas, on saute !
        if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
            self.change_y = -15


    def stop(self):
        """arret"""
        self.change_x=0


class ShurikenDR(pygame.sprite.Sprite):
 # initialisation
    def __init__(self):
        super().__init__()

        self.image=pygame.image.load('ShurikenRouge1.png').convert_alpha()
        self.rect = self.image.get_rect()

      #mouvement du shuriken
    def update(self):
        self.rect.x+=10
        if self.rect.x>1100:
            self.kill()

class ShurikenGR(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()

        self.image=pygame.image.load('ShurikenRouge1.png').convert_alpha()
        self.rect = self.image.get_rect()

        #mvt shuriken
    def update(self):
        self.rect.x-=10
        if self.rect.x<-21:
            self.kill()






class Stickmanbleu(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.Run1DB=pygame.image.load('1RunDB.png').convert_alpha()
        self.Run2DB=pygame.image.load('2RunDB.png').convert_alpha()
        self.Run3DB=pygame.image.load('3RunDB.png').convert_alpha()
        self.Run4DB=pygame.image.load('4RunDB.png').convert_alpha()
        self.Run5DB=pygame.image.load('5RunDB.png').convert_alpha()
        self.Run6DB=pygame.image.load('6RunDB.png').convert_alpha()
        self.Run1GB=pygame.image.load('1RunGB.png').convert_alpha()
        self.Run2GB=pygame.image.load('2RunGB.png').convert_alpha()
        self.Run3GB=pygame.image.load('3RunGB.png').convert_alpha()
        self.Run4GB=pygame.image.load('4RunGB.png').convert_alpha()
        self.Run5GB=pygame.image.load('5RunGB.png').convert_alpha()
        self.Run6GB=pygame.image.load('6RunGB.png').convert_alpha()
        self.baseGB=pygame.image.load('deboutGB.png').convert_alpha()
        self.baseDB=pygame.image.load('deboutDB.png').convert_alpha()
        self.Jump1DB=pygame.image.load('1JumpDB.png').convert_alpha()
        self.Jump2DB=pygame.image.load('2JumpDB.png').convert_alpha()
        self.Jump1GB=pygame.image.load('1JumpGB.png').convert_alpha()
        self.Jump2GB=pygame.image.load('2JumpGB.png').convert_alpha()
        self.image = self.baseGB
        self.rect = self.image.get_rect()


        self.change_x =0
        self.change_y =0
        self.vie=4
        self.orientation=1
        self.anim=0
        self.tir=0
        self.score=0
        self.level = None




    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y
        self.anim+=1

        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:
            # Si mouvement a droite, aligner le bord avec l'obstacle a droite
            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                # si mouvement a gauche, aligner le bord avec l'obstacle a gauche
                self.rect.left = block.rect.right

       # Mouvement haut / bas
        self.rect.y += self.change_y
        self.anim+=1

        # gestion des autres collisions (verticales)
        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:

            # alignement haut/bas
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            elif self.change_y < 0:
                self.rect.top = block.rect.bottom

        if self.rect.y<430:
            self.change_y+=0.5

        if self.rect.x<0:
            self.rect.x-=self.change_x

        if self.rect.x>1050:
            self.rect.x-=self.change_x

        if self.rect.y==430:
            self.saut=0

        if self.rect.y>430:
            self.rect.y=430
            self.change_y=0
            self.saut=0

        if self.orientation==0:

            if self.change_x==0 and self.change_y==0:
                self.image = self.baseGB
            elif self.change_y==0 :
                self.rect.y=455
                if 0<self.anim and self.anim<10:
                    self.image=self.Run1GB
                if 10<=self.anim and self.anim<20:
                    self.image=self.Run2GB
                if 20<=self.anim and self.anim<30:
                    self.image=self.Run3GB
                if 30<=self.anim and self.anim<40:
                    self.image=self.Run4GB
                if 40<=self.anim and self.anim<50:
                  self.image=self.Run5GB
                if 50<=self.anim and self.anim<60:
                   self.image=self.Run6GB
                if self.anim>=60:

                    self.anim=0
            else :
                if 0<self.anim and self.anim<40:
                    self.image=self.Jump1GB
                if 40<=self.anim and self.anim<80:
                    self.image=self.Jump2GB

                    self.anim=0





        if self.orientation==1:

            if self.change_x==0 and self.change_y==0:
                self.image = self.baseDB
            elif self.change_y==0 :
                self.rect.y=455
                if 0<self.anim and self.anim<10:
                    self.image=self.Run1DB
                if 10<=self.anim and self.anim<20:
                    self.image=self.Run2DB
                if 20<=self.anim and self.anim<30:
                    self.image=self.Run3DB
                if 30<=self.anim and self.anim<40:
                    self.image=self.Run4DB
                if 40<=self.anim and self.anim<50:
                  self.image=self.Run5DB
                if 50<=self.anim and self.anim<60:
                   self.image=self.Run6DB
                if self.anim>=60:
                    self.anim=0



            else :
                if 0<self.anim and self.anim<40:
                    self.image=self.Jump1DB
                if 40<=self.anim and self.anim<80:
                    self.image=self.Jump2DB

                    self.anim=0




    def mvtgauche(self):
        """Mouvement gauche """
        self.change_x = -4
        self.change_y = 0
        self.orientation=0

    def mvtdroit(self):
        """Mouvement droite"""
        self.change_x = 4
        self.change_y = 0
        self.orientation=1

    def jump(self):
        if self.saut==0:
            self.saut=1
            self.change_y=-16
        if self.orientation==0:
            if self.change_y==0:
                self.image = self.baseGB
            else :
                if 0<self.anim and self.anim<-2:
                    self.image=self.Jump1GB
                if self.anim>=-2:
                    self.anim=0
        if self.orientation==1:
            if self.change_y==0:
                self.image = self.baseDB
            else :
                if 0<self.anim and self.anim<-2:
                    self.image=self.Jump1DB
                if self.anim>=-2:
                    self.anim=0
        # On detecte s'il y a une plate-forme en dessous
        self.rect.y += 1
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        self.rect.y -= 1

        # Si c'est le cas, on saute !
        if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
            self.change_y = -15


    def stop(self):
        """arret"""
        self.change_x=0


class ShurikenDB(pygame.sprite.Sprite):
 # initialisation
    def __init__(self):
        super().__init__()

        self.image=pygame.image.load('ShurikenBleu1.png').convert_alpha()
        self.rect = self.image.get_rect()

      #mouvement du shuri
    def update(self):
        self.rect.x+=10
        if self.rect.x>1100:
            self.kill()

class ShurikenGB(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()

        self.image=pygame.image.load('ShurikenBleu1.png').convert_alpha()
        self.rect = self.image.get_rect()

        #mvt shuri
    def update(self):
        self.rect.x-=10
        if self.rect.x<-21:
            self.kill()

class Platform(pygame.sprite.Sprite):
    """classe des obstacles """

    def __init__(self, width, height):
        """initialisation"""
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(Noir)

        self.rect = self.image.get_rect()

class Level(object):
    """ Super classe a laquelle vont appartenir les differents niveaux """



    def __init__(self, Stickmanbleu, Stickmanrouge):
        """ initialisation des listes """
        self.platform_list = pygame.sprite.Group()

        self.stickmanbleu= Stickmanbleu
        self.stickmanrouge= Stickmanrouge

    def update(self):
        """ Mise a jour des listes globales"""

        self.platform_list.update()
    def draw(self, screen):
        """ affichage du niveau"""

        # Draw the background
        #screen.fill(BLUE)
##        self.fond.draw(screen)
        #screen.blit (fond, (0, 0))

        # Draw all the sprite lists that we have
        self.platform_list.draw(screen)

class Level_01(Level):
    """ contenu du niveau 1 """

    def __init__(self, Stickmanbleu, Stickmanrouge):

        Level.__init__(self, Stickmanbleu, Stickmanrouge)

        self.level_limit = -3000

##        #decor
##        fond=pygame.image.load('fond1.png').convert_alpha()
##        background=Fond(fond)
##        self.fond.add(background)

        # Tableau regroupant largeur, hauteur et position (verticale puis horizontale) des obstacles
        level = [
                 [100,20, 000, 100],
                 [100,20,000,300],
                 [80,20,200,200],
                 [100,20,120,450],
                 [100,20,360,380],
                 [20,350,700,250],
                 [100,20,400,200],

                 ]

        for platform in level:
            block = Platform(platform[0], platform[1])
            block.rect.x = platform[2]
            block.rect.y = platform[3]
            block.stickmanbleu = self.stickmanbleu
            block.stickmanrouge = self.stickmanrouge
            self.platform_list.add(block)



Noir = 0, 0, 0
Blanc = 255, 255, 255


def main():
    """ Programme principal """
    pygame.init()

    # definition de la fenetre
    screen = pygame.display.set_mode((1100,600))
    pygame.display.set_caption("StickArena !")

    stick=1

    while stick ==1:
        background = pygame.Surface(screen.get_size())
        background.fill(Noir)


        # Definition de la police
        bigText = pygame.font.SysFont('Calibri', 50 , True)

        # Definition du texte
        # render(text, antialias, rgb color tuple)
        title_text = bigText.render("STICKMAN Arena", True, Blanc)

        # Le centre du texte est au centre quelque soit le texte
        # Le texte est inscrit dans un rectangle
        textpos = title_text.get_rect()
        # Placement du texte en x et y
        textpos.centerx = screen.get_rect().centerx
        textpos.centery = 100
        # Collage du texte sur le fond
        background.blit(title_text, textpos,)



        lancer_text=bigText.render("Appuyer pour jouer",True, Blanc)

        textpos1 = lancer_text.get_rect()
        textpos1.centerx = screen.get_rect().centerx
        textpos1.centery = 500

        background.blit(lancer_text, textpos1)

        persobleu = pygame.image.load("bleuintro.png").convert()
        background.blit(persobleu, (200,300))

        persorouge = pygame.image.load("rougeintro.png").convert()
        background.blit(persorouge, (700,300))

        shurintro= pygame.image.load("shurintro.png").convert()
        background.blit(shurintro, (475,300))


        # Ajout du fond dans la fentre
        screen.blit(background, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                stick=0




        pygame.display.update()


    fond1=pygame.image.load('fond1.png').convert_alpha() # chargement et collage du fond
    screen.blit(fond1, (0, 0))


    level_list = []
    level_list.append( Level_01(Stickmanbleu, Stickmanrouge) )
    current_level_no = 0
    current_level = level_list[current_level_no]


    Stickmanbleu.level = current_level
    Stickmanrouge.level = current_level


            # creation personnage
    stickmanbleu = Stickmanbleu()
    stickmanbleu.rect.x = 250
    stickmanbleu.rect.y = 100
    stickmanrouge = Stickmanrouge()
    stickmanrouge.rect.x = 750
    stickmanrouge.rect.y = 100
    shuriken_list = pygame.sprite.Group()

        #creation des listes de sprites
    active_sprite_list = pygame.sprite.Group()
    active_sprite_list.add(stickmanbleu)
    active_sprite_list.add(stickmanrouge)
    shuriken_sprite_list = pygame.sprite.Group()





        #reglage de l'horloge
    clock = pygame.time.Clock()

        # Boucle principale
    continuer=1
    while continuer:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                continuer=0

                # touches enfoncees
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                        stickmanrouge.mvtgauche()
                if event.key == pygame.K_d:
                        stickmanrouge.mvtdroit()
                if event.key == pygame.K_LEFT:
                        stickmanbleu.mvtgauche()
                if event.key == pygame.K_RIGHT:
                        stickmanbleu.mvtdroit()
                if event.key == pygame.K_w:
                        stickmanrouge.jump()
                if event.key==pygame.K_UP:
                        stickmanbleu.jump()

                if event.key == pygame.K_s:
                        if stickmanrouge.tir<=2 :
                            if stickmanrouge.orientation==1:
                                stickmanrouge.tir+=1
                                shuriken=ShurikenDR()
                                shuriken.rect.x=stickmanrouge.rect.x+21
                                shuriken.rect.y=stickmanrouge.rect.y+20
                                shuriken_sprite_list.add(shuriken)
                                active_sprite_list.add(shuriken)
                                active_sprite_list.add(shuriken)
                            if stickmanrouge.orientation==0:
                                stickmanrouge.tir+=1
                                Shuriken=ShurikenGR()
                                Shuriken.rect.x=stickmanrouge.rect.x-21
                                Shuriken.rect.y=stickmanrouge.rect.y+20
                                shuriken_sprite_list.add(Shuriken)
                                active_sprite_list.add(Shuriken)
                if event.key==pygame.K_DOWN:
                    if event.key == pygame.K_DOWN:
                        if stickmanbleu.tir<=2 :
                            if stickmanbleu.orientation==1:
                                stickmanbleu.tir+=1
                                shuriken=ShurikenDB()
                                shuriken.rect.x=stickmanbleu.rect.x+21
                                shuriken.rect.y=stickmanbleu.rect.y+20
                                shuriken_sprite_list.add(shuriken)
                                active_sprite_list.add(shuriken)

                            if stickmanbleu.orientation==0:
                                stickmanbleu.tir+=1
                                shuriken=ShurikenGB()
                                shuriken.rect.x=stickmanbleu.rect.x-21
                                shuriken.rect.y=stickmanbleu.rect.y+20
                                shuriken_sprite_list.add(shuriken)
                                active_sprite_list.add(shuriken)



              # touches relevees
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and stickmanbleu.change_x < 0:
                    stickmanbleu.stop()
                if event.key == pygame.K_a and stickmanrouge.change_x < 0:
                    stickmanrouge.stop()
                if event.key == pygame.K_RIGHT and stickmanbleu.change_x > 0:
                    stickmanbleu.stop()
                if event.key == pygame.K_d and stickmanrouge.change_x > 0:
                    stickmanrouge.stop()


        # Mise a jour des Sprites
                """gestions des collisions stickman/ shuriken"""
        active_sprite_list.update()
        if len(shuriken_sprite_list)==0:
            stickmanrouge.tir=0
        hit_list = pygame.sprite.spritecollide(stickmanrouge,shuriken_sprite_list,False)

        for hit in hit_list:
            fondvictoireBleu=pygame.image.load('victoireBleu.png').convert_alpha()
            screen.blit(fondvictoireBleu,(0, 0))
            pygame.display.update()
            pygame.time.delay(4000)
            main()


        #stickmanbleu
        if len(shuriken_sprite_list)==0:
            stickmanbleu.tir=0

        hit_list = pygame.sprite.spritecollide(stickmanbleu,shuriken_sprite_list,False)

        for hit in hit_list:
            fondvictoirerouge=pygame.image.load('victoireRouge.png').convert_alpha()
            screen.blit(fondvictoirerouge,(0, 0))
            pygame.display.update()
            pygame.time.delay(4000)
            main()




        current_level.update()
        # Tous les objets a dessiner doivent se trouver ici
        screen.blit(fond1, (0, 0))

        current_level.draw(screen)
        active_sprite_list.draw(screen)



        # On limite le nombre d'images a 60 images par seconde
        clock.tick(60)

        # On raffraichit l'ecran avec tout ce qu'on a dessine
        pygame.display.flip()

    pygame.quit()





main()

1 réponse

Bonjour,

Il faudrait donner le traceback complet de l'erreur que te retourne python, sans ça, ça va être compliqué de déterminer sans exécution de savoir où ton code plante parmi ces 700 lignes.
0