【问题标题】:How do you clear sprites in Pygame?你如何在 Pygame 中清除精灵?
【发布时间】:2020-01-18 05:04:16
【问题描述】:

Pygame 为Group 类提供了clear 方法,用于将精灵组合在一起。当我对 Group 对象调用 clear 时,我可以成功清除该组中所有精灵的图像,但我的精灵的命中框仍然存在。我想知道是否有另一种方法可以在不破坏精灵对象的情况下同时删除我的精灵图像和矩形碰撞框。

【问题讨论】:

  • 也许您在其他组中拥有相同的精灵,用于检查碰撞。您可能必须从所有组中删除精灵。
  • 您可以将对象保留在其他组或列表中 - 这样它们就不会从内存中删除(不会被销毁)。但不要使用该组来绘制和/或检查碰撞。
  • 我只有一个 Group 对象。我想从视图中删除图形和命中框,但将数据保留在模型中。
  • 什么型号?并且 hitbox 应该与图形在同一个类 Sprite 中 - 所以如果你删除 sprite ,那么你会同时删除 - graphics 和 hitbox。
  • 也许你创造了一些与标准 Sprite 不同的东西?也许您应该针对您的问题创建最少的工作代码,以便我们可以运行它并查看问题。

标签: python pygame sprite


【解决方案1】:

您应该使用组中的元素和要从组中删除的kill() 元素检查鼠标位置。之后,您可以使用 clean() 从屏幕中删除所有元素,并再次使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多