错误提示: TypeError: pygame.sprite.Sprite.add() argument after * must be an iterable, not AlienInvasion
看着两段代码是一样的,为什么自己写的代码运行出现错误提示,而范例代码确可以运行? 两段代码差异在哪里?
自己写的代码:
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
def __int__(self, ai_game):
super().__init__()
self.screen = ai_game.screen
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x = float(self.rect.x)
范例代码:
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""A class to represent a single alien in the fleet."""
def __init__(self, ai_game):
"""Initialize the alien and set its starting position."""
super().__init__()
self.screen = ai_game.screen
# Load the alien image and set its rect attribute.
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# Start each new alien near the top left of the screen.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# Store the alien's exact horizontal position.
self.x = float(self.rect.x)