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

查看:145
本文介绍了pygame.sprite.Group() 做什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注一个关于 pygame 的视频,我看到了这段代码

I am following a video about pygame and I saw this code

crosshair = pygame.sprite.Group()

有人可以解释一下吗?

推荐答案

阅读 pygame.sprite.Group.

pygame.sprite.Group.update()pygame.sprite.Group.draw() 是由 pygame.sprite.Group 提供的方法.
前者将 委托给包含的 pygame.sprite.Sprites - 你必须实现这个方法.

pygame.sprite.Group.update() and pygame.sprite.Group.draw() are methods which are provided by pygame.sprite.Group.
The former delegates the to the update method of the contained pygame.sprite.Sprites - you have to implement the method.

pygame.sprite.Group.update()

对 Group 中的所有 Sprite 调用 update() 方法.

Calls the update() method on all Sprites in the Group.

后者使用包含的 pygame.sprite.Sprites 的 imagerect 属性来绘制对象 - 你必须确保pygame.sprite.Sprites 具有所需的属性

The later uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects - you have to ensure that the pygame.sprite.Sprites have the required attributes

pygame.sprite.Group.draw()

将包含的精灵绘制到 Surface 参数.这对源表面使用 Sprite.image 属性,对位置使用 Sprite.rect.

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.

Groups 中的 Sprites 可以通过调用 pygame.sprite.Sprite.kill.当对象不再被引用时,它就会被销毁:

The Sprites in the Groups can be removed and thus destroyed by calling pygame.sprite.Sprite.kill. When the object is no longer referenced, it is destroyed:

Sprite 从包含它的所有组中删除.这不会改变 Sprite 的任何状态.调用此方法后可以继续使用 Sprite,包括将其添加到 Groups.

The Sprite is removed from all the Groups that contain it. This won't change anything about the state of the Sprite. It is possible to continue to use the Sprite after this method has been called, including adding it to Groups.

另见精灵组

最小示例:

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()

这篇关于pygame.sprite.Group() 做什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆