pygame spritegroups需要帮助

时间:2011-09-06 11:26:11

标签: python pygame sprite

我修改了pygame Chimp示例,使用圆圈和线条替换Chimp图像,使用从圆心绘制的绘制方法,并用10 x 10黑框表面替换拳头。

我的问题是这个。

当黑框(拳头)击打圆圈(黑猩猩)时,圆圈(黑猩猩)必须旋转360度。它适用于黑猩猩类中描述的单个圆形精灵,但是当我创建多个精灵并将它们添加到sprite.group时,只创建的第一个精灵旋转但另一个圆形(黑猩猩)精灵不要回应。谁能告诉我如何解决这个问题。我正在使用python 3x和pygame 1.9

    #Import Modules
    import os, pygame
    from pygame.locals import *
    from pygame.compat import geterror
    import random

    def load_image(x=40,y=40,r=0,g=0,b=0):
        image = pygame.Surface((x,y))
        image.fill((r,g,b))
        pygame.draw.circle(image,(r,g,255),(20,20),10)
        pygame.draw.line(image,(r,255,b),(20,20),(30,30),2)
        image = image.convert()
        image.set_colorkey((255,0,0))
        return image, image.get_rect()
    class Fist(pygame.sprite.Sprite):
        def __init__(self):
             pygame.sprite.Sprite.__init__(self)
             self.image,self.rect = load_image(x=10,y=10)
             self.punching = 0

        def update(self):
             pos = pygame.mouse.get_pos()
             self.rect.midtop = pos
             if self.punching:
                 self.rect.move_ip(5,10)

        def punch(self,target) :
             if not self.punching:
                 self.punching = 1
                 hitbox = self.rect.inflate(-5,-5)
                 return hitbox.colliderect(target.rect)

        def unpunch(self):
                 self.punching = 0

    class Chimp (pygame.sprite.Sprite):

        def __init__(self):
             pygame.sprite.Sprite.__init__(self)
             self.image,self.rect = load_image(r=255)
             screen = pygame.display.get_surface()
             self.area = screen.get_rect()
             self.rect.topleft = random.randint(0,300),random.randint(0,300)
             self.move = 0
             self.dizzy = 0
        def update(self):
             if self.dizzy:
                   self._spin()
             else:
                   self._walk()
        def _walk(self):
             newpos = self.rect.move((self.move,0))
             if self.rect.left < self.area.left or self.rect.right > self.area.right:
                   self.move = -self.move
                   newpos = self.rect.move((self.move,0))
             self.rect = newpos
         def _spin(self) :
             center = self.rect.center
             self.dizzy = self.dizzy + 6
             if self.dizzy >= 360:
                   self.dizzy = 0
                   self.image = self.original
             else :
                   rotate = pygame.transform.rotate
                   self.image = rotate(self.original,self.dizzy)
             self.rect = self.image.get_rect(center=center)

         def punched(self):
             if not self.dizzy:
                   self.dizzy = 1
                   self.original = self.image

    def main():

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

         background = pygame.Surface(screen.get_size())
         background = background.convert()
         background.fill((250,250,250))

         screen.blit(background,(0,0))
         pygame.display.flip()

         clock = pygame.time.Clock()

         fist = Fist()
         allsprites = pygame.sprite.Group()
         for i in range(10):
             chimp = Chimp()
             allsprites.add(chimp)
         fistsprite = pygame.sprite.Group((fist))
         going = True
         while going:
             clock.tick(60)

             for event in pygame.event.get():
                  if event.type == QUIT:
                      going = False
                  elif event.type == MOUSEBUTTONDOWN:
                      for chimp in allsprites.sprites:
                           if fist.punch(chimp):
                               chimp.punched()

                  elif event.type == MOUSEBUTTONUP:
                      fist.unpunch()

             allsprites.update()
             fistsprite.update()

             screen.blit(background,(0,0))
             allsprites.draw(screen)
             fistsprite.draw(screen)
             pygame.display.flip()
         pygame.quit()

    if __name__ == '__main__':
         main()

1 个答案:

答案 0 :(得分:1)

答案在于punch方法:

def punch(self,target) :
    if not self.punching:
        self.punching = 1
        hitbox = self.rect.inflate(-5,-5)
        return hitbox.colliderect(target.rect)

第一只黑猩猩的属性self.punching设置为1。对于所有其他人,由于self.punching已经为1,因此不再执行该块。

请尝试取消紧接最后两行:

def punch(self,target) :
    if not self.punching:
        self.punching = 1
    hitbox = self.rect.inflate(-5,-5)
    return hitbox.colliderect(target.rect)

虽然这增加了新的“功能”,您可以在打孔之前再次打孔。