Pygame: pygame.sprite.Group() 中的项目没有属性 rect

Pygame: item in pygame.sprite.Group() has no attribute rect

我是 Python 的新手,遇到了一些 Pygame 代码(请参阅下面的回溯和完整代码)。我不明白为什么当我遍历 self.bullets 中存储的项目时,项目符号对象没有 rect 属性。任何帮助或指示将不胜感激。谢谢!

回溯

Traceback (most recent call last):
  File "/Users/svengerlach/PycharmProjects/AlienInvasion/practice_12_6.py", line 82, in <module>
    game.game_loop()
  File "/Users/svengerlach/PycharmProjects/AlienInvasion/practice_12_6.py", line 22, in game_loop
    self._bullet_update()
  File "/Users/svengerlach/PycharmProjects/AlienInvasion/practice_12_6.py", line 54, in _bullet_update
    if bullet.rect.left > self.screen_rect.right:
AttributeError: 'Bullet' object has no attribute 'rect'

完整代码

import pygame
import sys


class Game:
    def __init__(self):
        [...]
        self.bullets = pygame.sprite.Group()

    def game_loop(self):
        while True:
            self._check_user_inputs()
            self._ship_update()
            self._bullet_update()
            self._screen_update()

    def _check_user_inputs(self):
        for event in pygame.event.get():
            elif event.type == pygame.KEYDOWN:
                [...]
                elif event.key == pygame.K_SPACE:
                    new_bullet = Bullet(self)
                    self.bullets.add(new_bullet)
            elif event.type == pygame.KEYUP:
                [...]

    def _ship_update(self):
        [...]

    def _bullet_update(self):
        self.bullets.update()
        for bullet in self.bullets.copy():
            if bullet.rect.left > self.screen_rect.right:
                self.bullets.remove(bullet)

    def _screen_update(self):
        [...]
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        pygame.display.flip()


class Bullet(pygame.sprite.Sprite):
    def __init__(self, ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.ship_rect = ai_game.ship_rect
        self.bullet_rect = pygame.Rect(0, 0, 15, 3)
        self.bullet_rect.midleft = self.ship_rect.midright

    def update(self):
        self.bullet_rect.x += 5

    def draw_bullet(self):
        pygame.draw.rect(self.screen, (60, 60, 60), self.bullet_rect)


if __name__ == '__main__':
    game = Game()
    game.game_loop()

bullet 没有属性 rect,但它有属性 bullet_rect。我建议将 bullet_rect 重命名为 rect。这样你就可以使用 pygame.sprite.Group.draw():

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

进一步使用pygame.sprite.Sprite.kill():

The Sprite is removed from all the Groups that contain it. [...]

示例:

class Bullet(pygame.sprite.Sprite):
    def __init__(self, ai_game):
        super().__init__()
        self.image = pygame.Surface((15, 3))
        self.image.fill((60, 60, 60))
        self.rect = self.image.get_rect(midleft = ai_game.ship_rect.midright)

    def update(self):
        self.rect.x += 5
class Game:
    def __init__(self):
        # [...]
        self.bullets = pygame.sprite.Group()

    def game_loop(self):
        while True:
            self._check_user_inputs()
            self._ship_update()
            self._bullet_update()
            self._screen_update()

    def _check_user_inputs(self):
        for event in pygame.event.get():
            elif event.type == pygame.KEYDOWN:
                # [...]
                elif event.key == pygame.K_SPACE:
                    self.bullets.add(Bullet(self))
            elif event.type == pygame.KEYUP:
                # [...]

    def _bullet_update(self):
        self.bullets.update()
        for bullet in self.bullets:
            if bullet.rect.left > self.screen_rect.right:
                bullet.kill()

    def _screen_update(self):
        # [...]
        self.bullets.draw(ai_game.screen)
        pygame.display.flip()