Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何清除Pygame中的精灵?_Python_Pygame_Sprite - Fatal编程技术网

Python 如何清除Pygame中的精灵?

Python 如何清除Pygame中的精灵?,python,pygame,sprite,Python,Pygame,Sprite,Pygame为用于将精灵分组在一起的Group类提供了clear方法。当我在我的组对象上调用clear时,我可以成功地清除该组中所有精灵的图像,但精灵的点击框仍然存在。我想知道是否有其他方法可以在不破坏精灵对象的情况下删除精灵的图像和矩形点击框。您应该使用组中的元素和要从组中删除的kill()元素检查鼠标位置。之后,您可以使用clean()从屏幕中删除所有元素,并再次使用draw()仅重新绘制仍在组中的元素 x, y = pygame.mouse.get_pos()

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

您应该使用组中的元素和要从组中删除的
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()

顺便说一句:在Python3中,您可以使用

super().__init__() 
而不是

pygame.sprite.Sprite.__init__(self)

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