Partage
  • Partager sur Facebook
  • Partager sur Twitter

TypeError: 'Sprite' object is not iterable

n'arrive pas a gerer les certaines collisions

    24 avril 2016 à 17:20:39

    Bonjour, je suis élève en terminal S et dans le cadre de mon projet de fin d'année en ISN je dois finir un projet nous avons donc choisi de refaire un sorte de pacman. nous sommes arrivé assez loin cependent aujourd'hui je n'arrive pas a gérer mes collisions entre mes fantômes et mon pacman le but étant que quand il a collisions le jeu soit fini. J'ai essayé plusieurs choses avec mes camarades mais nous ne trouvons pas de solutions la plupart du temps que je tente quelque chose python me renvoie le message d'erreur suivant. TypeError: 'Sprite' object is not iterable nous avons en effet fait le choix de faire notre jeu avec python et plus précisement pygame enfin bref je suis actuellement bloqué dans mon codage a cause de cette erreur et je ne sais pas comment la résoudre. Merci par avance de votre temps pour m'aider je vous met le code ci-dessous:

    #level 1

    from random import choice

    import pygame

    tableau=[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],

    [1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1],

    [1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1],

    [1,3,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,3,1],

    [1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1],

    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

    [1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1],

    [1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1],

    [1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1],

    [1,1,1,1,1,1,0,1,1,1,1,1,2,1,1,2,1,1,1,1,1,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1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1],

    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

    TAILLE = 20

    VITESSE = 1

    vf=1

    pygame.init()

    fenetre = pygame.display.set_mode((560,600))

    background=pygame.Surface(fenetre.get_size())

    background.fill((0,0,0))

    mur_list = pygame.sprite.Group()

    point_list = pygame.sprite.Group()

    bonus_list = pygame.sprite.Group()

    ##man_list = pygame.sprite.Group()

    end = pygame.sprite.Sprite()

    end.image= pygame.image.load('end.png').convert()

    end.rect = end.image.get_rect()

    end.rect.x = 280

    end.rect.y = 440

    man = pygame.sprite.Sprite()

    man.image = pygame.image.load('man.png').convert()

    man.rect = man.image.get_rect()

    man.rect.x = rX = 280

    man.rect.y = rY = 440

    man.vx=0

    man.vy=0

    ##man_list.add(man)

    for i in range(len(tableau)):

            for j in range(len(tableau[i])):

                if tableau[i][j]==1:

                    mur = pygame.sprite.Sprite()

                    mur.image = pygame.Surface([20,20]).convert()

                    mur.image.fill((0,0,225))

                    mur.rect = mur.image.get_rect()

                    mur.rect.x = j*20

                    mur.rect.y = i*20

                    mur_list.add(mur)

                if tableau[i][j]==0:

                    point=pygame.sprite.Sprite()

                    point.image= pygame.image.load('point1.png').convert()

                    point.rect=point.image.get_rect()

                    point.rect.x=j*20

                    point.rect.y=i*20

                    point_list.add(point)

                if tableau[i][j]==3:

                    bonus=pygame.sprite.Sprite()

                    bonus.image= pygame.image.load('point.png').convert()

                    bonus.rect=bonus.image.get_rect()

                    bonus.rect.x=j*20

                    bonus.rect.y=i*20

                    bonus_list.add(bonus)

    class Fantome(pygame.sprite.Sprite) :

        def __init__(self,vx,vy,murs,couleur,joueur) :

            super().__init__()

            self.vx = vx

            self.vy = vy

            self.couleur = couleur

            self.image = pygame.Surface([TAILLE,TAILLE])

            self.image.fill(couleur)

            self.rect = self.image.get_rect()

            self.rect.x = 13*TAILLE

            self.rect.y = 13*TAILLE

            self.murs = murs

            self.joueur = joueur

        def update(self):

            z_collision = pygame.sprite.spritecollideany(self,self.joueur)

            for z in z_collision:

                Continuer=False

            if self.vx!=0 :

                self.rect.x += self.vx

                m_collision = pygame.sprite.spritecollide(self,self.murs, False)

                for m in m_collision:

                    if self.vx>0 : self.rect.right = m.rect.left;self.vy=choice([-vf,vf]);self.vx=0

                    if self.vx<0 : self.rect.left = m.rect.right;self.vy=choice([-vf,vf]);self.vx=0

            if self.vy!=0 :

                self.rect.y += self.vy

                m_collision = pygame.sprite.spritecollide(self,self.murs, False)

                for m in m_collision:

                    if self.vy>0 : self.rect.bottom = m.rect.top;self.vx=choice([-vf,vf]);self.vy=0

                    if self.vy<0 : self.rect.top = m.rect.bottom;self.vx=choice([-vf,vf]);self.vy=0

            #changement de direction

            if self.vy == 0 and self.rect.x % TAILLE == 0:

                self.vy = choice([-vf,vf])

                self.rect.y += self.vy

                m_collision = pygame.sprite.spritecollide(self,self.murs, False)

                if m_collision :

                    self.rect.y -= self.vy

                    self.vy=0

                else :

                    if choice([0,1])==1 :

                        self.vx=0

                        self.rect.y -= self.vy

                    else :

                        self.rect.y -= self.vy

                        self.vy=0

            elif self.vx == 0 and self.rect.y % TAILLE == 0:

                self.vx = choice([-vf,vf])

                self.rect.x += self.vx

                m_collision = pygame.sprite.spritecollide(self,self.murs, False)

                if m_collision :

                    self.rect.x -= self.vx

                    self.vx=0

                else :

                    if choice([0,1])==1 :

                        self.vy=0

                        self.rect.x -= self.vx

                    else :

                        self.rect.x -= self.vx

                        self.vx=0

    fant_list = pygame.sprite.Group()

    fant1 = Fantome(vf,0,mur_list,(250,150,0),man)

    fant_list.add(fant1)

    fant2 = Fantome(vf,0,mur_list,(255,0,0),man)

    fant_list.add(fant2)

    fant3 = Fantome(vf,0,mur_list,(255,150,150),man)

    fant_list.add(fant3)

    fant4 = Fantome(vf,0,mur_list,(25,200,200),man)

    fant_list.add(fant4)

    Continuer = True

    clock = pygame.time.Clock()

    while Continuer == True :

        mur_list.draw(fenetre)

        point_list.draw(fenetre)

        fenetre.blit (man.image, (rX,rY))

        touche = pygame.key.get_pressed()

        if touche[pygame.K_UP]  :

            man.vy = - VITESSE

        if touche[pygame.K_DOWN] :

            man.vy = VITESSE

        if touche[pygame.K_LEFT] :

            man.vx = - VITESSE

        if touche[pygame.K_RIGHT] :

            man.vx = VITESSE

        man.rect.x += man.vx

        m_collision = pygame.sprite.spritecollide(man, mur_list, False)

        for m in m_collision:

                if man.vx>0 : man.rect.right = m.rect.left;man.vx=0

                if man.vx<0 : man.rect.left = m.rect.right;man.vx=0

        man.rect.y  +=man.vy

        m_collision = pygame.sprite.spritecollide(man, mur_list, False)

        for m in m_collision:

                if man.vy>0 : man.rect.bottom = m.rect.top;man.vy=0

                if man.vy<0 : man.rect.top= m.rect.bottom;man.vy=0

        x_collision = pygame.sprite.spritecollide(man,point_list, True)

        y_collision = pygame.sprite.spritecollide(man,bonus_list, True)

        for event in pygame.event.get():

                if event.type == pygame.QUIT :

                    Continuer = False

                touche = pygame.key.get_pressed()

                if touche[pygame.K_ESCAPE]:

                    Continuer = False

        if len(point_list)==0 and len(bonus_list)==0:

            Continuer=False

        fenetre.blit(background,(0,0))

        fenetre.blit (man.image, man.rect)

        mur_list.draw(fenetre)

        point_list.draw(fenetre)

        bonus_list.draw(fenetre)

        fant_list.update()

        fant_list.draw(fenetre)

        clock.tick(200)

        pygame.display.update()

    pygame.quit()

    • Partager sur Facebook
    • Partager sur Twitter

    TypeError: 'Sprite' object is not iterable

    × 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