开发者

Help needed with pygame spritegroups

I have modified the pygame Chimp example by replacing the Chimp image with the circle and a line using the draw method drawn from the center of the circle and also by replacing the fist with a 10 by 10 black box surface.

My problem is this.

When the black box(fist) punches the circle(chimp), the circle(chimp) has to rotate by 360 degrees. It works fine for a single circle sprite which is described in the chimp class, but when i create more than one sprite and add them to a sprite.group only the first sprite that is created rotates but the other circle(Chimp) sprites don't respond. Can anyone please tell me how to solve this problem. I'm using python 3x and 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(1开发者_开发百科0):
             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()


The answer lies in the punch method:

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

The attribute self.punching is set to 1 for the first chimp. For all the others, the block isn't executed anymore, since self.punching is already 1.

Try unindenting the last two lines instead:

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

Though this adds the new "feature" that you can punch again before the punch has been unpunched.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜