How do I delete an image after a collision in pygame?

后端 未结 1 1716
北海茫月
北海茫月 2020-12-21 22:38

I\'m making a game involving powerups using python. There is a random chance of one spawning and it blits its image on the screen. it picks a random powerup from a list once

相关标签:
1条回答
  • 2020-12-21 23:16

    Here's an example I've constructed to demonstrate sprite removal upon collision with the mouse. See answers like this for other methods

    import random
    import pygame
    
    screen_width, screen_height = 640, 480
    def get_random_position():
        """return a random (x,y) position in the screen"""
        return (random.randint(0, screen_width - 1),  #randint includes both endpoints.
                random.randint(0, screen_height - 1)) 
    
    def get_random_named_color(allow_grey=False):
        """return one of the builtin colors"""
        if allow_grey:
            return random.choice(all_colors)
        else:
            return random.choice(non_grey)
    
    def non_grey(color):
        """Return true if the colour is not grey/gray"""
        return "grey" not in color[0] and "gray" not in color[0]
    
    all_colors = list(pygame.colordict.THECOLORS.items())  
    # convert color dictionary to a list for random selection once
    non_grey = list(filter(non_grey, all_colors))
    
    class PowerUp(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            width, height = 12, 10
            self.color, color = get_random_named_color()
            self.image = pygame.Surface([width, height])
            self.image.fill(color)
            # Fetch the rectangle object that has the dimensions of the image
            # Update the position by setting the values of rect.x and rect.y
            self.rect = self.image.get_rect().move(*get_random_position())
    
        def update(self):
            """move to a random position"""
            self.rect.center = get_random_position()
    
    if __name__ == "__main__":
        pygame.init()
        screen = pygame.display.set_mode((screen_width, screen_height))
        pygame.display.set_caption('Sprite Collision Demo')
        clock = pygame.time.Clock() #for limiting FPS
        FPS = 60
        exit_demo = False
    
        #create a sprite group to track the power ups.
        power_ups = pygame.sprite.Group()
        for _ in range(10):
            # create a new power up and add it to the group.
            power_ups.add(PowerUp())
    
        # main loop
        while not exit_demo:
            for event in pygame.event.get():            
                if event.type == pygame.QUIT:
                    exit_demo = True
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        exit_demo = True
                    elif event.key == pygame.K_SPACE:
                        power_ups.update()
                elif event.type == pygame.MOUSEBUTTONUP:
                    for _ in range(10):
                        power_ups.add(PowerUp())
            # check for collision
            for p in power_ups:
                if p.rect.collidepoint(pygame.mouse.get_pos()):
                    power_ups.remove(p)
                    print(f"Removed {p.color} power up")
    
            screen.fill(pygame.Color("black")) # use black background
            power_ups.draw(screen)
            pygame.display.update()
            clock.tick(FPS)
        pygame.quit()
        quit()
    
    0 讨论(0)
提交回复
热议问题