简体   繁体   English

pygame.sprite.Group() 有什么作用

[英]What does pygame.sprite.Group() do

I am following a video about pygame and I saw this code我正在关注有关 pygame 的视频,我看到了这段代码

crosshair = pygame.sprite.Group()

Could someone explain me this?有人可以解释一下吗?

Read the documentation ofpygame.sprite.Group .阅读pygame.sprite.Group的文档。

pygame.sprite.Group.update() and pygame.sprite.Group.draw() are methods which are provided by pygame.sprite.Group . pygame.sprite.Group.update()pygame.sprite.Group.draw()pygame.sprite.Group提供的方法。
The former delegates the to the update method of the contained pygame.sprite.Sprite s - you have to implement the method.前者委托给包含的pygame.sprite.Spriteupdate方法 - 你必须实现该方法。

pygame.sprite.Group.update()

Calls the update() method on all Sprites in the Group.调用 Group 中所有 Sprite 的 update() 方法。

The later uses the image and rect attributes of the contained pygame.sprite.Sprite s to draw the objects - you have to ensure that the pygame.sprite.Sprite s have the required attributes后者使用包含的pygame.sprite.Spriteimagerect属性来绘制对象 - 您必须确保pygame.sprite.Sprite具有所需的属性

pygame.sprite.Group.draw()

Draws the contained Sprites to the Surface argument.将包含的 Sprite 绘制到 Surface 参数。 This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.这将 Sprite.image 属性用于源表面,并将 Sprite.rect 用于 position。

The Sprites in the Groups can be removed and thus destroyed by calling pygame.sprite.Sprite.kill .可以通过调用pygame.sprite.Sprite.kill来移除并销毁中的pygame.sprite.Sprite.kill When the object is no longer referenced, it is destroyed:当不再引用 object 时,将其销毁:

The Sprite is removed from all the Groups that contain it. Sprite 将从包含它的所有组中删除。 This won't change anything about the state of the Sprite.这不会改变 Sprite 的 state 的任何内容。 It is possible to continue to use the Sprite after this method has been called, including adding it to Groups.调用此方法后,可以继续使用 Sprite,包括将其添加到 Groups。

See also Sprite Groups另请参阅Sprite 组


Minimal example:最小的例子:

import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self, center_pos):
        super().__init__() 
        self.image = pygame.Surface((40, 40))
        self.image.fill((255, 255, 0))
        self.rect = self.image.get_rect(center = center_pos)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, center_pos):
        super().__init__() 
        self.image = pygame.Surface((20, 10))
        self.image.fill((0, 255, 255))
        self.rect = self.image.get_rect(center = center_pos)
    
    def update(self):
        self.rect.x += 10
        if self.rect.right > 300:
            self.kill()

pygame.init()
window = pygame.display.set_mode((400, 300))
clock = pygame.time.Clock()

player = Player((25, window.get_height() // 2))
all_sprites = pygame.sprite.Group(player)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                all_sprites.add(Bullet(player.rect.center))

    all_sprites.update()
    print(len(all_sprites))

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), (300, 0, 10, window.get_height()))
    all_sprites.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM