Drag multiple sprites with different “update ()” methods from the same Sprite class in Pygame

前端 未结 2 1119
执念已碎
执念已碎 2020-12-12 03:10

I\'m trying to make multiple sprites that all come from the same pygame sprite class.

class animatedSprites(pygame.sprite.Sprite):
    def __init__(self, spri         


        
2条回答
  •  心在旅途
    2020-12-12 03:51

    For sprite management, a Sprite Group will help you.

    So during setup, do something like:

    all_sprites = pygame.sprite.Group()
    # create five initial sprites
    for number in range(5):
        new_sprite = animatedSprites(f"Sprite {number}")
        all_sprites.add(new_sprite)
    

    Then during your event handling:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # set termination condition here
            …
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # use the click position in the name
            new_sprite = animatedSprites(f"Sprite X: {event.pos[0]} Y: {event.pos[1]}")
            all_sprites.add(new_sprite)
    

    After processing events:

    # update game state
    all_sprites.update()
    # draw background
    …
    # draw sprites
    all_sprites.draw()
    # update display
    pygame.display.update()
    
    
                
    

提交回复
热议问题