簡體   English   中英

你如何清除 Pygame 中的精靈?

[英]How do you clear sprites in Pygame?

Pygame 為Group類提供了clear方法,該方法用於將精靈分組在一起。 當我在Group對象上調用 clear 時,我可以成功清除該組中所有精靈的圖像,但我的精靈的命中框仍然存在。 我想知道是否有另一種方法可以在不破壞精靈對象的情況下同時刪除我的精靈圖像和矩形命中框。

您應該使用組中的元素和要從組中刪除的kill()元素檢查鼠標位置。 之后,您可以使用clean()從屏幕中刪除所有元素並再次使用 draw draw()僅重繪仍在組中的元素

        x, y = pygame.mouse.get_pos()

        for item in my_group:
            if item.rect.collidepoint(x, y):
                print("Collision detected")
                item.kill()
                my_group.clear(screen, background)
                my_group.draw(screen)

我在組中創建了兩個元素,因此如果在clear()之后刪除draw() ,您會看到不同之處 - 它將從屏幕中刪除所有元素

import pygame

# --- constants ---

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

SIZE = (700, 500)

# --- classes ---

class Block(pygame.sprite.Sprite):
    # Constructor. Pass in the color of the block,
    # and its x and y position
    def __init__(self, color, width, height):
        # Call the parent class (Sprite) constructor
        super().__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load("Obrazy/images/square-1.png").convert()

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rect = self.image.get_rect()

# --- functions ---

# empty

# --- main ---

pygame.init()

# Set the height and width of the screen
screen = pygame.display.set_mode(SIZE)

pygame.display.set_caption("Testing Screen")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

my_block1 = Block(WHITE, 20, 20)
my_block2 = Block(WHITE, 20, 20)
my_block2.rect.x = 100

my_group = pygame.sprite.Group()
my_group.add(my_block1)
my_group.add(my_block2)

background = pygame.Surface(SIZE)

screen.fill(BLACK)
#screen.blit(background, (0,0))
my_group.draw(screen)

# -------- Main Program Loop -----------

while not done:
    # Set the screen background
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()

            for item in my_group:
                if item.rect.collidepoint(x, y):
                    print("Collision detected")
                    item.kill()
                    my_group.clear(screen, background)
                    my_group.draw(screen)

    # Limit to 60 frames per second
    clock.tick(60)

    #my_group.clear(screen, background)
    #my_group.draw(screen)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()

順便說一句:在 Python 3 中你可以使用

super().__init__() 

代替

pygame.sprite.Sprite.__init__(self)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM